Advertisement
robert_david_graham

State machine example

Sep 13th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.23 KB | None | 0 0
  1.  
  2. void handle_tcp_packet(struct TCB *tcb, const unsigned char *data, unsigned length)
  3. {
  4.     ...
  5.     for (i=0; i<length; i++)
  6.         tcb->state = http_parse(tcb->state, data[i], &tcb->http);
  7.     ...
  8. }
  9.                    
  10. /* States: note that the parser assumes this order of states
  11.  * as it sometimes returns "state + 1". */
  12. enum {START=0, METHOD, INTER_MU, URI, INTER_UV, VERSION,
  13.     NAME, VALUE, EOL, END_OF_HEADER=-1};
  14. int http_parse(int state, unsigned char c, struct HttpParser *h)
  15. {
  16.     static const char WHITESPACE = " \n\r\f\t\v";
  17.    
  18.     /* ignore nuls and carriage-returns 'cause they complicate
  19.      * stuff */
  20.     if (c == '\0' || c == '\r')
  21.         return state;
  22.    
  23.     /* Note that the order of states in the switches statement
  24.      * is not the same as the order of states in the enum */
  25.     switch (state) {
  26.         case START:
  27.             if (strchr(WHITESPACE, c)) {
  28.                 return state; /* no transition */
  29.             } else {
  30.                 search_start(h, METHOD, c);
  31.                 return METHOD;
  32.             }
  33.            
  34.             /* text fields*/
  35.         case METHOD:
  36.         case URL:
  37.         case VERSION:
  38.         case VALUE:
  39.             if (c == '\n') {
  40.                 search_end(h);
  41.                 return EOL;
  42.             } else if (strchr(WHITESPACE, c)) {
  43.                 search_end(h);
  44.                 return state+1;
  45.             } else {
  46.                 search_continue(h, c);
  47.                 return state; /* no transition */
  48.             }
  49.         case NAME:
  50.             if (c == '\n') {
  51.                 search_end(h);
  52.                 search_start(h, VALUE, '\0');
  53.                 search_end(h);
  54.                 return EOL;
  55.             } else if (c == ':')) {
  56.                 search_end(h);
  57.                 search_start(h, VALUE, '\0');
  58.                 return VALUE;
  59.             } else {
  60.                 search_continue(h, c);
  61.                 return state; /* no transition */
  62.             }
  63.            
  64.             /* space fields */
  65.         case INTER_MV:
  66.         case INTER_UV:
  67.             if (c == '\n') {
  68.                 return EOL;
  69.             } else if (strchr(WHITESPACE, c)) {
  70.                 return state; /* no transition */
  71.             } else {
  72.                 search_start(h, state+1, c);
  73.                 return state+1;
  74.             }
  75.         case POST_V:
  76.             if (c == '\n') {
  77.                 return EOL;
  78.             } else {
  79.                 return state; /* no transition */
  80.             }
  81.         case EOL:
  82.             if (c == '\n') {
  83.                 http_request_end(h);
  84.                 return END_OF_HEADER;
  85.             } else {
  86.                 search_start(h, NAME, c);
  87.                 return NAME;
  88.             }
  89.     }
  90. }
  91.    
  92. void search_start(struct HTTP *h, int search_id, int c)
  93. {
  94.     h->ac_table = aho_corasic_searches[search_id];
  95.     h->ac_state = 0;
  96.     search_continue(h, c);
  97. }
  98. void search_continue(struct HTTP *h, unsigned char c)
  99. {
  100.     int x;
  101.     int row = h->ac_state;
  102.     int column = translate(h, c);
  103.    
  104.     h->ac_state = h->ac_table[row][column];
  105.    
  106.     x = ac_match(h->ac_table, h->ac_state);
  107.     if (x)
  108.         report_ids_event(x);
  109. }
  110. void search_end(struct HTTP *h)
  111. {
  112.     search_continue(h, -1);
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement