2 * WSOCK32 specific functions
4 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
11 #include "debugtools.h"
14 #include "wscontrol.h"
16 #include <sys/ioctl.h>
20 #ifdef HAVE_SYS_SOCKIO_H
21 # include <sys/sockio.h>
27 DEFAULT_DEBUG_CHANNEL(winsock);
30 /***********************************************************************
33 * WsControl seems to be an undocumented Win95 function. A lot of
34 * discussion about WsControl can be found on the net, e.g.
35 * Subject: Re: WSOCK32.DLL WsControl Exported Function
36 * From: "Peter Rindfuss" <rindfuss-s@medea.wz-berlin.de>
39 * WSCNTL_TCPIP_QUERY_INFO option is partially implemeted based
40 * on observing the behaviour of WsControl with an app in
41 * Windows 98. It is not fully implemented, and there could
42 * be (are?) errors due to incorrect assumptions made.
45 * WsControl returns WSCTL_SUCCESS on success.
46 * STATUS_BUFFER_TOO_SMALL is returned if the output buffer length
47 * (*pcbResponseInfoLen) is too small, otherwise errors return -1.
49 * It doesn't seem to generate errors that can be retrieved by
54 DWORD WINAPI WsControl(DWORD protocoll,
57 LPDWORD pcbRequestInfoLen,
59 LPDWORD pcbResponseInfoLen)
61 /* Get the command structure into a pointer we can use,
63 TDIObjectID *pcommand = (TDIObjectID *)pRequestInfo;
65 TRACE (" WsControl TOI_ID=>0x%lx<, {TEI_ENTITY=0x%lx, TEI_INSTANCE=0x%lx}, TOI_CLASS=0x%lx, TOI_TYPE=0x%lx\n",
66 pcommand->toi_id, pcommand->toi_entity.tei_entity, pcommand->toi_entity.tei_instance,
67 pcommand->toi_class, pcommand->toi_type );
73 case WSCNTL_TCPIP_QUERY_INFO:
75 switch (pcommand->toi_id)
78 ENTITY_LIST_ID seems to get number of adapters in the system.
79 (almost like an index to be used when calling other WsControl options)
83 TDIEntityID *baseptr = pResponseInfo;
86 if (pcommand->toi_class != INFO_CLASS_GENERIC &&
87 pcommand->toi_type != INFO_TYPE_PROVIDER)
89 FIXME ("Unexpected Option for ENTITY_LIST_ID request -> toi_class=0x%lx, toi_type=0x%lx\n",
90 pcommand->toi_class, pcommand->toi_type);
91 return (WSAEOPNOTSUPP);
94 numInt = WSCNTL_GetInterfaceCount();
97 ERR ("Unable to open /proc filesystem to determine number of network interfaces!\n");
101 if (*pcbResponseInfoLen < sizeof(TDIEntityID)*(numInt*2) )
103 return (STATUS_BUFFER_TOO_SMALL);
107 memset(baseptr, 0, sizeof(TDIEntityID)*(numInt*2));
109 for (i=0; i<numInt; i++)
111 /* tei_instance is an network interface identifier.
112 I'm not quite sure what the difference is between tei_entity values of
113 CL_NL_ENTITY and IF_ENTITY */
114 baseptr->tei_entity = CL_NL_ENTITY; baseptr->tei_instance = i; baseptr++;
115 baseptr->tei_entity = IF_ENTITY; baseptr->tei_instance = i; baseptr++;
118 /* Calculate size of out buffer */
119 *pcbResponseInfoLen = sizeof(TDIEntityID)*(numInt*2);
125 /* ENTITY_TYPE_ID is used to obtain simple information about a
126 network card, such as MAC Address, description, interface type,
127 number of network addresses, etc. */
128 case ENTITY_TYPE_ID: /* ALSO: IP_MIB_STATS_ID */
130 if (pcommand->toi_class == INFO_CLASS_GENERIC && pcommand->toi_type == INFO_TYPE_PROVIDER)
132 if (pcommand->toi_entity.tei_entity == IF_ENTITY)
134 * ((ULONG *)pResponseInfo) = IF_MIB;
136 /* Calculate size of out buffer */
137 *pcbResponseInfoLen = sizeof (ULONG);
140 else if (pcommand->toi_entity.tei_entity == CL_NL_ENTITY)
142 * ((ULONG *)pResponseInfo) = CL_NL_IP;
144 /* Calculate size of out buffer */
145 *pcbResponseInfoLen = sizeof (ULONG);
148 else if (pcommand->toi_class == INFO_CLASS_PROTOCOL &&
149 pcommand->toi_type == INFO_TYPE_PROVIDER)
151 if (pcommand->toi_entity.tei_entity == IF_ENTITY)
153 /* In this case, we are requesting specific information about a
154 a particular network adapter. (MAC Address, speed, data transmitted/received,
157 IFEntry *IntInfo = (IFEntry *) pResponseInfo;
163 if (!WSCNTL_GetInterfaceName(pcommand->toi_entity.tei_instance, ifName))
165 ERR ("Unable to parse /proc filesystem!\n");
169 /* Get a socket so that we can use ioctl */
170 if ( (sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
172 ERR ("Error creating socket!\n");
176 /* 0 out return structure first */
177 memset (IntInfo, 0, sizeof(IFEntry));
180 IntInfo->if_index = pcommand->toi_entity.tei_instance;
182 /* MAC Address - Let's try to do this in a cross-platform way... */
183 #if defined(SIOCGIFHWADDR) /* Linux */
184 strcpy(ifInfo.ifr_name, ifName);
185 if (ioctl(sock, SIOCGIFHWADDR, &ifInfo) < 0)
187 ERR ("Error obtaining MAC Address!\n");
193 /* FIXME: Is it correct to assume size of 6? */
194 memcpy(IntInfo->if_physaddr, ifInfo.ifr_hwaddr.sa_data, 6);
195 IntInfo->if_physaddrlen=6;
197 #elif defined(SIOCGENADDR) /* Solaris */
198 if (ioctl(sock, SIOCGENADDR, &ifInfo) < 0)
200 ERR ("Error obtaining MAC Address!\n");
206 /* FIXME: Is it correct to assume size of 6? */
207 memcpy(IntInfo->if_physaddr, ifInfo.ifr_enaddr, 6);
208 IntInfo->if_physaddrlen=6;
211 memset (IntInfo->if_physaddr, 0, 6);
212 ERR ("Unable to determine MAC Address on your platform!\n");
216 /* Interface name and length */
217 strcpy (IntInfo->if_descr, ifName);
218 IntInfo->if_descrlen= strlen (IntInfo->if_descr);
220 /* Obtain bytes transmitted/received for interface */
221 if ( (WSCNTL_GetTransRecvStat(pcommand->toi_entity.tei_instance,
222 &IntInfo->if_inoctets, &IntInfo->if_outoctets)) < 0)
224 ERR ("Error obtaining transmit/receive stats for the network interface!\n");
230 /* FIXME: How should the below be properly calculated? ******************/
231 IntInfo->if_type = 0x6; /* Ethernet (?) */
232 IntInfo->if_speed = 1000000; /* Speed of interface (bits per second?) */
233 /************************************************************************/
236 *pcbResponseInfoLen = sizeof (IFEntry) + IntInfo->if_descrlen;
238 else if (pcommand->toi_entity.tei_entity == CL_NL_ENTITY)
240 IPSNMPInfo *infoStruc = (IPSNMPInfo *) pResponseInfo;
243 /* This case is used to obtain general statistics about the
246 if (*pcbResponseInfoLen < sizeof(IPSNMPInfo) )
248 return (STATUS_BUFFER_TOO_SMALL);
253 memset(infoStruc, 0, sizeof(IPSNMPInfo));
255 /* Get the number of interfaces */
256 numInt = WSCNTL_GetInterfaceCount();
259 ERR ("Unable to open /proc filesystem to determine number of network interfaces!\n");
263 infoStruc->ipsi_numif = numInt; /* # of interfaces */
264 infoStruc->ipsi_numaddr = numInt; /* # of addresses */
265 infoStruc->ipsi_numroutes = numInt; /* # of routes ~ FIXME - Is this right? */
267 /* FIXME: How should the below be properly calculated? ******************/
268 infoStruc->ipsi_forwarding = 0x0;
269 infoStruc->ipsi_defaultttl = 0x0;
270 infoStruc->ipsi_inreceives = 0x0;
271 infoStruc->ipsi_inhdrerrors = 0x0;
272 infoStruc->ipsi_inaddrerrors = 0x0;
273 infoStruc->ipsi_forwdatagrams = 0x0;
274 infoStruc->ipsi_inunknownprotos = 0x0;
275 infoStruc->ipsi_indiscards = 0x0;
276 infoStruc->ipsi_indelivers = 0x0;
277 infoStruc->ipsi_outrequests = 0x0;
278 infoStruc->ipsi_routingdiscards = 0x0;
279 infoStruc->ipsi_outdiscards = 0x0;
280 infoStruc->ipsi_outnoroutes = 0x0;
281 infoStruc->ipsi_reasmtimeout = 0x0;
282 infoStruc->ipsi_reasmreqds = 0x0;
283 infoStruc->ipsi_reasmoks = 0x0;
284 infoStruc->ipsi_reasmfails = 0x0;
285 infoStruc->ipsi_fragoks = 0x0;
286 infoStruc->ipsi_fragfails = 0x0;
287 infoStruc->ipsi_fragcreates = 0x0;
288 /************************************************************************/
290 /* Calculate size of out buffer */
291 *pcbResponseInfoLen = sizeof(IPSNMPInfo);
297 FIXME ("Unexpected Option for ENTITY_TYPE_ID request -> toi_class=0x%lx, toi_type=0x%lx\n",
298 pcommand->toi_class, pcommand->toi_type);
300 return (WSAEOPNOTSUPP);
307 /* IP_MIB_ADDRTABLE_ENTRY_ID is used to obtain more detailed information about a
308 particular network adapter */
309 case IP_MIB_ADDRTABLE_ENTRY_ID:
311 IPAddrEntry *baseIPInfo = (IPAddrEntry *) pResponseInfo;
316 if (*pcbResponseInfoLen < sizeof(IPAddrEntry))
318 return (STATUS_BUFFER_TOO_SMALL);
321 if (!WSCNTL_GetInterfaceName(pcommand->toi_entity.tei_instance, ifName))
323 ERR ("Unable to parse /proc filesystem!\n");
328 /* Get a socket so we can use ioctl */
329 if ( (sock = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
331 ERR ("Error creating socket!\n");
336 memset(baseIPInfo, 0, sizeof(IPAddrEntry) );
339 baseIPInfo->iae_index = pcommand->toi_entity.tei_instance;
342 strcpy (ifInfo.ifr_name, ifName);
343 ifInfo.ifr_addr.sa_family = AF_INET;
344 if (ioctl(sock, SIOCGIFADDR, &ifInfo) < 0)
346 baseIPInfo->iae_addr = 0x0;
350 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_addr;
351 baseIPInfo->iae_addr = ipTemp->sin_addr.S_un.S_addr;
354 /* Broadcast Address */
355 strcpy (ifInfo.ifr_name, ifName);
356 if (ioctl(sock, SIOCGIFBRDADDR, &ifInfo) < 0)
358 baseIPInfo->iae_bcastaddr = 0x0;
362 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_broadaddr;
363 baseIPInfo->iae_bcastaddr = ipTemp->sin_addr.S_un.S_addr;
367 strcpy(ifInfo.ifr_name, ifName);
368 if (ioctl(sock, SIOCGIFNETMASK, &ifInfo) < 0)
370 baseIPInfo->iae_mask = 0x0;
374 /* Trying to avoid some compile problems across platforms.
375 (Linux, FreeBSD, Solaris...) */
378 baseIPInfo->iae_mask = 0;
379 ERR ("Unable to determine Netmask on your platform!\n");
381 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_addr;
382 baseIPInfo->iae_mask = ipTemp->sin_addr.S_un.S_addr;
385 struct ws_sockaddr_in *ipTemp = (struct ws_sockaddr_in *)&ifInfo.ifr_netmask;
386 baseIPInfo->iae_mask = ipTemp->sin_addr.S_un.S_addr;
390 /* FIXME: How should the below be properly calculated? ******************/
391 baseIPInfo->iae_reasmsize = 0x0;
392 baseIPInfo->iae_context = 0x0;
393 baseIPInfo->iae_pad = 0x0;
394 /************************************************************************/
396 /* Calculate size of out buffer */
397 *pcbResponseInfoLen = sizeof(IPAddrEntry);
405 FIXME ("Command ID Not Supported -> toi_id=0x%lx, toi_entity={tei_entity=0x%lx, tei_instance=0x%lx}, toi_class=0x%lx, toi_type=0x%lx\n",
406 pcommand->toi_id, pcommand->toi_entity.tei_entity, pcommand->toi_entity.tei_instance,
407 pcommand->toi_class, pcommand->toi_type);
409 return (WSAEOPNOTSUPP);
416 case WSCNTL_TCPIP_ICMP_ECHO:
418 unsigned int addr = *(unsigned int*)pRequestInfo;
420 int timeout= *(unsigned int*)(inbuf+4);
421 short x1 = *(unsigned short*)(inbuf+8);
422 short sendbufsize = *(unsigned short*)(inbuf+10);
423 char x2 = *(unsigned char*)(inbuf+12);
424 char ttl = *(unsigned char*)(inbuf+13);
425 char service = *(unsigned char*)(inbuf+14);
426 char type= *(unsigned char*)(inbuf+15); /* 0x2: don't fragment*/
429 FIXME("(ICMP_ECHO) to 0x%08x stub \n", addr);
435 FIXME("Protocoll Not Supported -> protocoll=0x%lx, action=0x%lx, Request=%p, RequestLen=%p, Response=%p, ResponseLen=%p\n",
436 protocoll, action, pRequestInfo, pcbRequestInfoLen, pResponseInfo, pcbResponseInfoLen);
438 return (WSAEOPNOTSUPP);
443 return (WSCTL_SUCCESS);
449 Helper function for WsControl - Get count of the number of interfaces
450 by parsing /proc filesystem.
452 int WSCNTL_GetInterfaceCount(void)
455 char buf[512]; /* Size doesn't matter, something big */
459 /* Open /proc filesystem file for network devices */
460 procfs = fopen(PROCFS_NETDEV_FILE, "r");
463 /* If we can't open the file, return an error */
467 /* Omit first two lines, they are only headers */
468 fgets(buf, sizeof buf, procfs);
469 fgets(buf, sizeof buf, procfs);
471 while (fgets(buf, sizeof buf, procfs))
473 /* Each line in the file represents a network interface */
483 Helper function for WsControl - Get name of device from interface number
484 by parsing /proc filesystem.
486 int WSCNTL_GetInterfaceName(int intNumber, char *intName)
489 char buf[512]; /* Size doesn't matter, something big */
492 /* Open /proc filesystem file for network devices */
493 procfs = fopen(PROCFS_NETDEV_FILE, "r");
496 /* If we can't open the file, return an error */
500 /* Omit first two lines, they are only headers */
501 fgets(buf, sizeof(buf), procfs);
502 fgets(buf, sizeof(buf), procfs);
504 for (i=0; i<intNumber; i++)
506 /* Skip the lines that don't interest us. */
507 fgets(buf, sizeof(buf), procfs);
509 fgets(buf, sizeof(buf), procfs); /* This is the line we want */
512 /* Parse out the line, grabbing only the name of the device
513 to the intName variable
515 The Line comes in like this: (we only care about the device name)
516 lo: 21970 377 0 0 0 0 0 0 21970 377 0 0 0 0 0 0
519 while (isspace(buf[i])) /* Skip initial space(s) */
531 if (buf[i] == ':') /* FIXME: Not sure if this block (alias detection) works properly */
533 /* This interface could be an alias... */
535 char *dotname = intName;
536 *intName++ = buf[i++];
538 while (isdigit(buf[i]))
540 *intName++ = buf[i++];
545 /* ... It wasn't, so back up */
560 *intName++ = buf[i++];
570 Helper function for WsControl - This function returns the bytes (octets) transmitted
571 and received for the supplied interface number from the /proc fs.
573 int WSCNTL_GetTransRecvStat(int intNumber, unsigned long *transBytes, unsigned long *recvBytes)
576 char buf[512], result[512]; /* Size doesn't matter, something big */
577 int i, bufPos, resultPos;
579 /* Open /proc filesystem file for network devices */
580 procfs = fopen(PROCFS_NETDEV_FILE, "r");
583 /* If we can't open the file, return an error */
587 /* Omit first two lines, they are only headers */
588 fgets(buf, sizeof(buf), procfs);
589 fgets(buf, sizeof(buf), procfs);
591 for (i=0; i<intNumber; i++)
593 /* Skip the lines that don't interest us. */
594 fgets(buf, sizeof(buf), procfs);
596 fgets(buf, sizeof(buf), procfs); /* This is the line we want */
600 /* Parse out the line, grabbing the number of bytes transmitted
601 and received on the interface.
603 The Line comes in like this: (we care about columns 2 and 10)
604 lo: 21970 377 0 0 0 0 0 0 21970 377 0 0 0 0 0 0
607 /* Start at character 0 in the buffer */
610 /* Skip initial space(s) */
611 while (isspace(buf[bufPos]))
615 /* Skip the name and its trailing spaces (if any) */
618 if (isspace(buf[bufPos]))
621 if (buf[bufPos] == ':') /* Could be an alias */
625 while(isdigit (buf[bufPos]))
627 if (buf[bufPos] != ':')
629 if (buf[bufPos] == '\0')
641 while (isspace(buf[bufPos]))
645 /* This column (#2) is the number of bytes received. */
647 while (!isspace(buf[bufPos]))
649 result[resultPos] = buf[bufPos];
650 result[resultPos+1]='\0';
651 resultPos++; bufPos++;
653 *recvBytes = strtoul (result, NULL, 10); /* convert string to unsigned long, using base 10 */
656 /* Skip columns #3 to #9 (Don't need them) */
659 while (isspace(buf[bufPos]))
661 while (!isspace(buf[bufPos]))
666 /* This column (#10) is the number of bytes transmitted */
667 while (isspace(buf[bufPos]))
671 while (!isspace(buf[bufPos]))
673 result[resultPos] = buf[bufPos];
674 result[resultPos+1]='\0';
675 resultPos++; bufPos++;
677 *transBytes = strtoul (result, NULL, 10); /* convert string to unsigned long, using base 10 */
687 /***********************************************************************
688 * WS_s_perror (WSOCK32.1108)
690 void WINAPI WS_s_perror(LPCSTR message)
692 FIXME("(%s): stub\n",message);