2 * Copyright 2001 Mike McCormack
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include "wine/debug.h"
34 #ifdef HAVE_SYS_FILE_H
35 # include <sys/file.h>
37 #include <sys/ioctl.h>
38 #ifdef HAVE_SYS_SOCKET_H
39 # include <sys/socket.h>
41 #ifdef HAVE_SYS_SOCKIO_H
42 # include <sys/sockio.h>
47 #ifdef HAVE_NETINET_IN_H
48 # include <netinet/in.h>
51 #ifdef HAVE_SOCKADDR_SA_LEN
53 # define max(a,b) ((a) > (b) ? (a) : (b))
55 # define ifreq_size(i) max(sizeof(struct ifreq),\
56 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
58 # define ifreq_size(i) sizeof(struct ifreq)
59 # endif /* defined(HAVE_SOCKADDR_SA_LEN) */
61 WINE_DEFAULT_DEBUG_CHANNEL(netbios);
63 HMODULE NETAPI32_hModule = 0;
68 unsigned char address[6];
71 static struct NetBiosAdapter NETBIOS_Adapter[MAX_LANA];
74 int get_hw_address(int sd, struct ifreq *ifr, unsigned char *address)
76 if (ioctl(sd, SIOCGIFHWADDR, ifr) < 0)
78 memcpy(address, (unsigned char *)&ifr->ifr_hwaddr.sa_data, 6);
83 int get_hw_address(int sd, struct ifreq *ifr, unsigned char *address)
85 if (ioctl(sd, SIOCGENADDR, ifr) < 0)
87 memcpy(address, (unsigned char *) ifr->ifr_enaddr, 6);
91 int get_hw_address(int sd, struct ifreq *ifr, unsigned char *address)
95 # endif /* SIOCGENADDR */
96 # endif /* SIOCGIFHWADDR */
98 static UCHAR NETBIOS_Enum(PNCB ncb)
102 struct ifreq ifr, *ifrp;
104 unsigned char buf[1024];
107 LANA_ENUM *lanas = (PLANA_ENUM) ncb->ncb_buffer;
114 /* BSD 4.4 defines the size of an ifreq to be
115 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
116 * However, under earlier systems, sa_len isn't present, so
117 * the size is just sizeof(struct ifreq)
120 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
122 return NRC_OPENERROR;
124 memset(buf, 0, sizeof(buf));
125 ifc.ifc_len = sizeof(buf);
127 /* get the ifconf interface */
128 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0)
131 return NRC_OPENERROR;
134 /* loop through the interfaces, looking for a valid one */
135 /* n = ifc.ifc_len; */
137 for (i = 0; i < ifc.ifc_len; i++)
139 unsigned char *a = NETBIOS_Adapter[i].address;
141 ifrp = (struct ifreq *)((char *)ifc.ifc_buf+ofs);
142 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
144 /* try to get the address for this interface */
145 if(get_hw_address(sd, &ifr, a)==0)
147 /* make sure it's not blank */
148 /* if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
151 TRACE("Found valid adapter %d at %02x:%02x:%02x:%02x:%02x:%02x\n", i,
152 a[0],a[1],a[2],a[3],a[4],a[5]);
154 NETBIOS_Adapter[i].valid = TRUE;
155 lanas->lana[lanas->length] = i;
158 ofs += ifreq_size(ifr);
161 #endif /* HAVE_NET_IF_H */
166 static UCHAR NETBIOS_Astat(PNCB ncb)
168 struct NetBiosAdapter *nad = &NETBIOS_Adapter[ncb->ncb_lana_num];
169 PADAPTER_STATUS astat = (PADAPTER_STATUS) ncb->ncb_buffer;
171 TRACE("NCBASTAT (Adapter %d)\n", ncb->ncb_lana_num);
174 return NRC_INVADDRESS;
176 memset(astat, 0, sizeof astat);
177 memcpy(astat->adapter_address, nad->address, sizeof astat->adapter_address);
183 NETAPI32_LibMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
185 TRACE("%x,%lx,%p\n", hinstDLL, fdwReason, lpvReserved);
188 case DLL_PROCESS_ATTACH:
189 NETAPI32_hModule = hinstDLL;
191 case DLL_PROCESS_DETACH:
198 BOOL WINAPI Netbios(PNCB pncb)
200 UCHAR ret = NRC_ILLCMD;
202 TRACE("ncb = %p\n",pncb);
205 return NRC_INVADDRESS;
207 switch(pncb->ncb_command&0x7f)
210 FIXME("NCBRESET adapter %d\n",pncb->ncb_lana_num);
211 if( (pncb->ncb_lana_num < MAX_LANA ) &&
212 NETBIOS_Adapter[pncb->ncb_lana_num].valid)
215 ret = NRC_ILLCMD; /* NetBIOS emulator not found */
219 FIXME("NCBADDNAME\n");
223 FIXME("NCBADDGRNAME\n");
227 FIXME("NCBDELNAME\n");
239 FIXME("NCBHANGUP\n");
243 FIXME("NCBCANCEL\n");
247 FIXME("NCBLISTEN\n");
251 ret = NETBIOS_Astat(pncb);
255 ret = NETBIOS_Enum(pncb);
259 FIXME("(%p): command code %02x\n", pncb, pncb->ncb_command);
261 ret = NRC_ILLCMD; /* NetBIOS emulator not found */
263 pncb->ncb_retcode = ret;