/* $Header: /users/server/students/cs522/project/sharedraw/RCS/ua.c,v 1.3 1992/11/02 18:59:27 chow Exp $ */ /* sender for internet datagram demo */ /* with select and time-out */ #include #include #include #include #include #include #include #include "msg.h" #define ENDPACKET "end" printmsg(msg, cc) char *msg; int cc; { int i; printf("Got Message:%s\n", msg); for (i= 0; i <#timeout.usec> hostname portno\n"); printf(" option: d set debug mode\n"); } main(argc, argv) int argc; char *argv[]; { int sockTo; int sockFrom; int sockToGui; int sockFromGui; int sockToRUA; struct sockaddr_in to; struct sockaddr_in toRUA; int connected; struct sockaddr_in from; struct hostent *hp, *gethostbyname(); struct sockaddr_un name; struct sockaddr_un toGuiName; union Record r; int maxfdpl; struct timeval timeout; fd_set fdvar; long atol(); int setTimeOut = 1; /* flag for setting timeout */ int counter = 0; int n; int i; int pcount = 0; /* count of packets sent */ int quitFlag = 1; char toBuf[1024]; char fromBuf[1024]; char toGuiSocketName[40]; char fromGuiSocketName[40]; char hostname[64]; extern char *optarg; extern int optind; int option_exist = 0; int c; int debug = 0; char *rcv_name; /* receiver's socket name */ int toPort_no; int length; int flag; extern char* getlogin(); char* login = getlogin(); /* 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); } } if (argc < 5) { help_msg(); exit (1); } /* create a unix domain socket to relay msg to Gui */ sockToGui = initSockToGui(); /* create name structure for the UNIX domain socket */ toGuiName.sun_family = AF_UNIX; sprintf(toGuiSocketName, "/tmp/cs522.%s.UA2Gui", login); strcpy(toGuiName.sun_path, toGuiSocketName); printf("socket to gui-->%s\n", toGuiSocketName); /* create a unix domain socket from which to read */ sockFromGui = socket(AF_UNIX, SOCK_DGRAM, 0); if (sockFromGui < 0) { perror("opening datagram socket"); exit(1); } name.sun_family = AF_UNIX; sprintf(fromGuiSocketName, "/tmp/cs522.%s.Gui2UA", login); strcpy(name.sun_path, fromGuiSocketName); if (bind(sockFromGui, &name, sizeof(struct sockaddr_un))) { perror("binding name to datagram socket"); exit(1); } timeout.tv_sec = atol(argv[optind++]); timeout.tv_usec = atol(argv[optind++]); printf("timeout is set to %ld sec., %ld microsec.\n", timeout.tv_sec, timeout.tv_usec); rcv_name = argv[optind++]; if ((toPort_no = atoi(argv[optind])) < 1025) toPort_no = 1025; if (debug) printf("The receiver's host name is %s, port_no=%d\n", rcv_name, toPort_no); /* create socket from which to read */ sockFrom = socket(AF_INET, SOCK_DGRAM, 0); if (sockFrom < 0) { perror("opening datagram socket"); exit(1); } /* create name structure with wildcard using INADDR_ANY */ from.sin_family = AF_INET; from.sin_addr.s_addr = INADDR_ANY; from.sin_port = 0; /* port no. < 1024 IPPORT_RESERVED is for privillege process */ /* here just initialized it, it will be assigned value after call */ /* getsockname() */ if (bind(sockFrom, &from, sizeof(struct sockaddr_in))) { perror("binding name to datagram socket"); exit(1); } length = sizeof(from); if (getsockname(sockFrom, &from, &length)) { perror("getting socket name"); exit(1); } /* Find assigned port value and print out. */ printf("socket has port #%d\n", ntohs(from.sin_port)); /* create socket on which to sent */ sockTo = socket(AF_INET, SOCK_DGRAM, 0); if (sockTo < 0) { perror("opening datagram socket"); exit(1); } to.sin_family = AF_INET; to.sin_addr.s_addr = INADDR_ANY; to.sin_port = 0; if (bind(sockTo, &to, sizeof(to))) { perror("binding datagram socket"); exit(1); } /* 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 internet domain socket * use the name from cmd argument */ hp = gethostbyname(rcv_name); if (hp == 0) { fprintf(stderr, "%s: unknown host",rcv_name); exit(2); } bcopy(hp->h_addr, &to.sin_addr, hp->h_length); to.sin_family = AF_INET; to.sin_port = htons(toPort_no); /*send message */ gethostname(hostname, 64); printf("hostname=%s\n", hostname); printf("sending registration message: %s %d %s\n", hostname, ntohs(from.sin_port), login); sprintf(toBuf, "%s %d %s", hostname, ntohs(from.sin_port), login); if (sendto(sockTo, toBuf, strlen(toBuf), 0, &to, sizeof(to)) < 0) perror("sending datagram message"); /* create socket on which to sent data to remote user agent */ sockToRUA = socket(AF_INET, SOCK_DGRAM, 0); if (sockToRUA < 0) { perror("opening datagram socket"); exit(1); } toRUA.sin_family = AF_INET; toRUA.sin_addr.s_addr = INADDR_ANY; toRUA.sin_port = 0; if (bind(sockToRUA, &toRUA, sizeof(toRUA))) { perror("binding datagram socket"); exit(1); } maxfdpl = (sockFrom > sockFromGui) ? sockFrom+1 : sockFromGui+1; printf("maxfdpl=%d\n", maxfdpl); while (quitFlag) { if (debug) printf("The number of file descriptors to be tested is %d\n", maxfdpl+1); FD_ZERO(&fdvar); /* initialize the set -- all bits off */ FD_SET(sockFrom, &fdvar); /* turn on bit for fd sockFrom */ FD_SET(sockFromGui, &fdvar); /* turn on bit for fd sockFromGui */ if (setTimeOut) { if ((i=select(maxfdpl, &fdvar, (fd_set *) 0, (fd_set *) 0, &timeout)) < 0) perror("select error"); if (i == 0) { printf("Time out! The receiver does not respond.\n"); exit(1); } } else { /* wait indefinitely, no timeout */ if ((i=select(maxfdpl, &fdvar, (fd_set *) 0, (fd_set *) 0, (struct timeval *) 0)) < 0) perror("select error"); } if (FD_ISSET(sockFromGui, &fdvar)) { if (debug) printf("got message from sockFromGui\n"); /* n = recv(sockFromGui, fromBuf, 1024, flag); */ bzero(&(r.pr.msgType), sizeof(r)); n = recv(sockFromGui, &(r.pr.msgType), sizeof(r), flag); if (n < 0) perror("receiving datagram message"); printmsg(&(r.pr.msgType), n); switch (r.pr.msgType) { case PEN: case ERASER: case CURSOR: case TEXT: case CLEAN: case DISC: case BOB: case NOB: /* relay the message to the connected user */ if (connected == 0) break; /* not connected */ if (r.pr.msgType == DISC) connected = 0; if (sendto(sockToRUA, &(r.pr.msgType), n, 0, &toRUA, sizeof(toRUA)) < 0) perror("sending datagram message"); break; case QUIT: printf("The user is exiting.\n"); quitFlag = 0; break; case CONN: /* get the conn msg, forward it to IS */ /* Here the code simulates the NAK or ACK msg comes back */ counter++; if (counter%3==1) { struct ACKRecord ar; ar.msgType = ACK; strcpy(ar.originator, r.nr.originator); strcpy(ar.userLogin, r.nr.name); /* hostname and portId field is not used between GUI and UA */ printf("send ack record, ar.originator=%s, ar.userLogin=%s\n", ar.originator, ar.userLogin); connected = 1; /* relay this message to GUI */ if (sendto(sockToGui, &(ar.msgType), sizeof(struct ACKRecord), 0, &toGuiName, sizeof(toGuiName)) < 0) perror("sending datagram message"); } else if (counter%3==2) { struct NAKRecord nakr; nakr.msgType = NAK; strcpy(nakr.originator, r.nr.originator); strcpy(nakr.userLogin, r.nr.name); strcpy(nakr.reason, "reject"); /* hostname and portId field is not used between GUI and UA */ printf("send nak record,nakr.originator=%s,nakr.userLogin=%s\n", nakr.originator, nakr.userLogin); if (sendto(sockToGui, &(nakr.msgType),sizeof(struct NAKRecord), 0, &toGuiName, sizeof(toGuiName)) < 0) perror("sending datagram message"); } else { strcpy(r.nr.originator, "chow"); strcpy(r.nr.name, getlogin()); printf("send a connect message to gui, with originator=%s\n", r.nr.originator); if (sendto(sockToGui, &(r.nr.msgType),sizeof(r.nr), 0, &toGuiName, sizeof(toGuiName)) < 0) perror("sending datagram message"); } break; case ACK: /* user accepts the connection request */ connected = 1; /* send response back to IS */ if (sendto(sockTo, &(r.ackr.msgType), n, 0, &to, sizeof(to)) < 0) perror("sending datagram message"); break; case NAK: /* user rejects the connection request */ /* send response back to IS */ if (sendto(sockTo, &(r.nakr.msgType), n, 0, &to, sizeof(to)) < 0) perror("sending datagram message"); break; default: printf("undefined msg type.\n"); break; } } if (FD_ISSET(sockFrom, &fdvar)) { bzero(&(r.pr.msgType), sizeof(r)); n = recv(sockFrom, &(r.pr.msgType), sizeof(r), flag); if (n < 0) perror("receiving datagram message"); setTimeOut = 0; printmsg(&(r.ackr.msgType), n); switch (r.ackr.msgType) { case ACK: connected = 1; /* The message from IS contains the hostname and portId of the remote user agent. You need to save them for the datagram socket between the two UAs */ /* The code here did not show to set up the datagram sockets for sending and receiving msgs among two UAs */ /* For simplicity, we use the same ACK message structure between UA and GUI. In reality, GUI does not need the hostname and portId about the destination user */ /* relay the ACK message to GUI */ if (sendto(sockToGui, &(r.ackr.msgType), n, 0, &toGuiName, sizeof(toGuiName)) < 0) perror("sending datagram message"); break; case NAK: /* relay the NAK message to GUI */ if (sendto(sockToGui, &(r.nakr.msgType), n, 0, &toGuiName, sizeof(toGuiName)) < 0) perror("sending datagram message"); break; case PEN: case ERASER: case CURSOR: case TEXT: case CLEAN: case DISC: case BOB: case NOB: /* relay the message from RUA to the GUI */ if (connected == 0) break; /* not connected */ if (r.dr.msgType == DISC) connected = 0; if (sendto(sockToGui, &(r.pr.msgType), n, 0, &to, sizeof(to)) < 0) perror("sending datagram message"); break; case CONN: /* request from IS for permission to connect */ if (sendto(sockToGui, &(r.pr.msgType), n, 0, &to, sizeof(to)) < 0) perror("sending datagram message"); break; default: printf("undefined msg type.\n"); break; } } } close(sockToGui); unlink(toGuiSocketName); close(sockFromGui); unlink(fromGuiSocketName); close(sockFrom); close(sockTo); close(sockToRUA); }