#include #include #include #include #include #include #include #include #include #include #include #include #include #define SAMPLEPORT 3630 void print_inet_address(struct sockaddr_in *) ; int *pres_count ; int logfd ; void write_log(char *clnt_name, char *log_msg) { char outbuff[256] ; int wrz ; wrz = sprintf(outbuff, "%-38s -- %s\n", clnt_name, log_msg) ; if (wrz > 0) (void) write(logfd, outbuff, wrz) ; } void handle_child(int ignore_me) { pid_t chld_pid ; int chld_status ; char outbuff[80] ; chld_pid=wait(&chld_status) ; if (!WIFEXITED(chld_status)) (void) sprintf(outbuff, "abnormal termination (pid=%d)", chld_pid) ; else (void) sprintf(outbuff, "normal exit (pid=%d)", chld_pid) ; write_log("", outbuff) ; } void parse_inet_address(struct sockaddr_in *inet_ad, char *buff, int bsize) { struct hostent *hp; char *cname, bigbuff[1024] ; int cport, wrsz ; hp = gethostbyaddr((char *)&inet_ad->sin_addr, sizeof(struct in_addr), AF_INET) ; if (hp == (struct hostent *)NULL) cname = inet_ntoa(inet_ad->sin_addr) ; else cname = hp->h_name ; cport = ntohs(inet_ad->sin_port) ; wrsz = sprintf(bigbuff, "%s (%d)", cname, cport) ; if (wrsz <= 0) buff[0] = '\0' ; else if (wrsz < bsize) (void) memcpy(buff, bigbuff, wrsz+1) ; else { (void) memcpy(buff, bigbuff, bsize-1) ; buff[bsize] = '\0' ; } } #define CLIENT_NAME_SIZE 39 void count_em(int sock_fd, struct sockaddr_in * clnt_addr) { int bufpos, bufsiz, numout, lastcr, cptr, j ; char buff[1024], outbuff[80], lastfew[21] ; char clnt_name[CLIENT_NAME_SIZE] ; parse_inet_address(clnt_addr, clnt_name, CLIENT_NAME_SIZE) ; (void) sprintf(outbuff, "connected (pid=%d)", getpid()) ; write_log(clnt_name, outbuff) ; while ((bufsiz=read(sock_fd, buff, 1024)) > 0) { lastcr = -1 ; for (bufpos=0; bufpos 0) { /* parent process */ close(sio) ; } else { /* you better stop now ! */ write_log("", strerror(errno)) ; perror("fork") ; exit(1) ; } } }