2 * netsniff-ng - the packet sniffing beast
3 * By Daniel Borkmann <daniel@netsniff-ng.org>
4 * Copyright 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>,
5 * Swiss federal institute of technology (ETH Zurich)
6 * Subject to the GPL, version 2.
9 /* lex-func-prefix: yy */
18 #include <arpa/inet.h>
20 #include "trafgen_parser.tab.h"
25 extern void yyerror(const char *);
27 static char *try_convert_shellcode(char *sstr)
29 bool found_any = false;
30 char *bstr, *ostr = sstr, *hay, *orig = sstr;
31 size_t j = 0, blen, slen = strlen(sstr), tot = 0;
32 const char *needle = "\\x";
42 while ((hay = strstr(hay, needle)) != NULL ) {
43 hay += strlen(needle) + 2;
48 if (blen != tot || !found_any)
52 bstr = xzmalloc(blen);
56 bstr[j++] = (uint8_t) strtoul(sstr + 2, &sstr, 16);
74 number_oct ([0][0-9]+)
75 number_hex ([0]?[x][a-fA-F0-9]+)
76 number_bin ([0]?[b][0-1]+)
77 number_dec (([0])|([1-9][0-9]*))
78 number_ascii ([a-zA-Z])
80 mac_hex ([a-fA-F0-9]+)
81 mac ({mac_hex}:{mac_hex}:{mac_hex}:{mac_hex}:{mac_hex}:{mac_hex})
82 ip_addr ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)
86 "cpu" { return K_CPU; }
87 "fill" { return K_FILL; }
88 "rnd" { return K_RND; }
89 "csum16" { return K_CSUMIP; }
90 "csumip" { return K_CSUMIP; }
91 "csumip4" { return K_CSUMIP; }
92 "csumicmp" { return K_CSUMIP; }
93 "csumicmp4" { return K_CSUMIP; }
94 "csumudp" { return K_CSUMUDP; }
95 "csumtcp" { return K_CSUMTCP; }
96 "csumudp6" { return K_CSUMUDP6; }
97 "csumtcp6" { return K_CSUMTCP6; }
98 "drnd" { return K_DRND; }
99 "dinc" { return K_DINC; }
100 "ddec" { return K_DDEC; }
101 "seqinc" { return K_SEQINC; }
102 "seqdec" { return K_SEQDEC; }
103 "const8"|"c8" { return K_CONST8; }
104 "const16"|"c16" { return K_CONST16; }
105 "const32"|"c32" { return K_CONST32; }
106 "const64"|"c64" { return K_CONST64; }
108 "daddr"|"da" { return K_DADDR; }
109 "saddr"|"sa" { return K_SADDR; }
110 "prot"[o]? { return K_PROT; }
112 "sha"|"smac" { return K_SHA; }
113 "spa"|"sip" { return K_SPA; }
114 "tha"|"tmac" { return K_THA; }
115 "tpa"|"tip" { return K_TPA; }
116 "req"|"request" { return K_REQUEST; }
117 "reply" { return K_REPLY; }
118 "op"|"oper" { return K_OPER; }
119 "htype" { return K_HTYPE; }
120 "ptype" { return K_PTYPE; }
122 "ihl" { return K_IHL; }
123 "ver"|"version" { return K_VER; }
124 "ttl" { return K_TTL; }
125 "dscp" { return K_DSCP; }
126 "ecn" { return K_ECN; }
127 "tos" { return K_TOS; }
128 "len"|"length" { return K_LEN; }
129 "id" { return K_ID; }
130 "flags" { return K_FLAGS; }
131 "frag" { return K_FRAG; }
132 "csum" { return K_CSUM; }
133 "df" { return K_DF; }
134 "mf" { return K_MF; }
136 "eth" { return K_ETH; }
137 "arp" { return K_ARP; }
138 "ip4"|"ipv4" { return K_IP4; }
140 [ ]*"-"[ ]* { return '-'; }
141 [ ]*"+"[ ]* { return '+'; }
142 [ ]*"*"[ ]* { return '*'; }
143 [ ]*"/"[ ]* { return '/'; }
144 [ ]*"%"[ ]* { return '%'; }
145 [ ]*"&"[ ]* { return '&'; }
146 [ ]*"|"[ ]* { return '|'; }
147 [ ]*"<"[ ]* { return '<'; }
148 [ ]*">"[ ]* { return '>'; }
149 [ ]*"^"[ ]* { return '^'; }
162 "\""[^\"]+"\"" { yylval.str = try_convert_shellcode(xstrdup(yytext));
165 ([ \t\n]+)? { return K_WHITE; }
167 "/*"([^\*]|\*[^/])*"*/" { return K_COMMENT; }
169 "#"[^\n]* { return K_COMMENT; }
171 {number_hex} { yylval.number = strtoul(yytext + (yytext[0] == 'x' ? 1 : 0),
175 {number_dec} { yylval.number = strtol(yytext, NULL, 10);
178 {number_oct} { yylval.number = strtol(yytext + 1, NULL, 8);
181 {number_bin} { yylval.number = strtol(yytext + (yytext[0] == 'b' ? 1 : 2),
185 {number_ascii} { yylval.number = (uint8_t) (*yytext);
188 {mac} { if (str2mac(yytext, yylval.bytes, 256))
189 panic("Failed to parse MAC address %s\n", yytext);
192 {ip_addr} { if (inet_pton(AF_INET, yytext, &yylval.ip_addr) != 1)
193 panic("Failed to parse IPv4 address %s\n", yytext);
196 "'\\x"[a-fA-F0-9]{2}"'" { yylval.number = strtol(yytext + 3, NULL, 16);
199 "'"."'" { yylval.number = (uint8_t) (*(yytext + 1));
202 ";"[^\n]* {/* NOP */}
203 . { printf("Unknown character '%s'", yytext);
204 yyerror("lex Unknown character"); }