#include #include #include #include #include #include #include void printsockaddr(struct sockaddr_in *arg) { struct hostent *hp; printf(" sockaddr_in: "); printf("Domain=%d, ", arg->sin_family); hp=gethostbyaddr(&(arg->sin_addr), sizeof(arg->sin_addr), AF_INET); printf("Hostname=%s, ", hp->h_name); printf("Port=%d, ", ntohs(arg->sin_port)); printf("Address=%s,\n", inet_ntoa(arg->sin_addr)); } help_msg() { printf("format: coord -[d] sec usec tosec tousec pktsize sender receiver portno\n"); printf(" option: d set debug mode\n"); } main(argc, argv) int argc; char *argv[]; { int sock; struct sockaddr_in dst; struct sockaddr_in src; struct sockaddr_in coord; struct timeval timeout; struct timeval starttime; char coordinator[100]; char sender[100]; char receiver[100]; struct hostent *hp, *gethostbyname(); Union Uprobemsg upmsg; struct Uprobereq upreq; struct Uprobereport uprep; int sequenceno = 0; int n; char toBuf[1024]; char fromBuf[1024]; char buf[1024]; char hostname[64]; extern char *optarg; extern int optind; int option_exist = 0; int c; int debug = 0; int toPort_no; int length; int flag = 1; fd_set readfds; struct timeval to; int state = 1; int retry = 3; int r; int maxfdpl; int i; /* process command line args */ while ((c=getopt(argc,argv,"d:")) != EOF) { option_exist = 1; switch(c) { case 'd': debug = 1; if (debug) printf("turn on debug mode.\n"); break; case '?': help_msg(); exit (1); default: fprintf (stderr, "unrecognized arg >%s<\n", optarg); help_msg(); exit (1); } } starttime.tv_sec = atol(argv[optind++]); starttime.tv_usec = atol(argv[optind++]); timeout.tv_sec = atol(argv[optind++]); timeout.tv_usec = atol(argv[optind++]); if (gethostname(coordinator, sizeof(coordinator)) { perror("gethostname error!"); exit(3); } strcpy(sender, argv[optind++]); strcpy(receiver, argv[optind++]); printf("starttime is set to %d sec %d usec\n", starttime.tv_sec, starttime.tv_usec); printf("timeout is set to %d sec %d usec\n", timeout.tv_sec, timeout.tv_usec); printf("coordinator is %s\n", coordinator); printf("sender is %s\n", sender); printf("receiver is %s\n", receiver); if ((toPort_no = atoi(argv[optind])) < 1025) toPort_no = 1025; printf("port_no=%d\n", toPort_no); /* create socket from which to read */ sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { perror("opening datagram socket"); exit(1); } /* create name structure using IPPORT_USERRESERVED */ coord.sin_family = AF_INET; coord.sin_addr.s_addr = IPPORT_USERRESERVED; coord.sin_port = htons(toPort_no); /* port no. < 1024 IPPORT_RESERVED is for privillege process */ /* here just initialized it, it will be assigned value after call */ /* getsockname() */ if (bind(sock, &coord, sizeof(struct sockaddr_in))) { perror("binding name to datagram socket"); exit(1); } length = sizeof(coord); if (getsockname(sock, &coord, &length)) { perror("getting socket name"); exit(1); } /* Find assigned port value and print out. */ printf("socket has port #%d\n", ntohs(from.sin_port)); /* construct name of the socket to send to * gethostbyname() returns a structure including the network address * of the specified host from /etc/hosts. The port number is taken from * the cmd line * create name structure for the UNIX domain socket * use the name from cmd argument */ hp = gethostbyname(coordinator); if (hp == 0) { fprintf(stderr, "%s: unknown host", receiver); exit(2); } bcopy(hp->h_addr, &coord.sin_addr, hp->h_length); coord.sin_family = AF_INET; coord.sin_port = htons(toPort_no); hp = gethostbyname(receiver); if (hp == 0) { fprintf(stderr, "%s: unknown host", receiver); exit(2); } bcopy(hp->h_addr, &dst.sin_addr, hp->h_length); dst.sin_family = AF_INET; dst.sin_port = htons(toPort_no); hp = gethostbyname(sender); if (hp == 0) { fprintf(stderr, "%s: unknown host", sender); exit(2); } bcopy(hp->h_addr, &src.sin_addr, hp->h_length); src.sin_family = AF_INET; src.sin_port = htons(toPort_no); upr.msgtype = UPROBEREQ; upr.sequence = sequenceno++; bcopy(&coord, &upr.coord, sizeof(coord)); bcopy(&src, &upr.src, sizeof(src)); bcopy(&dst, &upr.dst, sizeof(dst)); printf("upr.coord value is\n"); printsockaddr(&upr.coord); printf("upr.src value is\n"); printsockaddr(&upr.src); printf("upr.dst value is\n"); printsockaddr(&upr.dst); while (flag) { printf("Enter state %d\n", state); switch(state) { case 1: printf("Enter the message="); gets(toBuf); if (strcmp(toBuf, "$") == 0) state = 4; else { state = 2; r = retry; } break; case 2: if (sendto(sock, toBuf, strlen(toBuf), 0, &to, sizeof(to)) < 0) perror("sending datagram message"); state = 3; break; case 3: FD_ZERO(&readfds); FD_SET(0, &readfds); FD_SET(sock, &readfds); maxfdpl = sock+1; if ((i=select(maxfdpl, &readfds, 0, 0, &timeout)) < 0) { perror("select error"); exit(1); } if (i==0) { printf("timeout!"); if (r == 0) state = 4; else { r--; printf("retry %d\n", r); state = 2; } } if (FD_ISSET(0, &readfds)) { gets(buf); if (strcmp(buf, "$") == 0) state = 4; else { printf("user input =%s != $, neglect it\n", buf); } } if (FD_ISSET(sock, &readfds)) { n = recv(sock, fromBuf, sizeof(fromBuf), 0); if (n <= 0) { perror("error in recv()"); exit(1); } printf("received ack =%s\n", fromBuf); state = 1; } break; case 4: flag = 0; printf("exit to while loop\n"); break; default: printf("incorrect state!\n"); flag = 0; break; } } close(sock); }