nb_net.c (125130) | nb_net.c (126269) |
---|---|
1/* 2 * Copyright (c) 2000, Boris Popov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 16 unchanged lines hidden (view full) --- 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $Id: nb_net.c,v 1.4 2001/02/16 02:46:12 bp Exp $ | 1/* 2 * Copyright (c) 2000, Boris Popov 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 16 unchanged lines hidden (view full) --- 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $Id: nb_net.c,v 1.4 2001/02/16 02:46:12 bp Exp $ |
33 * $FreeBSD: head/contrib/smbfs/lib/smb/nb_net.c 125130 2004-01-28 05:55:13Z tjr $ | 33 * $FreeBSD: head/contrib/smbfs/lib/smb/nb_net.c 126269 2004-02-26 08:49:19Z tjr $ |
34 */ 35#include <sys/param.h> 36#include <sys/socket.h> | 34 */ 35#include <sys/param.h> 36#include <sys/socket.h> |
37#include <sys/ioctl.h> | |
38 39#include <net/if.h> 40 41#include <ctype.h> 42#include <netdb.h> 43#include <err.h> 44#include <errno.h> 45#include <stdlib.h> 46#include <string.h> 47#include <stdio.h> 48#include <unistd.h> | 37 38#include <net/if.h> 39 40#include <ctype.h> 41#include <netdb.h> 42#include <err.h> 43#include <errno.h> 44#include <stdlib.h> 45#include <string.h> 46#include <stdio.h> 47#include <unistd.h> |
48#include <ifaddrs.h> |
|
49 50#include <netsmb/netbios.h> 51#include <netsmb/smb_lib.h> 52#include <netsmb/nb_lib.h> 53 54int 55nb_getlocalname(char *name) 56{ --- 40 unchanged lines hidden (view full) --- 97 sinp->sin_port = htons(SMB_TCP_PORT); 98 *dest = (struct sockaddr*)sinp; 99 return 0; 100} 101 102int 103nb_enum_if(struct nb_ifdesc **iflist, int maxif) 104{ | 49 50#include <netsmb/netbios.h> 51#include <netsmb/smb_lib.h> 52#include <netsmb/nb_lib.h> 53 54int 55nb_getlocalname(char *name) 56{ --- 40 unchanged lines hidden (view full) --- 97 sinp->sin_port = htons(SMB_TCP_PORT); 98 *dest = (struct sockaddr*)sinp; 99 return 0; 100} 101 102int 103nb_enum_if(struct nb_ifdesc **iflist, int maxif) 104{ |
105 struct ifconf ifc; 106 struct ifreq *ifrqp; | |
107 struct nb_ifdesc *ifd; | 105 struct nb_ifdesc *ifd; |
108 struct in_addr iaddr, imask; 109 char *ifrdata, *iname; 110 int s, rdlen, ifcnt, error, iflags, i; 111 size_t ifrlen; | 106 struct ifaddrs *ifp, *p; 107 int i; |
112 | 108 |
113 *iflist = NULL; 114 s = socket(AF_INET, SOCK_DGRAM, 0); 115 if (s == -1) | 109 if (getifaddrs(&ifp) < 0) |
116 return errno; 117 | 110 return errno; 111 |
118 rdlen = maxif * sizeof(struct ifreq); 119 ifrdata = malloc(rdlen); 120 if (ifrdata == NULL) { 121 error = ENOMEM; 122 goto bad; 123 } 124 ifc.ifc_len = rdlen; 125 ifc.ifc_buf = ifrdata; 126 if (ioctl(s, SIOCGIFCONF, &ifc) != 0) { 127 error = errno; 128 goto bad; 129 } 130 ifrqp = ifc.ifc_req; 131 ifcnt = ifc.ifc_len / sizeof(struct ifreq); 132 error = 0; 133 for (i = 0; i < ifcnt; i++) { 134 ifrlen = sizeof(struct ifreq); 135 if (ifrqp->ifr_addr.sa_len > sizeof(struct sockaddr)) 136 ifrlen += ifrqp->ifr_addr.sa_len 137 - sizeof(struct sockaddr); | 112 *iflist = NULL; 113 i = 0; 114 for (p = ifp; p; p = p->ifa_next) { |
138 | 115 |
139 if (ifrqp->ifr_addr.sa_family != AF_INET) 140 goto next; 141 iname = ifrqp->ifr_name; 142 if (strlen(iname) >= sizeof(ifd->id_name)) 143 goto next; | 116 if (i >= maxif) 117 break; |
144 | 118 |
145 iaddr = (*(struct sockaddr_in *)&ifrqp->ifr_addr).sin_addr; | 119 if ((p->ifa_addr->sa_family != AF_INET) || 120 ((p->ifa_flags & (IFF_UP|IFF_BROADCAST)) 121 != (IFF_UP|IFF_BROADCAST))) 122 continue; 123 if (strlen(p->ifa_name) >= sizeof(ifd->id_name)) 124 continue; |
146 | 125 |
147 if (ioctl(s, SIOCGIFNETMASK, ifrqp) != 0) 148 goto next; 149 imask = ((struct sockaddr_in *)&ifrqp->ifr_addr)->sin_addr; 150 151 if (ioctl(s, SIOCGIFFLAGS, ifrqp) != 0) 152 goto next; 153 iflags = ifrqp->ifr_flags; 154 if ((iflags & IFF_UP) == 0 || (iflags & IFF_BROADCAST) == 0) 155 goto next; 156 | |
157 ifd = malloc(sizeof(struct nb_ifdesc)); | 126 ifd = malloc(sizeof(struct nb_ifdesc)); |
158 if (ifd == NULL) | 127 if (ifd == NULL) { 128 freeifaddrs(ifp); 129 /* XXX should free stuff already in *iflist */ |
159 return ENOMEM; | 130 return ENOMEM; |
131 } |
|
160 bzero(ifd, sizeof(struct nb_ifdesc)); | 132 bzero(ifd, sizeof(struct nb_ifdesc)); |
161 strcpy(ifd->id_name, iname); 162 ifd->id_flags = iflags; 163 ifd->id_addr = iaddr; 164 ifd->id_mask = imask; | 133 strcpy(ifd->id_name, p->ifa_name); 134 ifd->id_flags = p->ifa_flags; 135 ifd->id_addr = ((struct sockaddr_in *)p->ifa_addr)->sin_addr; 136 ifd->id_mask = ((struct sockaddr_in *)p->ifa_netmask)->sin_addr; |
165 ifd->id_next = *iflist; 166 *iflist = ifd; | 137 ifd->id_next = *iflist; 138 *iflist = ifd; |
167 168next: 169 ifrqp = (struct ifreq *)((caddr_t)ifrqp + ifrlen); | 139 i++; |
170 } | 140 } |
171bad: 172 free(ifrdata); 173 close(s); 174 return error; 175} | |
176 | 141 |
142 freeifaddrs(ifp); 143 return 0; 144} 145 |
|
177/*ARGSUSED*/ 178/*int 179nbns_resolvename(const char *name, struct sockaddr **dest) 180{ 181 printf("NetBIOS name resolver is not included in this distribution.\n"); 182 printf("Please use '-I' option to specify an IP address of server.\n"); 183 return EHOSTUNREACH; 184}*/ --- 27 unchanged lines hidden --- | 146/*ARGSUSED*/ 147/*int 148nbns_resolvename(const char *name, struct sockaddr **dest) 149{ 150 printf("NetBIOS name resolver is not included in this distribution.\n"); 151 printf("Please use '-I' option to specify an IP address of server.\n"); 152 return EHOSTUNREACH; 153}*/ --- 27 unchanged lines hidden --- |