* $Id: libnet_cq.c,v 1.20 2005/11/29 22:13:48 carlosc Exp $
* libnet_cq.c - context queue management routines
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
* Copyright (c) 2002 Frédéric Raynal <pappy@security-labs.org>
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#include "../include/config.h"
#if (!(_WIN32) || (__CYGWIN__))
#include "../include/libnet.h"
#include "../include/win32/libnet.h"
/* private function prototypes */
static libnet_cq_t
*libnet_cq_find_internal(libnet_t
*);
static int libnet_cq_dup_check(libnet_t
*, char *);
static libnet_cq_t
*libnet_cq_find_by_label_internal(char *label
);
/* global context queue */
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 inline int clear_cq_lock(u_int x
)
libnet_cq_add(libnet_t
*l
, char *label
)
/* check for write lock on the context queue */
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): can't add, context queue is write locked\n", __func__
);
/* ensure there is a label */
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
, "%s(): empty label\n",
/* check to see if we're the first node on the list */
l_cq
= (libnet_cq_t
*)malloc(sizeof (libnet_cq_t
));
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): can't malloc initial context queue: %s\n",
__func__
, strerror(errno
));
/* label the context with the user specified string */
strncpy(l
->label
, label
, LIBNET_LABEL_SIZE
);
l
->label
[LIBNET_LABEL_SIZE
] = '\0';
/* track the number of nodes in the context queue */
/* check to see if the cq we're about to add is already in the list */
if (libnet_cq_dup_check(l
, label
))
/* error message set in libnet_cq_dup_check() */
new = (libnet_cq_t
*)malloc(sizeof (libnet_cq_t
));
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): can't malloc new context queue: %s\n",
__func__
, strerror(errno
));
/* label the context with the user specified string */
strncpy(l
->label
, label
, LIBNET_LABEL_SIZE
);
l
->label
[LIBNET_LABEL_SIZE
] = '\0';
/* track the number of nodes in the context queue */
libnet_cq_remove(libnet_t
*l
)
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): can't remove from empty context queue\n", __func__
);
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): can't remove empty libnet context\n", __func__
);
/* check for write lock on the cq */
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): can't remove, context queue is write locked\n",
if ((p
= libnet_cq_find_internal(l
)) == NULL
)
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): context not present in context queue\n", __func__
);
/* track the number of nodes in the cq */
libnet_cq_remove_by_label(char *label
)
if ((p
= libnet_cq_find_by_label_internal(label
)) == NULL
)
/* no context to write an error message */
/* now we have a context, but the user can't see it */
/* track the number of nodes in the cq */
libnet_cq_find_internal(libnet_t
*l
)
for (p
= l_cq
; p
; p
= p
->next
)
libnet_cq_dup_check(libnet_t
*l
, char *label
)
for (p
= l_cq
; p
; p
= p
->next
)
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): context already in context queue\n", __func__
);
if (strncmp(p
->context
->label
, label
, LIBNET_LABEL_SIZE
) == 0)
snprintf(l
->err_buf
, LIBNET_ERRBUF_SIZE
,
"%s(): duplicate label %s\n", __func__
, label
);
libnet_cq_find_by_label_internal(char *label
)
for (p
= l_cq
; p
; p
= p
->next
)
if (!strncmp(p
->context
->label
, label
, LIBNET_LABEL_SIZE
))
libnet_cq_find_by_label(char *label
)
p
= libnet_cq_find_by_label_internal(label
);
return (p
? p
->context
: NULL
);
libnet_cq_getlabel(libnet_t
*l
)
libnet_destroy(tmp
->context
);
if (!set_cq_lock(CQ_LOCK_WRITE
))
return (l_cqd
.current
->context
);
if (l_cqd
.current
== NULL
)
l_cqd
.current
= l_cqd
.current
->next
;
return (l_cqd
.current
? l_cqd
.current
->context
: NULL
);