|
楼主 |
发表于 2004-5-16 22:30:44
|
显示全部楼层
这是全部的程序- 1 /* include get_ifi_info1 */
- 2
- 3 #include "unpifi_a.h"
- 4 #include <net/if_a_a.h>
- 5 #define IFNAMSIZ 16
- 6 #define SIOCGIFFLAGS _IOWR('i', 17 ,struct ifreq) /* get ifnet flags */
- 7 #define SIOCGIFCONF _IOWR('i', 36, struct ifconf) /* get ifnet list */
- 8
- 9 struct ifi_info *
- 10 get_ifi_info(int family, int doaliases)
- 11 {
- 12 struct ifi_info *ifi, *ifihead, **ifipnext;
- 13 int sockfd, len, lastlen, flags, myflags;
- 14 char *ptr, *buf, lastname[IFNAMSIZ], *cptr;
- 15 struct ifconf ifc;
- 16 struct ifreq *ifr, ifrcopy;
- 17 struct sockaddr_in *sinptr;
- 18
- 19 if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0) {
- 20 perror("socket error");
- 21 exit(1);
- 22 }
- 23 lastlen = 0;
- 24 len = 100 * sizeof(struct ifreq); /* initial buffer size guess */
- 25 for ( ; ; ) {
- 26 buf = Malloc(len);
- 27 ifc.ifc_len = len;
- 28 ifc.ifc_buf = buf;
- 29 if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
- 30 if (errno != EINVAL || lastlen != 0) {
- 31 perror("ioctl error");
- 32 exit(1);
- 33 }
- 34 } else {
- 35 if (ifc.ifc_len == lastlen)
- 36 break; /* success, len has not changed */
- 37 lastlen = ifc.ifc_len;
- 38 }
- 39 len += 10 * sizeof(struct ifreq); /* increment */
- 40 free(buf);
- 41 }
- 42 ifihead = NULL;
- 43 ifipnext = &ifihead;
- 44 lastname[0] = 0;
- 45 /* end get_ifi_info1 */
- 46
- 47 /* include get_ifi_info2 */
- 48 for (ptr = buf; ptr < buf + ifc.ifc_len; ) {
- 49 ifr = (struct ifreq *) ptr;
- 50
- 51 #ifdef HAVE_SOCKADDR_SA_LEN
- 52 len = max(sizeof(struct sockaddr), ifr->ifr_addr.sa_len);
- 53 #else
- 54 switch (ifr->ifr_addr.sa_family) {
- 55 #ifdef IPV6
- 56 case AF_INET6:
- 57 len = sizeof(struct sockaddr_in6);
- 58 break;
- 59 #endif
- 60 case AF_INET:
- 61 default:
- 62 len = sizeof(struct sockaddr);
- 63 break;
- 64 }
- 65 #endif /* HAVE_SOCKADDR_SA_LEN */
- 66 ptr += sizeof(ifr->ifr_name)+len; /* for next one in buffer */
- 67
- 68 if (ifr->ifr_addr.sa_family !=family)
- 69 continue; /* ignore if not desired address family */
- 70
- 71 myflags = 0;
- 72 if ( (cptr = strchr(ifr->ifr_name, ':')) != NULL)
- 73 *cptr = 0; /* replace colon will null */
- 74 if (strncmp(lastname, ifr->ifr_name, IFNAMSIZ) == 0) {
- 75 if (doaliases == 0)
- 76 continue; /* already processed this interface */
- 77 myflags = IFI_ALIAS;
- 78 }
- 79 memcpy(lastname, ifr->ifr_name, IFNAMSIZ);
- 80
- 81 ifrcopy = *ifr;
- 82 Ioctl(sockfd, SIOCGIFFLAGS, &ifrcopy);
- 83 flags = ifrcopy.ifr_flags;
- 84 if ((flags & IFF_UP) == 0)
- 85 continue; /* ignore if interface not up */
- 86
- 87 ifi = Calloc(1, sizeof(struct ifi_info));
- 88 *ifipnext = ifi; /* prev points to this new one */
- 89 ifipnext = &ifi->ifi_next; /* pointer to next one goes here */
- 90
- 91 ifi->ifi_flags = flags; /* IFF_xxx values */
- 92 ifi->ifi_myflags = myflags; /* IFI_xxx values */
- 93 memcpy(ifi->ifi_name, ifr->ifr_name, IFI_NAME);
- 94 ifi->ifi_name[IFI_NAME-1] = '\0';
- 95 /* end get_ifi_info2 */
- 96 /* include get_ifi_info3 */
- 97 switch (ifr->ifr_addr.sa_family) {
- 98 case AF_INET:
- 99 sinptr = (struct sockaddr_in *) &ifr->ifr_addr;
- 100 if (ifi->ifi_addr == NULL) {
- 101 ifi->ifi_addr = Calloc(1, sizeof(struct sockaddr_in));
- 102 memcpy(ifi->ifi_addr, sinptr, sizeof(struct sockaddr_in));
- 103
- 104 #ifdef SIOCGIFBRDADDR
- 105 if (flags & IFF_BROADCAST) {
- 106 Ioctl(sockfd, SIOCGIFBRDADDR, &ifrcopy);
- 107 sinptr = (struct sockaddr_in *) &ifrcopy.ifr_broadaddr;
- 108 ifi->ifi_brdaddr = Calloc(1, sizeof(struct sockaddr_in));
- 109 memcpy(ifi->ifi_brdaddr, sinptr, sizeof(struct sockaddr_in));
- 110 }
- 111 #endif
- 112
- 113 #ifdef SIOCGIFDSTADDR
- 114 if (flags & IFF_POINTOPOINT) {
- 115 Ioctl(sockfd, SIOCGIFDSTADDR, &ifrcopy);
- 116 sinptr = (struct sockaddr_in *) &ifrcopy.ifr_dstaddr;
- 117 ifi->ifi_dstaddr = Calloc(1, sizeof(struct sockaddr_in));
- 118 memcpy(ifi->ifi_dstaddr, sinptr, sizeof(struct sockaddr_in));
- 119 }
- 120 #endif
- 121 }
- 122 break;
- 123
- 124 default:
- 125 break;
- 126 }
- 127 }
- 128 free(buf);
- 129 return(ifihead); /* pointer to first structure in linked list */
- 130 }
- 131 /* end get_ifi_info3 */
- 132
- 133 /* include free_ifi_info */
- 134 void
- 135 free_ifi_info(struct ifi_info *ifihead)
- 136 {
- 137 struct ifi_info *ifi, *ifinext;
- 138
- 139 for (ifi = ifihead; ifi != NULL; ifi = ifinext) {
- 140 if (ifi->ifi_addr != NULL)
- 141 free(ifi->ifi_addr);
- 142 if (ifi->ifi_brdaddr != NULL)
- 143 free(ifi->ifi_brdaddr);
- 144 if (ifi->ifi_dstaddr != NULL)
- 145 free(ifi->ifi_dstaddr);
- 146 ifinext = ifi->ifi_next; /* can't fetch ifi_next after free() */
- 147 free(ifi); /* the ifi_info{} itself */
- 148 }
- 149 }
- 150 /* end free_ifi_info */
- 151
- 152 struct ifi_info *
- 153 Get_ifi_info(int family, int doaliases)
- 154 {
- 155 struct ifi_info *ifi;
- 156
- 157 if ( (ifi = get_ifi_info(family, doaliases)) == NULL) {
- 158 perror("get_ifi_info error");
- 159 exit(1);
- 160 }
- 161 return(ifi);
- 162 }
复制代码 |
|