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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
/* Copyright (C) 2000,2001 Salvatore Sanfilippo <antirez@invece.org>
* See the LICENSE file for more information.
*
* ARS Packet Description System.
*
* Please, prefix all the function with ars_d_ */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "ars.h"
#define ARS_MAX_TSIZE 1024
char *ars_d_parser(char *t, char *next, size_t size)
{
int i = 0;
if (size == 0 || next == NULL || *t == '\0')
return NULL;
size--; /* space for nul term */
while (1) {
/* no space for the next char */
if (i == size) {
next[i] = '\0';
return t;
}
switch(*t) {
case '\0':
case '{':
case '}':
case ',':
case '=':
case '+':
if (i == 0) {
next[i] = *t;
next[i+1] = '\0';
return t+1;
} else {
next[i] = '\0';
return t;
}
default:
next[i++] = *t++;
break;
}
}
return NULL; /* unreached */
}
/* states */
#define ARS_G_LAYER 0
#define ARS_G_FIELD 1
#define ARS_G_VALUE 2
#define ARS_G_OBRACE_OR_PLUS 3
#define ARS_G_CBRACE 4
#define ARS_G_COMMA_OR_CBRACE 5
#define ARS_G_LEN_OR_PLUS 6
#define ARS_G_PLUS 7
#define ARS_G_EQUAL 8
struct ars_d_keyword_info {
char *ki_keyword;
int ki_opt;
void *(*ki_add) (struct ars_packet *pkt, int opt);
int (*ki_set) (struct ars_packet *pkt, int layer, char *f, char *v);
};
#define ARS_DKINFO_SIZE 64
#define BOGUS_SET_F(x) \
int (x)(struct ars_packet *pkt, int layer, char *f, char *v) { return 0; }
int ars_d_set_ip(struct ars_packet *pkt, int layer, char *f, char *v);
int ars_d_set_udp(struct ars_packet *pkt, int layer, char *f, char *v);
int ars_d_set_tcp(struct ars_packet *pkt, int layer, char *f, char *v);
int ars_d_set_icmp(struct ars_packet *pkt, int layer, char *f, char *v);
int ars_d_set_data(struct ars_packet *pkt, int layer, char *f, char *v);
BOGUS_SET_F(ars_d_set_ipopt_sec)
BOGUS_SET_F(ars_d_set_ipopt_sid)
BOGUS_SET_F(ars_d_set_ipopt_lsrr)
BOGUS_SET_F(ars_d_set_ipopt_ssrr)
BOGUS_SET_F(ars_d_set_ipopt_rr)
BOGUS_SET_F(ars_d_set_ipopt_ts)
BOGUS_SET_F(ars_d_set_tcpopt_mss)
BOGUS_SET_F(ars_d_set_tcpopt_wscale)
BOGUS_SET_F(ars_d_set_tcpopt_sackperm)
BOGUS_SET_F(ars_d_set_tcpopt_sack)
BOGUS_SET_F(ars_d_set_tcpopt_echo)
BOGUS_SET_F(ars_d_set_tcpopt_echoreply)
BOGUS_SET_F(ars_d_set_tcpopt_ts)
struct ars_d_keyword_info ars_dkinfo[ARS_DKINFO_SIZE] = {
/* KEYWORD OPT ADD function SET function *
* --------------------------------------------------------- */
{"ip", 0, ars_add_iphdr, ars_d_set_ip},
{"ipopt.eol", ARS_IPOPT_EOL, ars_add_ipopt, NULL},
{"ipopt.nop", ARS_IPOPT_NOP, ars_add_ipopt, NULL},
{"ipopt.sec", ARS_IPOPT_SEC, ars_add_ipopt, ars_d_set_ipopt_sec},
{"ipopt.sid", ARS_IPOPT_SID, ars_add_ipopt, ars_d_set_ipopt_sid},
{"ipopt.lsrr", ARS_IPOPT_LSRR, ars_add_ipopt, ars_d_set_ipopt_lsrr},
{"ipopt.ssrr", ARS_IPOPT_SSRR, ars_add_ipopt, ars_d_set_ipopt_ssrr},
{"ipopt.rr", ARS_IPOPT_RR, ars_add_ipopt, ars_d_set_ipopt_rr},
{"ipopt.ts", ARS_IPOPT_TIMESTAMP, ars_add_ipopt, ars_d_set_ipopt_ts},
{"udp", 0, ars_add_udphdr, ars_d_set_udp},
{"tcp", 0, ars_add_tcphdr, ars_d_set_tcp},
{"tcpopt.end", ARS_TCPOPT_EOL, ars_add_tcpopt, NULL},
{"tcpopt.nop", ARS_TCPOPT_NOP, ars_add_tcpopt, NULL},
{"tcpopt.mss", ARS_TCPOPT_MAXSEG, ars_add_tcpopt, ars_d_set_tcpopt_mss},
{"tcpopt.wscale", ARS_TCPOPT_WINDOW, ars_add_tcpopt, ars_d_set_tcpopt_wscale},
{"tcpopt.sackperm", ARS_TCPOPT_SACK_PERM, ars_add_tcpopt, ars_d_set_tcpopt_sackperm},
{"tcpopt.sack", ARS_TCPOPT_SACK, ars_add_tcpopt, ars_d_set_tcpopt_sack},
{"tcpopt.echo", ARS_TCPOPT_ECHOREQUEST, ars_add_tcpopt, ars_d_set_tcpopt_echo},
{"tcpopt.echoreply", ARS_TCPOPT_ECHOREPLY, ars_add_tcpopt, ars_d_set_tcpopt_echoreply},
{"tcpopt.ts", ARS_TCPOPT_TIMESTAMP, ars_add_tcpopt, ars_d_set_tcpopt_ts},
{"icmp", 0, ars_add_icmphdr, ars_d_set_icmp},
{"data", 0, ars_add_data, ars_d_set_data},
{NULL, 0, NULL, NULL} /* nul term */
};
struct ars_d_keyword_info *ars_get_keyword_by_name(char *name)
{
struct ars_d_keyword_info *k = ars_dkinfo;
while (k->ki_keyword) {
if (strcasecmp(k->ki_keyword, name) == 0)
return k;
k++;
}
return NULL;
}
int ars_d_setlayer_size(struct ars_packet *pkt, int layer, char *size)
{
size_t newsize;
if (layer == ARS_LAST_LAYER)
layer = pkt->p_layer_nr - 1;
if (ars_valid_layer(layer) != -ARS_OK)
return -ARS_INVALID;
newsize = ars_atou(size);
if (newsize < 1 || newsize > pkt->p_layer[layer].l_size) {
ars_set_error(pkt, "Invalid layer size in description");
return -ARS_INVALID;
}
pkt->p_layer[layer].l_size = newsize;
__D(printf("Setting the layer to size %s\n", size);)
return -ARS_OK;
}
int ars_d_set_ip(struct ars_packet *pkt, int layer, char *f, char *v)
{
struct ars_iphdr *ip;
if (layer == ARS_LAST_LAYER)
layer = pkt->p_layer_nr - 1;
if (ars_valid_layer(layer) != -ARS_OK)
return -ARS_INVALID;
ip = pkt->p_layer[layer].l_data;
if (strcasecmp(f, "saddr") == 0) {
return ars_resolve(pkt, &ip->saddr, v);
} else if (strcasecmp(f, "daddr") == 0) {
return ars_resolve(pkt, &ip->daddr, v);
} else if (strcasecmp(f, "ihl") == 0) {
ip->ihl = ars_atou(v);
pkt->p_layer[layer].l_flags |= ARS_TAKE_IP_HDRLEN;
} else if (strcasecmp(f, "ver") == 0) {
ip->version = ars_atou(v);
pkt->p_layer[layer].l_flags |= ARS_TAKE_IP_VERSION;
} else if (strcasecmp(f, "tos") == 0) {
ip->tos = ars_atou(v);
} else if (strcasecmp(f, "totlen") == 0) {
ip->tot_len = htons(ars_atou(v));
pkt->p_layer[layer].l_flags |= ARS_TAKE_IP_TOTLEN;
} else if (strcasecmp(f, "id") == 0) {
ip->id = htons(ars_atou(v));
} else if (strcasecmp(f, "fragoff") == 0) {
ip->frag_off = ip->frag_off & 0xE000;
ip->frag_off = htons(ars_atou(v) >> 3);
} else if (strcasecmp(f, "mf") == 0) {
if (ars_atou(v) == 0)
ip->frag_off &= htons(~ARS_IP_MF);
else
ip->frag_off |= htons(ARS_IP_MF);
} else if (strcasecmp(f, "df") == 0) {
if (ars_atou(v) == 0)
ip->frag_off &= htons(~ARS_IP_DF);
else
ip->frag_off |= htons(ARS_IP_DF);
} else if (strcasecmp(f, "rf") == 0) {
if (ars_atou(v) == 0)
ip->frag_off &= htons((u_int16_t)~ARS_IP_RF);
else
ip->frag_off |= htons(ARS_IP_RF);
} else if (strcasecmp(f, "ttl") == 0) {
ip->ttl = ars_atou(v);
} else if (strcasecmp(f, "proto") == 0) {
ip->protocol = ars_atou(v);
pkt->p_layer[layer].l_flags |= ARS_TAKE_IP_PROTOCOL;
} else if (strcasecmp(f, "cksum") == 0) {
ip->check = htons(ars_atou(v));
pkt->p_layer[layer].l_flags |= ARS_TAKE_IP_CKSUM;
} else {
ars_set_error(pkt, "Invalid field for IP layer");
return -ARS_INVALID;
}
return -ARS_OK;
}
int ars_d_set_udp(struct ars_packet *pkt, int layer, char *f, char *v)
{
struct ars_udphdr *udp;
if (layer == ARS_LAST_LAYER)
layer = pkt->p_layer_nr - 1;
if (ars_valid_layer(layer) != -ARS_OK)
return -ARS_INVALID;
udp = pkt->p_layer[layer].l_data;
if (strcasecmp(f, "sport") == 0) {
udp->uh_sport = htons(ars_atou(v));
} else if (strcasecmp(f, "dport") == 0) {
udp->uh_dport = htons(ars_atou(v));
} else if (strcasecmp(f, "len") == 0) {
udp->uh_ulen = htons(ars_atou(v));
pkt->p_layer[layer].l_flags |= ARS_TAKE_UDP_LEN;
} else if (strcasecmp(f, "cksum") == 0) {
udp->uh_sum = htons(ars_atou(v));
pkt->p_layer[layer].l_flags |= ARS_TAKE_UDP_CKSUM;
} else {
ars_set_error(pkt, "Invalid field for UDP layer");
return -ARS_INVALID;
}
return -ARS_OK;
}
int ars_d_set_tcp(struct ars_packet *pkt, int layer, char *f, char *v)
{
struct ars_tcphdr *tcp;
if (layer == ARS_LAST_LAYER)
layer = pkt->p_layer_nr - 1;
if (ars_valid_layer(layer) != -ARS_OK)
return -ARS_INVALID;
tcp = pkt->p_layer[layer].l_data;
if (strcasecmp(f, "sport") == 0) {
tcp->th_sport = htons(ars_atou(v));
} else if (strcasecmp(f, "dport") == 0) {
tcp->th_dport = htons(ars_atou(v));
} else if (strcasecmp(f, "seq") == 0) {
tcp->th_seq = htonl(ars_atou(v));
} else if (strcasecmp(f, "ack") == 0) {
tcp->th_ack = htonl(ars_atou(v));
} else if (strcasecmp(f, "x2") == 0) {
tcp->th_x2 = ars_atou(v);
} else if (strcasecmp(f, "off") == 0) {
tcp->th_off = ars_atou(v);
pkt->p_layer[layer].l_flags |= ARS_TAKE_TCP_HDRLEN;
} else if (strcasecmp(f, "flags") == 0) {
tcp->th_flags = 0;
if (strchr(v, 'f') || strchr(v, 'F'))
tcp->th_flags |= ARS_TCP_TH_FIN;
if (strchr(v, 's') || strchr(v, 'S'))
tcp->th_flags |= ARS_TCP_TH_SYN;
if (strchr(v, 'r') || strchr(v, 'R'))
tcp->th_flags |= ARS_TCP_TH_RST;
if (strchr(v, 'p') || strchr(v, 'P'))
tcp->th_flags |= ARS_TCP_TH_PUSH;
if (strchr(v, 'a') || strchr(v, 'A'))
tcp->th_flags |= ARS_TCP_TH_ACK;
if (strchr(v, 'u') || strchr(v, 'U'))
tcp->th_flags |= ARS_TCP_TH_URG;
if (strchr(v, 'x') || strchr(v, 'X'))
tcp->th_flags |= ARS_TCP_TH_X;
if (strchr(v, 'y') || strchr(v, 'Y'))
tcp->th_flags |= ARS_TCP_TH_Y;
} else if (strcasecmp(f, "win") == 0) {
tcp->th_win = htons(ars_atou(v));
} else if (strcasecmp(f, "cksum") == 0) {
tcp->th_sum = htons(ars_atou(v));
pkt->p_layer[layer].l_flags |= ARS_TAKE_TCP_CKSUM;
} else if (strcasecmp(f, "urp") == 0) {
tcp->th_urp = htons(ars_atou(v));
} else {
ars_set_error(pkt, "Invalid field for TCP layer");
return -ARS_INVALID;
}
return -ARS_OK;
}
int ars_d_set_icmp(struct ars_packet *pkt, int layer, char *f, char *v)
{
struct ars_icmphdr *icmp;
if (layer == ARS_LAST_LAYER)
layer = pkt->p_layer_nr - 1;
if (ars_valid_layer(layer) != -ARS_OK)
return -ARS_INVALID;
icmp = pkt->p_layer[layer].l_data;
if (strcasecmp(f, "type") == 0) {
icmp->type = ars_atou(v);
} else if (strcasecmp(f, "code") == 0) {
icmp->code = ars_atou(v);
} else if (strcasecmp(f, "cksum") == 0) {
icmp->checksum = htons(ars_atou(v));
pkt->p_layer[layer].l_flags |= ARS_TAKE_ICMP_CKSUM;
} else if (strcasecmp(f, "id") == 0) {
icmp->un.echo.id = htons(ars_atou(v));
} else if (strcasecmp(f, "seq") == 0) {
icmp->un.echo.sequence = htons(ars_atou(v));
} else if (strcasecmp(f, "gw") == 0) {
return ars_resolve(pkt, &icmp->un.gateway, v);
} else {
ars_set_error(pkt, "Invalid field for ICMP layer");
return -ARS_INVALID;
}
return -ARS_OK;
}
int ars_push_data(struct ars_packet *pkt, int layer, void *data, size_t size)
{
char *p;
int old_size;
if (layer == ARS_LAST_LAYER)
layer = pkt->p_layer_nr - 1;
if (ars_valid_layer(layer) != -ARS_OK)
return -ARS_INVALID;
old_size = pkt->p_layer[layer].l_size;
p = realloc(pkt->p_layer[layer].l_data, old_size + size);
if (p == NULL)
return -ARS_NOMEM;
memcpy(p+old_size, data, size);
pkt->p_layer[layer].l_data = p;
pkt->p_layer[layer].l_size += size;
return ARS_OK;
}
#define ARS_DATA_BUF_SIZE 4096
int ars_d_set_data(struct ars_packet *pkt, int layer, char *f, char *v)
{
if (layer == ARS_LAST_LAYER)
layer = pkt->p_layer_nr - 1;
if (ars_valid_layer(layer) != -ARS_OK)
return -ARS_INVALID;
if (strcasecmp(f, "file") == 0) {
int fd, n_read;
unsigned char buffer[ARS_DATA_BUF_SIZE];
if ((fd = open(v, O_RDONLY)) == -1) {
ars_set_error(pkt, "Can't open the DATA file");
return -ARS_ERROR;
}
if ((n_read = read(fd, buffer, ARS_DATA_BUF_SIZE)) == -1) {
close(fd);
ars_set_error(pkt, "Can't read DATA from file");
return -ARS_ERROR;
}
close(fd);
if (n_read == 0)
return -ARS_OK;
return ars_push_data(pkt, layer, buffer, n_read);
} else if (strcasecmp(f, "str") == 0) {
return ars_push_data(pkt, layer, v, strlen(v));
} else {
ars_set_error(pkt, "Invalid field for DATA layer");
return -ARS_INVALID;
}
return -ARS_OK;
}
/* A Finite state machine to build the packet using the description */
int ars_d_build(struct ars_packet *pkt, char *t)
{
struct ars_d_keyword_info *k = NULL;
char next[ARS_MAX_TSIZE];
char field[ARS_MAX_TSIZE];
int state = ARS_G_LAYER;
int error;
void *p;
while ((t = ars_d_parser(t, next, ARS_MAX_TSIZE)) != NULL) {
switch(state) {
case ARS_G_LAYER:
k = ars_get_keyword_by_name(next);
if (k == NULL) {
ars_set_error(pkt, "Unknown keyword");
return -ARS_INVALID;
}
__D(printf("Adding a new layer (%s)\n", next);)
p = k->ki_add(pkt, k->ki_opt);
if (p == NULL)
return -ARS_INVALID;
state = ARS_G_OBRACE_OR_PLUS;
break;
case ARS_G_FIELD:
strncpy(field, next, ARS_MAX_TSIZE);
state = ARS_G_EQUAL;
break;
case ARS_G_VALUE:
if (k->ki_set == NULL) {
ars_set_error(pkt, "Field specified for"
"a layer that doesn't support fields");
return -ARS_INVALID;
}
error = k->ki_set(pkt, ARS_LAST_LAYER, field, next);
if (error != -ARS_OK)
return error;
state = ARS_G_COMMA_OR_CBRACE;
break;
case ARS_G_OBRACE_OR_PLUS:
if (next[0] == '{' && next[1] == '\0') {
state = ARS_G_FIELD;
break;
} else if (next[0] == '+' && next[1] == '\0') {
state = ARS_G_LAYER;
break;
} else {
ars_set_error(pkt, "Missing brace or plus");
return -ARS_INVALID;
}
break;
case ARS_G_CBRACE:
if (next[0] != '}' || next[1] != '\0') {
ars_set_error(pkt, "Missing closed brace");
return -ARS_INVALID;
}
state = ARS_G_LEN_OR_PLUS;
break;
case ARS_G_COMMA_OR_CBRACE:
if (next[0] == '}' && next[1] == '\0') {
state = ARS_G_LEN_OR_PLUS;
break;
} else if (next[0] == ',' && next[1] == '\0') {
state = ARS_G_FIELD;
break;
} else {
ars_set_error(pkt, "Missing brace or comma");
return -ARS_INVALID;
}
break;
case ARS_G_LEN_OR_PLUS:
if (next[0] == '+' && next[1] == '\0') {
state = ARS_G_LAYER;
break;
}
error = ars_d_setlayer_size(pkt, ARS_LAST_LAYER, next);
if (error != -ARS_OK)
return error;
state = ARS_G_PLUS;
break;
case ARS_G_PLUS:
if (next[0] != '+' || next[1] != '\0') {
ars_set_error(pkt, "Missing plus");
return -ARS_INVALID;
}
state = ARS_G_LAYER;
break;
case ARS_G_EQUAL:
if (next[0] != '=' || next[1] != '\0') {
ars_set_error(pkt, "Missing equal");
return -ARS_INVALID;
}
state = ARS_G_VALUE;
break;
}
}
if (state != ARS_G_LEN_OR_PLUS && state != ARS_G_PLUS &&
state != ARS_G_OBRACE_OR_PLUS) {
ars_set_error(pkt, "Packet description truncated");
return -ARS_INVALID;
}
return -ARS_OK;
}
|