-
Notifications
You must be signed in to change notification settings - Fork 33
/
qserver.c
146 lines (120 loc) · 3.07 KB
/
qserver.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
/*
* qstat
* by Steve Jankowski
*
* qserver functions
* Copyright 2004 Ludwig Nussel
*
* Licensed under the Artistic License, see LICENSE.txt for license terms
*/
#include "qstat.h"
#include "qserver.h"
#include "debug.h"
#ifndef _WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
// TODO: get rid of this and use send_packet instead, remove n_requests hack from a2s
query_status_t
qserver_send_initial(struct qserver *server, const char *data, size_t len)
{
query_status_t status;
status = send_packet_raw(server, data, len);
if ((server->retry1 == n_retries) || server->flags & FLAG_BROADCAST) {
gettimeofday(&server->packet_time1, NULL);
} else {
server->n_retries++;
}
server->retry1--;
server->n_packets++;
return (status);
}
query_status_t
qserver_send(struct qserver *server, const char *data, size_t len)
{
query_status_t status;
status = send_packet_raw(server, data, len);
server->retry1 = n_retries - 1;
gettimeofday(&server->packet_time1, NULL);
server->n_requests++;
server->n_packets++;
return (status);
}
int
send_broadcast(struct qserver *server, const char *pkt, size_t pktlen)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
if (no_port_offset || server->flags & TF_NO_PORT_OFFSET) {
addr.sin_port = htons(server->port);
} else {
addr.sin_port = htons((unsigned short)(server->port + server->type->port_offset));
}
addr.sin_addr.s_addr = server->ipaddr;
memset(&(addr.sin_zero), 0, sizeof(addr.sin_zero));
return (sendto(server->fd, (const char *)pkt, pktlen, 0, (struct sockaddr *)&addr, sizeof(addr)));
}
int
register_send(struct qserver *server)
{
if ((server->retry1 == n_retries) || server->flags & FLAG_BROADCAST) {
server->n_requests++;
} else {
server->n_retries++;
}
// New request so reset the sent time. This ensures
// that we record an accurate ping time even on retry
gettimeofday(&server->packet_time1, NULL);
server->retry1--;
server->n_packets++;
return (INPROGRESS);
}
query_status_t
send_packet_raw(struct qserver *server, const char *data, size_t len)
{
debug(2, "[%s] send", server->type->type_prefix);
if (4 <= get_debug_level()) {
output_packet(server, data, len, 1);
}
if (data) {
int ret;
if (server->flags & FLAG_BROADCAST) {
ret = send_broadcast(server, data, len);
} else {
ret = send(server->fd, data, len, 0);
}
if (ret == SOCKET_ERROR) {
return (send_error(server, ret));
}
}
return (INPROGRESS);
}
query_status_t
send_packet(struct qserver *server, const char *data, size_t len)
{
query_status_t rc;
rc = send_packet_raw(server, data, len);
if (rc == SYS_ERROR) {
return (rc);
}
return (register_send(server));
}
query_status_t
send_error(struct qserver *server, int rc)
{
unsigned int ipaddr = ntohl(server->ipaddr);
const char *errstr = strerror(errno);
fprintf(stderr, "Error on %d.%d.%d.%d: %s, skipping ...\n",
(ipaddr >> 24) & 0xff,
(ipaddr >> 16) & 0xff,
(ipaddr >> 8) & 0xff,
ipaddr & 0xff,
errstr
);
return (SYS_ERROR);
}