Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <ctype.h>
- #include <netdb.h>
- #include <arpa/inet.h>
- #include <netinet/udp.h>
- #define PORT_STR_LEN 5
- struct tfc_host_info {
- int tfc_fd;
- char tfc_ip[INET_ADDRSTRLEN + 1];
- short tfc_port;
- struct sockaddr_in tfc_addr;
- };
- struct tfc_tx_info {
- uint32_t id;
- uint8_t pad;
- char query[20];
- } __attribute__((__packed__));
- struct tfc_rx_info {
- uint32_t id;
- uint8_t pad;
- uint8_t version;
- char *srv_name;
- char *map;
- char *game_directory;
- char *game_dsc;
- #define TFC_DEDICATED_SERVER 310
- #define TFC_LINUX_DEDICATED_SERVER 311
- #define TFC_APPID_TF2 440
- #define TFC_TF2_CONTENT 441
- #define TFC_TF2_MATERIALS 442
- #define TFC_TF2_CLIENT_CONTENT 443
- uint16_t app_id;
- uint8_t num_players;
- uint8_t max_players;
- uint8_t num_bots;
- #define TFC_LISTEN 'l'
- #define TFC_DEDICATED 'd'
- #define TFC_SOURCE_TV 'p'
- uint8_t dedicated;
- #define TFC_LINUX 'l'
- #define TFC_WINDOWS 'w'
- uint8_t os;
- uint8_t password;
- uint8_t secure;
- char *game_version;
- #define TFC_PORT_INCLUDED 0x80
- #define TFC_SPECPORT_INCLUDED 0x40
- #define TFC_GAMETAG_STRING_INCLUDED 0x20
- #define TFC_STEAMID_INCLUDED 0x10
- #define TFC_GAMEID_INCLUDED 0x01
- uint8_t extra_data_flag;
- };
- int tfc_rx_info_query (struct tfc_host_info *host);
- int tfc_tx_info_query (struct tfc_host_info *host);
- int tfc_addr_init (struct tfc_host_info *host);
- int tfc_req_info (struct tfc_host_info *host);
- int tfc_tx_info_query (struct tfc_host_info *host)
- {
- size_t mon;
- struct tfc_tx_info *tfi = malloc(sizeof(struct tfc_tx_info));
- tfi->id = 0xFFFFFFFF;
- tfi->pad = 'T';
- strcpy(tfi->query, "Source Engine Query");
- mon = sendto(host->tfc_fd, (void *)tfi, sizeof(struct tfc_tx_info), 0,
- (struct sockaddr *)&host->tfc_addr, sizeof(struct sockaddr_in));
- printf("Sent %ld bytes\n",mon);
- free(tfi);
- return ( (mon > 0) ? 0 : 1);
- }
- //69.162.90.221:10000
- int tfc_rx_info_query (struct tfc_host_info *host)
- {
- char *reply = malloc(101);
- struct tfc_rx_info *tfi = malloc(sizeof(struct tfc_rx_info));
- memset(reply, 0x0, 100);
- memset(tfi, 0x0, sizeof(struct tfc_rx_info));
- register uint16_t rcd, i = 0;
- socklen_t slen = sizeof(struct sockaddr_in);
- rcd = recvfrom(host->tfc_fd, reply, 100, 0,
- (struct sockaddr *)&host->tfc_addr, &slen);
- printf("Received %d bytes\n",rcd);
- memcpy(&tfi->id, reply, sizeof(uint32_t));
- reply += sizeof(uint32_t);
- tfi->pad = *reply++;
- tfi->version = *reply++;
- tfi->srv_name = malloc(100);
- while ( (*(reply++) != 0) )
- tfi->srv_name[i++] = *(reply-1);
- i = 0;
- tfi->map = malloc(50);
- while ( (*(reply++) != 0) )
- tfi->map[i++] = *(reply-1);
- i = 0;
- tfi->game_directory = malloc(50);
- while ( (*(reply++) != 0) )
- tfi->game_directory[i++] = *(reply-1);
- i = 0;
- tfi->game_dsc = malloc(100);
- while ( (*(reply++) != 0) )
- tfi->game_dsc[i++] = *(reply-1);
- i = 0;
- memcpy(&tfi->app_id, reply, sizeof(uint16_t));
- reply += sizeof(uint16_t);
- tfi->num_players = *reply++;
- tfi->max_players = *reply++;
- tfi->num_bots = *reply++;
- tfi->dedicated = *reply++;
- tfi->os = *reply++;
- tfi->password = *reply++;
- tfi->secure = *reply++;
- tfi->game_version = malloc(25);
- while ( (*(reply++) != 0) )
- tfi->game_version[i++] = *(reply-1);
- printf("Got INFO response!\n"
- "....Server: %s\n"
- "....Map: %s\n"
- "....Players: %d/%d\n"
- "....Bots: %d\n"
- "....OS: %s\n"
- "....Version: %s\n"
- "....VAC: %s\n",
- tfi->srv_name, tfi->map, tfi->num_players,
- tfi->max_players, tfi->num_bots,
- (tfi->os == TFC_LINUX ? "Linux" : "Windows"),
- tfi->game_version,
- (tfi->secure ? "YES" : "NO"));
- return 0;
- }
- int tfc_addr_init (struct tfc_host_info *host)
- {
- memset(&host->tfc_addr, 0x0, sizeof(struct sockaddr_in));
- if (!inet_pton(AF_INET, host->tfc_ip, &host->tfc_addr.sin_addr))
- return -1;
- host->tfc_addr.sin_family = AF_INET;
- host->tfc_addr.sin_port = htons(host->tfc_port);
- return 0;
- }
- int tfc_req_info (struct tfc_host_info *host)
- {
- char *input = malloc(INET_ADDRSTRLEN + PORT_STR_LEN + 1);
- register int8_t i = 0;
- puts("Enter connection data in \"IP:PORT\" form: ");
- fgets(input, (INET_ADDRSTRLEN + PORT_STR_LEN + 1), stdin);
- if (!input)
- return -1;
- while ( (*(input++) != ':') ) {
- if (i > (INET_ADDRSTRLEN - 1) )
- return -1;
- host->tfc_ip[i++] = *(input-1);
- }
- host->tfc_port = atoi(input);
- if (!host->tfc_port || (host->tfc_ip[0] == 0) )
- return -1;
- printf("Using server %s port %d\n",host->tfc_ip, host->tfc_port);
- return 0;
- }
- int main (void)
- {
- struct tfc_host_info *host;
- host = malloc(sizeof(struct tfc_host_info));
- host->tfc_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
- if (tfc_req_info(host)) {
- printf("Error retreiving host information, quitting\n");
- free(host);
- return 0;
- }
- if (tfc_addr_init(host)) {
- printf("Error converting host information, quitting\n");
- free(host);
- return 0;
- }
- tfc_tx_info_query(host);
- tfc_rx_info_query(host);
- free(host);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement