--- embedaddon/libnet/src/libnet_init.c 2012/02/21 22:14:23 1.1 +++ embedaddon/libnet/src/libnet_init.c 2023/09/27 11:11:38 1.1.1.3 @@ -1,5 +1,5 @@ /* - * $Id: libnet_init.c,v 1.1 2012/02/21 22:14:23 misho Exp $ + * $Id: libnet_init.c,v 1.1.1.3 2023/09/27 11:11:38 misho Exp $ * * libnet * libnet_init.c - Initilization routines. @@ -30,34 +30,20 @@ * */ -#if (HAVE_CONFIG_H) -#include "../include/config.h" -#endif -#if (!(_WIN32) || (__CYGWIN__)) -#include "../include/libnet.h" -#else -#include "../include/win32/libnet.h" -#endif +#include "common.h" libnet_t * -libnet_init(int injection_type, char *device, char *err_buf) +libnet_init(int injection_type, const char *device, char *err_buf) { libnet_t *l = NULL; -#if !defined(__WIN32__) - if (getuid() && geteuid()) - { - snprintf(err_buf, LIBNET_ERRBUF_SIZE, - "%s(): UID or EUID of 0 required\n", __func__); - goto bad; - } -#else +#if defined(__WIN32__) WSADATA wsaData; if ((WSAStartup(0x0202, &wsaData)) != 0) { snprintf(err_buf, LIBNET_ERRBUF_SIZE, - "%s(): unable to initialize winsock 2\n", __func__); + "%s(): unable to initialize winsock 2", __func__); goto bad; } #endif @@ -65,7 +51,7 @@ libnet_init(int injection_type, char *device, char *er l = (libnet_t *)malloc(sizeof (libnet_t)); if (l == NULL) { - snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s(): malloc(): %s\n", __func__, + snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s(): malloc(): %s", __func__, strerror(errno)); goto bad; } @@ -75,22 +61,25 @@ libnet_init(int injection_type, char *device, char *er l->injection_type = injection_type; l->ptag_state = LIBNET_PTAG_INITIALIZER; l->device = (device ? strdup(device) : NULL); + l->fd = -1; strncpy(l->label, LIBNET_LABEL_DEFAULT, LIBNET_LABEL_SIZE); - l->label[sizeof(l->label)] = '\0'; + l->label[LIBNET_LABEL_SIZE - 1] = '\0'; switch (l->injection_type) { + case LIBNET_NONE: + break; case LIBNET_LINK: case LIBNET_LINK_ADV: if (libnet_select_device(l) == -1) { - snprintf(err_buf, LIBNET_ERRBUF_SIZE, l->err_buf); + snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf); goto bad; } if (libnet_open_link(l) == -1) { - snprintf(err_buf, LIBNET_ERRBUF_SIZE, l->err_buf); + snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf); goto bad; } break; @@ -98,7 +87,7 @@ libnet_init(int injection_type, char *device, char *er case LIBNET_RAW4_ADV: if (libnet_open_raw4(l) == -1) { - snprintf(err_buf, LIBNET_ERRBUF_SIZE, l->err_buf); + snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf); goto bad; } break; @@ -106,13 +95,13 @@ libnet_init(int injection_type, char *device, char *er case LIBNET_RAW6_ADV: if (libnet_open_raw6(l) == -1) { - snprintf(err_buf, LIBNET_ERRBUF_SIZE, l->err_buf); + snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf); goto bad; } break; default: snprintf(err_buf, LIBNET_ERRBUF_SIZE, - "%s(): unsupported injection type\n", __func__); + "%s(): unsupported injection type", __func__); goto bad; break; } @@ -132,11 +121,9 @@ libnet_destroy(libnet_t *l) { if (l) { - close(l->fd); - if (l->device) - { - free(l->device); - } + if (l->fd != -1) + close(l->fd); + free(l->device); libnet_clear_packet(l); free(l); } @@ -146,26 +133,19 @@ void libnet_clear_packet(libnet_t *l) { libnet_pblock_t *p; - libnet_pblock_t *next; - if (l) + if (!l) { - p = l->protocol_blocks; - if (p) - { - for (; p; p = next) - { - next = p->next; - if (p->buf) - { - free(p->buf); - } - free(p); - } - } - l->protocol_blocks = NULL; - l->total_size = 0; + return; } + + while((p = l->protocol_blocks)) + { + libnet_pblock_delete(l, p); + } + + /* All pblocks are deleted, so start the tag count over from 1. */ + l->ptag_state = 0; } void @@ -189,10 +169,10 @@ libnet_getfd(libnet_t *l) return (-1); } - return (l->fd); + return (int)(l->fd); } -int8_t * +const char * libnet_getdevice(libnet_t *l) { if (l == NULL) @@ -203,7 +183,7 @@ libnet_getdevice(libnet_t *l) return (l->device); } -u_int8_t * +uint8_t * libnet_getpbuf(libnet_t *l, libnet_ptag_t ptag) { libnet_pblock_t *p; @@ -225,7 +205,7 @@ libnet_getpbuf(libnet_t *l, libnet_ptag_t ptag) } } -u_int32_t +uint32_t libnet_getpbuf_size(libnet_t *l, libnet_ptag_t ptag) { libnet_pblock_t *p; @@ -247,11 +227,12 @@ libnet_getpbuf_size(libnet_t *l, libnet_ptag_t ptag) } } -u_int32_t +uint32_t libnet_getpacket_size(libnet_t *l) { + /* Why doesn't this return l->total_size? */ libnet_pblock_t *p; - u_int32_t n; + uint32_t n; if (l == NULL) { @@ -270,4 +251,9 @@ libnet_getpacket_size(libnet_t *l) return (n); } -/* EOF */ +/** + * Local Variables: + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + */