flowtop: Remove unused parameters from draw_help() and draw_footer()
[netsniff-ng.git] / trafgen_lexer.l
blobeb438a8c582e6292275792e658ebb295a2f54cb0
1 /*
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.
7  */
9 /* lex-func-prefix: yy */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <stdbool.h>
18 #include <arpa/inet.h>
20 #include "trafgen_parser.tab.h"
21 #include "xmalloc.h"
22 #include "built_in.h"
23 #include "str.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";
34         sstr++;
35         slen -= 2;
37         if (slen % 4 != 0)
38                 return orig;
40         blen = slen / 4;
41         hay = sstr;
42         while ((hay = strstr(hay, needle)) != NULL ) {
43                 hay += strlen(needle) + 2;
44                 found_any = true;
45                 tot++;
46         }
48         if (blen != tot || !found_any)
49                 return orig;
51         blen += 2;
52         bstr = xzmalloc(blen);
54         bstr[j++] = '\"';
55         while (j < blen - 1)
56                 bstr[j++] = (uint8_t) strtoul(sstr + 2, &sstr, 16);
57         bstr[j++] = '\"';
59         xfree(ostr);
60         return bstr;
65 %option align
66 %option nounput
67 %option noyywrap
68 %option noreject
69 %option 8bit
70 %option caseless
71 %option noinput
72 %option nodefault
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 a_hex           ([a-fA-F0-9]+)
81 mac             ({a_hex}:{a_hex}:{a_hex}:{a_hex}:{a_hex}:{a_hex})
82 ip4_addr        ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)
83         /* We're very permissive about IPv6 addresses the grammar accepts, as
84          * they can come in various different formats. In any case,
85          * inet_pton(AF_INET6, ...) will reject the invalid ones later on. */
86 ip6_addr        (({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex}?:)?({a_hex})?)
90 "cpu"           { return K_CPU; }
91 "fill"          { return K_FILL; }
92 "rnd"           { return K_RND; }
93 "csum16"        { return K_CSUMIP; }
94 "csumip"        { return K_CSUMIP; }
95 "csumip4"       { return K_CSUMIP; }
96 "csumicmp"      { return K_CSUMIP; }
97 "csumicmp4"     { return K_CSUMIP; }
98 "csumudp"       { return K_CSUMUDP; }
99 "csumtcp"       { return K_CSUMTCP; }
100 "csumudp6"      { return K_CSUMUDP6; }
101 "csumtcp6"      { return K_CSUMTCP6; }
102 "drnd"          { return K_DRND; }
103 "dinc"          { return K_DINC; }
104 "ddec"          { return K_DDEC; }
105 "seqinc"        { return K_SEQINC; }
106 "seqdec"        { return K_SEQDEC; }
107 "const8"|"c8"   { return K_CONST8; }
108 "const16"|"c16" { return K_CONST16; }
109 "const32"|"c32" { return K_CONST32; }
110 "const64"|"c64" { return K_CONST64; }
112 "prot"[o]?      { return K_PROT; }
113 "tc"|"tclass"   { return K_TC; }
115         /* Ethernet */
116 "daddr"|"da"    { return K_DADDR; }
117 "saddr"|"sa"    { return K_SADDR; }
118 [e]?"type"      { return K_ETYPE; }
120         /* VLAN (802.1Q & 802.1ad) */
121 "tpid"          { return K_TPID; }
122 "tci"           { return K_TCI; }
123 "pcp"           { return K_PCP; }
124 "dei"|"cfi"     { return K_DEI; }
125 "1ad"           { return K_1AD; }
126 "1q"            { return K_1Q; }
128         /* MPLS (Multi Protocol Label Switching) */
129 "lbl"|"label"   { return K_LABEL; }
130 "last"          { return K_LAST; }
131 "exp"           { return K_EXP; }
133         /* ARP */
134 "sha"|"smac"    { return K_SHA; }
135 "spa"|"sip"     { return K_SPA; }
136 "tha"|"tmac"    { return K_THA; }
137 "tpa"|"tip"     { return K_TPA; }
138 "req"|"request" { return K_REQUEST; }
139 "reply"         { return K_REPLY; }
140 "op"|"oper"     { return K_OPER; }
141 "htype"         { return K_HTYPE; }
142 "ptype"         { return K_PTYPE; }
144         /* IPv4 */
145 "ihl"           { return K_IHL; }
146 "ver"|"version" { return K_VER; }
147 "ttl"           { return K_TTL; }
148 "dscp"          { return K_DSCP; }
149 "ecn"           { return K_ECN; }
150 "tos"           { return K_TOS; }
151 "len"|"length"  { return K_LEN; }
152 "id"            { return K_ID; }
153 "flags"         { return K_FLAGS; }
154 "frag"          { return K_FRAG; }
155 "csum"          { return K_CSUM; }
156 "df"            { return K_DF; }
157 "mf"            { return K_MF; }
159         /* IPv6 */
160 "fl"|"flow"     { return K_FLOW; }
161 "nh"|"nexthdr"  { return K_NEXT_HDR; }
162 "hl"|"hoplimit" { return K_HOP_LIMIT; }
164         /* ICMPv6 */
165 "mtype"         { return K_MTYPE; }
166 "code"          { return K_CODE; }
167 "echorequest"   { return K_ECHO_REQUEST; }
168 "echoreply"     { return K_ECHO_REPLY; }
170         /* UDP */
171 "sp"|"sport"    { return K_SPORT; }
172 "dp"|"dport"    { return K_DPORT; }
174         /* TCP */
175 "seq"           { return K_SEQ; }
176 "ackseq"|"aseq" { return K_ACK_SEQ; }
177 "doff"|hlen     { return K_DOFF; }
178 "cwr"           { return K_CWR; }
179 "ece"|"ecn"     { return K_ECE; }
180 "urg"           { return K_URG; }
181 "ack"           { return K_ACK; }
182 "psh"           { return K_PSH; }
183 "rst"           { return K_RST; }
184 "syn"           { return K_SYN; }
185 "fin"           { return K_FIN; }
186 "win"|"window"  { return K_WINDOW; }
187 "urgptr"        { return K_URG_PTR; }
189 "eth"           { return K_ETH; }
190 "vlan"          { return K_VLAN; }
191 "mpls"          { return K_MPLS; }
192 "arp"           { return K_ARP; }
193 "ip4"|"ipv4"    { return K_IP4; }
194 "ip6"|"ipv6"    { return K_IP6; }
195 "icmp6"|"icmpv6"        { return K_ICMP6; }
196 "udp"           { return K_UDP; }
197 "tcp"           { return K_TCP; }
199 [ ]*"-"[ ]*     { return '-'; }
200 [ ]*"+"[ ]*     { return '+'; }
201 [ ]*"*"[ ]*     { return '*'; }
202 [ ]*"/"[ ]*     { return '/'; }
203 [ ]*"%"[ ]*     { return '%'; }
204 [ ]*"&"[ ]*     { return '&'; }
205 [ ]*"|"[ ]*     { return '|'; }
206 [ ]*"<"[ ]*     { return '<'; }
207 [ ]*">"[ ]*     { return '>'; }
208 [ ]*"^"[ ]*     { return '^'; }
209 "{"             { return '{'; }
210 "}"             { return '}'; }
211 "("             { return '('; }
212 ")"             { return ')'; }
213 "["             { return '['; }
214 "]"             { return ']'; }
215 ","             { return ','; }
216 ":"             { return ':'; }
217 "="             { return '='; }
219 "\n"            { yylineno++; }
221 "\""[^\"]+"\""  { yylval.str = try_convert_shellcode(xstrdup(yytext));
222                   return string; }
224 ([ \t\n]+)?     { return K_WHITE; }
226 "/*"([^\*]|\*[^/])*"*/" { return K_COMMENT; }
228 "#"[^\n]*       { return K_COMMENT; }
230 {number_hex}    { yylval.number = strtoul(yytext + (yytext[0] == 'x' ? 1 : 0),
231                                           NULL, 16);
232                   return number; }
234 {number_dec}    { yylval.number = strtol(yytext, NULL, 10);
235                   return number; }
237 {number_oct}    { yylval.number = strtol(yytext + 1, NULL, 8);
238                   return number; }
240 {number_bin}    { yylval.number = strtol(yytext + (yytext[0] == 'b' ? 1 : 2),
241                                          NULL, 2);
242                   return number; }
244 {number_ascii}  { yylval.number = (uint8_t) (*yytext);
245                   return number; }
247 {mac}           { if (str2mac(yytext, yylval.bytes, 256))
248                         panic("Failed to parse MAC address %s\n", yytext);
249                   return mac; }
251 {ip4_addr}      { if (inet_pton(AF_INET, yytext, &yylval.ip4_addr) != 1)
252                         panic("Failed to parse IPv4 address %s\n", yytext);
253                   return ip4_addr; };
255 {ip6_addr}      { if (inet_pton(AF_INET6, yytext, &yylval.ip6_addr) != 1)
256                         panic("Failed to parse IPv6 address %s\n", yytext);
257                   return ip6_addr; };
259 "'\\x"[a-fA-F0-9]{2}"'" { yylval.number = strtol(yytext + 3, NULL, 16);
260                   return number; }
262 "'"."'"         { yylval.number = (uint8_t) (*(yytext + 1));
263                   return number; }
265 ";"[^\n]*       {/* NOP */}
266 .               { printf("Unknown character '%s'", yytext);
267                   yyerror("lex Unknown character"); }