version 1.1, 2012/02/21 22:14:23
|
version 1.1.1.3, 2023/09/27 11:11:38
|
Line 30
|
Line 30
|
* |
* |
*/ |
*/ |
|
|
#if (HAVE_CONFIG_H) | #include "common.h" |
#include "../include/config.h" | |
| #ifndef HAVE_SOCKLEN_T |
| typedef int socklen_t; |
#endif |
#endif |
#if (!(_WIN32) || (__CYGWIN__)) |
|
#include "../include/libnet.h" |
|
#else |
|
#include "../include/win32/libnet.h" |
|
#endif |
|
|
|
|
/* TODO this doesn't make any sense, the code in the #else branch is littered |
|
with conditionals on __WIN32__ that are never reachable, what happened? |
|
*/ |
#if defined (__WIN32__) |
#if defined (__WIN32__) |
int |
int |
libnet_open_raw4(libnet_t *l) |
libnet_open_raw4(libnet_t *l) |
Line 64 libnet_close_raw6(libnet_t *l)
|
Line 64 libnet_close_raw6(libnet_t *l)
|
return (libnet_close_link_interface(l)); |
return (libnet_close_link_interface(l)); |
} |
} |
#else |
#else |
int |
|
libnet_open_raw4(libnet_t *l) |
|
{ |
|
int len; |
|
|
|
|
static int libnet_finish_setup_socket(libnet_t *l) |
|
{ |
#if !(__WIN32__) |
#if !(__WIN32__) |
int n = 1; |
int n = 1; |
#if (__svr4__) |
#if (__svr4__) |
Line 76 libnet_open_raw4(libnet_t *l)
|
Line 74 libnet_open_raw4(libnet_t *l)
|
#else |
#else |
int *nptr = &n; |
int *nptr = &n; |
#endif /* __svr4__ */ |
#endif /* __svr4__ */ |
#else | #else |
BOOL n; |
BOOL n; |
#endif |
#endif |
|
unsigned len; |
|
|
if (l == NULL) |
|
{ |
|
return (-1); |
|
} |
|
|
|
l->fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); |
|
if (l->fd == -1) |
|
{ |
|
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
|
"%s(): SOCK_RAW allocation failed: %s\n", |
|
__func__, strerror(errno)); |
|
goto bad; |
|
} |
|
|
|
#ifdef IP_HDRINCL |
|
/* |
|
* man raw |
|
* |
|
* The IPv4 layer generates an IP header when sending a packet unless |
|
* the IP_HDRINCL socket option is enabled on the socket. When it |
|
* is enabled, the packet must contain an IP header. For |
|
* receiving the IP header is always included in the packet. |
|
*/ |
|
#if !(__WIN32__) |
|
if (setsockopt(l->fd, IPPROTO_IP, IP_HDRINCL, nptr, sizeof(n)) == -1) |
|
#else |
|
n = TRUE; |
|
if (setsockopt(l->fd, IPPROTO_IP, IP_HDRINCL, (int8_t *)&n, |
|
sizeof(n)) == -1) |
|
#endif |
|
|
|
{ |
|
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
|
"%s(): set IP_HDRINCL failed: %s\n", |
|
__func__, strerror(errno)); |
|
goto bad; |
|
} |
|
#endif /* IP_HDRINCL */ |
|
|
|
#ifdef SO_SNDBUF |
#ifdef SO_SNDBUF |
|
|
/* |
/* |
Line 131 libnet_open_raw4(libnet_t *l)
|
Line 91 libnet_open_raw4(libnet_t *l)
|
len = sizeof(n); |
len = sizeof(n); |
if (getsockopt(l->fd, SOL_SOCKET, SO_SNDBUF, &n, &len) < 0) |
if (getsockopt(l->fd, SOL_SOCKET, SO_SNDBUF, &n, &len) < 0) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): get SO_SNDBUF failed: %s\n", | "%s(): get SO_SNDBUF failed: %s", |
__func__, strerror(errno)); |
__func__, strerror(errno)); |
goto bad; |
goto bad; |
} |
} |
|
|
for (n += 128; n < 1048576; n += 128) |
for (n += 128; n < 1048576; n += 128) |
{ |
{ |
if (setsockopt(l->fd, SOL_SOCKET, SO_SNDBUF, &n, len) < 0) | if (setsockopt(l->fd, SOL_SOCKET, SO_SNDBUF, &n, len) < 0) |
{ |
{ |
if (errno == ENOBUFS) |
if (errno == ENOBUFS) |
{ |
{ |
break; |
break; |
} |
} |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): set SO_SNDBUF failed: %s\n", | "%s(): set SO_SNDBUF failed: %s", |
__func__, strerror(errno)); |
__func__, strerror(errno)); |
goto bad; |
goto bad; |
} |
} |
Line 165 libnet_open_raw4(libnet_t *l)
|
Line 125 libnet_open_raw4(libnet_t *l)
|
if (setsockopt(l->fd, SOL_SOCKET, SO_BROADCAST, nptr, sizeof(n)) == -1) |
if (setsockopt(l->fd, SOL_SOCKET, SO_BROADCAST, nptr, sizeof(n)) == -1) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): set SO_BROADCAST failed: %s\n", | "%s(): set SO_BROADCAST failed: %s", |
__func__, strerror(errno)); |
__func__, strerror(errno)); |
goto bad; |
goto bad; |
} |
} |
#endif /* SO_BROADCAST */ |
#endif /* SO_BROADCAST */ |
|
|
|
#if (__linux__) |
|
if(l->device != NULL) |
|
if(setsockopt(l->fd, SOL_SOCKET, SO_BINDTODEVICE, l->device, strlen(l->device)) == -1) { |
|
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
|
"%s(): set SO_BINDTODEVICE failed: %s", __func__, strerror(errno)); |
|
goto bad; |
|
} |
|
#endif /* __linux__ */ |
|
|
|
return 0; |
|
|
|
bad: |
|
return (-1); |
|
} |
|
|
|
|
|
|
|
|
|
int |
|
libnet_open_raw4(libnet_t *l) |
|
{ |
|
#if !(__WIN32__) |
|
int n = 1; |
|
#if (__svr4__) |
|
void *nptr = &n; |
|
#else |
|
int *nptr = &n; |
|
#endif /* __svr4__ */ |
|
#else |
|
BOOL n; |
|
#endif |
|
|
|
if (l == NULL) |
|
{ |
|
return (-1); |
|
} |
|
|
|
l->fd = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); |
|
if (l->fd == -1) |
|
{ |
|
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
|
"%s(): SOCK_RAW allocation failed: %s", |
|
__func__, strerror(errno)); |
|
goto bad; |
|
} |
|
|
|
#ifdef IP_HDRINCL |
|
/* |
|
* man raw |
|
* |
|
* The IPv4 layer generates an IP header when sending a packet unless |
|
* the IP_HDRINCL socket option is enabled on the socket. When it |
|
* is enabled, the packet must contain an IP header. For |
|
* receiving the IP header is always included in the packet. |
|
*/ |
|
#if !(__WIN32__) |
|
if (setsockopt(l->fd, IPPROTO_IP, IP_HDRINCL, nptr, sizeof(n)) == -1) |
|
#else |
|
n = TRUE; |
|
if (setsockopt(l->fd, IPPROTO_IP, IP_HDRINCL, &n, sizeof(n)) == -1) |
|
#endif |
|
|
|
{ |
|
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
|
"%s(): set IP_HDRINCL failed: %s", |
|
__func__, strerror(errno)); |
|
goto bad; |
|
} |
|
#endif /* IP_HDRINCL */ |
|
|
|
|
|
if (libnet_finish_setup_socket(l) == -1) |
|
goto bad; |
|
|
return (l->fd); |
return (l->fd); |
|
|
bad: |
bad: |
return (-1); | return (-1); |
} |
} |
|
|
|
|
Line 198 int libnet_open_raw6(libnet_t *l)
|
Line 233 int libnet_open_raw6(libnet_t *l)
|
int |
int |
libnet_open_raw6(libnet_t *l) |
libnet_open_raw6(libnet_t *l) |
{ |
{ |
#if !(__WIN32__) |
|
#if (__svr4__) |
|
int one = 1; |
|
void *oneptr = &one; |
|
#else |
|
#if (__linux__) |
|
int one = 1; |
|
int *oneptr = &one; |
|
#endif |
|
#endif /* __svr4__ */ |
|
#else |
|
BOOL one; |
|
#endif |
|
|
|
/* Solaris IPv6 stuff */ |
|
|
|
if (l == NULL) |
if (l == NULL) |
{ |
{ |
return (-1); |
return (-1); |
} |
} |
|
|
l->fd = socket(PF_INET6, SOCK_RAW, IPPROTO_RAW); | l->fd = socket(AF_INET6, SOCK_RAW, IPPROTO_RAW); |
if (l->fd == -1) |
if (l->fd == -1) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"%s(): SOCK_RAW allocation failed: %s\n", __func__, | "%s(): SOCK_RAW allocation failed: %s", __func__, |
strerror(errno)); |
strerror(errno)); |
goto bad; |
goto bad; |
} |
} |
|
|
#if (__linux__) | |
if (setsockopt(l->fd, SOL_SOCKET, SO_BROADCAST, oneptr, sizeof(one)) == -1) | if (libnet_finish_setup_socket(l) == -1) |
{ | |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, | |
"%s(): set SO_BROADCAST failed: %s\n", __func__, | |
strerror(errno)); | |
goto bad; |
goto bad; |
} | |
#endif /* __linux__ */ | |
return (l->fd); |
return (l->fd); |
|
|
bad: |
bad: |
Line 254 libnet_close_raw6(libnet_t *l)
|
Line 268 libnet_close_raw6(libnet_t *l)
|
return (close(l->fd)); |
return (close(l->fd)); |
} |
} |
#endif |
#endif |
/* EOF */ | |
| /** |
| * Local Variables: |
| * indent-tabs-mode: nil |
| * c-file-style: "stroustrup" |
| * End: |
| */ |