Broadcast forwarder for ip-bound gameservers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bcastfwd.c 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <netinet/in.h>
  6. #include <netinet/ip.h>
  7. #include <netinet/udp.h>
  8. #include <time.h>
  9. #include "configfile.h"
  10. #include "event.h"
  11. #include "list.h"
  12. #include "logging.h"
  13. #include "sockaddr.h"
  14. struct host_entry {
  15. struct list_head list;
  16. struct sockaddr_in address;
  17. int flags;
  18. };
  19. #define HOST_ACTIVE 0x0001
  20. #define HOST_LOCAL 0x0010
  21. #define HOST_REMOTE 0x0020
  22. static LIST_HEAD(host_list);
  23. static int raw_sock;
  24. static int listen_sock;
  25. static struct event_fd *listen_event;
  26. static struct event_timeout *local_timeout;
  27. static struct event_timeout *remote_timeout;
  28. static int add_host_cb(const char *parameter, void *privdata)
  29. {
  30. struct host_entry *entry = malloc(sizeof(struct host_entry));
  31. if (entry == NULL) {
  32. log_print(LOG_WARN, "add_host_cb(): out of memory");
  33. return -1;
  34. }
  35. if (parse_sockaddr(parameter, &entry->address)) {
  36. log_print(LOG_WARN, "add_host_cb(): invalid host: '%s'", parameter);
  37. free(entry);
  38. return -1;
  39. }
  40. entry->flags = (int)privdata | HOST_ACTIVE;
  41. log_print(LOG_INFO, "Forwarding to %s (%s)", get_sockaddr_buf(&entry->address), (entry->flags == HOST_LOCAL) ? "local" : "remote");
  42. list_add_tail(&entry->list, &host_list);
  43. return 0;
  44. }
  45. static int check_local_host_cb(void *privdata)
  46. {
  47. struct host_entry *entry;
  48. list_for_each_entry(entry, &host_list, list) {
  49. if (!(entry->flags & HOST_LOCAL))
  50. continue;
  51. int sock = socket(AF_INET, SOCK_DGRAM, 0);
  52. if (sock < 0) {
  53. log_print(LOG_WARN, "check_local_host_cb(): socket()");
  54. continue;
  55. }
  56. if (bind(sock,(struct sockaddr *)&entry->address, sizeof(struct sockaddr_in)) < 0) {
  57. /* port in use, host seems to be active */
  58. if (!(entry->flags & HOST_ACTIVE)) {
  59. log_print(LOG_INFO, "Host active: %s", get_sockaddr_buf(&entry->address));
  60. entry->flags |= HOST_ACTIVE;
  61. }
  62. } else {
  63. /* port not in use, host can not be active */
  64. if (entry->flags & HOST_ACTIVE) {
  65. log_print(LOG_INFO, "Host not active: %s", get_sockaddr_buf(&entry->address));
  66. entry->flags &= ~(HOST_ACTIVE);
  67. }
  68. }
  69. close(sock);
  70. }
  71. return 0;
  72. }
  73. #if 0
  74. static int check_remote_host_cb(void *privdata)
  75. {
  76. // TODO: don't know how to check
  77. return 0;
  78. }
  79. #endif
  80. static int send_raw_udp(int fd, struct sockaddr_in *src, struct sockaddr_in *dst, const char *buf, int offset, int size)
  81. {
  82. if (offset < sizeof(struct iphdr) + sizeof(struct udphdr))
  83. return -1;
  84. struct iphdr *ip = (struct iphdr *)buf;
  85. struct udphdr *udp = (struct udphdr *)(buf + sizeof(struct iphdr));
  86. ip->version = 4; /* IPv4 */
  87. ip->ihl = sizeof(struct iphdr) / sizeof(int);
  88. ip->tos = 0;
  89. ip->id = 0;
  90. ip->frag_off = 0;
  91. ip->ttl = 64;
  92. ip->protocol = IPPROTO_UDP;
  93. ip->check = 0; /* computed by kernel */
  94. ip->saddr = src->sin_addr.s_addr;
  95. ip->daddr = dst->sin_addr.s_addr;
  96. int packetsize = sizeof(struct iphdr) + sizeof(struct udphdr) + size;
  97. ip->tot_len = htons(packetsize);
  98. udp->source = src->sin_port;
  99. udp->dest = dst->sin_port;
  100. udp->check = 0; /* ignore */
  101. udp->len = htons(sizeof(struct udphdr) + size);
  102. if (sendto(fd, buf, packetsize, 0, (struct sockaddr *)dst, sizeof(struct sockaddr)) < 0) {
  103. log_print(LOG_WARN, "send_raw_udp(): sendto()");
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. static int listen_cb(int fd, void *privdata)
  109. {
  110. struct sockaddr_in src_addr;
  111. char buf[512];
  112. socklen_t i = sizeof(src_addr);
  113. int offset = sizeof(struct iphdr) + sizeof(struct udphdr);
  114. int len = recvfrom(fd, buf + offset, sizeof(buf) - offset, 0, (struct sockaddr *)&src_addr, &i);
  115. if (len <= 0) {
  116. log_print(LOG_WARN, "listen_cb(): recvfrom()");
  117. return -1;
  118. }
  119. struct host_entry *entry;
  120. list_for_each_entry(entry, &host_list, list) {
  121. if (!(entry->flags & HOST_ACTIVE))
  122. continue;
  123. send_raw_udp(raw_sock, &src_addr, &entry->address, buf, offset, len);
  124. }
  125. return 0;
  126. }
  127. int bcast_close(void)
  128. {
  129. if (remote_timeout != NULL) {
  130. event_remove_timeout(remote_timeout);
  131. remote_timeout = NULL;
  132. }
  133. if (local_timeout != NULL) {
  134. event_remove_timeout(local_timeout);
  135. local_timeout = NULL;
  136. }
  137. struct host_entry *entry, *tmp;
  138. list_for_each_entry_safe(entry, tmp, &host_list, list) {
  139. list_del(&entry->list);
  140. free(entry);
  141. }
  142. event_remove_fd(listen_event);
  143. close(listen_sock);
  144. close(raw_sock);
  145. return 0;
  146. }
  147. int bcast_init(void)
  148. {
  149. raw_sock = socket(PF_INET, SOCK_RAW, IPPROTO_UDP);
  150. if (raw_sock < 0) {
  151. log_print(LOG_ERROR, "bcast_init(): socket(raw)");
  152. return -1;
  153. }
  154. int i = 1;
  155. if (setsockopt(raw_sock, IPPROTO_IP, IP_HDRINCL, &i, sizeof(i)) < 0 ) {
  156. log_print(LOG_ERROR, "bcast_init(): setsockopt(IP_HDRINCL)");
  157. close(raw_sock);
  158. return -1;
  159. }
  160. int listen_port = 0;
  161. if (config_get_int("global", "listen", &listen_port, 0)) {
  162. log_print(LOG_ERROR, "bcast_init(): Invalid listen port: %d", listen_port);
  163. close(raw_sock);
  164. return -1;
  165. }
  166. struct sockaddr_in listen_addr;
  167. listen_addr.sin_family = AF_INET,
  168. listen_addr.sin_addr.s_addr = INADDR_ANY;
  169. listen_addr.sin_port = htons(listen_port);
  170. listen_sock = socket(AF_INET, SOCK_DGRAM, 0);
  171. if (listen_sock < 0) {
  172. log_print(LOG_ERROR, "bcast_init(): socket(listen)");
  173. close(raw_sock);
  174. return -1;
  175. }
  176. if (bind(listen_sock, (struct sockaddr *)&listen_addr, sizeof(struct sockaddr_in)) < 0) {
  177. log_print(LOG_ERROR, "bcast_init(): bind()");
  178. close(listen_sock);
  179. close(raw_sock);
  180. return -1;
  181. }
  182. log_print(LOG_INFO, "Listening on %s", get_sockaddr_buf(&listen_addr));
  183. listen_event = event_add_readfd(NULL, listen_sock, listen_cb, NULL);
  184. int localcount, remotecount;
  185. localcount = config_get_strings("local", "host", add_host_cb, (void *)HOST_LOCAL);
  186. remotecount = config_get_strings("remote", "host", add_host_cb, (void *)HOST_REMOTE);
  187. if ((localcount + remotecount) == 0) {
  188. log_print(LOG_ERROR, "bcast_init(): no hosts given");
  189. event_remove_fd(listen_event);
  190. close(listen_sock);
  191. close(raw_sock);
  192. return -1;
  193. }
  194. int check_interval = 0;
  195. struct timeval tv;
  196. tv.tv_usec = 0;
  197. if (localcount > 0) {
  198. config_get_int("local", "check_interval", &check_interval, 10);
  199. tv.tv_sec = check_interval;
  200. local_timeout = event_add_timeout(&tv, check_local_host_cb, NULL);
  201. }
  202. #if 0
  203. if (remotecount > 0) {
  204. config_get_int("remote", "check_interval", &check_interval, 60);
  205. tv.tv_sec = check_interval;
  206. remote_timeout = event_add_timeout(&tv, check_remote_host_cb, NULL);
  207. }
  208. #endif
  209. return 0;
  210. }