Annotation of embedaddon/hping2/listen.c, revision 1.1

1.1     ! misho       1: /* 
        !             2:  * $smu-mark$ 
        !             3:  * $name: listen.c$ 
        !             4:  * $author: Salvatore Sanfilippo <antirez@invece.org>$ 
        !             5:  * $copyright: Copyright (C) 1999 by Salvatore Sanfilippo$ 
        !             6:  * $license: This software is under GPL version 2 of license$ 
        !             7:  * $date: Fri Nov  5 11:55:48 MET 1999$ 
        !             8:  * $rev: 8$ 
        !             9:  */ 
        !            10: 
        !            11: #include <stdlib.h>
        !            12: #include <stdio.h>
        !            13: #include <string.h>
        !            14: #include <unistd.h>
        !            15: #include <sys/types.h>
        !            16: #include <sys/socket.h>
        !            17: #include <netinet/in.h>
        !            18: 
        !            19: #include "hping2.h" /* hping2.h includes hcmp.h */
        !            20: #include "globals.h"
        !            21: 
        !            22: void listenmain(void)
        !            23: {
        !            24:        int size, ip_size;
        !            25:        int stdoutFD = fileno(stdout);
        !            26:        char packet[IP_MAX_SIZE+linkhdr_size];
        !            27:        char *p, *ip_packet;
        !            28:        struct myiphdr ip;
        !            29:        __u16 id;
        !            30:        static __u16 exp_id; /* expected id */
        !            31: 
        !            32:        exp_id = 1;
        !            33: 
        !            34:        while(1) {
        !            35:                size = read_packet(packet, IP_MAX_SIZE+linkhdr_size);
        !            36:                switch(size) {
        !            37:                case 0:
        !            38:                        continue;
        !            39:                case -1:
        !            40:                        exit(1);
        !            41:                }
        !            42:        
        !            43:                /* Skip truncated packets */
        !            44:                if (size < linkhdr_size+IPHDR_SIZE)
        !            45:                        continue;
        !            46:                ip_packet = packet + linkhdr_size;
        !            47: 
        !            48:                /* copy the ip header so it will be aligned */
        !            49:                memcpy(&ip, ip_packet, sizeof(ip));
        !            50:                id = ntohs(ip.id);
        !            51:                ip_size = ntohs(ip.tot_len);
        !            52:                if (size-linkhdr_size > ip_size)
        !            53:                        size = ip_size;
        !            54:                else
        !            55:                        size -= linkhdr_size;
        !            56: 
        !            57:                if ((p = memstr(ip_packet, sign, size))) {
        !            58:                        if (opt_verbose)
        !            59:                                fprintf(stderr, "packet %d received\n", id);
        !            60: 
        !            61:                        if (opt_safe) {
        !            62:                                if (id == exp_id)
        !            63:                                        exp_id++;
        !            64:                                else {
        !            65:                                        if (opt_verbose)
        !            66:                                                fprintf(stderr, "packet not in sequence (id %d) received\n", id);
        !            67:                                        send_hcmp(HCMP_RESTART, exp_id);
        !            68:                                        if (opt_verbose)
        !            69:                                                fprintf(stderr, "HCMP restart from %d sent\n", exp_id);
        !            70:                                        continue; /* discard this packet */
        !            71:                                }
        !            72:                        }
        !            73: 
        !            74:                        p+=strlen(sign);
        !            75:                        write(stdoutFD, p, size-(p-ip_packet));
        !            76:                }
        !            77:        }
        !            78: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>