-
-
-
-
-
- #define __EXTENSIONS__
- #define __USE_BSD
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define __EXTENSIONS__
- #define __USE_XOPEN_EXTENDED
- #include <strings.h>
- #include <time.h>
- #include <signal.h>
- #include <unistd.h>
- #define _XOPEN_SOURCE
- #include <xti.h>
- #include <fcntl.h>
-
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <sys/select.h>
- #include <sys/socket.h>
- #include <sys/stropts.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/time.h>
- #include <netdb.h>
- #include <unistd.h>
-
- #define DEF_PORT 0xdeaf
-
- #define MAXBUF 10240
-
- #ifndef HL_AIX
- #define DEF_T_OPEN_TCP_IPV4 "/dev/tcp"
- #else
- #define DEF_T_OPEN_TCP_IPV4 "/dev/xti/tcp"
- #endif
-
- extern int t_errno;
-
- int cl_transport = 0;
-
- void sigchld_handler (int sig)
- {
- int pid;
-
- while ((pid = waitpid (-1, NULL, WNOHANG)) > 0);
- signal (SIGCHLD, sigchld_handler);
- }
-
- void sigint_handler(int sig)
- {
- printf("\n (G+) SIGINT cought! Closing connection\n");
- t_snddis(cl_transport, NULL);
- t_unbind(cl_transport);
- t_close(cl_transport);
- exit(0);
- }
-
- int check_discon(int fd)
- {
- if (t_errno == TLOOK) {
- if (t_look(fd) == T_DISCONNECT)
- {
- t_rcvdis(fd, NULL);
- t_unbind(fd);
- t_close(fd);
- printf(" (G-) server closed connection\n");
- return 1;
- }
- }
- return 0;
- }
- int main(int argc, char **argv)
- {
- int fl, n, rurka[2], child, ret;
- struct sockaddr_in serv;
- struct t_call make_a_call;
- struct hostent *host_info;
- struct timeval tv;
- char buf[MAXBUF];
- fd_set readset;
-
- if (argc < 3)
- {
- printf("usage: %s hostname nickname\n", argv[0]);
- return 1;
- }
- signal(SIGINT, SIG_IGN);
- setbuffer(stdout, 0, 0);
- if (pipe(rurka) < 0)
- {
- perror(" (G!) pipe()");
- return 1;
- }
-
- if ((cl_transport = t_open(DEF_T_OPEN_TCP_IPV4, O_RDWR, 0)) < 0)
- {
- t_error(" (G!) t_open()");
- return 1;
- }
- printf(" (G+) endpoint opened\n");
-
-
- if (t_bind(cl_transport, NULL, NULL) < 0)
- {
- t_error(" (G!) t_bind()");
- t_close(cl_transport);
- return 1;
- }
- printf(" (G+) endpoint binded\n");
-
- bzero((char *)&serv, sizeof(struct sockaddr_in));
- serv.sin_family = AF_INET;
-
- if ((child = fork()) < 0)
- {
- printf(" (G+) running synchronous resolver\n");
- close(rurka[0]);
- close(rurka[1]);
- if ((host_info = gethostbyname(argv[1])) != NULL) {
- memcpy(&serv.sin_addr, host_info->h_addr, sizeof(struct in_addr));
- } else {
- perror(" (G!) gethostbyname()");
- t_snddis(cl_transport, NULL);
- t_unbind(cl_transport);
- t_close(cl_transport);
- return 0;
- }
- printf(" (G+) address resolved: %s\n",inet_ntoa(serv.sin_addr));
- } else if (child == 0) {
- close(rurka[0]);
- host_info = gethostbyname(argv[1]);
- if (host_info == NULL)
- {
- write(rurka[1], host_info->h_addr, 1);
- close(rurka[1]);
- exit(1);
- }
- write(rurka[1], host_info->h_addr, sizeof(struct in_addr));
- close(rurka[1]);
- exit (0);
- } else {
- printf(" (G+) running asynchronous resolver\n");
- close(rurka[1]);
- do {
- tv.tv_sec = 0;
- tv.tv_usec = 10;
- FD_ZERO(&readset);
- FD_SET(rurka[0], &readset);
- printf(".");
- } while ((ret=select(rurka[0]+1, &readset, NULL, NULL,
- &tv)) ==0);
- printf("\n");
-
- if (ret>0) {
- waitpid(child, NULL, 0);
- ret = read(rurka[0], &serv.sin_addr,
- sizeof(struct in_addr));
- if (ret == sizeof(struct in_addr))
- printf(" (G+) address resolved: %s\n", inet_ntoa(serv.sin_addr));
- else
- ret = -2;
- }
- if (ret <0) {
- if (ret == -1)
- perror(" (G!) select()");
- else
- perror(" (G!) read()");
- t_snddis(cl_transport, NULL);
- t_unbind(cl_transport);
- t_close(cl_transport);
- return 1;
- }
- }
-
- serv.sin_port = htons(DEF_PORT);
-
- bzero((char *)&make_a_call, sizeof(struct t_call));
- make_a_call.addr.maxlen = sizeof(struct sockaddr_in);
- make_a_call.addr.len = sizeof(struct sockaddr_in);
- make_a_call.addr.buf = (char *) &serv;
- make_a_call.opt.maxlen = 0;
- make_a_call.opt.len = 0;
- make_a_call.udata.maxlen = 0;
- make_a_call.udata.len = 0;
-
- if (t_connect(cl_transport, &make_a_call, NULL) < 0)
- {
- if (check_discon(cl_transport))
- return 1;
- t_error(" (G!) t_connect()");
- t_unbind(cl_transport);
- t_close(cl_transport);
- return 1;
- }
- printf(" (G+) We're in! Waiting for user input\n");
-
- signal(SIGINT, sigint_handler);
- while(1)
- {
- if (!argv[3])
- printf("<%s> ", argv[2]);
- if(fgets(buf, MAXBUF, stdin) == NULL)
- break;
-
- if (t_snd(cl_transport, buf, strlen(buf), 0) != strlen(buf))
- {
- if (check_discon(cl_transport))
- return 1;
- t_error(" (G!) t_snd()");
- t_unbind(cl_transport);
- t_close(cl_transport);
- return 1;
- }
-
- if ((n=t_rcv(cl_transport, buf, MAXBUF-1, &fl)) <0)
- {
- if (check_discon(cl_transport))
- return 1;
- t_error(" (G!) t_rcv()");
- t_unbind(cl_transport);
- t_close(cl_transport);
- return 1;
- }
- buf[n] = 0;
- if (!argv[3])
- printf("<cattyv3> ");
- printf(buf);
- }
- printf("\n (G+) Closing connection\n");
- t_snddis(cl_transport, NULL);
- t_unbind(cl_transport);
- t_close(cl_transport);
- return 0;
- }
This document should validate, please check by clicking:
