INFO: Header and Library Requirement When Set/Get Socket Options at the IPPROTO_IP Level
来源:互联网 发布:淘宝小二招聘条件 编辑:程序博客网 时间:2024/05/01 16:53
Article ID : 257460 Last Review : July 11, 2005 Revision : 1.2
This article was previously published under Q257460
SUMMARY
When setting or getting socket options at the IPPROTO_IP level in a C/C++ Winsock application targeted for Windows NT, Windows 2000, Windows 95, or Windows 98, including the correct header and library file in the program project is critical. If the header and library files are not properly matched, setsockopt or getsockopt may fail with runtime error 10042 (WSAENOPROTOOPT). In certain cases, even if the API returns successfully, the option value that you set or get may not be what you would have expected.
To avoid this problem, observe the following guidelines: • A program that includes Winsock.h should only link with Wsock32.lib. • A program that includes Ws2tcpip.h may link with either Ws2_32.lib or Wsock32.lib. Note that Ws2tcpip.h must be explicitly included after Winsock2.h in order to use socket options at this level.
To avoid this problem, observe the following guidelines: • A program that includes Winsock.h should only link with Wsock32.lib. • A program that includes Ws2tcpip.h may link with either Ws2_32.lib or Wsock32.lib. Note that Ws2tcpip.h must be explicitly included after Winsock2.h in order to use socket options at this level.
Back to the top
MORE INFORMATION
The IPPROTO_IP level socket options are defined differently in Winsock.h than in Ws2tcpip.h. In Winsock.h, the definitions are the following:
In contrast, the options at this level are defined in Ws2tcpip.h as:
If you do not correctly match the header and library files, setsockopt or getsockopt may fail with runtime error 10042 (WSAENOPROTOOPT) or the option value you set or get may not be as you would have expected.
Assume that you intend to join a multicast group by running code similar to the following:
Note that this includes Winsock.h. If the project is linked with Ws2_32.lib, setsockopt will fail with runtime error 10042 (WSAENOPROTOOPT). This is because in Winsock.h, IP_ADD_MEMBERSHIP is defined as "5". The corresponding Winsock runtime can not resolve option 5 at the IPPROTO_IP level, so the failure occurs with error code 10042.
/* * Options for use with [gs]etsockopt at the IP level. */ #define IP_OPTIONS 1 /* set/get IP per-packet options */ #define IP_MULTICAST_IF 2 /* set/get IP multicast interface */ #define IP_MULTICAST_TTL 3 /* set/get IP multicast timetolive */ #define IP_MULTICAST_LOOP 4 /* set/get IP multicast loopback */ #define IP_ADD_MEMBERSHIP 5 /* add an IP group membership */ #define IP_DROP_MEMBERSHIP 6 /* drop an IP group membership */ #define IP_TTL 7 /* set/get IP Time To Live */ #define IP_TOS 8 /* set/get IP Type Of Service */ #define IP_DONTFRAGMENT 9 /* set/get IP Don't Fragment flag */ #define IP_DEFAULT_MULTICAST_TTL 1 /* normally limit m'casts to 1 hop */ #define IP_DEFAULT_MULTICAST_LOOP 1 /* normally hear sends if a member */ #define IP_MAX_MEMBERSHIPS 20 /* per socket; must fit in one mbuf */
In contrast, the options at this level are defined in Ws2tcpip.h as:
/* Option to use with [gs]etsockopt at the IPPROTO_IP level */ #defineIP_OPTIONS1 /* set/get IP options */ #defineIP_HDRINCL2 /* header is included with data */ #defineIP_TOS3 /* IP type of service and preced*/ #defineIP_TTL4 /* IP time to live */ #defineIP_MULTICAST_IF9 /* set/get IP multicast i/f */ #defineIP_MULTICAST_TTL 10 /* set/get IP multicast ttl */ #defineIP_MULTICAST_LOOP 11 /*set/get IP multicast loopback */ #defineIP_ADD_MEMBERSHIP 12 /* add an IP group membership */ #defineIP_DROP_MEMBERSHIP 13/* drop an IP group membership */ #define IP_DONTFRAGMENT 14 /* don't fragment IP datagrams */
If you do not correctly match the header and library files, setsockopt or getsockopt may fail with runtime error 10042 (WSAENOPROTOOPT) or the option value you set or get may not be as you would have expected.
Case 1. Runtime Error 10042 (WSAENOPROTOOPT)
Options that fall into this category include:IP_ADD_MEMBERSHIPIP_DROP_MEMBERSHIPIP_TTLIP_TOS
Assume that you intend to join a multicast group by running code similar to the following:
#include <stdio.h>#include <stdlib.h>#include <winsock.h>int main(int argc, char* argv[]){ ... ... ... if (setsockopt(sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char FAR *)&mreq, sizeof (mreq)) == SOCKET_ERROR) { printf ("setsockopt failed: %d"), WSAGetLastError()); closesocket (sock); return FALSE; } ... ... ...}
Note that this includes Winsock.h. If the project is linked with Ws2_32.lib, setsockopt will fail with runtime error 10042 (WSAENOPROTOOPT). This is because in Winsock.h, IP_ADD_MEMBERSHIP is defined as "5". The corresponding Winsock runtime can not resolve option 5 at the IPPROTO_IP level, so the failure occurs with error code 10042.
Case 2. Options Set or Get Appear Do Not Take Effect
Options that fall into this category include:IP_MULTICAST_IFIP_MULTICAST_TTLIP_MULTICAST_LOOPIP_DONTFRAGMENT
Take IP_MULTICAST_TTL as an example. In Winsock.h, IP_MULTICAST_TTL is defined as "3". In ws2tcpip.h, the constant is defined as "10" and IP_TOS is defined as "3". Try to change the default TTL value: #include <stdio.h>#include <stdlib.h>#include <winsock.h>int main(int argc, char* argv[]){ int ttl = 7 ; // Arbitrary TTL value. ... ... ... source_sin.sin_family = AF_INET; source_sin.sin_port = htons(0); source_sin.sin_addr.s_addr = htonl (INADDR_ANY); if (bind(sock, (struct sockaddr FAR *)&source_sin, sizeof(source_sin)) == SOCKET_ERROR) { printf ("bind() failed: %d"), WSAGetLastError()); closesocket (sock); return FALSE; } if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl, sizeof(ttl))) == SOCKET_ERROR) { printf ("setsockopt failed: %d"), WSAGetLastError()); closesocket (sock); return FALSE; } ... ... ...}
If you link the project with Ws2_32.lib and run the application, setsockopt with IP_MULTICAST_TTL will succeed. However, the multicast TTL setting will not take effect. If you examine the network trace, you see that the TTL value still remains as "1" (the default). Back to the top
REFERENCES
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
124876 (http://support.microsoft.com/kb/124876/EN-US/) INFO: Sockets Applications on Microsoft Windows Platforms
138268 (http://support.microsoft.com/kb/138268/EN-US/) BUG: Winsock Sends IP Packets with TTL 0
131978 (http://support.microsoft.com/kb/131978/EN-US/) HOWTO: Receive/Send Multicasts Under WinNT/Win95 Using WinSock
Back to the top
APPLIES TO
• Microsoft Platform Software Development Kit-January 2000 EditionBack to the top
Keywords:
kbinfo kbwinsock kbapi kbnetwork kbdswnet2003swept KB257460Back to the top
- INFO: Header and Library Requirement When Set/Get Socket Options at the IPPROTO_IP Level
- Option to use with setsockopt at the IPPROTO_IP level
- IP-level Socket Options
- socket udp client port get and set
- How to get and set the system socket buffer in AIX?
- IPPROTO_IP Option Level
- The X-Frame-Options response header
- The X-Frame-Options response header
- unable to get system library for the project" error when import android project to eclips
- How to set trace for others sessions, for your own session and at instance level
- Linux: Where to set hostname and how to get IP info, as well as how to set DNS
- Get Local IP Address and Set in the IP Control
- How to get a notification from Linux when the set of network interfaces changes
- When and Why the Superclass Method Has a Different Access Level
- Test Library Requirement
- Agent Login–Level and DN-Level Options
- Set&Get and MACRO
- C# get and set
- 真正开始锻炼动手能力
- 浅谈内存泄漏
- 钩子要点
- 计算器之父——冯·诺依曼
- 用UDP实现可靠文件传输
- INFO: Header and Library Requirement When Set/Get Socket Options at the IPPROTO_IP Level
- 表的连接操作
- 有关递归的若干例子收集
- Wap 购物网站使用说明
- Java疑惑点解析(二)
- 关于二叉树中的递归小结
- 常用sql语句收集
- iframe自动自适应高度
- 生日快乐,我对自己说!...(^_^)