Thread: unknown socket error

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    28

    unknown socket error

    I am using the following piece of code for performing a connectionless packet sending to a remote server. The domain' s IP is found by pinging it with cmd. However, the sendto() function fails, whereas 0 socket error code is returned. As I cannot spot any mistake, could you please help me fix it?

    Code:
    IPHEADER ipHeader;
    char szSendbuf[60]={0}; 
    TCPHEADER tcpHeader; 
    PSDHEADER psdHeader;
    SOCKADDR_IN ssin;
    memset(&ssin, 0, sizeof(ssin));	
    ssin.sin_family=AF_INET; 
    ssin.sin_port=htons(0);
    ssin.sin_addr.s_addr=inet_addr(target_ip); 
    
    SOCKET ssock;
    ssock=socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
    BOOL flag = TRUE; 
    setsockopt(ssock, IPPROTO_IP, IP_HDRINCL, (char *)&flag, sizeof(flag));
    
    ipHeader.verlen=(4<<4 | sizeof(ipHeader)/sizeof(unsigned long)); 
    ipHeader.total_len=htons(sizeof(ipHeader)+sizeof(tcpHeader)); 
    ipHeader.ident=1; 
    ipHeader.frag_and_flags=0; 
    ipHeader.ttl=128; 
    ipHeader.proto=IPPROTO_TCP; 
    ipHeader.checksum=0; 
    ipHeader.sourceIP=my_ip;
    ipHeader.destIP=ssin.sin_addr.s_addr;
    tcpHeader.sport=htons((unsigned short)(rand()%1025));  
    tcpHeader.seq=htonl(0x12345678); 
    tcpHeader.flags=SYN;
    tcpHeader.lenres=(sizeof(tcpHeader)/4<<4|0); 
    tcpHeader.window=htons(512); 
    tcpHeader.urg_ptr=0; 
    tcpHeader.checksum=0;	
    psdHeader.saddr=ipHeader.sourceIP; 
    psdHeader.daddr=ipHeader.destIP; 
    psdHeader.zero=0; 
    psdHeader.proto=IPPROTO_TCP; 
    psdHeader.length=htons((unsigned short)(sizeof(tcpHeader))); 
    memcpy(szSendBuf, &psdHeader, sizeof(psdHeader)); 
    memcpy(szSendBuf+sizeof(psdHeader), &tcpHeader, sizeof(tcpHeader)); 
    tcpHeader.checksum=checksum((USHORT *)szSendBuf,sizeof(psdHeader)+sizeof(tcpHeader)); 
    memcpy(szSendBuf, &ipHeader, sizeof(ipHeader)); 
    memcpy(szSendBuf+sizeof(ipHeader), &tcpHeader, sizeof(tcpHeader)); 
    memset(szSendBuf+sizeof(ipHeader)+sizeof(tcpHeader), 0, 4); 
    ipHeader.checksum=checksum((USHORT *)szSendBuf, sizeof( ipHeader)+sizeof(tcpHeader)); 
    memcpy(szSendBuf, &ipHeader, sizeof(ipHeader)); 
    
    if (sendto(ssock, (char *)&szSendBuf, sizeof(szSendBuf), 0, (LPSOCKADDR)&ssin, sizeof(ssin)) == SOCKET_ERROR) {
    			closesocket(ssock);
    			printf("Error: %d\n", WSAGetLastError());
    }
    Last edited by heisel; 10-17-2009 at 08:25 AM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    first, tell us the platform your code is for. you said you get the ip using "cmd", which we could then assume your on windows, but its certainly possible your just using "cmd" to get the ip, then manually enter it into the program on a different computer which may or may not be windows. we might be able to find out the platform, from platform specific types or calls from your code, but thats basically telling us you dont want to help us help you ("here you go, fix it").

    next:
    Quote Originally Posted by http://msdn.microsoft.com/en-us/library/ms741580%28VS.85%29.aspx
    If a function call's return value indicates that error or other relevant data was returned in the error code, WSAGetLastError should be called immediately. This is necessary because some functions may reset the last extended error code to 0 if they succeed, overwriting the extended error code returned by a previously failed function
    Code:
    if (sendto(ssock, (char *)&szSendBuf, sizeof(szSendBuf), 0, (LPSOCKADDR)&ssin, sizeof(ssin)) == SOCKET_ERROR) {
    			closesocket(ssock);
    			printf("Error: %d\n", WSAGetLastError());
    }
    from the horses mouth in the quote above, its possible that the "closesocket" is resetting the error code to 0. so, as the document says, check the error code immediately after the failed call (i.e. before the "closesocket" line).

    also, after taking a quick look through the possible socket error codes on msdn, there are none that have value "0". this seems to confirm that your second call is overwriting the error code.
    Last edited by nadroj; 10-17-2009 at 12:15 PM.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    Sorry. You guessed right that the code is intended for Windows platforms. Now that I transposed the two lines, the error returned is #10004, which means "Interrupted system call". Any ideas for its nature and solution?

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    After a thorough Google search I found out that sending of raw data is disabled in XP SP2 and later. Is there any way around?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    sorry, ive never worked with sockets in windows, so i dont have any suggestions. i dont know what you mean by "sending of raw data", but if your trying to open a socket and send/receive stuff i dont think its true that its "disabled" or not possible.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    Quote Originally Posted by nadroj View Post
    sorry, ive never worked with sockets in windows, so i dont have any suggestions. i dont know what you mean by "sending of raw data", but if your trying to open a socket and send/receive stuff i dont think its true that its "disabled" or not possible.
    As you may have noticed, I opened the socket with SOCK_RAW parameter. This is needed in order to dig some levels lower and create the packets headers manually.

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Raw socket support in Windows is deliberately broken. Have you read the "Limitations on Raw Sockets" section of the following page:

    TCP/IP Raw Sockets (Windows)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    Quote Originally Posted by brewbuck View Post
    Raw socket support in Windows is deliberately broken. Have you read the "Limitations on Raw Sockets" section of the following page:

    TCP/IP Raw Sockets (Windows)
    Now I read it. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 02:58 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM