185 lines
4.6 KiB
C
185 lines
4.6 KiB
C
#ifndef WIN32
|
|
|
|
# include <sys/types.h>
|
|
# include <sys/socket.h>
|
|
# include <net/if.h>
|
|
# include <netdb.h>
|
|
|
|
# if HAVE_SOCKET_IOCTLS
|
|
# include <sys/ioctl.h>
|
|
# include <netinet/in.h>
|
|
# include <arpa/inet.h>
|
|
#if defined(__sun)
|
|
#include <unistd.h>
|
|
#include <stropts.h>
|
|
#include <sys/sockio.h>
|
|
#endif
|
|
# endif /* HAVE_SOCKET_IOCTLS */
|
|
|
|
/* For logical interfaces support we convert all names to same name prefixed with l */
|
|
#if HAVE_SIOCGLIFNUM
|
|
#define CNAME(x) l##x
|
|
#else
|
|
#define CNAME(x) x
|
|
#endif
|
|
|
|
#if HAVE_NET_IF_DL_H
|
|
# include <net/if_dl.h>
|
|
#endif
|
|
|
|
/* For Linux, include all the sockaddr
|
|
definitions we can lay our hands on. */
|
|
#if !HAVE_SOCKADDR_SA_LEN
|
|
# if HAVE_NETASH_ASH_H
|
|
# include <netash/ash.h>
|
|
# endif
|
|
# if HAVE_NETATALK_AT_H
|
|
# include <netatalk/at.h>
|
|
# endif
|
|
# if HAVE_NETAX25_AX25_H
|
|
# include <netax25/ax25.h>
|
|
# endif
|
|
# if HAVE_NETECONET_EC_H
|
|
# include <neteconet/ec.h>
|
|
# endif
|
|
# if HAVE_NETIPX_IPX_H
|
|
# include <netipx/ipx.h>
|
|
# endif
|
|
# if HAVE_NETPACKET_PACKET_H
|
|
# include <netpacket/packet.h>
|
|
# endif
|
|
# if HAVE_NETROSE_ROSE_H
|
|
# include <netrose/rose.h>
|
|
# endif
|
|
# if HAVE_LINUX_IRDA_H
|
|
# include <linux/irda.h>
|
|
# endif
|
|
# if HAVE_LINUX_ATM_H
|
|
# include <linux/atm.h>
|
|
# endif
|
|
# if HAVE_LINUX_LLC_H
|
|
# include <linux/llc.h>
|
|
# endif
|
|
# if HAVE_LINUX_TIPC_H
|
|
# include <linux/tipc.h>
|
|
# endif
|
|
# if HAVE_LINUX_DN_H
|
|
# include <linux/dn.h>
|
|
# endif
|
|
|
|
/* Map address families to sizes of sockaddr structs */
|
|
static int af_to_len(int af)
|
|
{
|
|
switch (af)
|
|
{
|
|
case AF_INET: return sizeof (struct sockaddr_in);
|
|
#if defined(AF_INET6) && HAVE_SOCKADDR_IN6
|
|
case AF_INET6: return sizeof (struct sockaddr_in6);
|
|
#endif
|
|
#if defined(AF_AX25) && HAVE_SOCKADDR_AX25
|
|
# if defined(AF_NETROM)
|
|
case AF_NETROM: /* I'm assuming this is carried over x25 */
|
|
# endif
|
|
case AF_AX25: return sizeof (struct sockaddr_ax25);
|
|
#endif
|
|
#if defined(AF_IPX) && HAVE_SOCKADDR_IPX
|
|
case AF_IPX: return sizeof (struct sockaddr_ipx);
|
|
#endif
|
|
#if defined(AF_APPLETALK) && HAVE_SOCKADDR_AT
|
|
case AF_APPLETALK: return sizeof (struct sockaddr_at);
|
|
#endif
|
|
#if defined(AF_ATMPVC) && HAVE_SOCKADDR_ATMPVC
|
|
case AF_ATMPVC: return sizeof (struct sockaddr_atmpvc);
|
|
#endif
|
|
#if defined(AF_ATMSVC) && HAVE_SOCKADDR_ATMSVC
|
|
case AF_ATMSVC: return sizeof (struct sockaddr_atmsvc);
|
|
#endif
|
|
#if defined(AF_X25) && HAVE_SOCKADDR_X25
|
|
case AF_X25: return sizeof (struct sockaddr_x25);
|
|
#endif
|
|
#if defined(AF_ROSE) && HAVE_SOCKADDR_ROSE
|
|
case AF_ROSE: return sizeof (struct sockaddr_rose);
|
|
#endif
|
|
#if defined(AF_DECnet) && HAVE_SOCKADDR_DN
|
|
case AF_DECnet: return sizeof (struct sockaddr_dn);
|
|
#endif
|
|
#if defined(AF_PACKET) && HAVE_SOCKADDR_LL
|
|
case AF_PACKET: return sizeof (struct sockaddr_ll);
|
|
#endif
|
|
#if defined(AF_ASH) && HAVE_SOCKADDR_ASH
|
|
case AF_ASH: return sizeof (struct sockaddr_ash);
|
|
#endif
|
|
#if defined(AF_ECONET) && HAVE_SOCKADDR_EC
|
|
case AF_ECONET: return sizeof (struct sockaddr_ec);
|
|
#endif
|
|
#if defined(AF_IRDA) && HAVE_SOCKADDR_IRDA
|
|
case AF_IRDA: return sizeof (struct sockaddr_irda);
|
|
#endif
|
|
}
|
|
return sizeof (struct sockaddr);
|
|
}
|
|
|
|
#define SA_LEN(sa) af_to_len(sa->sa_family)
|
|
#if HAVE_SIOCGLIFNUM
|
|
#define SS_LEN(sa) af_to_len(sa->ss_family)
|
|
#else
|
|
#define SS_LEN(sa) SA_LEN(sa)
|
|
#endif
|
|
#else
|
|
//remove a warning on openbsd
|
|
#ifndef SA_LEN
|
|
#define SA_LEN(sa) sa->sa_len
|
|
#endif
|
|
#endif /* !HAVE_SOCKADDR_SA_LEN */
|
|
|
|
# if HAVE_GETIFADDRS
|
|
# include <ifaddrs.h>
|
|
# endif /* HAVE_GETIFADDRS */
|
|
|
|
# if !HAVE_GETIFADDRS && (!HAVE_SOCKET_IOCTLS || !HAVE_SIOCGIFCONF)
|
|
/* If the platform doesn't define, what we need, barf. If you're seeing this,
|
|
it means you need to write suitable code to retrieve interface information
|
|
on your system. */
|
|
# error You need to add code for your platform.
|
|
# endif
|
|
|
|
#else /* defined(WIN32) */
|
|
|
|
#include <windows.h>
|
|
#include <winsock2.h>
|
|
#include <iphlpapi.h>
|
|
|
|
#endif /* defined(WIN32) */
|
|
|
|
#ifndef TRUE
|
|
#define TRUE 1
|
|
#endif
|
|
|
|
#ifndef FALSE
|
|
#define FALSE 0
|
|
#endif
|
|
|
|
/* On systems without AF_LINK (Windows, for instance), define it anyway, but
|
|
give it a crazy value. On Linux, which has AF_PACKET but not AF_LINK,
|
|
define AF_LINK as the latter instead. */
|
|
#ifndef AF_LINK
|
|
# ifdef AF_PACKET
|
|
# define AF_LINK AF_PACKET
|
|
# else
|
|
# define AF_LINK -1000
|
|
# endif
|
|
# define HAVE_AF_LINK 0
|
|
#else
|
|
# define HAVE_AF_LINK 1
|
|
#endif
|
|
|
|
|
|
//Prototypes
|
|
//Get a list of the adresses for a network interface
|
|
VALUE rbnetifaces_s_addresses (VALUE class, VALUE dev);
|
|
//Get a list of the network interfaces
|
|
VALUE rbnetifaces_s_interfaces (VALUE self);
|
|
//This function is usefull only under windows to retrieve some additionnal interfaces informations
|
|
VALUE rbnetifaces_s_interface_info (VALUE self, VALUE dev);
|
|
|