version 1.1, 2012/02/21 22:14:23
|
version 1.1.1.3, 2023/09/27 11:11:38
|
Line 24
|
Line 24
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
*/ |
*/ |
|
|
#if (HAVE_CONFIG_H) | #include "common.h" |
#include "../include/config.h" | |
#endif | |
#include <sys/time.h> |
#include <sys/time.h> |
|
|
#include <net/if.h> |
#include <net/if.h> |
#if (__GLIBC__) |
|
#include <netinet/if_ether.h> |
#include <netinet/if_ether.h> |
#include <net/if_arp.h> |
#include <net/if_arp.h> |
#else |
|
#include <linux/if_arp.h> |
|
#include <linux/if_ether.h> |
|
#endif |
|
|
|
#if (HAVE_PACKET_SOCKET) |
#if (HAVE_PACKET_SOCKET) |
#ifndef SOL_PACKET |
#ifndef SOL_PACKET |
#define SOL_PACKET 263 |
#define SOL_PACKET 263 |
#endif /* SOL_PACKET */ |
#endif /* SOL_PACKET */ |
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1 |
|
#include <netpacket/packet.h> |
#include <netpacket/packet.h> |
#include <net/ethernet.h> /* the L2 protocols */ |
#include <net/ethernet.h> /* the L2 protocols */ |
#else |
|
#include <asm/types.h> |
|
#include <linux/if_packet.h> |
|
#include <linux/if_ether.h> /* The L2 protocols */ |
|
#endif |
|
#endif /* HAVE_PACKET_SOCKET */ |
#endif /* HAVE_PACKET_SOCKET */ |
|
|
#include "../include/bpf.h" |
|
#include "../include/libnet.h" |
#include "../include/libnet.h" |
|
|
|
/* These should not vary across linux systems, and are only defined in |
|
* <pcap-bpf.h>, included from <pcap.h>, but since we have no other dependency |
|
* on libpcap right now, define locally. I'm not sure if this is a good idea, |
|
* but we'll try. |
|
*/ |
|
#define DLT_PRONET 4 /* Proteon ProNET Token Ring */ |
|
#define DLT_FDDI 10 /* FDDI */ |
|
#define DLT_RAW 12 /* raw IP */ |
|
|
#include "../include/gnuc.h" |
#include "../include/gnuc.h" |
#ifdef HAVE_OS_PROTO_H |
#ifdef HAVE_OS_PROTO_H |
#include "../include/os-proto.h" |
#include "../include/os-proto.h" |
Line 79 libnet_open_link(libnet_t *l)
|
Line 76 libnet_open_link(libnet_t *l)
|
#endif |
#endif |
if (l->fd == -1) |
if (l->fd == -1) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, | if (errno == EPERM) { |
"socket: %s", strerror(errno)); | snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
| "%s(): UID/EUID 0 or capability CAP_NET_RAW required", |
| __func__); |
| |
| } else { |
| snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
| "socket: %s", strerror(errno)); |
| } |
goto bad; |
goto bad; |
} |
} |
|
|
Line 110 libnet_open_link(libnet_t *l)
|
Line 114 libnet_open_link(libnet_t *l)
|
case ARPHRD_SLIP6: |
case ARPHRD_SLIP6: |
case ARPHRD_CSLIP6: |
case ARPHRD_CSLIP6: |
case ARPHRD_PPP: |
case ARPHRD_PPP: |
|
case ARPHRD_NONE: |
l->link_type = DLT_RAW; |
l->link_type = DLT_RAW; |
break; |
break; |
case ARPHRD_FDDI: |
case ARPHRD_FDDI: |
Line 126 libnet_open_link(libnet_t *l)
|
Line 131 libnet_open_link(libnet_t *l)
|
|
|
default: |
default: |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"unknown physical layer type 0x%x\n", | "unknown physical layer type 0x%x", |
ifr.ifr_hwaddr.sa_family); |
ifr.ifr_hwaddr.sa_family); |
goto bad; |
goto bad; |
} |
} |
Line 142 libnet_open_link(libnet_t *l)
|
Line 147 libnet_open_link(libnet_t *l)
|
if (setsockopt(l->fd, SOL_SOCKET, SO_BROADCAST, &n, sizeof(n)) == -1) |
if (setsockopt(l->fd, SOL_SOCKET, SO_BROADCAST, &n, 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; |
} |
} |
Line 175 libnet_close_link(libnet_t *l)
|
Line 180 libnet_close_link(libnet_t *l)
|
|
|
#if (HAVE_PACKET_SOCKET) |
#if (HAVE_PACKET_SOCKET) |
static int |
static int |
get_iface_index(int fd, const int8_t *device) | get_iface_index(int fd, const char *device) |
{ |
{ |
struct ifreq ifr; |
struct ifreq ifr; |
|
|
Line 194 get_iface_index(int fd, const int8_t *device)
|
Line 199 get_iface_index(int fd, const int8_t *device)
|
|
|
|
|
int |
int |
libnet_write_link(libnet_t *l, u_int8_t *packet, u_int32_t size) | libnet_write_link(libnet_t *l, const uint8_t *packet, uint32_t size) |
{ |
{ |
int c; |
int c; |
#if (HAVE_PACKET_SOCKET) |
#if (HAVE_PACKET_SOCKET) |
Line 227 libnet_write_link(libnet_t *l, u_int8_t *packet, u_int
|
Line 232 libnet_write_link(libnet_t *l, u_int8_t *packet, u_int
|
if (c != size) |
if (c != size) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"libnet_write_link(): only %d bytes written (%s)\n", c, | "libnet_write_link(): only %d bytes written (%s)", c, |
strerror(errno)); |
strerror(errno)); |
} |
} |
return (c); |
return (c); |
Line 239 libnet_get_hwaddr(libnet_t *l)
|
Line 244 libnet_get_hwaddr(libnet_t *l)
|
{ |
{ |
int fd; |
int fd; |
struct ifreq ifr; |
struct ifreq ifr; |
struct libnet_ether_addr *eap; |
|
/* |
|
* XXX - non-re-entrant! |
|
*/ |
|
static struct libnet_ether_addr ea; |
|
|
|
if (l == NULL) |
if (l == NULL) |
{ |
{ |
Line 255 libnet_get_hwaddr(libnet_t *l)
|
Line 255 libnet_get_hwaddr(libnet_t *l)
|
if (libnet_select_device(l) == -1) |
if (libnet_select_device(l) == -1) |
{ |
{ |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"libnet_get_hwaddr: can't figure out a device to use\n"); | "libnet_get_hwaddr: can't figure out a device to use"); |
return (NULL); |
return (NULL); |
} |
} |
} |
} |
Line 272 libnet_get_hwaddr(libnet_t *l)
|
Line 272 libnet_get_hwaddr(libnet_t *l)
|
} |
} |
|
|
memset(&ifr, 0, sizeof(ifr)); |
memset(&ifr, 0, sizeof(ifr)); |
eap = &ea; | strncpy(ifr.ifr_name, l->device, sizeof(ifr.ifr_name) - 1); |
strncpy(ifr.ifr_name, l->device, sizeof(ifr.ifr_name) - 1); | |
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; |
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0'; |
|
|
if (ioctl(fd, SIOCGIFHWADDR, (int8_t *)&ifr) < 0) | if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) |
{ |
{ |
close(fd); |
close(fd); |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE, |
"ioctl: %s", strerror(errno)); |
"ioctl: %s", strerror(errno)); |
goto bad; |
goto bad; |
} |
} |
memcpy(eap, &ifr.ifr_hwaddr.sa_data, ETHER_ADDR_LEN); |
|
close(fd); |
close(fd); |
return (eap); |
|
|
|
|
return memcpy(l->link_addr.ether_addr_octet, &ifr.ifr_hwaddr.sa_data, |
|
ETHER_ADDR_LEN); |
|
|
bad: |
bad: |
return (NULL); |
return (NULL); |
} |
} |
|
|
| /** |
/* EOF */ | * Local Variables: |
| * indent-tabs-mode: nil |
| * c-file-style: "stroustrup" |
| * End: |
| */ |