Advertisement
Guest User

Untitled

a guest
Nov 29th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <errno.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <sys/time.h>
  9. #include <sys/select.h>
  10. #include <netinet/in.h>
  11. #include <arpa/inet.h>
  12. #include <netdb.h>
  13.  
  14. #define MYPORT "20444" // the port users will be connecting to
  15. #define MAXBUFLEN 1024 //maximum packet length
  16. #define SERVER_R 142.66.140.13 //Server to the "right" of current
  17. #define RTEX_R_PORT "20445" //Port for routing table exchange
  18.  
  19. typedef enum {false, true} bool;
  20.  
  21. /*struct to store packet fields into
  22. seq: sequence number;
  23. type: message type; send get ACK
  24. src: client's unique 10 digit number
  25. dst: destination's unique 10 digit number
  26. payload: the message being transferred, if there is any
  27. */
  28. struct packet
  29. {
  30. char seq[4];
  31. char type[5];
  32. char src[11];
  33. char dst[11];
  34. char payload[MAXBUFLEN];
  35. };
  36.  
  37. // get sockaddr, IPv4 or IPv6:
  38. void *get_in_addr(struct sockaddr *sa)
  39. {
  40. if (sa->sa_family == AF_INET) {
  41. return &(((struct sockaddr_in*)sa)->sin_addr);
  42. }
  43.  
  44. return &(((struct sockaddr_in6*)sa)->sin6_addr);
  45. }
  46.  
  47. int main(void)
  48. {
  49. //rr: reading from server to the "right"
  50. //rw: writing to the server to the "right"
  51. int sockfd, rtex_rr_sockfd, rtex_rw_sockfd, rv, rrr, rrw, numbytes, i, j, first, max_fd;
  52. struct addrinfo hints, *servinfo, *p, *p2, *p3;
  53. struct sockaddr_storage their_addr, right_addr;
  54. fd_set readfds, writefds;
  55. char buf[MAXBUFLEN];
  56. char temp_buf[MAXBUFLEN];
  57. char d_to_s[MAXBUFLEN];
  58. char *field;
  59. socklen_t addr_len;
  60. char s[INET6_ADDRSTRLEN];
  61. FILE *m_storage;
  62. struct packet inet_packet;
  63. static const struct packet EmptyPacket;
  64. static int rt[51][4];
  65. bool re_exists=false;
  66. bool rt_empty=true;
  67. struct timeval tv;
  68.  
  69. memset(&hints, 0, sizeof hints);//"zero out" the hints struct
  70. hints.ai_family = AF_UNSPEC; // set to AF_INET to force IPv4
  71. hints.ai_socktype = SOCK_DGRAM;
  72. hints.ai_flags = AI_PASSIVE; // use my IP
  73.  
  74. //prepare socket address structures and store them in servinfo and store in linked list
  75. if ((rv = getaddrinfo(NULL, MYPORT, &hints, &servinfo)) != 0) {
  76. fprintf(stderr, "getaddrinfo: %sn", gai_strerror(rv));
  77. return 1;
  78. }
  79.  
  80. // loop through all the results and bind to the first we can
  81. for(p = servinfo; p != NULL; p = p->ai_next) {
  82. if ((sockfd = socket(p->ai_family, p->ai_socktype,
  83. p->ai_protocol)) == -1) {
  84. perror("listener: socket");
  85. continue;
  86. }
  87.  
  88. if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
  89. close(sockfd);
  90. perror("listener: bind");
  91. continue;
  92. }
  93.  
  94. break;
  95. }
  96.  
  97. if (p == NULL) {
  98. fprintf(stderr, "listener: failed to bind socketn");
  99. return 2;
  100. }
  101.  
  102. if ((rrr = getaddrinfo(NULL, RTEX_R_PORT, &hints, &servinfo)) != 0) {
  103. fprintf(stderr, "getaddrinfo: %sn", gai_strerror(rrr));
  104. return 1;
  105. }
  106.  
  107. for(p2 = servinfo; p2 != NULL; p2 = p2->ai_next) {
  108. if ((rtex_rr_sockfd = socket(p2->ai_family, p2->ai_socktype,
  109. p2->ai_protocol)) == -1) {
  110. perror("listener: socket");
  111. continue;
  112. }
  113.  
  114. if (bind(rtex_rr_sockfd, p2->ai_addr, p2->ai_addrlen) == -1) {
  115. close(rtex_rr_sockfd);
  116. perror("listener: bind");
  117. continue;
  118. }
  119. break;
  120. }
  121.  
  122. if (p2 == NULL) {
  123. fprintf(stderr, "listener: failed to bind socketn");
  124. return 2;
  125. }
  126.  
  127. if((rrw = getaddrinfo(NULL, RTEX_R_PORT, &hints, &servinfo)) != 0) {
  128. fprintf(stderr, "getaddrinfo: %sn", gai_strerror(rrw));
  129. return 1;
  130. }
  131.  
  132. for(p3 = servinfo; p3 != NULL; p3 = p3->ai_next) {
  133. if((rtex_rw_sockfd = socket(p3->ai_family, p3->ai_socktype,
  134. p3->ai_protocol)) == -1) {
  135. perror("server: socket");
  136. continue;
  137. }
  138. break;
  139. }
  140.  
  141. if (p3 == NULL) {
  142. fprintf(stderr, "listener: failed to bind socketn");
  143. return 2;
  144. }
  145.  
  146. //free up memory no longer needed after binding has completed
  147. freeaddrinfo(servinfo);
  148.  
  149. FD_ZERO(&readfds);
  150. FD_ZERO(&writefds);
  151. FD_SET(rv, &readfds);
  152. FD_SET(rrr, &readfds);
  153. FD_SET(rrw, &writefds);
  154.  
  155. printf("Listen Moden");
  156.  
  157. //main while loop, listens for packets.
  158. //Upon receipt of packet, information is stored in a struct for processing.
  159. first=0;
  160. while(1)
  161. {
  162. i=0;
  163. inet_packet = EmptyPacket;
  164. rt_empty=true;
  165. tv.tv_sec = 50;
  166. if(rv > rrr && rv > rrw)
  167. max_fd = (rv + 1);
  168. else if(rrr > rv && rrr > rrw)
  169. max_fd = (rrr + 1);
  170. else if(rrw > rv && rrw > rrr)
  171. max_fd = (rrw + 1);
  172.  
  173. printf("before select...n");
  174. select(max_fd, &readfds, &writefds, NULL, NULL);
  175. printf("after select...n");
  176. addr_len = sizeof their_addr;
  177.  
  178. if(FD_ISSET(rv, &readfds))
  179. {
  180. printf("rv is set...n");
  181. if((numbytes = recvfrom(sockfd, buf, sizeof(buf), 0,
  182. (struct sockaddr *)&their_addr, &addr_len)) == -1) {
  183. perror("recvfrom");
  184. exit(1);
  185. }
  186. } else if(FD_ISSET(rrr, &readfds))
  187. {
  188. printf("rr read is set...n");
  189. if((numbytes = recvfrom(rtex_rr_sockfd, buf, sizeof(buf), 0,
  190. (struct sockaddr *)&right_addr, &addr_len)) == -1) {
  191. perror("recvfrom");
  192. exit(1);
  193. }
  194. } else if(FD_ISSET(rrw, &writefds))
  195. {
  196. printf("rr write is set...n");
  197. if((numbytes = sendto(rtex_rw_sockfd, inet_packet.payload, sizeof(inet_packet.payload),
  198. 0, p3->ai_addr, p3->ai_addrlen)) == -1) {
  199. perror("sendto rr");
  200. exit(1);
  201. }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement