Click here to Skip to main content
15,908,776 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: dialog created with DialogBox not receiving all messages Pin
Kibble31-May-03 11:26
Kibble31-May-03 11:26 
GeneralCEdit updates in dialog box Pin
rpadrela30-May-03 11:49
rpadrela30-May-03 11:49 
GeneralRe: CEdit updates in dialog box Pin
valikac30-May-03 13:24
valikac30-May-03 13:24 
GeneralRe: CEdit updates in dialog box Pin
Peter Weyzen30-May-03 21:49
Peter Weyzen30-May-03 21:49 
GeneralKeep the cursor inside a window Pin
Daniela12330-May-03 11:36
Daniela12330-May-03 11:36 
GeneralRe: Keep the cursor inside a window Pin
Michael Dunn30-May-03 11:50
sitebuilderMichael Dunn30-May-03 11:50 
GeneralRe: Keep the cursor inside a window Pin
Anonymous31-May-03 17:18
Anonymous31-May-03 17:18 
Generalraw packet with setsockopt() and sendto() Pin
Kuniva30-May-03 11:32
Kuniva30-May-03 11:32 
Can someone please explain why my code isn't working???
I'm trying to send a raw UDP packet with a self constructed header.
I posted something like this earlier but i investigated the code since then and changed a bit cause it seemed to contain some bugs. BUT the problem remains the same, the code fails at runtime because the sendto() function returns 10049
WSAEADDRNOTAVAIL: Cannot assign requested address. This can also result from connect, sendto!!!, WSAConnect, WSAJoinLeaf, or WSASendTo when the remote address or port is not valid for a remote computer (for example, address or port 0).

Does this mean something might be wrong with my sin structure???? I don't understand, how can it be wrong, i mean, why would it even be USED as it is a raw packet and i constructed the header with the destination ip and port myself!!?? WTF | :WTF: I really don't understand why it won't work, and its been bugging me for several days now so if anyone knows why it won't work, please let me know.

Here's the code:
<code>
#pragma comment( lib, "Ws2_32.lib" )

#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <conio.h>

#define IP               "172.27.2.15"
#define PORT          123
#define IP_DEST          "192.168.200.9"
#define PORT_DEST     123

#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define MAX_ADDR_LEN 16
#define MAX_HOSTNAME_LAN 255


USHORT checksum(USHORT *buffer, int size);
void checkPacket(char * pacote);

struct tcpheader {
    unsigned short int th_sport;
    unsigned short int th_dport;
    unsigned int th_seq;
    unsigned int th_ack;
    unsigned char th_x2:4;
    unsigned char th_off:4;
    unsigned char th_flags;
    unsigned short int th_win;
    unsigned short int th_sum;
    unsigned short int th_urp;
}; /* total tcp header length: 20 bytes (=160 bits) */


struct ipheader {
    unsigned char ip_hl:4, ip_v:4; /* this means that each member is 4 bits */
    unsigned char ip_tos;
    unsigned short int ip_len;
    unsigned short int ip_id;
    unsigned short int ip_off;
    unsigned char ip_ttl;
    unsigned char ip_proto;
    unsigned short int ip_sum;
    unsigned int ip_src;
    unsigned int ip_dst;
}; /* total ip header length: 20 bytes (=160 bits) */


struct udpheader {
    unsigned short int uh_sport;
    unsigned short int uh_dport;
    unsigned short int uh_len;
    unsigned short int uh_sum;

}; /* 8 bytes */


int main (void)
{
    WSADATA wsd;
    char datagram[5000];
    unsigned int bOpt=1;


    if (WSAStartup(MAKEWORD(2,2), &wsd) != 0){
       printf("WSAStartup() failed: %d\n", GetLastError());
       return -1;
    }

    // Create a raw socket
    SOCKET s = WSASocket(AF_INET, SOCK_RAW, IPPROTO_UDP, NULL, 0,0);

    if (s == INVALID_SOCKET){
       printf("WSASocket() failed: %d\n", WSAGetLastError());
       return -1;
    }

    // ENABLE IPHDRINCL
    if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&bOpt, sizeof(bOpt)) == SOCKET_ERROR){
       printf("setsockopt(IP_HDRINCL) failed: %d\n", WSAGetLastError());
       return -1;
    }

    struct ipheader *iph = (struct ipheader *) datagram;
    struct udpheader *udph = (struct udpheader *) (datagram + 20);
    struct sockaddr_in sin; //, sockTeste;



    sin.sin_family = AF_INET;
    sin.sin_port = htons (PORT_DEST);
    sin.sin_addr.s_addr = inet_addr (IP_DEST);

    memset (datagram, 0, sizeof(datagram));     /* zero out the buffer */


    // IP Header
    
    iph->ip_hl           = 5;
    iph->ip_v          = 4;
    iph->ip_tos           = 0;
    iph->ip_len           = sizeof (struct ipheader) + sizeof (struct udpheader);
    iph->ip_id           = 1;
    iph->ip_off           = 0;
    iph->ip_ttl           = 100;
    iph->ip_proto      = 17;     // UDP
    iph->ip_sum           = 0;
    iph->ip_src           = inet_addr(IP);
    iph->ip_dst           = inet_addr(IP_DEST); //sin.sin_addr.s_addr;

    
    // UDP Header

    udph->uh_sport = htons (PORT);
    udph->uh_dport = htons (PORT_DEST);
    udph->uh_len   = sizeof (struct ipheader) + sizeof (struct udpheader);
    udph->uh_sum   = 0;
    
     
    // Put something in the packet
    strcpy(datagram+28,"Teste");
	datagram[5000] = NULL;


    // Calculate Checksum

    iph->ip_sum  = checksum((unsigned short *)&iph, sizeof(ipheader));
    //tcph->th_sum = checksum((unsigned short *)&tcph, sizeof(tcpheader));
    udph->uh_sum = checksum((unsigned short *)&udph, sizeof(udpheader));

    // check the packet made
    checkPacket(datagram);

    //while (1)
    //{
         //Send The Packet

         if (sendto(s, datagram, strlen(datagram), 0, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
         {
              printf("sendto() failed: %d\n", WSAGetLastError());
              return -1;
         }
         else{
              printf("OK\n");
         }
         //if (sendto(s, datagram, strlen(datagram), 0, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
         //{
         //     printf("sendto() failed: %d\n", WSAGetLastError());
         //     return -1;
         //}
         //else{
         //     printf("NOPE\n");
         //}
    //}
	getch();
    return 0;
}



// IP/TCP/UDP Checksum Function

USHORT checksum(USHORT *buffer, int size)
{

   unsigned long cksum=0;
   while (size > 1)
   {
       cksum += *buffer++;
       size  -= sizeof(USHORT);   
    }
   if (size)
   {
       cksum += *(UCHAR*)buffer;   
    }
   cksum = (cksum >> 16) + (cksum & 0xffff);
   cksum += (cksum >>16); 
    return (USHORT)(~cksum); 
}

void checkPacket(char * buff)
{
         
    ipheader* pIpheader;
    tcpheader* pTcpheader;
    udpheader* pUdpheader;
    
     SOCKADDR_IN saSource, saDest;
    int iNextheader;
    char * packetData;

    pIpheader = (ipheader *) buff;

    //IP Header
    printf("IP Header \n");
    printf("IHL: %d\n",(int)pIpheader->ip_hl);
    printf("VERSION: %d\n",pIpheader->ip_v);
    printf("TYPE OF SERVICE: %d\n",(int)pIpheader->ip_tos);
    printf("TOTAL LENGTH: %d\n",pIpheader->ip_len);
    printf("IDENTIFICATION: %d\n",pIpheader->ip_id);
    printf("FRAGMENT OFFSET: %d\n",pIpheader->ip_off);
    printf("TTL: %d\n",(int)pIpheader->ip_ttl);
    printf("PROTOCOL: %d\n", (int)pIpheader->ip_proto);
    printf("CHECKSUM: %d\n",pIpheader->ip_sum);

    saSource.sin_addr.s_addr = pIpheader->ip_src;
    printf("SOURCE ADDRESS: %s\n",inet_ntoa(saSource.sin_addr));
    
    saDest.sin_addr.s_addr = pIpheader->ip_dst;
    printf("DESTINATION ADDRESS: %s\n",inet_ntoa(saDest.sin_addr));

    iNextheader = (20);

    
     if ((int)pIpheader->ip_proto == 6)
    {
         //TCP Header

         pTcpheader = (tcpheader *) (buff + iNextheader);

         printf("\nTCP Header\n");

         saSource.sin_port = pTcpheader->th_sport;
         printf ("SOURCE PORT: %d\n", htons(saSource.sin_port));
         
         saDest.sin_port = pTcpheader->th_dport;
         printf ("DESTINATION PORT: %d\n", htons(saDest.sin_port));
         
         printf ("SEQUENCE: %d\n", pTcpheader->th_seq);
         printf ("ACK: %d\n", pTcpheader->th_ack);
         printf ("X2: %d\n", (int)pTcpheader->th_x2);
         printf ("OFF SET: %d\n", (int)pTcpheader->th_off);
         printf ("FLAGS: %d\n", (int)pTcpheader->th_flags);
         printf ("WINDOW: %d\n", pTcpheader->th_win);
         printf ("CHECKSUM: %d\n", pTcpheader->th_sum);
         printf ("URP: %d\n", pTcpheader->th_urp);

         // Data

         printf("\nDATA\n");

         iNextheader = (pIpheader->ip_hl * 4 + sizeof(tcpheader));

         packetData = (char *) (buff + iNextheader);
         printf ("DATA: %s\n", packetData);
    
     }
    if ((int)pIpheader->ip_proto == 17)
    {
         
         printf("\nUDP Header\n");
         
         pUdpheader = (udpheader *) (buff + iNextheader);

         saSource.sin_port = pUdpheader->uh_sport;
         printf ("SOURCE PORT: %d\n", htons(saSource.sin_port));
         
         saDest.sin_port = pUdpheader->uh_dport;
         printf ("DESTINATION PORT: %d\n", htons(saDest.sin_port));
         
         printf ("LEN: %d\n", pUdpheader->uh_len);
         printf ("CHECKSUM: %d\n", pUdpheader->uh_sum);
         
         // DATA
         
         printf("\nDATA\n");

         iNextheader = (pIpheader->ip_hl * 4 + sizeof(udpheader));

         packetData = (char *) (buff + iNextheader);
         printf ("DATA: %s\n", packetData);
    }
    

      getch();
}
</code>


Thanks

Kuniva
--------------------------------------------
GeneralRe: raw packet with setsockopt() and sendto() Pin
JohnnyG30-May-03 18:34
JohnnyG30-May-03 18:34 
GeneralRe: raw packet with setsockopt() and sendto() Pin
Kuniva31-May-03 5:11
Kuniva31-May-03 5:11 
GeneralRe: raw packet with setsockopt() and sendto() Pin
JohnnyG31-May-03 13:20
JohnnyG31-May-03 13:20 
GeneralRe: raw packet with setsockopt() and sendto() Pin
Peter Weyzen30-May-03 22:01
Peter Weyzen30-May-03 22:01 
GeneralMySQL in a small app... Pin
Gnascher30-May-03 10:25
Gnascher30-May-03 10:25 
GeneralRe: MySQL in a small app... Pin
Robert Little30-May-03 11:12
Robert Little30-May-03 11:12 
GeneralRe: MySQL in a small app... Pin
Anonymous30-May-03 12:00
Anonymous30-May-03 12:00 
GeneralRe: MySQL in a small app... Pin
Robert Little30-May-03 18:21
Robert Little30-May-03 18:21 
GeneralRe: MySQL in a small app... Pin
Neville Franks30-May-03 13:29
Neville Franks30-May-03 13:29 
GeneralRe: MySQL in a small app... Pin
Gnascher30-May-03 14:20
Gnascher30-May-03 14:20 
GeneralRe: MySQL in a small app... Pin
Neville Franks31-May-03 0:21
Neville Franks31-May-03 0:21 
QuestionHow to get control(button) ID in runtime Pin
Roman Muntyanu30-May-03 10:22
Roman Muntyanu30-May-03 10:22 
AnswerRe: How to get control(button) ID in runtime Pin
David Crow30-May-03 10:57
David Crow30-May-03 10:57 
QuestionHow to highlight an item in a list control in report view? Pin
julych30-May-03 10:16
julych30-May-03 10:16 
AnswerRe: How to highlight an item in a list control in report view? Pin
skaanji30-May-03 10:49
skaanji30-May-03 10:49 
AnswerRe: How to highlight an item in a list control in report view? Pin
Michael Dunn30-May-03 11:32
sitebuilderMichael Dunn30-May-03 11:32 
QuestionWhat is the Address of the Active IE ? Pin
Anonymous30-May-03 9:18
Anonymous30-May-03 9:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.