views:

354

answers:

3

Hi!

I found more source codes which are working like ping. My only problem with them is, that if i run the program with a non administrative user, then i get back errorcode 10013 which means : "An attempt was made to access a socket in a way forbidden by its access permissions." If i run the program with a user which is member of the administrator goup then it's working fine.

nResult = sendto (sock, pSendBuffer, sizeof (ICMPheader) + nMessageSize, 0, (SOCKADDR *)&dest, sizeof (SOCKADDR_IN));  
     ::GetSystemTime (&timeSend);  
     ++nPacketsSent;  
     if (nResult == SOCKET_ERROR)  
     {    
     cerr << endl << "An error occured in sendto operation: "  << "WSAGetLastError () = " << WSAGetLastError () << endl;  
                  }

Can anyone help me to solve this problem, or tell me why can't a non administrator user use this code? If not, then i would appriciate some code, which i can use with a user which isn't member of the administrator group.

Thanks in advance!

kampi

+2  A: 

Is your socket of type SOCK_RAW? In that case this is by design:

RAW Socket Access Denied to Non-Admin Windows NT 4.0 and Windows 2000 Users

Anders Lindahl
+1  A: 

by any chance, is your code using a raw socket ? if this code really implements a PING request, then it should be using raw sockets.

due to security considerations (people that considered those considerations did not consider much, but that's for another post), raw sockets are only available for accounts which have administrator privileges.

Adrien Plisson
so in other words, one can issue a "ping" command only while being an administrator? Seems like the world works a bit differently
Rom
Malkocoglu
@Malkocoglu: there is no version/service pack of Windows where you need to be an administrator to run 'ping' command. There is no equivalent of "setuid" flags in Windows, since the security on Windows is implemented on the user level (programs have the rights of the user they're running under). Note that the question has the "windows" tag
Rom
@Rom: cmeerw has the right answer. I just disassembled the ping.exe and saw that it first tries to find/execute the mentioned function, if not successfull, it then creates a RAW socket and does the ping manually. So in fact, the behaviour changes by Windows version...
Malkocoglu
IcmpSendEcho2 is part of the IPHelper API, which looks like an interface to a system service (which also manages ARP, DHCP, routing...). that explains why this function call is available to all users contrary to raw socket. and yes, behaviour changes depending on Windows version: raw sockets appeared around 2001, ping was there long before.
Adrien Plisson
+1  A: 

If you want to implement ping functionality in your application on Windows, then you should have a look at the IcmpSendEcho2 function instead of trying to use raw sockets.

cmeerw
You are right! This would be much easier, but i need icmplib.lib to compile my program with ICMPSendEcho2, but i can't found icmplib.lib anywhere. Do you know where can i get it? Thank is advance!
kampi
You mean iphlpapi.lib? This should be included with your compiler (it certainly is with Visual Stdio 2008). Alternatively, you can download the Windows SDK (see http://msdn.microsoft.com/en-us/windows/bb980924.aspx)
cmeerw
No, i meant icmplib.lib. But it was my mistake:) I ususally use Dev-C++ which hasn't recognized ICMPSendEcho2, because it needed icmplib.lib, but i couldn't find it, so i compiled with Visual Studio, and this one doesn't needed the icmplib.lib. My mistake, sorry :)
kampi
no, you really mean Icmpapi.h and Iphlpapi.lib. really...
Adrien Plisson