Annotation of embedaddon/hping2/datafiller.c, revision 1.1.1.1

1.1       misho       1: /* 
                      2:  * $smu-mark$ 
                      3:  * $name: datafiller.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:47 MET 1999$ 
                      8:  * $rev: 8$ 
                      9:  */ 
                     10: 
                     11: #include <stdio.h>
                     12: #include <stdlib.h>
                     13: #include <unistd.h>
                     14: #include <sys/types.h>
                     15: #include <sys/stat.h>
                     16: #include <fcntl.h>
                     17: #include <string.h> /* memset */
                     18: 
                     19: #include "hping2.h"
                     20: #include "globals.h"
                     21: 
                     22: void datafiller(char *p, int size)
                     23: {
                     24:        static int fd = 0;
                     25:        int readed, diff;
                     26: 
                     27:        if (!fd) {
                     28:                fd = open(datafilename, O_RDONLY);
                     29:                if (fd == -1) {
                     30:                        perror("[datafiller] open()");
                     31:                        fd = 0; /* will retry to open the file for
                     32:                                 * the next packet */
                     33:                        memset(p, 'X', size);
                     34:                        return;
                     35:                }
                     36:        }
                     37: 
                     38:        if (p == NULL && fd != -1) { /* seek operation */
                     39:                /* size-1 because packet with id 1 start from 0 */
                     40:                lseek(fd, (data_size-signlen)*(size-1), SEEK_SET);
                     41:                return;
                     42:        }
                     43: 
                     44: restart: /* if EOF occurs, after rewind, restart */
                     45: 
                     46:        readed = read(fd, p, size);
                     47:        if (readed == size)
                     48:                return;
                     49:        else if (readed == -1) {
                     50:                perror("[datafiller] read()");
                     51:                close(fd);
                     52:                fd = 0; /* will retry to open the file for the next packet */
                     53:                memset(p, 'X', size);
                     54:                return;
                     55:        }
                     56:        else if (readed < size && opt_end == FALSE) {
                     57:                lseek(fd, 0, SEEK_SET);
                     58:                if (readed == 0)
                     59:                        goto restart;
                     60:        }
                     61:        else if (readed < size && opt_end == TRUE) {
                     62:                fprintf(stderr, "EOF reached, wait some second than press "
                     63:                                "ctrl+c\n");
                     64:                eof_reached = TRUE;
                     65:        } else {
                     66:                printf("[datafiller.c INTERNAL ERROR] readed = %d - "
                     67:                        "opt_end == %d\n", readed, opt_end);
                     68:                exit(1);
                     69:        }
                     70:        diff = size - readed;
                     71:        memset(p+readed, '\0', diff); /* padding */
                     72:        lseek(fd, 0, SEEK_SET);
                     73:        return;
                     74: }

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