trafgen: proto: Add set_next_proto callback to struct proto_hdr
[netsniff-ng-new.git] / trafgen_lexer.l
blobef7ec2a10159ea4f73a336b54a736ee4f01451f9
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 mac_hex         ([a-fA-F0-9]+)
81 mac             ({mac_hex}:{mac_hex}:{mac_hex}:{mac_hex}:{mac_hex}:{mac_hex})
82 ip4_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 "prot"[o]?      { return K_PROT; }
110         /* Ethernet */
111 "daddr"|"da"    { return K_DADDR; }
112 "saddr"|"sa"    { return K_SADDR; }
113 [e]?"type"      { return K_ETYPE; }
115         /* ARP */
116 "sha"|"smac"    { return K_SHA; }
117 "spa"|"sip"     { return K_SPA; }
118 "tha"|"tmac"    { return K_THA; }
119 "tpa"|"tip"     { return K_TPA; }
120 "req"|"request" { return K_REQUEST; }
121 "reply"         { return K_REPLY; }
122 "op"|"oper"     { return K_OPER; }
123 "htype"         { return K_HTYPE; }
124 "ptype"         { return K_PTYPE; }
126         /* IPv4 */
127 "ihl"           { return K_IHL; }
128 "ver"|"version" { return K_VER; }
129 "ttl"           { return K_TTL; }
130 "dscp"          { return K_DSCP; }
131 "ecn"           { return K_ECN; }
132 "tos"           { return K_TOS; }
133 "len"|"length"  { return K_LEN; }
134 "id"            { return K_ID; }
135 "flags"         { return K_FLAGS; }
136 "frag"          { return K_FRAG; }
137 "csum"          { return K_CSUM; }
138 "df"            { return K_DF; }
139 "mf"            { return K_MF; }
141         /* UDP */
142 "sp"|"sport"    { return K_SPORT; }
143 "dp"|"dport"    { return K_DPORT; }
145         /* TCP */
146 "seq"           { return K_SEQ; }
147 "ackseq"|"aseq" { return K_ACK_SEQ; }
148 "doff"|hlen     { return K_DOFF; }
149 "cwr"           { return K_CWR; }
150 "ece"|"ecn"     { return K_ECE; }
151 "urg"           { return K_URG; }
152 "ack"           { return K_ACK; }
153 "psh"           { return K_PSH; }
154 "rst"           { return K_RST; }
155 "syn"           { return K_SYN; }
156 "fin"           { return K_FIN; }
157 "win"|"window"  { return K_WINDOW; }
158 "urgptr"        { return K_URG_PTR; }
160 "eth"           { return K_ETH; }
161 "arp"           { return K_ARP; }
162 "ip4"|"ipv4"    { return K_IP4; }
163 "udp"           { return K_UDP; }
164 "tcp"           { return K_TCP; }
166 [ ]*"-"[ ]*     { return '-'; }
167 [ ]*"+"[ ]*     { return '+'; }
168 [ ]*"*"[ ]*     { return '*'; }
169 [ ]*"/"[ ]*     { return '/'; }
170 [ ]*"%"[ ]*     { return '%'; }
171 [ ]*"&"[ ]*     { return '&'; }
172 [ ]*"|"[ ]*     { return '|'; }
173 [ ]*"<"[ ]*     { return '<'; }
174 [ ]*">"[ ]*     { return '>'; }
175 [ ]*"^"[ ]*     { return '^'; }
176 "{"             { return '{'; }
177 "}"             { return '}'; }
178 "("             { return '('; }
179 ")"             { return ')'; }
180 "["             { return '['; }
181 "]"             { return ']'; }
182 ","             { return ','; }
183 ":"             { return ':'; }
184 "="             { return '='; }
186 "\n"            { yylineno++; }
188 "\""[^\"]+"\""  { yylval.str = try_convert_shellcode(xstrdup(yytext));
189                   return string; }
191 ([ \t\n]+)?     { return K_WHITE; }
193 "/*"([^\*]|\*[^/])*"*/" { return K_COMMENT; }
195 "#"[^\n]*       { return K_COMMENT; }
197 {number_hex}    { yylval.number = strtoul(yytext + (yytext[0] == 'x' ? 1 : 0),
198                                           NULL, 16);
199                   return number; }
201 {number_dec}    { yylval.number = strtol(yytext, NULL, 10);
202                   return number; }
204 {number_oct}    { yylval.number = strtol(yytext + 1, NULL, 8);
205                   return number; }
207 {number_bin}    { yylval.number = strtol(yytext + (yytext[0] == 'b' ? 1 : 2),
208                                          NULL, 2);
209                   return number; }
211 {number_ascii}  { yylval.number = (uint8_t) (*yytext);
212                   return number; }
214 {mac}           { if (str2mac(yytext, yylval.bytes, 256))
215                         panic("Failed to parse MAC address %s\n", yytext);
216                   return mac; }
218 {ip4_addr}      { if (inet_pton(AF_INET, yytext, &yylval.ip4_addr) != 1)
219                         panic("Failed to parse IPv4 address %s\n", yytext);
220                   return ip4_addr; };
222 "'\\x"[a-fA-F0-9]{2}"'" { yylval.number = strtol(yytext + 3, NULL, 16);
223                   return number; }
225 "'"."'"         { yylval.number = (uint8_t) (*(yytext + 1));
226                   return number; }
228 ";"[^\n]*       {/* NOP */}
229 .               { printf("Unknown character '%s'", yytext);
230                   yyerror("lex Unknown character"); }