- improve WsControl error checking
[wine/wine-kai.git] / dlls / wsock32 / socket.c
blobb61e26dde47da90f9e0a26df17a7d34dd87af07f
1 /*
2 * WSOCK32 specific functions
4 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
5 * Copyright (C) 2003 Juan Lang.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "config.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wine/debug.h"
27 #include "winsock2.h"
28 #include "winnt.h"
29 #include "wscontrol.h"
30 #include "iphlpapi.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
34 /* internal remapper function for the IP_ constants */
35 static INT _remap_optname(INT level, INT optname)
37 TRACE("level=%d, optname=%d\n", level, optname);
38 if (level == IPPROTO_IP) {
39 switch (optname) { /***** from value *****/
40 case 2: return 9; /* IP_MULTICAST_IF */
41 case 3: return 10; /* IP_MULTICAST_TTL */
42 case 4: return 11; /* IP_MULTICAST_LOOP */
43 case 5: return 12; /* IP_ADD_MEMBERSHIP */
44 case 6: return 13; /* IP_DROP_MEMBERSHIP */
45 case 7: return 4; /* IP_TTL */
46 case 8: return 3; /* IP_TOS */
47 case 9: return 14; /* IP_DONTFRAGMENT */
48 default: FIXME("Unknown optname %d, can't remap!\n", optname); return optname;
50 } else {
51 /* don't need to do anything */
52 return optname;
56 /***********************************************************************
57 * setsockopt (WSOCK32.21)
59 * We have these forwarders because, for reasons unknown to us mere mortals,
60 * the values of the IP_ constants changed between winsock.h and winsock2.h.
61 * So, we need to remap them here.
63 INT WINAPI WS1_setsockopt(SOCKET s, INT level, INT optname, char *optval, INT optlen)
65 return setsockopt(s, level, _remap_optname(level, optname), optval, optlen);
68 /***********************************************************************
69 * getsockopt (WSOCK32.7)
71 INT WINAPI WS1_getsockopt(SOCKET s, INT level, INT optname, char *optval, INT *optlen)
73 return getsockopt(s, level, _remap_optname(level, optname), optval, optlen);
76 /***********************************************************************
77 * WsControl (WSOCK32.1001)
79 * WsControl seems to be an undocumented Win95 function. A lot of
80 * discussion about WsControl can be found on the net, e.g.
81 * Subject: Re: WSOCK32.DLL WsControl Exported Function
82 * From: "Peter Rindfuss" <rindfuss-s@medea.wz-berlin.de>
83 * Date: 1997/08/17
85 * The WSCNTL_TCPIP_QUERY_INFO option is partially implemented based
86 * on observing the behaviour of WsControl with an app in
87 * Windows 98. It is not fully implemented, and there could
88 * be (are?) errors due to incorrect assumptions made.
91 * WsControl returns WSCTL_SUCCESS on success.
92 * ERROR_LOCK_VIOLATION is returned if the output buffer length
93 * (*pcbResponseInfoLen) is too small. This is an unusual error code, but
94 * it matches Win98's behavior. Other errors come from winerror.h, not from
95 * winsock.h. Again, this is to match Win98 behavior.
99 DWORD WINAPI WsControl(DWORD protocol,
100 DWORD action,
101 LPVOID pRequestInfo,
102 LPDWORD pcbRequestInfoLen,
103 LPVOID pResponseInfo,
104 LPDWORD pcbResponseInfoLen)
107 /* Get the command structure into a pointer we can use,
108 rather than void */
109 TDIObjectID *pcommand = (TDIObjectID *)pRequestInfo;
111 /* validate input parameters. Error codes are from winerror.h, not from
112 * winsock.h. pcbResponseInfoLen is apparently allowed to be NULL for some
113 * commands, since winipcfg.exe fails if we ensure it's non-NULL in every
114 * case.
116 if (protocol != IPPROTO_TCP) return ERROR_INVALID_PARAMETER;
117 if (!pcommand) return ERROR_INVALID_PARAMETER;
118 if (!pcbRequestInfoLen) return ERROR_INVALID_ACCESS;
119 if (*pcbRequestInfoLen < sizeof(TDIObjectID)) return ERROR_INVALID_ACCESS;
120 if (!pResponseInfo) return ERROR_INVALID_PARAMETER;
121 if (pcommand->toi_type != INFO_TYPE_PROVIDER) return ERROR_INVALID_PARAMETER;
123 TRACE (" WsControl TOI_ID=>0x%lx<, {TEI_ENTITY=0x%lx, TEI_INSTANCE=0x%lx}, TOI_CLASS=0x%lx, TOI_TYPE=0x%lx\n",
124 pcommand->toi_id, pcommand->toi_entity.tei_entity,
125 pcommand->toi_entity.tei_instance,
126 pcommand->toi_class, pcommand->toi_type );
128 switch (action)
130 case WSCNTL_TCPIP_QUERY_INFO:
132 if (pcommand->toi_class != INFO_CLASS_GENERIC &&
133 pcommand->toi_class != INFO_CLASS_PROTOCOL)
135 ERR("Unexpected class %ld for WSCNTL_TCPIP_QUERY_INFO",
136 pcommand->toi_class);
137 return ERROR_BAD_ENVIRONMENT;
140 switch (pcommand->toi_id)
142 /* ENTITY_LIST_ID gets the list of "entity IDs", where an entity
143 may represent an interface, or a datagram service, or address
144 translation, or other fun things. Typically an entity ID represents
145 a class of service, which is further queried for what type it is.
146 Different types will then have more specific queries defined.
148 case ENTITY_LIST_ID:
150 TDIEntityID *baseptr = (TDIEntityID *)pResponseInfo;
151 DWORD numInt, i, ifTable, spaceNeeded;
152 PMIB_IFTABLE table;
154 if (!pcbResponseInfoLen)
155 return ERROR_BAD_ENVIRONMENT;
156 if (pcommand->toi_class != INFO_CLASS_GENERIC)
158 FIXME ("Unexpected Option for ENTITY_LIST_ID request -> toi_class=0x%lx",
159 pcommand->toi_class);
160 return (ERROR_BAD_ENVIRONMENT);
163 GetNumberOfInterfaces(&numInt);
164 spaceNeeded = sizeof(TDIEntityID) * (numInt + 4);
166 if (*pcbResponseInfoLen < spaceNeeded)
167 return (ERROR_LOCK_VIOLATION);
169 ifTable = 0;
170 GetIfTable(NULL, &ifTable, FALSE);
171 table = (PMIB_IFTABLE)calloc(1, ifTable);
172 if (!table)
173 return ERROR_NOT_ENOUGH_MEMORY;
174 GetIfTable(table, &ifTable, FALSE);
176 spaceNeeded = sizeof(TDIEntityID) * (table->dwNumEntries + 4);
177 if (*pcbResponseInfoLen < spaceNeeded)
179 free(table);
180 return (ERROR_LOCK_VIOLATION);
183 memset(baseptr, 0, spaceNeeded);
185 for (i = 0; i < table->dwNumEntries; i++)
187 /* Return IF_GENERIC on every interface, and AT_ENTITY,
188 * CL_NL_ENTITY, CL_TL_ENTITY, and CO_TL_ENTITY on the first
189 * interface. MS returns them only on the loopback
190 * interface, but it doesn't seem to matter.
192 if (i == 0)
194 baseptr->tei_entity = CO_TL_ENTITY;
195 baseptr->tei_instance = table->table[i].dwIndex;
196 baseptr++;
197 baseptr->tei_entity = CL_TL_ENTITY;
198 baseptr->tei_instance = table->table[i].dwIndex;
199 baseptr++;
200 baseptr->tei_entity = CL_NL_ENTITY;
201 baseptr->tei_instance = table->table[i].dwIndex;
202 baseptr++;
203 baseptr->tei_entity = AT_ENTITY;
204 baseptr->tei_instance = table->table[i].dwIndex;
205 baseptr++;
207 baseptr->tei_entity = IF_GENERIC;
208 baseptr->tei_instance = table->table[i].dwIndex;
209 baseptr++;
212 *pcbResponseInfoLen = spaceNeeded;
213 free(table);
215 break;
218 /* Returns MIB-II statistics for an interface */
219 case ENTITY_TYPE_ID:
220 switch (pcommand->toi_entity.tei_entity)
222 case IF_GENERIC:
223 if (pcommand->toi_class == INFO_CLASS_GENERIC)
225 if (!pcbResponseInfoLen)
226 return ERROR_BAD_ENVIRONMENT;
227 *((ULONG *)pResponseInfo) = IF_MIB;
228 *pcbResponseInfoLen = sizeof(ULONG);
230 else if (pcommand->toi_class == INFO_CLASS_PROTOCOL)
232 MIB_IFROW row;
233 DWORD index = pcommand->toi_entity.tei_instance, ret;
234 DWORD size = sizeof(row) - sizeof(row.wszName) -
235 sizeof(row.bDescr);
237 if (!pcbResponseInfoLen)
238 return ERROR_BAD_ENVIRONMENT;
239 if (*pcbResponseInfoLen < size)
240 return (ERROR_LOCK_VIOLATION);
241 row.dwIndex = index;
242 ret = GetIfEntry(&row);
243 if (ret != NO_ERROR)
245 ERR ("Error retrieving data for interface index %lu\n",
246 index);
247 return ret;
249 size = sizeof(row) - sizeof(row.wszName) -
250 sizeof(row.bDescr) + row.dwDescrLen;
251 if (*pcbResponseInfoLen < size)
252 return (ERROR_LOCK_VIOLATION);
253 memcpy(pResponseInfo, &row.dwIndex, size);
254 *pcbResponseInfoLen = size;
256 break;
258 /* Returns address-translation related data. In our case, this is
259 * ARP.
260 * FIXME: Win98 seems to assume ARP will always be on interface
261 * index 1, so arp.exe fails when this isn't the case.
263 case AT_ENTITY:
264 if (pcommand->toi_class == INFO_CLASS_GENERIC)
266 if (!pcbResponseInfoLen)
267 return ERROR_BAD_ENVIRONMENT;
268 *((ULONG *)pResponseInfo) = AT_ARP;
269 *pcbResponseInfoLen = sizeof(ULONG);
271 else if (pcommand->toi_class == INFO_CLASS_PROTOCOL)
273 PMIB_IPNETTABLE table;
274 DWORD size;
275 PULONG output = (PULONG)pResponseInfo;
277 if (!pcbResponseInfoLen)
278 return ERROR_BAD_ENVIRONMENT;
279 if (*pcbResponseInfoLen < sizeof(ULONG) * 2)
280 return (ERROR_LOCK_VIOLATION);
281 GetIpNetTable(NULL, &size, FALSE);
282 table = (PMIB_IPNETTABLE)calloc(1, size);
283 if (!table)
284 return ERROR_NOT_ENOUGH_MEMORY;
285 GetIpNetTable(table, &size, FALSE);
286 /* FIXME: I don't understand the meaning of the ARP output
287 * very well, but it seems to indicate how many ARP entries
288 * exist. I don't know whether this should reflect the
289 * number per interface, as I'm only testing with a single
290 * interface. So, I lie and say all ARP entries exist on
291 * a single interface--the first one that appears in the
292 * ARP table.
294 *(output++) = table->dwNumEntries;
295 *output = table->table[0].dwIndex;
296 free(table);
297 *pcbResponseInfoLen = sizeof(ULONG) * 2;
299 break;
301 /* Returns connectionless network layer statistics--in our case,
302 * this is IP.
304 case CL_NL_ENTITY:
305 if (pcommand->toi_class == INFO_CLASS_GENERIC)
307 if (!pcbResponseInfoLen)
308 return ERROR_BAD_ENVIRONMENT;
309 *((ULONG *)pResponseInfo) = CL_NL_IP;
310 *pcbResponseInfoLen = sizeof(ULONG);
312 else if (pcommand->toi_class == INFO_CLASS_PROTOCOL)
314 if (!pcbResponseInfoLen)
315 return ERROR_BAD_ENVIRONMENT;
316 if (*pcbResponseInfoLen < sizeof(MIB_IPSTATS))
317 return ERROR_LOCK_VIOLATION;
318 GetIpStatistics((PMIB_IPSTATS)pResponseInfo);
320 *pcbResponseInfoLen = sizeof(MIB_IPSTATS);
322 break;
324 /* Returns connectionless transport layer statistics--in our case,
325 * this is UDP.
327 case CL_TL_ENTITY:
328 if (pcommand->toi_class == INFO_CLASS_GENERIC)
330 if (!pcbResponseInfoLen)
331 return ERROR_BAD_ENVIRONMENT;
332 *((ULONG *)pResponseInfo) = CL_TL_UDP;
333 *pcbResponseInfoLen = sizeof(ULONG);
335 else if (pcommand->toi_class == INFO_CLASS_PROTOCOL)
337 if (!pcbResponseInfoLen)
338 return ERROR_BAD_ENVIRONMENT;
339 if (*pcbResponseInfoLen < sizeof(MIB_UDPSTATS))
340 return ERROR_LOCK_VIOLATION;
341 GetUdpStatistics((PMIB_UDPSTATS)pResponseInfo);
342 *pcbResponseInfoLen = sizeof(MIB_UDPSTATS);
344 break;
346 /* Returns connection-oriented transport layer statistics--in our
347 * case, this is TCP.
349 case CO_TL_ENTITY:
350 if (pcommand->toi_class == INFO_CLASS_GENERIC)
352 if (!pcbResponseInfoLen)
353 return ERROR_BAD_ENVIRONMENT;
354 *((ULONG *)pResponseInfo) = CO_TL_TCP;
355 *pcbResponseInfoLen = sizeof(ULONG);
357 else if (pcommand->toi_class == INFO_CLASS_PROTOCOL)
359 if (!pcbResponseInfoLen)
360 return ERROR_BAD_ENVIRONMENT;
361 if (*pcbResponseInfoLen < sizeof(MIB_TCPSTATS))
362 return ERROR_LOCK_VIOLATION;
363 GetTcpStatistics((PMIB_TCPSTATS)pResponseInfo);
364 *pcbResponseInfoLen = sizeof(MIB_TCPSTATS);
366 break;
368 default:
369 ERR("Unknown entity %ld for ENTITY_TYPE_ID query",
370 pcommand->toi_entity.tei_entity);
372 break;
374 /* This call returns the IP address, subnet mask, and broadcast
375 * address for an interface. If there are multiple IP addresses for
376 * the interface with the given index, returns the "first" one.
378 case IP_MIB_ADDRTABLE_ENTRY_ID:
380 DWORD index = pcommand->toi_entity.tei_instance;
381 PMIB_IPADDRROW baseIPInfo = (PMIB_IPADDRROW) pResponseInfo;
382 PMIB_IPADDRTABLE table;
383 DWORD tableSize, i;
385 if (!pcbResponseInfoLen)
386 return ERROR_BAD_ENVIRONMENT;
387 if (*pcbResponseInfoLen < sizeof(MIB_IPADDRROW))
388 return (ERROR_LOCK_VIOLATION);
390 /* get entire table, because there isn't an exported function that
391 gets just one entry. */
392 tableSize = 0;
393 GetIpAddrTable(NULL, &tableSize, FALSE);
394 table = (PMIB_IPADDRTABLE)calloc(1, tableSize);
395 if (!table)
396 return ERROR_NOT_ENOUGH_MEMORY;
397 GetIpAddrTable(table, &tableSize, FALSE);
398 for (i = 0; i < table->dwNumEntries; i++)
400 if (table->table[i].dwIndex == index)
402 memcpy(baseIPInfo, &table->table[i],
403 sizeof(MIB_IPADDRROW));
404 break;
407 free(table);
409 *pcbResponseInfoLen = sizeof(MIB_IPADDRROW);
410 break;
413 /* This call returns the routing table.
414 * No official documentation found, even the name of the command is unknown.
415 * Work is based on
416 * http://www.cyberport.com/~tangent/programming/winsock/articles/wscontrol.html
417 * and testings done with winipcfg.exe, route.exe and ipconfig.exe.
418 * pcommand->toi_entity.tei_instance seems to be the interface number
419 * but route.exe outputs only the information for the last interface
420 * if only the routes for the pcommand->toi_entity.tei_instance
421 * interface are returned. */
422 case IP_MIB_ROUTETABLE_ENTRY_ID: /* FIXME: not real name. Value is 0x101 */
424 DWORD routeTableSize, numRoutes, ndx;
425 PMIB_IPFORWARDTABLE table;
426 IPRouteEntry *winRouteTable = (IPRouteEntry *) pResponseInfo;
428 if (!pcbResponseInfoLen)
429 return ERROR_BAD_ENVIRONMENT;
430 GetIpForwardTable(NULL, &routeTableSize, FALSE);
431 numRoutes = min(routeTableSize - sizeof(MIB_IPFORWARDTABLE), 0)
432 / sizeof(MIB_IPFORWARDROW) + 1;
433 if (*pcbResponseInfoLen < sizeof(IPRouteEntry) * numRoutes)
434 return (ERROR_LOCK_VIOLATION);
435 table = (PMIB_IPFORWARDTABLE)calloc(1, routeTableSize);
436 if (!table)
437 return ERROR_NOT_ENOUGH_MEMORY;
438 GetIpForwardTable(table, &routeTableSize, FALSE);
440 memset(pResponseInfo, 0, sizeof(IPRouteEntry) * numRoutes);
441 for (ndx = 0; ndx < table->dwNumEntries; ndx++)
443 winRouteTable->ire_addr = table->table[ndx].dwForwardDest;
444 winRouteTable->ire_index = table->table[ndx].dwForwardIfIndex;
445 winRouteTable->ire_metric =
446 table->table[ndx].dwForwardMetric1;
447 /* winRouteTable->ire_option4 =
448 winRouteTable->ire_option5 =
449 winRouteTable->ire_option6 = */
450 winRouteTable->ire_gw = table->table[ndx].dwForwardNextHop;
451 /* winRouteTable->ire_option8 =
452 winRouteTable->ire_option9 =
453 winRouteTable->ire_option10 = */
454 winRouteTable->ire_mask = table->table[ndx].dwForwardMask;
455 /* winRouteTable->ire_option12 = */
456 winRouteTable++;
459 /* calculate the length of the data in the output buffer */
460 *pcbResponseInfoLen = sizeof(IPRouteEntry) *
461 table->dwNumEntries;
463 free(table);
464 break;
468 default:
470 FIXME ("Command ID Not Supported -> toi_id=0x%lx, toi_entity={tei_entity=0x%lx, tei_instance=0x%lx}, toi_class=0x%lx\n",
471 pcommand->toi_id, pcommand->toi_entity.tei_entity,
472 pcommand->toi_entity.tei_instance, pcommand->toi_class);
474 return (ERROR_BAD_ENVIRONMENT);
478 break;
481 case WSCNTL_TCPIP_ICMP_ECHO:
483 unsigned int addr = *(unsigned int*)pRequestInfo;
484 #if 0
485 int timeout= *(unsigned int*)(inbuf+4);
486 short x1 = *(unsigned short*)(inbuf+8);
487 short sendbufsize = *(unsigned short*)(inbuf+10);
488 char x2 = *(unsigned char*)(inbuf+12);
489 char ttl = *(unsigned char*)(inbuf+13);
490 char service = *(unsigned char*)(inbuf+14);
491 char type= *(unsigned char*)(inbuf+15); /* 0x2: don't fragment*/
492 #endif
494 FIXME("(ICMP_ECHO) to 0x%08x stub \n", addr);
495 break;
498 default:
499 FIXME("Protocol Not Supported -> protocol=0x%lx, action=0x%lx, Request=%p, RequestLen=%p, Response=%p, ResponseLen=%p\n",
500 protocol, action, pRequestInfo, pcbRequestInfoLen, pResponseInfo, pcbResponseInfoLen);
502 return (WSAEOPNOTSUPP);
506 return (WSCTL_SUCCESS);
511 /***********************************************************************
512 * WSARecvEx (WSOCK32.1107)
514 * WSARecvEx is a Microsoft specific extension to winsock that is identical to recv
515 * except that has an in/out argument call flags that has the value MSG_PARTIAL ored
516 * into the flags parameter when a partial packet is read. This only applies to
517 * sockets using the datagram protocol. This method does not seem to be implemented
518 * correctly by microsoft as the winsock implementation does not set the MSG_PARTIAL
519 * flag when a fragmented packet arrives.
521 INT WINAPI WSARecvEx(SOCKET s, char *buf, INT len, INT *flags)
523 FIXME("(WSARecvEx) partial packet return value not set \n");
524 return recv(s, buf, len, *flags);
528 /***********************************************************************
529 * s_perror (WSOCK32.1108)
531 void WINAPI s_perror(LPCSTR message)
533 FIXME("(%s): stub\n",message);
534 return;