getifname.c
来自「网络流量生成工具,开源软件,也可以作为网络流量检测软件使用」· C语言 代码 · 共 238 行
C
238 行
/* getifname.c -- network interface handling * Copyright(C) 1999,2000,2001 Salvatore Sanfilippo <antirez@invece.org> * Copyright(C) 2001 by Nicolas Jombart <Nicolas.Jombart@hsc.fr> * This code is under the GPL license *//* BSD support thanks to Nicolas Jombart <Nicolas.Jombart@hsc.fr> */#include <stdio.h> /* perror */#include <string.h>#include <sys/ioctl.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h> /* struct sockaddr_in */#include <arpa/inet.h> /* inet_ntoa */#include <net/if.h>#include <unistd.h> /* close */#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)#include <stdlib.h>#include <ifaddrs.h>#include <net/route.h>#include <net/if_dl.h> #endif /* defined(__*BSD__) */#include "hping2.h"#include "globals.h"#if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__linux__) && !defined(__sun__)#error Sorry, interface code not implemented.#endif#ifdef __sun__#include <sys/sockio.h>#include <net/route.h>#include <net/if_dl.h>#endif#if (defined __linux__) || (defined __sun__)static int get_output_if(struct sockaddr_in *dest, struct sockaddr_in *ifip);#endif#if (defined OSTYPE_LINUX) || (defined __sun__)int get_if_name(void){ int fd; struct ifconf ifc; struct ifreq ibuf[16], ifr, *ifrp, *ifend; struct sockaddr_in sa; struct sockaddr_in output_if_addr; int known_output_if = 0; /* Try to get the output interface address according to * the OS routing table */ if (ifname[0] == '\0') { if (get_output_if(&remote, &output_if_addr) == 0) { known_output_if = 1; if (opt_debug) printf("Output interface address: %s\n", inet_ntoa(sa.sin_addr)); } else { fprintf(stderr, "Warning: Unable to guess the output " "interface\n"); } } if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("[get_if_name] socket(AF_INET, SOCK_DGRAM, 0)"); return -1; } memset(ibuf, 0, sizeof(struct ifreq)*16); ifc.ifc_len = sizeof ibuf; ifc.ifc_buf = (caddr_t) ibuf; /* gets interfaces list */ if ( ioctl(fd, SIOCGIFCONF, (char*)&ifc) == -1 || ifc.ifc_len < sizeof(struct ifreq) ) { perror("[get_if_name] ioctl(SIOCGIFCONF)"); close(fd); return -1; } /* ifrp points to buffer and ifend points to buffer's end */ ifrp = ibuf; ifend = (struct ifreq*) ((char*)ibuf + ifc.ifc_len); for (; ifrp < ifend; ifrp++) { strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name)); if ( ioctl(fd, SIOCGIFFLAGS, (char*)&ifr) == -1) { if (opt_debug) perror("[get_if_name] ioctl(SIOCGIFFLAGS)"); continue; } if (opt_debug) printf("if %s: ", ifr.ifr_name); /* Down interface? */ if ( !(ifr.ifr_flags & IFF_UP) ) { if (opt_debug) printf("DOWN\n"); continue; } if (known_output_if) { /* Get the interface address */ if (ioctl(fd, SIOCGIFADDR, (char*)&ifr) == -1) { perror("[get_if_name] ioctl(SIOCGIFADDR)"); continue; } /* Copy it */ memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in)); /* Check if it is what we are locking for */ if (sa.sin_addr.s_addr != output_if_addr.sin_addr.s_addr) { if (opt_debug) printf("The address doesn't match\n"); continue; } } else if (ifname[0] != '\0' && !strstr(ifr.ifr_name, ifname)) { if (opt_debug) printf("Don't Match (but seems to be UP)\n"); continue; } if (opt_debug) printf("OK\n"); /* interface found, save if name */ strncpy(ifname, ifr.ifr_name, 1024); /* get if address */ if ( ioctl(fd, SIOCGIFADDR, (char*)&ifr) == -1) { perror("[get_if_name] ioctl(SIOCGIFADDR)"); exit(1); } /* save if address */ memcpy(&sa, &ifr.ifr_addr, sizeof(struct sockaddr_in)); strncpy(ifstraddr, inet_ntoa(sa.sin_addr), 1024); /* get if mtu */ if ( ioctl(fd, SIOCGIFMTU, (char*)&ifr) == -1) { perror("Warning: [get_if_name] ioctl(SIOCGIFMTU)"); fprintf(stderr, "Using a fixed MTU of 1500\n"); h_if_mtu = 1500; } else {#ifdef __sun__ /* somehow solaris is braidamaged in wrt ifr_mtu */ h_if_mtu = ifr.ifr_metric;#else h_if_mtu = ifr.ifr_mtu;#endif } close(fd); return 0; } /* interface not found, use 'lo' */ strncpy(ifname, "lo", 1024); strncpy(ifstraddr, "127.0.0.1", 1024); h_if_mtu = 1500; close(fd); return 0;}#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)/* *BSD code written by Nicolas Jombart <ecureuil@hsc.fr> Version: 4*/#define BUFLEN (sizeof(struct rt_msghdr) + 512)/* from Stevens' Unix Network Programming */#define ROUNDUP(a, size) (((a) & ((size)-1)) ? (1 + ((a) | ((size)-1))) : (a))#define NEXT_SA(ap) ap = (struct sockaddr *) \ ((caddr_t) ap + (ap->sa_len ? ROUNDUP(ap->sa_len, sizeof (u_long)) : \ sizeof(u_long)))static voidget_rtaddrs(int addrs, struct sockaddr *sa, struct sockaddr **rti_info){ int i; for (i = 0; i < RTAX_MAX; i++) { if (addrs & (1 << i)) { rti_info[i] = sa; NEXT_SA(sa); } else rti_info[i] = NULL; }}/* get GW. return 1 if IP address of the gateway, 2 if interface address */static int get_gw(char *target, char *address) {int fd,pid,n;char *buf;struct rt_msghdr *rtm;struct sockaddr_in *sin;struct sockaddr *sa,*rti_info[RTAX_MAX]; /* bad hack if loopback interface */ if(!strncmp(target,"127.0.0.1",9)) { strncpy(address,"lo0\0",4); return 2; } /* try first to get interface from the kernel internal routing table */ if( (fd = socket(PF_ROUTE,SOCK_RAW,0)) < 0) { perror("Can't open routing socket.\n"); exit(1); } buf = (char *)calloc(1, BUFLEN); rtm = (struct rt_msghdr *) buf; /* data structure to talk with the kernel */ rtm->rtm_msglen = sizeof(struct rt_msghdr) + sizeof(struct sockaddr_in); rtm->rtm_version = RTM_VERSION; rtm->rtm_type = RTM_GET; rtm->rtm_addrs = RTA_DST; /* socket address structure contains destination address */ rtm->rtm_pid = pid = getpid(); rtm->rtm_seq = 9999; sin = (struct sockaddr_in *) (rtm + 1); sin->sin_len = sizeof(struct sockaddr_in); sin->sin_family = AF_INET; inet_pton(AF_INET, target ,&sin->sin_addr); if (opt_debug) printf("adresse
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?