Advertisement
memcpy

tf2 info ret.

Jan 11th, 2012
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. #include <netdb.h>
  8. #include <arpa/inet.h>
  9. #include <netinet/udp.h>
  10.  
  11. #define PORT_STR_LEN 5
  12.  
  13. struct tfc_host_info {
  14.     int tfc_fd;
  15.     char tfc_ip[INET_ADDRSTRLEN + 1];
  16.     short tfc_port;
  17.     struct sockaddr_in tfc_addr;
  18. };
  19.  
  20.  
  21. struct tfc_tx_info {
  22.     uint32_t id;
  23.     uint8_t pad;
  24.     char query[20];
  25. } __attribute__((__packed__));
  26.  
  27. struct tfc_rx_info {
  28.     uint32_t id;
  29.     uint8_t pad;
  30.     uint8_t version;
  31.     char *srv_name;
  32.     char *map;
  33.     char *game_directory;
  34.     char *game_dsc;
  35.    
  36. #define TFC_DEDICATED_SERVER        310
  37. #define TFC_LINUX_DEDICATED_SERVER  311
  38. #define TFC_APPID_TF2           440
  39. #define TFC_TF2_CONTENT         441
  40. #define TFC_TF2_MATERIALS       442
  41. #define TFC_TF2_CLIENT_CONTENT      443
  42.     uint16_t app_id;
  43.    
  44.     uint8_t num_players;
  45.     uint8_t max_players;
  46.     uint8_t num_bots;
  47.    
  48. #define TFC_LISTEN          'l'
  49. #define TFC_DEDICATED           'd'
  50. #define TFC_SOURCE_TV           'p'
  51.     uint8_t dedicated;
  52.    
  53. #define TFC_LINUX           'l'
  54. #define TFC_WINDOWS         'w'
  55.     uint8_t os;
  56.    
  57.     uint8_t password;
  58.     uint8_t secure;
  59.    
  60.     char *game_version;
  61.    
  62. #define TFC_PORT_INCLUDED       0x80
  63. #define TFC_SPECPORT_INCLUDED       0x40
  64. #define TFC_GAMETAG_STRING_INCLUDED 0x20
  65. #define TFC_STEAMID_INCLUDED        0x10
  66. #define TFC_GAMEID_INCLUDED     0x01
  67.     uint8_t extra_data_flag;
  68.    
  69. };
  70.  
  71. int tfc_rx_info_query (struct tfc_host_info *host);
  72. int tfc_tx_info_query (struct tfc_host_info *host);
  73.  
  74. int tfc_addr_init (struct tfc_host_info *host);
  75. int tfc_req_info (struct tfc_host_info *host);
  76.  
  77.  
  78. int tfc_tx_info_query (struct tfc_host_info *host)
  79. {
  80.     size_t mon;
  81.  
  82.     struct tfc_tx_info *tfi = malloc(sizeof(struct tfc_tx_info));
  83.    
  84.     tfi->id = 0xFFFFFFFF;
  85.     tfi->pad = 'T';
  86.     strcpy(tfi->query, "Source Engine Query");
  87.  
  88.     mon = sendto(host->tfc_fd, (void *)tfi, sizeof(struct tfc_tx_info), 0,
  89.              (struct sockaddr *)&host->tfc_addr, sizeof(struct sockaddr_in));
  90.    
  91.     printf("Sent %ld bytes\n",mon);
  92.    
  93.     free(tfi);
  94.    
  95.     return ( (mon > 0) ? 0 : 1);
  96. }
  97. //69.162.90.221:10000
  98. int tfc_rx_info_query (struct tfc_host_info *host)
  99. {
  100.     char *reply = malloc(101);
  101.     struct tfc_rx_info *tfi = malloc(sizeof(struct tfc_rx_info));
  102.    
  103.     memset(reply, 0x0, 100);
  104.     memset(tfi, 0x0, sizeof(struct tfc_rx_info));
  105.    
  106.     register uint16_t rcd, i = 0;
  107.     socklen_t slen = sizeof(struct sockaddr_in);
  108.    
  109.     rcd = recvfrom(host->tfc_fd, reply, 100, 0,
  110.                (struct sockaddr *)&host->tfc_addr, &slen);
  111.    
  112.     printf("Received %d bytes\n",rcd);
  113.    
  114.     memcpy(&tfi->id, reply, sizeof(uint32_t));
  115.     reply += sizeof(uint32_t);
  116.  
  117.     tfi->pad = *reply++;
  118.     tfi->version = *reply++;
  119.  
  120.     tfi->srv_name = malloc(100);
  121.     while ( (*(reply++) != 0) )
  122.         tfi->srv_name[i++] = *(reply-1);
  123.     i = 0;
  124.  
  125.     tfi->map = malloc(50);
  126.     while ( (*(reply++) != 0) )
  127.         tfi->map[i++] = *(reply-1);
  128.     i = 0;
  129.    
  130.     tfi->game_directory = malloc(50);
  131.     while ( (*(reply++) != 0) )
  132.         tfi->game_directory[i++] = *(reply-1);
  133.     i = 0;
  134.    
  135.     tfi->game_dsc = malloc(100);
  136.     while ( (*(reply++) != 0) )
  137.         tfi->game_dsc[i++] = *(reply-1);
  138.     i = 0;
  139.    
  140.     memcpy(&tfi->app_id, reply, sizeof(uint16_t));
  141.     reply += sizeof(uint16_t);
  142.    
  143.     tfi->num_players = *reply++;
  144.     tfi->max_players = *reply++;
  145.     tfi->num_bots = *reply++;
  146.     tfi->dedicated = *reply++;
  147.     tfi->os = *reply++;
  148.     tfi->password = *reply++;
  149.     tfi->secure = *reply++;
  150.  
  151.     tfi->game_version = malloc(25);
  152.     while ( (*(reply++) != 0) )
  153.         tfi->game_version[i++] = *(reply-1);
  154.    
  155.    
  156.     printf("Got INFO response!\n"
  157.            "....Server:   %s\n"
  158.            "....Map:      %s\n"
  159.            "....Players:  %d/%d\n"
  160.            "....Bots:     %d\n"
  161.            "....OS:       %s\n"
  162.            "....Version:  %s\n"
  163.            "....VAC:      %s\n",
  164.            tfi->srv_name, tfi->map, tfi->num_players,
  165.            tfi->max_players, tfi->num_bots,
  166.            (tfi->os == TFC_LINUX ? "Linux" : "Windows"),
  167.            tfi->game_version,
  168.            (tfi->secure ? "YES" : "NO"));
  169.    
  170.    
  171.     return 0;
  172. }
  173.  
  174. int tfc_addr_init (struct tfc_host_info *host)
  175. {
  176.     memset(&host->tfc_addr, 0x0, sizeof(struct sockaddr_in));
  177.    
  178.     if (!inet_pton(AF_INET, host->tfc_ip, &host->tfc_addr.sin_addr))
  179.         return -1;
  180.     host->tfc_addr.sin_family = AF_INET;
  181.     host->tfc_addr.sin_port = htons(host->tfc_port);
  182.  
  183.     return 0;
  184. }
  185.  
  186. int tfc_req_info (struct tfc_host_info *host)
  187. {
  188.     char *input = malloc(INET_ADDRSTRLEN + PORT_STR_LEN + 1);
  189.     register int8_t i = 0;
  190.    
  191.     puts("Enter connection data in \"IP:PORT\" form: ");
  192.     fgets(input, (INET_ADDRSTRLEN + PORT_STR_LEN + 1), stdin);
  193.     if (!input)
  194.         return -1;
  195.    
  196.     while ( (*(input++) != ':') ) {
  197.         if (i > (INET_ADDRSTRLEN - 1) )
  198.             return -1;
  199.         host->tfc_ip[i++] = *(input-1);
  200.     }
  201.     host->tfc_port = atoi(input);
  202.    
  203.     if (!host->tfc_port || (host->tfc_ip[0] == 0) )
  204.         return -1;
  205.     printf("Using server %s port %d\n",host->tfc_ip, host->tfc_port);
  206.     return 0;
  207. }
  208.  
  209. int main (void)
  210. {
  211.     struct tfc_host_info *host;
  212.    
  213.     host = malloc(sizeof(struct tfc_host_info));
  214.    
  215.     host->tfc_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  216.  
  217.     if (tfc_req_info(host)) {
  218.         printf("Error retreiving host information, quitting\n");
  219.         free(host);
  220.         return 0;
  221.     }
  222.    
  223.     if (tfc_addr_init(host)) {
  224.         printf("Error converting host information, quitting\n");
  225.         free(host);
  226.         return 0;
  227.     }
  228.    
  229.     tfc_tx_info_query(host);
  230.     tfc_rx_info_query(host);
  231.    
  232.     free(host);
  233.     return 0;
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement