Reputation: 512
I am trying to send a ICMPV6 message to remote network, as an initial step I tried to create a ICMPv6 socket in a simple class (SendICMPv6.c
) which contains the winsock2
, ws2tcpip
, stdio
and stdlib
headers. But I am unable to create socket. Could someone help What's wrong?
The code is:
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
wprintf(L"WSAStartup failed: %d\n", iResult);
return 1;
}
int fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
if (fd < 0) {
perror("creating socket failed");
}
Can someone figure out the problem?
Upvotes: 0
Views: 529
Reputation: 595961
SOCK_RAW
requires admin rights. Is your app running in an elevated state?
When socket()
fails, use WSAGetLastError()
to find out why, eg:
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}
SOCKET fd = socket(AF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
if (fd == INVALID_SOCKET) {
printf("creating socket failed: %d\n", WSAGetLastError());
return 1;
}
Upvotes: 1
Reputation:
If you read the documentation carefully, there are few options you can check:
Can your OS create SOCK_RAW
sockets?
If a Winsock service provider supports SOCK_RAW sockets for the AF_INET or AF_INET6 address families, the socket type of SOCK_RAW should be included in the WSAPROTOCOL_INFO structure returned by WSAEnumProtocols function for one or more of the available transport providers.
Are you running as an Administrator?
Therefore, only members of the Administrators group can create sockets of type SOCK_RAW on Windows 2000 and later.
You need to add more error checks in your code. For instance, if socket()
fails:
If no error occurs, socket returns a descriptor referencing the new socket. Otherwise, a value of INVALID_SOCKET is returned, and a specific error code can be retrieved by calling WSAGetLastError.
Upvotes: 2