Commit 62abf7a6 authored by Robert David Graham's avatar Robert David Graham
Browse files

refactored TCP/IP stack

parent 056ca5b3
Loading
Loading
Loading
Loading
+1 −1
Changes for src/main.c: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -890,7 +890,7 @@ receive_thread(void *v)
                        0, seqno_me, secs, usecs, seqno_them);
                }

                /* If this contains payload, handle that */
                /* If this contains payload, handle that second */
                if (parsed.app_length) {
                    tcpcon_handle(tcpcon, tcb, TCP_WHAT_DATA,
                        px + parsed.app_offset, parsed.app_length,
+1 −2
Changes for src/proto-banner1.h: 1 added line, 2 removed lines.
Original line number Diff line number Diff line
#ifndef PROTO_BANNER1_H
#define PROTO_BANNER1_H
#include <stdint.h>
#define STATE_DONE 0xFFFFFFFF

#include <stdio.h>
#include "masscan-app.h"
#include "proto-banout.h"
@@ -212,7 +212,6 @@ struct ProtocolState {
    unsigned short port;
    unsigned short app_proto;
    unsigned is_sent_sslhello:1;
    unsigned is_done:1;
    struct BannerBase64 base64;

    union {
+10 −5
Changes for src/proto-ftp.c: 10 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -41,7 +41,8 @@ ftp_parse( const struct Banner1 *banner1,
            case 102:
            case 103:
                if (!isdigit(px[i]&0xFF)) {
                    state = STATE_DONE;
                    state = 0xffffffff;
                    tcp_close(more);
                } else {
                    ftp->code *= 10;
                    ftp->code += (px[i] - '0');
@@ -60,7 +61,8 @@ ftp_parse( const struct Banner1 *banner1,
                    state++;
                    banout_append_char(banout, PROTO_FTP, px[i]);
                } else {
                    state = STATE_DONE;
                    state = 0xffffffff;
                    tcp_close(more);
                }
                break;
            case 5:
@@ -76,7 +78,8 @@ ftp_parse( const struct Banner1 *banner1,
                        state = 0;
                    }
                } else if (px[i] == '\0' || !isprint(px[i])) {
                    state = STATE_DONE;
                    state = 0xffffffff;
                    tcp_close(more);
                    continue;
                } else {
                    banout_append_char(banout, PROTO_FTP, px[i]);
@@ -100,10 +103,12 @@ ftp_parse( const struct Banner1 *banner1,
                        tcp_transmit(more, banner_ssl.hello, banner_ssl.hello_length, 0);
                        
                    } else {
                        state = STATE_DONE;
                        state = 0xffffffff;
                        tcp_close(more);
                    }
                } else if (px[i] == '\0' || !isprint(px[i])) {
                    state = STATE_DONE;
                    state = 0xffffffff;
                    tcp_close(more);
                    continue;
                } else {
                    banout_append_char(banout, PROTO_FTP, px[i]);
+19 −11
Changes for src/proto-http.c: 19 added lines, 11 removed lines.
Original line number Diff line number Diff line
#include "proto-http.h"
#include "proto-banner1.h"
#include "proto-interactive.h"
#include "smack.h"
#include "unusedparm.h"
#include "string_s.h"
@@ -235,7 +236,9 @@ http_parse(
        FIELD_VALUE,
        CONTENT,
        CONTENT_TAG,
        CONTENT_FIELD
        CONTENT_FIELD,
        
        DONE_PARSING
    };

    UNUSEDPARM(banner1_private);
@@ -248,22 +251,27 @@ http_parse(
    for (i=0; i<length; i++)
    switch (state) {
    case 0: case 1: case 2: case 3: case 4:
        if (toupper(px[i]) != "HTTP/"[state])
            state = STATE_DONE;
        else
        if (toupper(px[i]) != "HTTP/"[state]) {
            state = DONE_PARSING;
            tcp_close(more);
        } else
            state++;
        break;
    case 5:
        if (px[i] == '.')
            state++;
        else if (!isdigit(px[i]))
            state = STATE_DONE;
        else if (!isdigit(px[i])) {
            state = DONE_PARSING;
            tcp_close(more);
        }
        break;
    case 6:
        if (isspace(px[i]))
            state++;
        else if (!isdigit(px[i]))
            state = STATE_DONE;
        else if (!isdigit(px[i])) {
            state = DONE_PARSING;
            tcp_close(more);
        }
        break;
    case 7:
        /* TODO: look for 1xx response code */
@@ -389,7 +397,7 @@ http_parse(
            banout_append_char(banout, PROTO_HTML_TITLE, px[i]);
        }
        break;
    case STATE_DONE:
    case DONE_PARSING:
    default:
        i = (unsigned)length;
        break;
@@ -402,11 +410,11 @@ http_parse(



    if (state == STATE_DONE)
    if (state == DONE_PARSING)
        pstate->state = state;
    else
        pstate->state = (state2 & 0xFFFF) << 16
                | (id & 0xFF) << 8
                | ((unsigned)id & 0xFF) << 8
                | (state & 0xFF);
}

+41 −20
Changes for src/proto-imap4.c: 41 added lines, 20 removed lines.
Original line number Diff line number Diff line
@@ -41,29 +41,37 @@ imap4_parse( const struct Banner1 *banner1,
                banout_append_char(banout, PROTO_IMAP4, px[i]);
                if (px[i] == '*')
                    state++;
                else
                    state = STATE_DONE;
                else {
                    state = 0xffffffff;
                    tcp_close(more);
                }
                break;
            case 1:
                if (px[i] == ' ') {
                    banout_append_char(banout, PROTO_IMAP4, px[i]);
                    continue;
                } else
                    state++;
                } else {
                    state = 0xffffffff;
                    tcp_close(more);
                }
                /* fall through */
            case 2:
                banout_append_char(banout, PROTO_IMAP4, px[i]);
                if (px[i] == 'O')
                    state++;
                else
                    state = STATE_DONE;
                else {
                    state = 0xffffffff;
                    tcp_close(more);
                }
                break;
            case 3:
                banout_append_char(banout, PROTO_IMAP4, px[i]);
                if (px[i] == 'K')
                    state++;
                else
                    state = STATE_DONE;
                else {
                    state = 0xffffffff;
                    tcp_close(more);
                }
                break;
            case 4:
                if (px[i] == ' ') {
@@ -92,46 +100,58 @@ imap4_parse( const struct Banner1 *banner1,
                    state += 100;
                else if (px[i] == 'a')
                    state++;
                else
                    state = STATE_DONE;
                else {
                    state = 0xffffffff;
                    tcp_close(more);
                }
                break;
            case 101:
            case 301:
                banout_append_char(banout, PROTO_IMAP4, px[i]);
                if (px[i] == '0')
                    state++;
                else 
                    state = STATE_DONE;
                else {
                    state = 0xffffffff;
                    tcp_close(more);
                }
                break;
            case 102:
            case 302:
                banout_append_char(banout, PROTO_IMAP4, px[i]);
                if (px[i] == '0')
                    state++;
                else 
                    state = STATE_DONE;
                else {
                    state = 0xffffffff;
                    tcp_close(more);
                }
                break;
            case 103:
                banout_append_char(banout, PROTO_IMAP4, px[i]);
                if (px[i] == '1')
                    state++;
                else 
                    state = STATE_DONE;
                else {
                    state = 0xffffffff;
                    tcp_close(more);
                }
                break;
            case 303:
                banout_append_char(banout, PROTO_IMAP4, px[i]);
                if (px[i] == '2')
                    state++;
                else 
                    state = STATE_DONE;
                else {
                    state = 0xffffffff;
                    tcp_close(more);
                }
                break;
            case 104:
            case 304:
                banout_append_char(banout, PROTO_IMAP4, px[i]);
                if (px[i] == ' ')
                    state++;
                else 
                    state = STATE_DONE;
                else {
                    state = 0xffffffff;
                    tcp_close(more);
                }
                break;
            case 105:
                banout_append_char(banout, PROTO_IMAP4, px[i]);
@@ -163,6 +183,7 @@ imap4_parse( const struct Banner1 *banner1,
                }
                break;
                
            case 0xffffffff:
            default:
                i = (unsigned)length;
                break;
Loading