Linux HLSW LAN Master
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.

258 lines
5.2 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <getopt.h>
  6. #include <sys/time.h>
  7. #include <sys/types.h>
  8. #include <sys/socket.h>
  9. #include <sys/ioctl.h>
  10. #include <arpa/inet.h>
  11. #include <netinet/in.h>
  12. #include <netinet/ip.h>
  13. #include <inttypes.h>
  14. struct _entry {
  15. uint16_t gameid;
  16. uint32_t ip;
  17. uint16_t port1;
  18. uint16_t port2;
  19. } __attribute__ ((packed));
  20. static char hlswheader[] = "\xFF\xFF\xFF\xFFHLSWLANSEARCH";
  21. #define id2name_count (sizeof(id2name) / sizeof(char *) -1)
  22. static char *id2name[] = {
  23. "Unknown", // 0
  24. "Halflife",
  25. "Quake 1",
  26. "Quake 2",
  27. "Q3Comp",
  28. "Unreal Tournament", // 5
  29. "Quake 3 Arena",
  30. "Elite Force",
  31. "Return to Castle Wolfenstein",
  32. "GSProt",
  33. "Command & Conquer Renegade", // 10
  34. "Medal of Honor: Allied Assault",
  35. "Jedi Knight 2",
  36. "Soldier of Fortune",
  37. "Unreal Tournament 2003",
  38. "America's Army: Operations", // 15
  39. "Battlefield 1942",
  40. "Alien vs. Predator 2",
  41. "Rune",
  42. "Project IGI2: Covert Strike",
  43. "Never Winter Nights", // 20
  44. "Medal of Honor: Allied Assault Spearhead",
  45. "Operation Flashpoint",
  46. "Operation Flashpoint Resistance",
  47. "Devastation",
  48. "Wolfenstein - Enemy Territory", //25
  49. "Elite Force 2",
  50. "Jedi Knight 3",
  51. "Medal of Honor: Allied Assault Breakthrough",
  52. "Tribes 2",
  53. "Halo", // 30
  54. "Call of Duty",
  55. "Savage: The Battle for Newerth",
  56. "Unreal Tournament 2004",
  57. "HLSteam",
  58. "Battlefield Vietnam", // 35
  59. "GS2Prot",
  60. "Pain Killer",
  61. "Doom 3",
  62. "OGPProt",
  63. "Halflife 2", // 40
  64. "Tribes Vengeance",
  65. "Call of Duty: United Offensive",
  66. "Starwars: Battlefront (?)",
  67. "SWAT 4",
  68. "Battlefield 2", // 45
  69. "unknown",
  70. "Quake 4",
  71. "Call of Duty 2",
  72. "unknown",
  73. "FEAR", // 50
  74. "Warsow(?)"
  75. };
  76. static int sock, verbose = 0;
  77. static void parse_pkt(struct sockaddr_in *src, void *pkt, unsigned int size)
  78. {
  79. struct _entry *server;
  80. struct in_addr tmp;
  81. if (size < sizeof(hlswheader) || strncmp(pkt, hlswheader, sizeof(hlswheader))) {
  82. printf("received INVALID packet from: %15s:%-5d size=%d\n",
  83. inet_ntoa(src->sin_addr), ntohs(src->sin_port), size);
  84. } else {
  85. printf("received hlsw packet from: %15s:%-5d size=%d count=%d\n",
  86. inet_ntoa(src->sin_addr), ntohs(src->sin_port), size,
  87. ((size > sizeof(hlswheader)) ? (size - sizeof(hlswheader)) / sizeof(struct _entry) : 0));
  88. if (verbose) {
  89. server = pkt + sizeof(hlswheader);
  90. while ((void *)server < pkt + size) {
  91. tmp.s_addr = server->ip;
  92. printf(" ip=%15s port1=%5d port2=%5d gameid=%2d (%s)\n",
  93. inet_ntoa(tmp), server->port1, server->port2,
  94. server->gameid, server->gameid <= id2name_count ? id2name[server->gameid] : "unknown");
  95. server++;
  96. }
  97. }
  98. }
  99. }
  100. static int scan_init()
  101. {
  102. sock = socket(PF_INET, SOCK_DGRAM, 0);
  103. if (sock < 0) {
  104. perror("socket()");
  105. return -1;
  106. }
  107. unsigned int i = 1;
  108. if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &i, sizeof(i))) {
  109. perror("setsockopt()");
  110. return -1;
  111. }
  112. return 0;
  113. }
  114. static int scan_transmit(struct sockaddr_in *dst)
  115. {
  116. if (sendto(sock, hlswheader, sizeof(hlswheader), 0, (struct sockaddr *)dst, sizeof(struct sockaddr_in)) < 0) {
  117. perror("sendto()");
  118. return -1;
  119. }
  120. return 0;
  121. }
  122. static int scan_receive()
  123. {
  124. struct sockaddr_in src;
  125. struct timeval tv;
  126. fd_set fdsel;
  127. unsigned int i = 1, recvsize;
  128. void *pkt;
  129. FD_ZERO(&fdsel);
  130. FD_SET(sock, &fdsel);
  131. tv.tv_sec = 1;
  132. tv.tv_usec = 0;
  133. /* timeout */
  134. while (tv.tv_sec > 0 || tv.tv_usec > 0) {
  135. if (select(FD_SETSIZE, &fdsel, NULL, NULL, &tv) < 0) {
  136. perror("select()");
  137. return -1;
  138. }
  139. /* get packetsize */
  140. if (ioctl(sock, FIONREAD, &recvsize) == -1) {
  141. perror("ioctl()");
  142. return -1;
  143. }
  144. if (recvsize > 0) {
  145. if (!(pkt = malloc(recvsize))) {
  146. perror("malloc()");
  147. return -1;
  148. }
  149. i = sizeof(struct sockaddr_in);
  150. recvsize = recvfrom(sock, pkt, recvsize, 0, (struct sockaddr *)&src, &i);
  151. parse_pkt(&src, pkt, recvsize);
  152. free(pkt);
  153. }
  154. }
  155. return 0;
  156. }
  157. static struct option opts[] = {
  158. {"destination", 1, 0, 'd'},
  159. {"intervall", 1, 0, 'i'},
  160. {"verbose", 0, 0, 'v'},
  161. {"help", 0, 0, 'h'},
  162. {0, 0, 0, 0}
  163. };
  164. int main(int argc, char *argv[])
  165. {
  166. struct sockaddr_in dst;
  167. int arg = 0, code = 0;
  168. int freq = -1;
  169. dst.sin_family = AF_INET;
  170. dst.sin_port = htons(7140);
  171. inet_aton("255.255.255.255", &dst.sin_addr);
  172. while (code != -1) {
  173. code = getopt_long(argc, argv, "d:i:vh", opts, &arg);
  174. switch (code) {
  175. case 'd': /* destination */
  176. if (inet_pton(dst.sin_family, optarg, &dst.sin_addr) <= 0) {
  177. fprintf(stderr, "invalid destination: %s\n", optarg);
  178. exit(-1);
  179. }
  180. break;
  181. case 'i': /* intervall */
  182. freq = atoi(optarg);
  183. if (freq < 1) {
  184. fprintf(stderr, "invalid interval: %s\n", optarg);
  185. exit(-1);
  186. }
  187. break;
  188. case 'v': /* verbose */
  189. verbose = 1;
  190. break;
  191. case 'h': /* help */
  192. printf("Usage: masterquery [options]\n"
  193. "Options: \n"
  194. " --destination -d scan destination <ip>\n"
  195. " --intervall -i scan intervall in seconds\n"
  196. " --verbose -v verbose: show packet content\n"
  197. " --help -h this help\n"
  198. "\n");
  199. exit(0);
  200. break;
  201. case '?': /* error */
  202. exit(-1);
  203. break;
  204. default: /* unknown / all options parsed */
  205. break;
  206. }
  207. }
  208. if (scan_init())
  209. exit(-1);
  210. do {
  211. if (scan_transmit(&dst))
  212. exit(-1);
  213. if (scan_receive())
  214. exit(-1);
  215. } while (freq >= 1 && !sleep(freq -1));
  216. close(sock);
  217. return 0;
  218. }