Annotation of embedaddon/libnet/src/libnet_init.c, revision 1.1.1.3
1.1 misho 1: /*
2: * $Id: libnet_init.c,v 1.17 2004/03/16 18:40:59 mike Exp $
3: *
4: * libnet
5: * libnet_init.c - Initilization routines.
6: *
7: * Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
8: * All rights reserved.
9: *
10: * Redistribution and use in source and binary forms, with or without
11: * modification, are permitted provided that the following conditions
12: * are met:
13: * 1. Redistributions of source code must retain the above copyright
14: * notice, this list of conditions and the following disclaimer.
15: * 2. Redistributions in binary form must reproduce the above copyright
16: * notice, this list of conditions and the following disclaimer in the
17: * documentation and/or other materials provided with the distribution.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29: * SUCH DAMAGE.
30: *
31: */
32:
1.1.1.3 ! misho 33: #include "common.h"
1.1 misho 34:
35: libnet_t *
1.1.1.2 misho 36: libnet_init(int injection_type, const char *device, char *err_buf)
1.1 misho 37: {
38: libnet_t *l = NULL;
39:
1.1.1.2 misho 40: #if defined(__WIN32__)
1.1 misho 41: WSADATA wsaData;
42:
43: if ((WSAStartup(0x0202, &wsaData)) != 0)
44: {
45: snprintf(err_buf, LIBNET_ERRBUF_SIZE,
1.1.1.3 ! misho 46: "%s(): unable to initialize winsock 2", __func__);
1.1 misho 47: goto bad;
48: }
49: #endif
50:
51: l = (libnet_t *)malloc(sizeof (libnet_t));
52: if (l == NULL)
53: {
1.1.1.3 ! misho 54: snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s(): malloc(): %s", __func__,
1.1 misho 55: strerror(errno));
56: goto bad;
57: }
58:
59: memset(l, 0, sizeof (*l));
60:
61: l->injection_type = injection_type;
62: l->ptag_state = LIBNET_PTAG_INITIALIZER;
63: l->device = (device ? strdup(device) : NULL);
1.1.1.2 misho 64: l->fd = -1;
1.1 misho 65:
66: strncpy(l->label, LIBNET_LABEL_DEFAULT, LIBNET_LABEL_SIZE);
1.1.1.2 misho 67: l->label[LIBNET_LABEL_SIZE - 1] = '\0';
1.1 misho 68:
69: switch (l->injection_type)
70: {
1.1.1.2 misho 71: case LIBNET_NONE:
72: break;
1.1 misho 73: case LIBNET_LINK:
74: case LIBNET_LINK_ADV:
75: if (libnet_select_device(l) == -1)
76: {
1.1.1.2 misho 77: snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf);
1.1 misho 78: goto bad;
79: }
80: if (libnet_open_link(l) == -1)
81: {
1.1.1.2 misho 82: snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf);
1.1 misho 83: goto bad;
84: }
85: break;
86: case LIBNET_RAW4:
87: case LIBNET_RAW4_ADV:
88: if (libnet_open_raw4(l) == -1)
89: {
1.1.1.2 misho 90: snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf);
1.1 misho 91: goto bad;
92: }
93: break;
94: case LIBNET_RAW6:
95: case LIBNET_RAW6_ADV:
96: if (libnet_open_raw6(l) == -1)
97: {
1.1.1.2 misho 98: snprintf(err_buf, LIBNET_ERRBUF_SIZE, "%s", l->err_buf);
1.1 misho 99: goto bad;
100: }
101: break;
102: default:
103: snprintf(err_buf, LIBNET_ERRBUF_SIZE,
1.1.1.3 ! misho 104: "%s(): unsupported injection type", __func__);
1.1 misho 105: goto bad;
106: break;
107: }
108:
109: return (l);
110:
111: bad:
112: if (l)
113: {
114: libnet_destroy(l);
115: }
116: return (NULL);
117: }
118:
119: void
120: libnet_destroy(libnet_t *l)
121: {
122: if (l)
123: {
1.1.1.3 ! misho 124: if (l->fd != -1)
! 125: close(l->fd);
1.1.1.2 misho 126: free(l->device);
1.1 misho 127: libnet_clear_packet(l);
128: free(l);
129: }
130: }
131:
132: void
133: libnet_clear_packet(libnet_t *l)
134: {
135: libnet_pblock_t *p;
136:
1.1.1.2 misho 137: if (!l)
1.1 misho 138: {
1.1.1.2 misho 139: return;
1.1 misho 140: }
1.1.1.2 misho 141:
142: while((p = l->protocol_blocks))
143: {
144: libnet_pblock_delete(l, p);
145: }
146:
147: /* All pblocks are deleted, so start the tag count over from 1. */
148: l->ptag_state = 0;
1.1 misho 149: }
150:
151: void
152: libnet_stats(libnet_t *l, struct libnet_stats *ls)
153: {
154: if (l == NULL)
155: {
156: return;
157: }
158:
159: ls->packets_sent = l->stats.packets_sent;
160: ls->packet_errors = l->stats.packet_errors;
161: ls->bytes_written = l->stats.bytes_written;
162: }
163:
164: int
165: libnet_getfd(libnet_t *l)
166: {
167: if (l == NULL)
168: {
169: return (-1);
170: }
171:
1.1.1.3 ! misho 172: return (int)(l->fd);
1.1 misho 173: }
174:
1.1.1.2 misho 175: const char *
1.1 misho 176: libnet_getdevice(libnet_t *l)
177: {
178: if (l == NULL)
179: {
180: return (NULL);
181: }
182:
183: return (l->device);
184: }
185:
1.1.1.2 misho 186: uint8_t *
1.1 misho 187: libnet_getpbuf(libnet_t *l, libnet_ptag_t ptag)
188: {
189: libnet_pblock_t *p;
190:
191: if (l == NULL)
192: {
193: return (NULL);
194: }
195:
196: p = libnet_pblock_find(l, ptag);
197: if (p == NULL)
198: {
199: /* err msg set in libnet_pblock_find() */
200: return (NULL);
201: }
202: else
203: {
204: return (p->buf);
205: }
206: }
207:
1.1.1.2 misho 208: uint32_t
1.1 misho 209: libnet_getpbuf_size(libnet_t *l, libnet_ptag_t ptag)
210: {
211: libnet_pblock_t *p;
212:
213: if (l == NULL)
214: {
215: return (0);
216: }
217:
218: p = libnet_pblock_find(l, ptag);
219: if (p == NULL)
220: {
221: /* err msg set in libnet_pblock_find() */
222: return (0);
223: }
224: else
225: {
226: return (p->b_len);
227: }
228: }
229:
1.1.1.2 misho 230: uint32_t
1.1 misho 231: libnet_getpacket_size(libnet_t *l)
232: {
1.1.1.2 misho 233: /* Why doesn't this return l->total_size? */
1.1 misho 234: libnet_pblock_t *p;
1.1.1.2 misho 235: uint32_t n;
1.1 misho 236:
237: if (l == NULL)
238: {
239: return (0);
240: }
241:
242: n = 0;
243: p = l->protocol_blocks;
244: if (p)
245: {
246: for (; p; p = p->next)
247: {
248: n += p->b_len;
249: }
250: }
251: return (n);
252: }
253:
1.1.1.3 ! misho 254: /**
! 255: * Local Variables:
! 256: * indent-tabs-mode: nil
! 257: * c-file-style: "stroustrup"
! 258: * End:
! 259: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>