muxnet.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* $Id: muxnet.c,v 1.8 2002/08/11 20:00:41 dijkstra Exp $ */
  2. /*
  3. * Copyright (c) 2001-2002 Willem Dijkstra
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * - Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following
  14. * disclaimer in the documentation and/or other materials provided
  15. * with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. *
  30. */
  31. #include <sys/types.h>
  32. #include <sys/socket.h>
  33. #include <netinet/in.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. #include "data.h"
  39. #include "error.h"
  40. #include "monmux.h"
  41. #include "muxnet.h"
  42. #include "net.h"
  43. #include "xmalloc.h"
  44. #include "share.h"
  45. /* Obtain a udp socket for incoming mon traffic */
  46. int
  47. getmonsocket(struct mux *mux)
  48. {
  49. struct sockaddr_in sockaddr;
  50. int sock;
  51. if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
  52. fatal("could not obtain socket: %.200s", strerror(errno));
  53. sockaddr.sin_family = AF_INET;
  54. sockaddr.sin_port = htons(mux->port);
  55. sockaddr.sin_addr.s_addr = htonl(mux->ip);
  56. bzero(&sockaddr.sin_zero, sizeof(sockaddr.sin_zero));
  57. if (bind(sock, (struct sockaddr *) &sockaddr,
  58. sizeof(struct sockaddr)) == -1)
  59. fatal("could not bind socket: %.200s", strerror(errno));
  60. mux->monsocket = sock;
  61. info("listening for incoming mon traffic on udp:%s:%d",
  62. mux->name, mux->port);
  63. return sock;
  64. }
  65. /* Obtain a listen socket for new clients */
  66. int
  67. getclientsocket(struct mux *mux)
  68. {
  69. struct sockaddr_in sockaddr;
  70. int sock;
  71. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  72. fatal("could not obtain socket: %.200s", strerror(errno));
  73. sockaddr.sin_family = AF_INET;
  74. sockaddr.sin_port = htons(mux->port);
  75. sockaddr.sin_addr.s_addr = htonl(mux->ip);
  76. bzero(&sockaddr.sin_zero, sizeof(sockaddr.sin_zero));
  77. if (bind(sock, (struct sockaddr *) &sockaddr,
  78. sizeof(struct sockaddr)) == -1)
  79. fatal("could not bind socket: %.200s", strerror(errno));
  80. if (listen(sock, MONMUX_TCPBACKLOG) == -1)
  81. fatal("could not listen to socket: %.200s", strerror(errno));
  82. fcntl(sock, O_NONBLOCK);
  83. mux->clientsocket = sock;
  84. info("listening for incoming connections on tcp:%s:%d",
  85. mux->name, mux->port);
  86. return sock;
  87. }
  88. /*
  89. * Wait for traffic (mon reports from a source in sourclist | clients trying to connect
  90. * Returns the <source> and <packet>
  91. * Silently forks off clienthandlers
  92. */
  93. void
  94. waitfortraffic(struct mux *mux, struct sourcelist *sourcelist,
  95. struct source **source, struct monpacket *packet)
  96. {
  97. fd_set readset;
  98. int socksactive;
  99. int maxsock;
  100. maxsock = ((mux->clientsocket > mux->monsocket) ?
  101. mux->clientsocket :
  102. mux->monsocket);
  103. maxsock++;
  104. for (;;) { /* FOREVER - until a valid mon packet is received */
  105. FD_ZERO(&readset);
  106. FD_SET(mux->clientsocket, &readset);
  107. FD_SET(mux->monsocket, &readset);
  108. socksactive = select(maxsock, &readset, NULL, NULL, NULL);
  109. if (socksactive != -1) {
  110. if (FD_ISSET(mux->clientsocket, &readset)) {
  111. spawn_client(mux->clientsocket);
  112. }
  113. if (FD_ISSET(mux->monsocket, &readset)) {
  114. if (recvmonpacket(mux, sourcelist, source, packet))
  115. return;
  116. }
  117. } else {
  118. if (errno == EINTR)
  119. return; /* signal received while waiting, bail out */
  120. }
  121. }
  122. }
  123. /* Receive a mon packet for mux. Checks if the source is allowed and returns the source found.
  124. * return 0 if no valid packet found
  125. */
  126. int
  127. recvmonpacket(struct mux *mux, struct sourcelist *sourcelist,
  128. struct source **source, struct monpacket *packet)
  129. {
  130. struct sockaddr_in sind;
  131. u_int32_t sourceaddr;
  132. int size;
  133. unsigned int received;
  134. socklen_t sl;
  135. int tries;
  136. u_int32_t crc;
  137. received = 0;
  138. tries = 0;
  139. do {
  140. sl = sizeof(sind);
  141. size = recvfrom(mux->monsocket, (void *)packet + received,
  142. sizeof(struct monpacket) - received,
  143. 0, (struct sockaddr *)&sind, &sl);
  144. if (size > 0)
  145. received += size;
  146. tries++;
  147. } while ((size == -1) &&
  148. (errno == EAGAIN || errno == EINTR) &&
  149. (tries < MONMUX_MAXREADTRIES) &&
  150. (received < sizeof(struct monpacket)));
  151. if ((size == -1) &&
  152. errno)
  153. warning("recvfrom failed: %s", strerror(errno));
  154. sourceaddr = ntohl((u_int32_t)sind.sin_addr.s_addr);
  155. *source = find_source_ip(sourcelist, sourceaddr);
  156. if (*source == NULL) {
  157. debug("ignored data from %u.%u.%u.%u",
  158. IPAS4BYTES(sourceaddr));
  159. return 0;
  160. } else {
  161. /* check crc */
  162. crc = ntohl(packet->header.crc);
  163. packet->header.crc = 0;
  164. crc ^= crc32(packet, received);
  165. if ( crc != 0 ) {
  166. warning("ignored packet with bad crc from %u.%u.%u.%u",
  167. IPAS4BYTES(sourceaddr));
  168. return 0;
  169. }
  170. /* check packet version */
  171. if (packet->header.mon_version != MON_PACKET_VER) {
  172. warning("ignored packet with illegal type %d",
  173. packet->header.mon_version);
  174. return 0;
  175. } else {
  176. /* rewrite structs to host order */
  177. packet->header.length = ntohs(packet->header.length);
  178. packet->header.crc = ntohs(packet->header.crc);
  179. packet->header.timestamp = ntohq(packet->header.timestamp);
  180. if (flag_debug)
  181. debug("good data received from %u.%u.%u.%u",
  182. IPAS4BYTES(sourceaddr));
  183. return 1; /* good packet received */
  184. }
  185. }
  186. }
  187. int
  188. acceptconnection(int sock)
  189. {
  190. struct sockaddr_in sind;
  191. socklen_t len;
  192. u_int32_t clientaddr;
  193. int clientsock;
  194. if ((clientsock = accept(sock, (struct sockaddr *)&sind, &len)) < 0)
  195. fatal("Failed to accept an incoming connection. (.200%s)",
  196. strerror(errno));
  197. clientaddr = ntohl((u_int32_t)sind.sin_addr.s_addr);
  198. info("Accepted incoming client connection from %u.%u.%u.%u",
  199. IPAS4BYTES(clientaddr));
  200. return clientsock;
  201. }