trafgen: Preprocess packets directly after compiling
[netsniff-ng.git] / trafgen_lexer.l
blob6c27b0ccd849d4c5b23bbe68b05bd693c2e6324b
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>
19 #include "trafgen_parser.tab.h"
20 #include "xmalloc.h"
21 #include "built_in.h"
23 extern void yyerror(const char *);
25 static char *try_convert_shellcode(char *sstr)
27         bool found_any = false;
28         char *bstr, *ostr = sstr, *hay, *orig = sstr;
29         size_t j = 0, blen, slen = strlen(sstr), tot = 0;
30         const char *needle = "\\x";
32         sstr++;
33         slen -= 2;
35         if (slen % 4 != 0)
36                 return orig;
38         blen = slen / 4;
39         hay = sstr;
40         while ((hay = strstr(hay, needle)) != NULL ) {
41                 hay += strlen(needle) + 2;
42                 found_any = true;
43                 tot++;
44         }
46         if (blen != tot || !found_any)
47                 return orig;
49         blen += 2;
50         bstr = xzmalloc(blen);
52         bstr[j++] = '\"';
53         while (j < blen - 1)
54                 bstr[j++] = (uint8_t) strtoul(sstr + 2, &sstr, 16);
55         bstr[j++] = '\"';
57         xfree(ostr);
58         return bstr;
63 %option align
64 %option nounput
65 %option noyywrap
66 %option noreject
67 %option 8bit
68 %option caseless
69 %option noinput
70 %option nodefault
72 number_oct      ([0][0-9]+)
73 number_hex      ([0]?[x][a-fA-F0-9]+)
74 number_bin      ([0]?[b][0-1]+)
75 number_dec      (([0])|([1-9][0-9]*))
76 number_ascii    ([a-zA-Z])
80 "cpu"           { return K_CPU; }
81 "fill"          { return K_FILL; }
82 "rnd"           { return K_RND; }
83 "csum16"        { return K_CSUMIP; }
84 "csumip"        { return K_CSUMIP; }
85 "csumip4"       { return K_CSUMIP; }
86 "csumicmp"      { return K_CSUMIP; }
87 "csumicmp4"     { return K_CSUMIP; }
88 "csumudp"       { return K_CSUMUDP; }
89 "csumtcp"       { return K_CSUMTCP; }
90 "csumudp6"      { return K_CSUMUDP6; }
91 "csumtcp6"      { return K_CSUMTCP6; }
92 "drnd"          { return K_DRND; }
93 "dinc"          { return K_DINC; }
94 "ddec"          { return K_DDEC; }
95 "seqinc"        { return K_SEQINC; }
96 "seqdec"        { return K_SEQDEC; }
97 "const8"|"c8"   { return K_CONST8; }
98 "const16"|"c16" { return K_CONST16; }
99 "const32"|"c32" { return K_CONST32; }
100 "const64"|"c64" { return K_CONST64; }
102 [ ]*"-"[ ]*     { return '-'; }
103 [ ]*"+"[ ]*     { return '+'; }
104 [ ]*"*"[ ]*     { return '*'; }
105 [ ]*"/"[ ]*     { return '/'; }
106 [ ]*"%"[ ]*     { return '%'; }
107 [ ]*"&"[ ]*     { return '&'; }
108 [ ]*"|"[ ]*     { return '|'; }
109 [ ]*"<"[ ]*     { return '<'; }
110 [ ]*">"[ ]*     { return '>'; }
111 [ ]*"^"[ ]*     { return '^'; }
112 "{"             { return '{'; }
113 "}"             { return '}'; }
114 "("             { return '('; }
115 ")"             { return ')'; }
116 "["             { return '['; }
117 "]"             { return ']'; }
118 ","             { return ','; }
119 ":"             { return ':'; }
121 "\n"            { yylineno++; }
123 "\""[^\"]+"\""  { yylval.str = try_convert_shellcode(xstrdup(yytext));
124                   return string; }
126 ([ \t\n]+)?     { return K_WHITE; }
128 "/*"([^\*]|\*[^/])*"*/" { return K_COMMENT; }
130 "#"[^\n]*       { return K_COMMENT; }
132 {number_hex}    { yylval.number = strtoul(yytext + (yytext[0] == 'x' ? 1 : 0),
133                                           NULL, 16);
134                   return number; }
136 {number_dec}    { yylval.number = strtol(yytext, NULL, 10);
137                   return number; }
139 {number_oct}    { yylval.number = strtol(yytext + 1, NULL, 8);
140                   return number; }
142 {number_bin}    { yylval.number = strtol(yytext + (yytext[0] == 'b' ? 1 : 2),
143                                          NULL, 2);
144                   return number; }
146 {number_ascii}  { yylval.number = (uint8_t) (*yytext);
147                   return number; }
149 "'\\x"[a-fA-F0-9]{2}"'" { yylval.number = strtol(yytext + 3, NULL, 16);
150                   return number; }
152 "'"."'"         { yylval.number = (uint8_t) (*(yytext + 1));
153                   return number; }
155 ";"[^\n]*       {/* NOP */}
156 .               { printf("Unknown character '%s'", yytext);
157                   yyerror("lex Unknown character"); }