/*********** F_TCP.C ********/ #include #include #include "f_tcp.h" #include /*******************************************************************/ int config_tcp_port_client(int *pfd) /* in output file descriptor */ { int sk; if ((sk = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Configuring TCP socket failed"); return(-1); } *pfd = sk; return(0); } /*******************************************************************/ int config_tcp_port_server( int serv_addr, /* input, address desired */ int *pfd) /* output, file descriptor */ { struct sockaddr_in server; int sk, len, optval; if ((sk = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("Configuring TCP socket failed"); return(-1); } *pfd = sk; optval = 1; if (setsockopt(sk,SOL_SOCKET,SO_REUSEADDR,(char *)&optval, sizeof(int)) == -1) { perror("Configuring TCP socket options failed"); return(-1); } server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons((unsigned short)serv_addr); if (bind (sk, (struct sockaddr *)&server, sizeof(server)) == -1) { perror("Binding TCP socket failed"); return(-1); } len = sizeof(server); if (getsockname(sk, (struct sockaddr *)&server, &len) == -1) { perror("TCP getsockname function failed"); return(-1); } if (server.sin_port != htons((unsigned short)serv_addr)) { printf("Wrong TCP socket address assigned"); return(-1); } return(0); } /*******************************************************************/ void unconfig_tcp_port(int fd) { ioctl(fd, I_FLUSH, FLUSHRW); if (shutdown(fd, 2) == -1) perror("Shutdown failed"); if (close(fd) == -1) perror("Close failed"); } /*******************************************************************/ void tcp_port_sign(int fd) { ioctl(fd, I_SETSIG, S_INPUT /*| S_ERROR*/ ); } /*******************************************************************/ int cl_to_ser_connection( char *server_name, /* input */ int serv_addr, /* input */ int sk) /* input */ { struct sockaddr_in server; struct hostent *hp; int fileflags; server.sin_family = AF_INET; if ((hp = gethostbyname(server_name)) == (struct hostent *)NULL) { perror("Gethostbyname failed"); return(-1); } memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length); server.sin_port = htons((unsigned short)serv_addr); if (connect(sk, (struct sockaddr *)&server, sizeof(server)) == -1) { perror("Connect failed"); return(-1); } if ((fileflags = fcntl(sk, F_GETFL, 0)) == -1) { perror("Fcntl F_GETFL failed"); return(-1); } if (fcntl(sk, F_SETFL, fileflags | /*FNDELAY |*/ FASYNC ) == -1) { perror("Fcntl F_SETFL failed"); return(-1); } printf("TCP socket connection established\n"); return(1); } /*******************************************************************/ int ser_to_cl_connection (int fd, int * pmsgsock, pid_t * childproc_num) { int fileflags; pid_t forkres; int ww; int *wstat; wstat = &ww; listen(fd, SOMAXCONN); for (;;) { *pmsgsock = accept (fd, (struct sockaddr *)0, (int *)0); if (*pmsgsock == -1) { perror("Accept failed"); exit(0); } else { if (*childproc_num) { kill(*childproc_num, SIGTERM); wait(wstat); *childproc_num = 0; } forkres = fork(); switch (forkres) { case -1: perror("Fork failed"); sleep(1); exit(1); break; case 0: /*child*/ if ((fileflags = fcntl(*pmsgsock, F_GETFL, 0)) == -1) { perror("Fcntl F_GETFL failed"); return(-1); } if (fcntl(*pmsgsock, F_SETFL, fileflags/*|FNDELAY*/|FASYNC) == -1) { perror("Fcntl F_SETFL failed"); return(-1); } /* go back to the calling process that can start to tx/rx with send and recv */ return(1); default: *childproc_num = forkres; break; } /*switch()*/ } /*if()...else...*/ } /*for(;;)*/ } /* ser_to_cl_connection()*/