--- embedaddon/libnet/src/libnet_cq.c 2012/02/21 22:14:23 1.1.1.1 +++ embedaddon/libnet/src/libnet_cq.c 2023/09/27 11:11:38 1.1.1.4 @@ -1,5 +1,5 @@ /* - * $Id: libnet_cq.c,v 1.1.1.1 2012/02/21 22:14:23 misho Exp $ + * $Id: libnet_cq.c,v 1.1.1.4 2023/09/27 11:11:38 misho Exp $ * * libnet * libnet_cq.c - context queue management routines @@ -31,14 +31,7 @@ * */ -#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" /* private function prototypes */ static libnet_cq_t *libnet_cq_find_internal(libnet_t *); @@ -50,7 +43,8 @@ static libnet_cq_t *l_cq = NULL; static libnet_cqd_t l_cqd = {0, CQ_LOCK_UNLOCKED, NULL}; -static inline int set_cq_lock(u_int x) +static int +set_cq_lock(uint32_t x) { if (check_cq_lock(x)) { @@ -61,7 +55,8 @@ static inline int set_cq_lock(u_int x) return (1); } -static inline int clear_cq_lock(u_int x) +static int +clear_cq_lock(uint32_t x) { if (!check_cq_lock(x)) { @@ -86,14 +81,14 @@ libnet_cq_add(libnet_t *l, char *label) if (cq_is_wlocked()) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): can't add, context queue is write locked\n", __func__); + "%s(): can't add, context queue is write locked", __func__); return (-1); } /* ensure there is a label */ if (label == NULL) { - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): empty label\n", + snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, "%s(): empty label", __func__); return (-1); } @@ -105,7 +100,7 @@ libnet_cq_add(libnet_t *l, char *label) if (l_cq == NULL) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): can't malloc initial context queue: %s\n", + "%s(): can't malloc initial context queue: %s", __func__, strerror(errno)); return (-1); } @@ -114,7 +109,7 @@ libnet_cq_add(libnet_t *l, char *label) /* label the context with the user specified string */ strncpy(l->label, label, LIBNET_LABEL_SIZE); - l->label[LIBNET_LABEL_SIZE] = '\0'; + l->label[LIBNET_LABEL_SIZE - 1] = '\0'; l_cq->next = NULL; l_cq->prev = NULL; @@ -136,7 +131,7 @@ libnet_cq_add(libnet_t *l, char *label) if (l_cq == NULL) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): can't malloc new context queue: %s\n", + "%s(): can't malloc new context queue: %s", __func__, strerror(errno)); return (-1); } @@ -145,7 +140,7 @@ libnet_cq_add(libnet_t *l, char *label) /* label the context with the user specified string */ strncpy(l->label, label, LIBNET_LABEL_SIZE); - l->label[LIBNET_LABEL_SIZE] = '\0'; + l->label[LIBNET_LABEL_SIZE -1] = '\0'; new->next = l_cq; new->prev = NULL; @@ -168,14 +163,12 @@ libnet_cq_remove(libnet_t *l) if (l_cq == NULL) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): can't remove from empty context queue\n", __func__); + "%s(): can't remove from empty context queue", __func__); return (NULL); } if (l == NULL) { - snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): can't remove empty libnet context\n", __func__); return(NULL); } @@ -183,7 +176,7 @@ libnet_cq_remove(libnet_t *l) if (cq_is_wlocked()) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): can't remove, context queue is write locked\n", + "%s(): can't remove, context queue is write locked", __func__); return (NULL); } @@ -191,7 +184,7 @@ libnet_cq_remove(libnet_t *l) if ((p = libnet_cq_find_internal(l)) == NULL) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): context not present in context queue\n", __func__); + "%s(): context not present in context queue", __func__); return (NULL); } @@ -282,13 +275,13 @@ libnet_cq_dup_check(libnet_t *l, char *label) if (p->context == l) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): context already in context queue\n", __func__); + "%s(): context already in context queue", __func__); return (1); } if (strncmp(p->context->label, label, LIBNET_LABEL_SIZE) == 0) { snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, - "%s(): duplicate label %s\n", __func__, label); + "%s(): duplicate label %s", __func__, label); return (1); } } @@ -325,7 +318,7 @@ libnet_cq_find_by_label(char *label) return (p ? p->context : NULL); } -int8_t * +const char * libnet_cq_getlabel(libnet_t *l) { return (l->label); @@ -344,6 +337,8 @@ libnet_cq_destroy() libnet_destroy(tmp->context); free(tmp); } + l_cq = NULL; + memset(&l_cqd, 0, sizeof(l_cqd)); } libnet_t * @@ -388,8 +383,26 @@ libnet_cq_next() return (l_cqd.current ? l_cqd.current->context : NULL); } -u_int32_t +uint32_t libnet_cq_size() { return (l_cqd.node); } + +uint32_t +libnet_cq_end_loop() +{ + if (! clear_cq_lock(CQ_LOCK_WRITE)) + { + return (0); + } + l_cqd.current = l_cq; + return (1); +} + +/** + * Local Variables: + * indent-tabs-mode: nil + * c-file-style: "stroustrup" + * End: + */