-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathbfbc2.c
149 lines (121 loc) · 2.49 KB
/
bfbc2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* qstat
* by Steve Jankowski
*
* Battlefield Bad Company 2 query protocol
* Copyright 2009 Steven Hartland
*
* Licensed under the Artistic License, see LICENSE.txt for license terms
*
*/
#include <sys/types.h>
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#else
#include <winsock.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "debug.h"
#include "qstat.h"
#include "packet_manip.h"
query_status_t
send_bfbc2_request_packet(struct qserver *server)
{
char buf[50];
int size = 0;
switch (server->challenge) {
case 0:
// Initial connect send serverInfo
size = 27;
memcpy(buf, "\x00\x00\x00\x00\x1b\x00\x00\x00\x01\x00\x00\x00\x0a\x00\x00\x00serverInfo\x00", size);
break;
case 1:
// All Done send quit
size = 21;
memcpy(buf, "\x01\x00\x00\x00\x15\x00\x00\x00\x01\x00\x00\x00\x04\x00\x00\x00quit\x00", size);
break;
case 2:
return (DONE_FORCE);
}
debug(3, "send_bfbc2_request_packet: state = %ld", server->challenge);
return (send_packet(server, buf, size));
}
query_status_t
deal_with_bfbc2_packet(struct qserver *server, char *rawpkt, int pktlen)
{
char *s, *end, *crlf;
int words, word = 0;
debug(2, "processing...");
if (17 > pktlen) {
// Invalid packet
return (REQ_ERROR);
}
rawpkt[pktlen - 1] = '\0';
end = &rawpkt[pktlen - 1];
s = rawpkt;
server->ping_total = time_delta(&packet_recv_time, &server->packet_time1);
server->n_requests++;
// Header Sequence
s += 4;
// Packet Size
s += 4;
// Num Words
words = *(int *)s;
s += 4;
// Words
while (words > 0 && s + 5 < end) {
// Size
int ws = *(int *)s;
s += 4;
// Content
debug(6, "word: %s\n", s);
switch (word) {
case 0:
// Status
break;
case 1:
// Server Name
// prevent CR & LF in the server name
crlf = strchr(s, '\015');
if (NULL != crlf) {
*crlf = '\0';
}
crlf = strchr(s, '\012');
if (NULL != crlf) {
*crlf = '\0';
}
server->server_name = strdup(s);
break;
case 2:
// Player Count
server->num_players = atoi(s);
break;
case 3:
// Max Players
server->max_players = atoi(s);
break;
case 4:
// Game Mode
add_rule(server, "gametype", s, NO_FLAGS);
break;
case 5:
// Map
server->map_name = strdup(s);
break;
}
word++;
s += ws + 1;
words--;
}
server->challenge++;
gettimeofday(&server->packet_time1, NULL);
if (1 == server->challenge) {
send_bfbc2_request_packet(server);
return (INPROGRESS);
}
return (DONE_FORCE);
}