Changeset 1568

Show
Ignore:
Timestamp:
02/25/08 09:23:11 (6 months ago)
Author:
till
Message:

nebula
- endian awareness added
- memory initialization fixed

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • nebula/trunk/src/session.c

    r1566 r1568  
    1919 */ 
    2020 
     21#include <arpa/inet.h> 
    2122#include <errno.h> 
    2223#include <poll.h> 
     
    110111int session_handle_data(struct pollfd *pfd, submission *s) { 
    111112        int             rv; 
     113        u_int16_t       hmac_port; 
    112114        u_int32_t       nonce; 
    113115        char            *md5sum; 
     
    116118        trie_node       *t; 
    117119 
     120 
    118121        if (!pfd || !s) { 
    119122                fprintf(stderr, "Error - Invalid parameters to submission handler.\n"); 
    120123                exit(EXIT_FAILURE); 
    121124        } 
     125 
    122126 
    123127        switch (s->state) { 
     
    125129                // send random nonce 
    126130                srand(time(0)); 
    127                 nonce = (u_int32_t) (RAND_MAX * (rand() / (RAND_MAX + 1.0))); 
    128                 if (write(pfd->fd, &nonce, 4) == -1) { 
     131                nonce = htonl((u_int32_t) (RAND_MAX * (rand() / (RAND_MAX + 1.0)))); 
     132                if (write(pfd->fd, &(nonce), 4) == -1) { 
    129133                        fprintf(stderr, "Error - Unable to send session nonce: %s.\n", strerror(errno)); 
    130134                        return(-1); 
    131135                } 
    132                 s->nonce        = nonce
     136                s->nonce        = ntohl(nonce)
    133137                s->state        = NONCE_SENT; 
    134138                s->bytes_read   = 0; 
     
    235239                        return(-1); 
    236240                } else if (rv == 2) { 
     241                        s->port = ntohs(s->port); 
    237242                        if (verbose > 1) printf("  port: %u\n", s->port); 
    238243                        s->state        = PORT_READ; 
     
    245250                        return(-1); 
    246251                } else if (rv == 4) { 
     252                        s->attack_len = ntohl(s->attack_len); 
    247253                        if (verbose > 1) printf("  bytes of attacks: %lu\n", s->attack_len); 
    248254                        s->state        = ATTACK_LEN_READ; 
     
    255261                        return(-1); 
    256262                } else if (rv == 4) { 
     263                        s->cattack_len = ntohl(s->cattack_len); 
    257264                        if (verbose > 1) printf("  bytes of compressed attacks: %u\n", s->cattack_len); 
    258265                        s->state        = CATTACK_LEN_READ; 
     
    262269        case CATTACK_LEN_READ: 
    263270                // read compressed attack 
    264                 if (!s->cattack && ((s->cattack = malloc(s->cattack_len)) == NULL)) { 
     271                if (!s->cattack && ((s->cattack = calloc(1, s->cattack_len)) == NULL)) { 
    265272                        fprintf(stderr, "Error - Unable to allocate memory: %s.\n", strerror(errno)); 
    266273                        exit(EXIT_FAILURE); 
    267274                } 
    268                 memset(s->cattack, 0, s->cattack_len); 
    269275                if ((rv = session_read_data(pfd->fd, s, s->cattack, s->cattack_len)) == -1) { 
    270276                        return(-1); 
     
    277283                // read length of HMAC 
    278284                s->hmac_len = 0xffff; 
    279                 if ((rv = session_read_data(pfd->fd, s, (u_char *) &(s->hmac_len), 2)) == -1) { 
     285                if ((rv = session_read_data(pfd->fd, s, (u_char *) &(s->hmac_len), sizeof(s->hmac_len))) == -1) { 
    280286                        return(-1); 
    281287                } else if (rv == 2) { 
     288                        s->hmac_len = ntohs(s->hmac_len); 
    282289                        if (verbose > 1) printf("  length of HMAC: %u\n", s->hmac_len); 
    283290                        s->state        = HMAC_LEN_READ; 
     
    294301                } else if (rv == s->hmac_len) { 
    295302                        if (verbose > 1) printf("  HMAC read: %s\n", s->hmac); 
     303                        hmac_port = htons(s->port); 
    296304 
    297305                        // send OK 
     
    309317                        } 
    310318                        memcpy(s->cattack+s->cattack_len, &s->protocol, 1); 
    311                         memcpy(s->cattack+s->cattack_len+1, &s->port, 2); 
     319                        memcpy(s->cattack+s->cattack_len+1, &hmac_port, 2); 
    312320 
    313321                        if ((sha512sum = session_hmac(&s->cattack, s->cattack_len+3)) == NULL) { 
  • nebula/trunk/src/session.h

    r1566 r1568  
    5959typedef struct submission { 
    6060        sstate          state;                  // session state 
    61         ssize_t               bytes_read;             // number of bytes read so far 
    62         u_int32_t       nonce; 
     61        u_int32_t     bytes_read;             // number of bytes read so far 
     62        u_int32_t       nonce;                 // session nonce, stored in network byte order 
    6363        char            secret_hash[129];       // hash(nonce+secret) 
    6464        char            *md5sum;                // md5 hash of uncompressed attack data