Click here to Skip to main content
15,921,646 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralNo such property ... Pin
kydfru25-May-03 5:08
kydfru25-May-03 5:08 
AnswerRe: How to disable an initial selection in CEdit? Pin
peterchen25-May-03 8:15
peterchen25-May-03 8:15 
GeneralArray access like a snail Pin
Dominik Reichl25-May-03 3:27
Dominik Reichl25-May-03 3:27 
GeneralRe: Array access like a snail Pin
Nitron25-May-03 4:04
Nitron25-May-03 4:04 
GeneralRe: Array access like a snail - ?????????????? Pin
Dominik Reichl25-May-03 4:06
Dominik Reichl25-May-03 4:06 
GeneralRe: Array access like a snail Pin
Nick Parker25-May-03 4:17
protectorNick Parker25-May-03 4:17 
GeneralRe: Array access like a snail - !??? Pin
Dominik Reichl25-May-03 4:31
Dominik Reichl25-May-03 4:31 
Generalraw socket sendto() failure (WIN XP) Pin
Kuniva25-May-03 2:40
Kuniva25-May-03 2:40 
Hi, i found this code somewhere to open a raw socket under WIN XP and to send a packet with your own TCP/UDP header. The author posted it on a forum because the sendto() function failed at runtime. Someone then modified the code slightly (replaced sizeof() in sendto() to strlen()) and for them it worked, but i still get the same error. 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               "192.168.200.18"
#define PORT          123
#define IP_DEST          "192.168.200.19"
#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;

};


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 tcpheader *tcph = (struct tcpheader *) (datagram + (4*4));
    struct udpheader *udph = (struct udpheader *) (datagram + (4*4));
    struct sockaddr_in sin; //, sockTeste;

    /*sockTeste.sin_family = AF_INET;
    sockTeste.sin_port = htons(6000);
    sockTeste.sin_addr.s_addr = inet_addr ("192.168.200.18");

    if (bind(s, (SOCKADDR *)&sockTeste, sizeof(sockTeste)) == SOCKET_ERROR){
       printf("bind failed: %d\n", WSAGetLastError());
       return -1;
    }*/


    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      = 6;     // TCP
    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;

    
     // TCP Header
/*     tcph->th_sport      = sin.sin_port;
    tcph->th_dport      = htons (PORT_DEST);
    tcph->th_seq      = rand();
    tcph->th_ack      = 0;
    tcph->th_x2           = 0;
    tcph->th_off      = 0;
    tcph->th_flags      = 2; // SYN
    tcph->th_win      = htons(65535);
    tcph->th_sum      = 0;
    tcph->th_urp      = 0;*/


    // 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,"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");
         }
    //}

    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 = (pIpheader->ip_hl * 4);

    
     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>


Anyone know whats wrong here?
A possible clue might be: When the program runs and it displays all the info, for destination address it says "0.123.0.123" and i don't think thats right...
Also sendto() returns 10049, this is something to do with an address-not-available-error.

Thanks

Kuniva
--------------------------------------------
GeneralGetting the currently selected text in a CHtmlView Pin
Taka Muraoka25-May-03 1:09
Taka Muraoka25-May-03 1:09 
GeneralRe: Getting the currently selected text in a CHtmlView Pin
Michael Dunn25-May-03 1:32
sitebuilderMichael Dunn25-May-03 1:32 
GeneralList View Control Pin
Dennis L25-May-03 0:34
Dennis L25-May-03 0:34 
GeneralRe: List View Control Pin
Kuniva25-May-03 2:33
Kuniva25-May-03 2:33 
Generaltoolbar Pin
Drawil25-May-03 0:29
Drawil25-May-03 0:29 
GeneralRe: toolbar Pin
Michael Dunn25-May-03 1:35
sitebuilderMichael Dunn25-May-03 1:35 
GeneralRe: toolbar Pin
Drawil25-May-03 1:57
Drawil25-May-03 1:57 
GeneralOverruling WH_CBT hook behaviour Pin
S van Leent25-May-03 0:16
S van Leent25-May-03 0:16 
Generalabsolute path of the console is running Pin
liongqi24-May-03 22:37
liongqi24-May-03 22:37 
GeneralRe: absolute path of the console is running Pin
Michael Dunn25-May-03 1:38
sitebuilderMichael Dunn25-May-03 1:38 
QuestionHow to open a database without MFC ? Pin
zakarias24-May-03 21:58
zakarias24-May-03 21:58 
AnswerRe: How to open a database without MFC ? Pin
randal bryant24-May-03 23:17
randal bryant24-May-03 23:17 
AnswerRe: How to open a database without MFC ? Pin
John M. Drescher25-May-03 3:42
John M. Drescher25-May-03 3:42 
QuestionIf 1 = true, 0 =false, why return 0 = successful? Pin
Link260024-May-03 21:01
Link260024-May-03 21:01 
AnswerRe: If 1 = true, 0 =false, why return 0 = successful? Pin
Ryan Binns24-May-03 21:27
Ryan Binns24-May-03 21:27 
AnswerRe: If 1 = true, 0 =false, why return 0 = successful? Pin
Michael Dunn25-May-03 2:53
sitebuilderMichael Dunn25-May-03 2:53 
GeneralRe: If 1 = true, 0 =false, why return 0 = successful? Pin
Link260025-May-03 3:01
Link260025-May-03 3:01 

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.