Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / verif / env / common / vera / niu_ippktgen / C / libnet / src / libnet_raw.c
CommitLineData
86530b38
AT
1/*
2 * $Id: libnet_raw.c,v 1.20 2005/11/29 22:11:10 carlosc Exp $
3 *
4 * libnet
5 * libnet_raw.c - raw sockets 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
33#if (HAVE_CONFIG_H)
34#include "../include/config.h"
35#endif
36#if (!(_WIN32) || (__CYGWIN__))
37#include "../include/libnet.h"
38#else
39#include "../include/win32/libnet.h"
40#endif
41
42#if defined (__WIN32__)
43int
44libnet_open_raw4(libnet_t *l)
45{
46 return (libnet_open_link(l));
47}
48
49int
50libnet_open_raw6(libnet_t *l)
51{
52 return (libnet_open_link(l));
53}
54
55int
56libnet_close_raw4(libnet_t *l)
57{
58 return (libnet_close_link_interface(l));
59}
60
61int
62libnet_close_raw6(libnet_t *l)
63{
64 return (libnet_close_link_interface(l));
65}
66#else
67int
68libnet_open_raw4(libnet_t *l)
69{
70 int len;
71
72#if !(__WIN32__)
73 int n = 1;
74#if (__svr4__)
75 void *nptr = &n;
76#else
77 int *nptr = &n;
78#endif /* __svr4__ */
79#else
80 BOOL n;
81#endif
82
83 if (l == NULL)
84 {
85 return (-1);
86 }
87
88 l->fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
89 if (l->fd == -1)
90 {
91 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
92 "%s(): SOCK_RAW allocation failed: %s\n",
93 __func__, strerror(errno));
94 goto bad;
95 }
96
97#ifdef IP_HDRINCL
98/*
99 * man raw
100 *
101 * The IPv4 layer generates an IP header when sending a packet unless
102 * the IP_HDRINCL socket option is enabled on the socket. When it
103 * is enabled, the packet must contain an IP header. For
104 * receiving the IP header is always included in the packet.
105 */
106#if !(__WIN32__)
107 if (setsockopt(l->fd, IPPROTO_IP, IP_HDRINCL, nptr, sizeof(n)) == -1)
108#else
109 n = TRUE;
110 if (setsockopt(l->fd, IPPROTO_IP, IP_HDRINCL, (int8_t *)&n,
111 sizeof(n)) == -1)
112#endif
113
114 {
115 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
116 "%s(): set IP_HDRINCL failed: %s\n",
117 __func__, strerror(errno));
118 goto bad;
119 }
120#endif /* IP_HDRINCL */
121
122#ifdef SO_SNDBUF
123
124/*
125 * man 7 socket
126 *
127 * Sets and gets the maximum socket send buffer in bytes.
128 *
129 * Taken from libdnet by Dug Song
130 */
131 len = sizeof(n);
132 if (getsockopt(l->fd, SOL_SOCKET, SO_SNDBUF, &n, &len) < 0)
133 {
134 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
135 "%s(): get SO_SNDBUF failed: %s\n",
136 __func__, strerror(errno));
137 goto bad;
138 }
139
140 for (n += 128; n < 1048576; n += 128)
141 {
142 if (setsockopt(l->fd, SOL_SOCKET, SO_SNDBUF, &n, len) < 0)
143 {
144 if (errno == ENOBUFS)
145 {
146 break;
147 }
148 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
149 "%s(): set SO_SNDBUF failed: %s\n",
150 __func__, strerror(errno));
151 goto bad;
152 }
153 }
154#endif
155
156#ifdef SO_BROADCAST
157/*
158 * man 7 socket
159 *
160 * Set or get the broadcast flag. When enabled, datagram sockets
161 * receive packets sent to a broadcast address and they are allowed
162 * to send packets to a broadcast address. This option has no
163 * effect on stream-oriented sockets.
164 */
165 if (setsockopt(l->fd, SOL_SOCKET, SO_BROADCAST, nptr, sizeof(n)) == -1)
166 {
167 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
168 "%s(): set SO_BROADCAST failed: %s\n",
169 __func__, strerror(errno));
170 goto bad;
171 }
172#endif /* SO_BROADCAST */
173 return (l->fd);
174
175bad:
176 return (-1);
177}
178
179
180int
181libnet_close_raw4(libnet_t *l)
182{
183 if (l == NULL)
184 {
185 return (-1);
186 }
187
188 return (close(l->fd));
189}
190
191#if ((defined HAVE_SOLARIS && !defined HAVE_SOLARIS_IPV6) || defined (__WIN32__))
192int libnet_open_raw6(libnet_t *l)
193{
194 return (-1);
195}
196
197#else
198int
199libnet_open_raw6(libnet_t *l)
200{
201#if !(__WIN32__)
202#if (__svr4__)
203 int one = 1;
204 void *oneptr = &one;
205#else
206#if (__linux__)
207 int one = 1;
208 int *oneptr = &one;
209#endif
210#endif /* __svr4__ */
211#else
212 BOOL one;
213#endif
214
215/* Solaris IPv6 stuff */
216
217 if (l == NULL)
218 {
219 return (-1);
220 }
221
222 l->fd = socket(PF_INET6, SOCK_RAW, IPPROTO_RAW);
223 if (l->fd == -1)
224 {
225 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
226 "%s(): SOCK_RAW allocation failed: %s\n", __func__,
227 strerror(errno));
228 goto bad;
229 }
230
231#if (__linux__)
232 if (setsockopt(l->fd, SOL_SOCKET, SO_BROADCAST, oneptr, sizeof(one)) == -1)
233 {
234 snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
235 "%s(): set SO_BROADCAST failed: %s\n", __func__,
236 strerror(errno));
237 goto bad;
238 }
239#endif /* __linux__ */
240 return (l->fd);
241
242bad:
243 return (-1);
244}
245#endif
246
247int
248libnet_close_raw6(libnet_t *l)
249{
250 if (l == NULL)
251 {
252 return (-1);
253 }
254 return (close(l->fd));
255}
256#endif
257/* EOF */