2
* A helper library to send and parse master server pings. See the relevant
5
* This code was written in 2006 by Emil Brink. It is released as public domain.
16
/* Build and send a MS:GET packet. */
17
void verse_ms_get_send(const char *address, int fields, const char *tags)
21
strcpy(req, "MS:GET IP=");
22
if(fields & VERSE_MS_FIELD_DESCRIPTION)
29
verse_send_ping(address, req);
32
/* Skip assign, i.e. "NAME=" string, at <msg>. Stores name into <put>, and then updates
33
* it. Returns NULL on parse error, in which case the <put> pointer is not advanced.
35
static const char * skip_assign(char **put, const char *msg)
39
char *p = put != NULL ? *put : NULL;
44
while(*msg && (isalnum(*msg) || *msg == '_'))
62
/** Skip value at <msg>, optionally storing de-quoted version through <put>,
63
* which is advanced. Returns NULL on parse error, without updating <put>.
65
static const char * skip_value(char **put, const char *msg)
67
char *p = (put != NULL) ? *put : NULL;
72
while(*msg != '\0' && *msg != '"')
92
if(*msg == '\0' || isspace(*msg))
97
while(*msg && !isspace(*msg))
112
static const char * put_field(VMSField *field, char **put, const char *src)
117
if((ptr = skip_assign(put, src)) != NULL && ptr - src > 1)
122
if((ptr = skip_value(put, src)) != NULL)
131
static int cmp_fields(const void *a, const void *b)
133
return strcmp(((const VMSField *) a)->name, ((const VMSField *) b)->name);
136
VMSServer ** verse_ms_list_parse(const char *msg)
138
const char *word[384]; /* Takes quite a lot of stack space. */
141
size_t num_word = 0, i, j, num_ip = 0, num_field, space = 0;
142
VMSServer **desc, *next;
145
if(strncmp(msg, "MS:LIST", 7) == 0)
150
/* Step one: split the string into words, at whitespace. Split is aware
151
* of quoting rules for value assignment, this is crucial. This split is
152
* non-invasive, meaning each "word" will be a suffix.
158
ptr = skip_assign(NULL, msg);
162
word[num_word++] = msg;
164
ptr = skip_value(NULL, msg);
167
fprintf(stderr, "Parse error\n");
170
space += ptr - msg + 1;
173
else if(*msg != '\0')
175
fprintf(stderr, "Parse error\n");
179
/* Now, count how many words begin with "IP=". */
180
for(i = 0; i < num_word; i++)
182
if(strncmp(word[i], "IP=", 3) == 0)
185
/* printf("found %u IPs, %u bytes\n", num_ip, space);
186
printf("%u IP and %u words -> %u fields total\n", num_ip, num_word, num_word - num_ip);
187
*/ num_field = num_word - num_ip;
188
/* Allocate the descriptions. */
189
/* printf("allocating %u bytes\n", (num_ip + 1) * (sizeof *desc) + num_ip * sizeof **desc + num_field * sizeof (VMSField) + space);
190
printf(" %u for pointers, %u for structs, %u for fields, %u string\n",
191
(num_ip + 1) * (sizeof *desc), num_ip * sizeof **desc, num_field * sizeof (VMSField), space);
192
*/ desc = malloc((num_ip + 1) * (sizeof *desc) + num_ip * sizeof **desc + num_field * sizeof (VMSField) + space);
193
next = (VMSServer *) (desc + (num_ip + 1));
194
/* printf("desc store at %u\n", (char *) next - (char *) desc);*/
195
field = (VMSField *) (next + num_ip);
196
/* printf("field store at %u\n", (char *) field - (char *) desc);*/
197
put = (char *) (field + num_field);
198
/* printf("string store at %u\n", put - (char *) desc);*/
199
for(i = j = 0; i < num_word;)
201
if(strncmp(word[i], "IP=", 3) == 0)
205
ptr = skip_value(&put, word[i] + 3);
206
next->num_fields = 0;
208
for(i++; i < num_word && strncmp(word[i], "IP=", 3) != 0; i++, next->num_fields++, field++)
209
put_field(&next->field[next->num_fields], &put, word[i]);
210
if(next->num_fields > 0) /* Sort the fields, for binary search later. */
211
qsort(next->field, next->num_fields, sizeof *next->field, cmp_fields);
222
/* A binary search, exploiting that the fields are sorted. */
223
static const VMSField * field_find(const VMSServer *ms, const char *name)
225
int lo, hi, mid, rel;
227
if(ms == NULL || name == NULL)
234
rel = strcmp(name, ms->field[mid].name);
236
return &ms->field[mid];
245
int verse_ms_field_exists(const VMSServer *ms, const char *name)
247
if(ms == NULL || name == NULL)
249
return field_find(ms, name) != NULL;
252
const char * verse_ms_field_value(const VMSServer *ms, const char *name)
256
if((f = field_find(ms, name)) != NULL)
261
#if defined VERSE_MS_STANDALONE
265
VMSServer **servers = verse_ms_list_parse("MS:LIST IP=127.0.0.1:4951 DE=\"A test server, mainly for Eskil\" COOL=yes BACKUP=daily LANG=sv_SE "
266
"IP=130.237.221.74 DE=\"Test server on a puny laptop\" COOL=yes DORKY=no OPEN=absolutely "
267
"IP=127.0.0.1:5151 DE=\"This is a back slash: '\\\\', cool huh?\" "
268
"IP=127.0.0.1:6676 DE=\"a quote looks like this: \\\"\" IP=127.0.0.1:1122 ");
274
printf("Server info:\n");
275
for(i = 0; servers[i] != NULL; i++)
277
printf("%u: IP=%s\n", i, servers[i]->ip);
278
for(j = 0; j < servers[i]->num_fields; j++)
279
printf(" %s='%s'\n", servers[i]->field[j].name, servers[i]->field[j].value);
286
#endif /* VERSE_MS_STANDALONE */