1
0
Fork 0
mirror of https://repo.or.cz/socat.git synced 2025-07-15 15:43:24 +00:00

merged feature raw network interface

This commit is contained in:
Gerhard Rieger 2008-09-23 00:09:19 +02:00
commit 784e378ede
18 changed files with 293 additions and 25 deletions

View file

@ -116,7 +116,7 @@ socklen_t socket_init(int af, union sockaddr_union *sa) {
#if WITH_IP6
case AF_INET6: socket_in6_init(&sa->ip6); return sizeof(sa->ip6);
#endif
default: Error1("socket_init(): unknown address family %d", af);
default: Info1("socket_init(): unknown address family %d", af);
memset(sa, 0, sizeof(union sockaddr_union));
sa->soa.sa_family = af;
return 0;
@ -452,8 +452,12 @@ int parseport(const char *portname, int ipproto) {
#if WITH_IP4 || WITH_IP6
/* check the systems interfaces for ifname and return its index
or -1 if no interface with this name was found */
int ifindexbyname(const char *ifname) {
or -1 if no interface with this name was found
The system calls require an arbitrary socket; the calling program may
provide one in anysock to avoid creation of a dummy socket. anysock must be
<0 if it does not specify a socket fd.
*/
int ifindexbyname(const char *ifname, int anysock) {
/* Linux: man 7 netdevice */
/* FreeBSD: man 4 networking */
/* Solaris: man 7 if_tcp */
@ -461,29 +465,35 @@ int ifindexbyname(const char *ifname) {
#if defined(HAVE_STRUCT_IFREQ) && defined(SIOCGIFCONF) && defined(SIOCGIFINDEX)
/* currently we support Linux, FreeBSD; not Solaris */
#define IFBUFSIZ 1024
#define IFBUFSIZ 32*sizeof(struct ifreq) /*1024*/
int s;
struct ifreq ifr;
if (ifname[0] == '\0') {
return -1;
}
if ((s = Socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
if (anysock >= 0) {
s = anysock;
} else if ((s = Socket(PF_INET, SOCK_DGRAM, IPPROTO_IP)) < 0) {
Error1("socket(PF_INET, SOCK_DGRAM, IPPROTO_IP): %s", strerror(errno));
return -1;
}
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
if (Ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
Close(s);
Info3("ioctl(%d, SIOCGIFINDEX, {%s}): %s",
Info3("ioctl(%d, SIOCGIFINDEX, {\"%s\"}): %s",
s, ifr.ifr_name, strerror(errno));
Close(s);
return -1;
}
Close(s);
#if HAVE_STRUCT_IFREQ_IFR_INDEX
Info3("ioctl(%d, SIOCGIFINDEX, {\"%s\"}) -> { %d }",
s, ifname, ifr.ifr_index);
return ifr.ifr_index;
#elif HAVE_STRUCT_IFREQ_IFR_IFINDEX
Info3("ioctl(%d, SIOCGIFINDEX, {\"%s\"}) -> { %d }",
s, ifname, ifr.ifr_ifindex);
return ifr.ifr_ifindex;
#endif /* HAVE_STRUCT_IFREQ_IFR_IFINDEX */
@ -493,10 +503,11 @@ int ifindexbyname(const char *ifname) {
}
/* like ifindexbyname(), but allows an index number as input.
/* like ifindexbyname(), but also allows the index number as input - in this
case it does not lookup the index.
writes the resulting index to *ifindex and returns 0,
or returns -1 on error */
int ifindex(const char *ifname, unsigned int *ifindex) {
int ifindex(const char *ifname, unsigned int *ifindex, int anysock) {
char *endptr;
long int val;
@ -509,7 +520,7 @@ int ifindex(const char *ifname, unsigned int *ifindex) {
return 0;
}
if ((val = ifindexbyname(ifname)) < 0) {
if ((val = ifindexbyname(ifname, anysock)) < 0) {
return -1;
}
*ifindex = val;