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.

429 lines
10KB

  1. #include "announce.h"
  2. #include "network.h"
  3. #include <time.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>
  7. #include <unistd.h>
  8. #include <string.h>
  9. #include <avahi-client/client.h>
  10. #include <avahi-client/lookup.h>
  11. #include <avahi-client/publish.h>
  12. #include <avahi-common/alternative.h>
  13. #include <avahi-common/error.h>
  14. #include <avahi-common/malloc.h>
  15. #include <avahi-common/thread-watch.h>
  16. #define SERVICE_TYPE "_sdlbomber._udp"
  17. static AvahiClient *client = NULL;
  18. static AvahiThreadedPoll *threaded_poll = NULL;
  19. static AvahiEntryGroup *group = NULL;
  20. static AvahiServiceBrowser *browser = NULL;
  21. static char* name = NULL;
  22. static uint16_t port = 0;
  23. static uint32_t version = 0;
  24. static gamelistentry buffer_glentries[10];
  25. static int buffer_glsize = 0;
  26. static char buffer_glchanged[10];
  27. gamelistentry gamelistentries[10];
  28. int gamelistsize = 0;
  29. static void myerror(AvahiClient *c, const char *s) {
  30. fprintf(stderr, "Error in: %s\n (%s)", s, avahi_strerror(avahi_client_errno(client)));
  31. }
  32. static void create_services(AvahiClient *c);
  33. static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
  34. group = g;
  35. switch (state) {
  36. case AVAHI_ENTRY_GROUP_ESTABLISHED :
  37. break;
  38. case AVAHI_ENTRY_GROUP_COLLISION :
  39. {
  40. char *n = avahi_alternative_service_name(name);
  41. avahi_free(name);
  42. name = n;
  43. }
  44. /* And recreate the services */
  45. avahi_entry_group_reset(group);
  46. create_services(avahi_entry_group_get_client(g));
  47. break;
  48. case AVAHI_ENTRY_GROUP_FAILURE :
  49. fprintf(stderr, "Entry group failure: %s\n", avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
  50. avahi_threaded_poll_quit(threaded_poll);
  51. break;
  52. case AVAHI_ENTRY_GROUP_UNCOMMITED:
  53. case AVAHI_ENTRY_GROUP_REGISTERING:
  54. break;
  55. }
  56. }
  57. static void create_services(AvahiClient *c) {
  58. int ret;
  59. if (!group) {
  60. if (!(group = avahi_entry_group_new(c, entry_group_callback, NULL))) {
  61. myerror(c, "avahi_entry_group_new");
  62. goto fail;
  63. }
  64. }
  65. again:
  66. if (avahi_entry_group_is_empty(group)) {
  67. char buf_version[128];
  68. snprintf(buf_version, sizeof(buf_version), "version=%X", (unsigned int) version);
  69. if ((ret = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, name, SERVICE_TYPE, NULL, NULL, port, buf_version, NULL)) < 0) {
  70. if (ret == AVAHI_ERR_COLLISION)
  71. goto collision;
  72. fprintf(stderr, "Failed to add "SERVICE_TYPE": %s\n", avahi_strerror(ret));
  73. goto fail;
  74. }
  75. if ((ret = avahi_entry_group_commit(group)) < 0) {
  76. fprintf(stderr, "Failed to commit entry group: %s\n", avahi_strerror(ret));
  77. goto fail;
  78. }
  79. }
  80. return;
  81. collision:
  82. {
  83. char *n = avahi_alternative_service_name(name);
  84. avahi_free(name);
  85. name = n;
  86. }
  87. avahi_entry_group_reset(group);
  88. goto again;
  89. fail:
  90. avahi_threaded_poll_quit(threaded_poll);
  91. }
  92. static void client_callback(AvahiClient *c, AvahiClientState state, void * userdata) {
  93. client = c;
  94. switch (state) {
  95. case AVAHI_CLIENT_S_RUNNING:
  96. if (port != 0) create_services(c);
  97. break;
  98. case AVAHI_CLIENT_FAILURE:
  99. myerror(c, "client failure");
  100. avahi_threaded_poll_quit(threaded_poll);
  101. break;
  102. case AVAHI_CLIENT_S_COLLISION:
  103. case AVAHI_CLIENT_S_REGISTERING:
  104. if (group) {
  105. avahi_entry_group_reset(group);
  106. }
  107. break;
  108. case AVAHI_CLIENT_CONNECTING:
  109. break;
  110. }
  111. }
  112. int registergame(const char *playername, uint16_t p, const unsigned char v[4]) {
  113. if (name) avahi_free(name);
  114. name = avahi_strdup(playername);
  115. port = p;
  116. memcpy(&version, v, 4);
  117. version = htonl(version);
  118. avahi_threaded_poll_lock(threaded_poll);
  119. create_services(client);
  120. avahi_threaded_poll_unlock(threaded_poll);
  121. return 1;
  122. }
  123. void unregistergame(void) {
  124. port = 0;
  125. avahi_threaded_poll_lock(threaded_poll);
  126. if (group) avahi_entry_group_reset(group);
  127. group = NULL;
  128. avahi_threaded_poll_unlock(threaded_poll);
  129. }
  130. static void remove_game_with_name(const char *name) {
  131. int i;
  132. for (i = 0; i < buffer_glsize; i++) {
  133. if (0 == strcmp(buffer_glentries[i].name, name)) {
  134. /* Remove it */
  135. buffer_glsize--;
  136. if (i != buffer_glsize) {
  137. buffer_glentries[i] = buffer_glentries[buffer_glsize];
  138. buffer_glchanged[i] = 1;
  139. }
  140. }
  141. }
  142. }
  143. static void resolve_callback(AvahiServiceResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event,
  144. const char *name, const char *type, const char *domain, const char *host_name, const AvahiAddress *address,
  145. uint16_t port, AvahiStringList *txt, AvahiLookupResultFlags flags, void* userdata) {
  146. int i;
  147. uint32_t want_version;
  148. assert(r);
  149. if (protocol != AVAHI_PROTO_INET) goto done; /* ignore non IPv4 for now */
  150. if (buffer_glsize >= GAMELIST_MAXSIZE) goto done;
  151. memcpy(&want_version, gameversion, 4);
  152. want_version = htonl(want_version);
  153. /* Called whenever a service has been resolved successfully or timed out */
  154. switch (event) {
  155. case AVAHI_RESOLVER_FAILURE:
  156. fprintf(stderr, "(Resolver) Failed to resolve service '%s' of type '%s' in domain '%s': %s\n", name, type, domain, avahi_strerror(avahi_client_errno(avahi_service_resolver_get_client(r))));
  157. break;
  158. case AVAHI_RESOLVER_FOUND: {
  159. gamelistentry *ge;
  160. unsigned int version;
  161. int have_version = 0;
  162. AvahiStringList *psl;
  163. for (psl = txt ; psl ; psl = psl->next) {
  164. if (0 == strncmp("version=", (const char*) psl->text, 8)) {
  165. sscanf((const char*) psl->text, "version=%X", &version);
  166. if (version != want_version) goto done; /* version mismatch */
  167. have_version = 1;
  168. }
  169. }
  170. if (!have_version) goto done;
  171. remove_game_with_name(name);
  172. i = buffer_glsize++;
  173. ge = &buffer_glentries[i];
  174. buffer_glchanged[i] = 1;
  175. memset(ge, 0, sizeof(*ge));
  176. ge->netname.sin_addr.s_addr = address->data.ipv4.address;
  177. ge->netname.sin_family = AF_INET;
  178. ge->netname.sin_port = port;
  179. strncpy(ge->name, name, 15);
  180. }
  181. }
  182. done:
  183. avahi_service_resolver_free(r);
  184. }
  185. static void browse_callback(AvahiServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event,
  186. const char *name, const char *type, const char *domain, AvahiLookupResultFlags flags, void* userdata) {
  187. assert(b);
  188. /* Called whenever a new services becomes available on the LAN or is removed from the LAN */
  189. switch (event) {
  190. case AVAHI_BROWSER_FAILURE:
  191. fprintf(stderr, "(Browser) %s\n", avahi_strerror(avahi_client_errno(client)));
  192. avahi_threaded_poll_quit(threaded_poll);
  193. return;
  194. case AVAHI_BROWSER_NEW:
  195. /* fprintf(stderr, "(Browser) NEW: service '%s' of type '%s' in domain '%s'\n", name, type, domain); */
  196. /* We ignore the returned resolver object. In the callback
  197. function we free it. If the server is terminated before
  198. the callback function is called the server will free
  199. the resolver for us. */
  200. if (!(avahi_service_resolver_new(client, interface, protocol, name, type, domain, AVAHI_PROTO_UNSPEC, 0, resolve_callback, userdata)))
  201. fprintf(stderr, "Failed to resolve service '%s': %s\n", name, avahi_strerror(avahi_client_errno(client)));
  202. break;
  203. case AVAHI_BROWSER_REMOVE:
  204. remove_game_with_name(name);
  205. break;
  206. case AVAHI_BROWSER_ALL_FOR_NOW:
  207. break;
  208. case AVAHI_BROWSER_CACHE_EXHAUSTED:
  209. break;
  210. }
  211. }
  212. static void freefoundgames(void) {
  213. memset(gamelistentries, 0, sizeof(gamelistentries));
  214. memset(buffer_glentries, 0, sizeof(buffer_glentries));
  215. memset(buffer_glchanged, 0, sizeof(buffer_glchanged));
  216. gamelistsize = 0;
  217. buffer_glsize = 0;
  218. }
  219. int searchgames(void) {
  220. freefoundgames();
  221. avahi_threaded_poll_lock(threaded_poll);
  222. if (NULL == (browser = avahi_service_browser_new(client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, SERVICE_TYPE, NULL, 0, browse_callback, &gameversion))) {
  223. fprintf(stderr, "Failed to create service browser: %s\n", avahi_strerror(avahi_client_errno(client)));
  224. avahi_threaded_poll_unlock(threaded_poll);
  225. return 0;
  226. }
  227. avahi_threaded_poll_unlock(threaded_poll);
  228. usleep(200000);
  229. find_more_games();
  230. return 1;
  231. }
  232. int find_more_games(void) {
  233. int i, res = 0;
  234. avahi_threaded_poll_lock(threaded_poll);
  235. for (i = 0; i < buffer_glsize; i++) {
  236. if (!buffer_glchanged[i]) continue;
  237. buffer_glchanged[i] = 0;
  238. gamelistentries[i] = buffer_glentries[i];
  239. res = 1;
  240. }
  241. if (gamelistsize != buffer_glsize) {
  242. res = 1;
  243. gamelistsize = buffer_glsize;
  244. }
  245. avahi_threaded_poll_unlock(threaded_poll);
  246. return res;
  247. }
  248. void stop_search(void) {
  249. avahi_threaded_poll_lock(threaded_poll);
  250. avahi_service_browser_free(browser);
  251. avahi_threaded_poll_unlock(threaded_poll);
  252. }
  253. int initannouncer(void) {
  254. if (!(threaded_poll = avahi_threaded_poll_new())) {
  255. fprintf(stderr, "avahi_threaded_poll_new failed\n");
  256. return 0;
  257. }
  258. if (!(client = avahi_client_new(avahi_threaded_poll_get(threaded_poll), 0, client_callback, NULL, NULL))) {
  259. fprintf(stderr, "avahi_client_new failed\n");
  260. avahi_threaded_poll_free(threaded_poll);
  261. threaded_poll = NULL;
  262. return 0;
  263. }
  264. if (avahi_threaded_poll_start(threaded_poll) < 0) {
  265. fprintf(stderr, "avahi_threaded_poll_start failed\n");
  266. avahi_client_free(client);
  267. avahi_threaded_poll_free(threaded_poll);
  268. client = NULL;
  269. threaded_poll = NULL;
  270. return 0;
  271. }
  272. return 1;
  273. }
  274. void freeannouncer(void) {
  275. freefoundgames();
  276. avahi_threaded_poll_stop(threaded_poll);
  277. if (client) avahi_client_free(client);
  278. if (threaded_poll) avahi_threaded_poll_free(threaded_poll);
  279. client = NULL;
  280. threaded_poll = NULL;
  281. avahi_free(name);
  282. name = NULL;
  283. }
  284. #if 0
  285. int openmatcher()
  286. {
  287. struct hostent *hostptr;
  288. if(matcheropened) return 1;
  289. hostptr=gethostbyname(mname);
  290. if(!hostptr)
  291. {
  292. hostptr=gethostbyaddr(mname,strlen(mname),AF_INET);
  293. if(!hostptr)
  294. return 0;
  295. }
  296. memset(&matchername,0,sizeof(matchername));
  297. matchername.sin_family=AF_INET;
  298. matchername.sin_port=htons(PORT);
  299. memcpy(&matchername.sin_addr,hostptr->h_addr,hostptr->h_length);
  300. matcheropened=1;
  301. return 1;
  302. }
  303. int registergame()
  304. {
  305. long now;
  306. int size;
  307. long lastreg;
  308. if(!openmatcher()) return 0;
  309. pulseoff();
  310. now=longtime();
  311. lastreg=now-1;
  312. while(longtime()-now<10)
  313. {
  314. if(longtime()-lastreg>=1)
  315. {
  316. lastreg=longtime();
  317. putmsg(&matchername,regpacket,REGISTERLEN);
  318. }
  319. size=getmsg(1000);
  320. if(size<REGISTERLEN+1) continue;
  321. if(mesg[0]!=PKT_ACK) continue;
  322. if(memcmp(regpacket,mesg+1,REGISTERLEN)) continue;
  323. return 1;
  324. }
  325. return 0;
  326. }
  327. int unregistergame()
  328. {
  329. long now;
  330. int size;
  331. if(!openmatcher()) return 0;
  332. pulseoff();
  333. now=longtime();
  334. clearreg();
  335. while(longtime()-now<10)
  336. {
  337. putmsg(&matchername,regpacket,REGISTERLEN);
  338. size=getmsg(1000);
  339. if(size<REGISTERLEN+1) continue;
  340. if(mesg[0]!=PKT_ACK) continue;
  341. if(memcmp(regpacket,mesg+1,REGISTERLEN)) continue;
  342. return 1;
  343. }
  344. return 0;
  345. }
  346. #endif