man: start netsniff-ng.8 man page
[netsniff-ng.git] / trafgen_lexer.l
blob20b6efe26f80b2cf9a38b2e39f159a1a63f2189a
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         int j = 0;
28         bool found_any = false;
29         char *bstr, *ostr = sstr, *hay;
30         size_t blen, slen = strlen(sstr), tot = 0;
31         const char *needle = "\\x";
33         sstr++;
34         slen -= 2;
36         if (slen % 4 != 0)
37                 return sstr;
39         blen = slen / 4;
40         hay = sstr;
41         while ((hay = strstr(hay, needle)) != NULL ) {
42                 hay += strlen(needle) + 2;
43                 found_any = true;
44                 tot++;
45         }
47         if (blen != tot || !found_any)
48                 return sstr;
50         blen += 2;
51         bstr = xzmalloc(blen);
53         bstr[j++] = '\"';
54         while (j < blen - 1)
55                 bstr[j++] = (uint8_t) strtoul(sstr + 2, &sstr, 16);
56         bstr[j++] = '\"';
58         xfree(ostr);
59         return bstr;
64 %option align
65 %option nounput
66 %option noyywrap
67 %option noreject
68 %option 8bit
69 %option caseless
70 %option noinput
71 %option nodefault
73 number_oct      ([0][0-9]+)
74 number_hex      ([0][x][a-fA-F0-9]+)
75 number_bin      ([0][b][0-1]+)
76 number_dec      (([0])|([1-9][0-9]*))
77 number_ascii    ([a-zA-Z])
81 "cpu"           { return K_CPU; }
82 "fill"          { return K_FILL; }
83 "rnd"           { return K_RND; }
84 "csum16"        { return K_CSUMIP; }
85 "csumip"        { return K_CSUMIP; }
86 "csumip4"       { return K_CSUMIP; }
87 "csumicmp"      { return K_CSUMIP; }
88 "csumicmp4"     { return K_CSUMIP; }
89 "csumudp"       { return K_CSUMUDP; }
90 "csumtcp"       { return K_CSUMTCP; }
91 "drnd"          { return K_DRND; }
92 "dinc"          { return K_DINC; }
93 "ddec"          { return K_DDEC; }
94 "seqinc"        { return K_SEQINC; }
95 "seqdec"        { return K_SEQDEC; }
96 "const8"|"c8"   { return K_CONST8; }
97 "const16"|"c16" { return K_CONST16; }
98 "const32"|"c32" { return K_CONST32; }
99 "const64"|"c64" { return K_CONST64; }
101 [ ]*"-"[ ]*     { return '-'; }
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 ':'; }
120 "\n"            { yylineno++; }
122 "\""[^\"]+"\""  { yylval.str = try_convert_shellcode(xstrdup(yytext));
123                   return string; }
125 ([ \t\n]+)?     { return K_WHITE; }
127 "/*"([^\*]|\*[^/])*"*/" { return K_COMMENT; }
129 "#"[^\n]*       { return K_COMMENT; }
131 {number_hex}    { yylval.number = strtoul(yytext, NULL, 16);
132                   return number; }
134 {number_dec}    { yylval.number = strtol(yytext, NULL, 10);
135                   return number; }
137 {number_oct}    { yylval.number = strtol(yytext + 1, NULL, 8);
138                   return number; }
140 {number_bin}    { yylval.number = strtol(yytext + 2, NULL, 2);
141                   return number; }
143 {number_ascii}  { yylval.number = (uint8_t) (*yytext);
144                   return number; }
146 "'\\x"[a-fA-F0-9]{2}"'" { yylval.number = strtol(yytext + 3, NULL, 16);
147                   return number; }
149 "'"."'"         { yylval.number = (uint8_t) (*(yytext + 1));
150                   return number; }
152 ";"[^\n]*       {/* NOP */}
153 .               { printf("Unknown character '%s'", yytext);
154                   yyerror("lex Unknown character"); }