Click here to Skip to main content
15,916,398 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralGetting Assertion Failure Pin
Ph@ntom2-Mar-03 23:33
Ph@ntom2-Mar-03 23:33 
GeneralRe: Getting Assertion Failure Pin
KingsGambit3-Mar-03 0:02
KingsGambit3-Mar-03 0:02 
GeneralRe: Getting Assertion Failure Pin
KarstenK3-Mar-03 0:17
mveKarstenK3-Mar-03 0:17 
GeneralRe: Getting Assertion Failure Pin
Ph@ntom3-Mar-03 1:41
Ph@ntom3-Mar-03 1:41 
GeneralRe: Getting Assertion Failure Pin
Ph@ntom3-Mar-03 2:18
Ph@ntom3-Mar-03 2:18 
GeneralRe: Getting Assertion Failure Pin
Alexinuk3-Mar-03 4:13
Alexinuk3-Mar-03 4:13 
GeneralRe: Getting Assertion Failure Pin
Alexinuk3-Mar-03 2:57
Alexinuk3-Mar-03 2:57 
Generalcreate and send tcp packet Pin
summo2-Mar-03 23:29
summo2-Mar-03 23:29 
I am in trouble. I am trying hard to send my tcp packet on the network but the packet is not going. I am using this strategy to do it.

1. create tcp packet using tcp structure.
2. Send the Packet using "Send to" method.

Posting the complete Code for Console applicationCry | :((
#include "ip.h"
#define PORT 25

int main (void)
{

WSADATA wsd;
char datagram[4096];
int bOpt = 64;

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

// Create a raw socket

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

struct ipheader *iph = (struct ipheader *) datagram;
struct tcpheader *tcph = (struct tcpheader *) datagram + sizeof (struct ipheader);
struct sockaddr_in sin;

PS_HDR pseudo_header;

sin.sin_family = AF_INET;
sin.sin_port = htons (PORT);
sin.sin_addr.s_addr = inet_addr ("192.168.2.64");

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

iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof (struct ipheader) + sizeof (struct tcpheader);
iph->ip_id = 1;
iph->ip_off = 0;
iph->ip_ttl = 255;



iph->ip_p = 6;
iph->ip_sum = 0;
iph->ip_src = inet_addr ("192.168.2.61");
iph->ip_dst = sin.sin_addr.s_addr;

tcph->th_sport = htons (1234);
tcph->th_dport = htons (PORT);
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;

// Build the Psuedo Header

pseudo_header.source_address = inet_addr ("192.168.2.61");
pseudo_header.dest_address = sin.sin_addr.s_addr;
pseudo_header.placeholder = 0;
pseudo_header.protocol = IPPROTO_TCP;
pseudo_header.tcp_length = htons(sizeof(tcpheader));

// Calculate Checksum

tcph->th_sum = checksum((unsigned short *)&pseudo_header, sizeof(pseudo_header));
iph->ip_sum = checksum((unsigned short *)&iph, sizeof(ipheader));
// ENABLE IPHDRINCL

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

// Send The Packet
if (sendto(s, datagram, sizeof(datagram), 0, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("sendto() failed: %d\n", WSAGetLastError());
return -1;
}
printf("message sent");

return 0;
}

/*********************** ip.h header file *************************/

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

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, 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_p;
unsigned short int ip_sum;
unsigned int ip_src;
unsigned int ip_dst;
}; /* total ip header length: 20 bytes (=160 bits) */

// Psuedo Header

typedef struct ps_hdr
{
unsigned int source_address; // Source Address => 4 Bytes
unsigned int dest_address; // Destination Address => 4 Bytes
unsigned char placeholder; // Place Holder => 1 Bytes
unsigned char protocol; // Protocol => 1 Bytes
unsigned short tcp_length; // TCP Length => + 2 Bytes
// = 12 Bytes
struct tcpheader tcp;

}PS_HDR;

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

/*********************** ip.h header file *************************/

//The code is executing perfectly but the packet does not reach its destination.

//plz help me




Reply me soon
GeneralEncryption Pin
San2-Mar-03 23:17
San2-Mar-03 23:17 
GeneralEncryption Pin
San2-Mar-03 23:16
San2-Mar-03 23:16 
GeneralRe: Encryption Pin
karl_w3-Mar-03 0:56
karl_w3-Mar-03 0:56 
GeneralC++: Doing the right construction work Pin
tilli again2-Mar-03 23:03
susstilli again2-Mar-03 23:03 
GeneralRe: C++: Doing the right construction work Pin
jhwurmbach2-Mar-03 23:10
jhwurmbach2-Mar-03 23:10 
GeneralRe: C++: Doing the right construction work Pin
tilliagain3-Mar-03 0:10
susstilliagain3-Mar-03 0:10 
GeneralRe: C++: Doing the right construction work Pin
Alvaro Mendez3-Mar-03 4:37
Alvaro Mendez3-Mar-03 4:37 
Generalproblem on get the text from other window Pin
kbyiu2-Mar-03 20:20
kbyiu2-Mar-03 20:20 
GeneralRe: problem on get the text from other window Pin
Tony Chung : )2-Mar-03 20:24
Tony Chung : )2-Mar-03 20:24 
GeneralRe: problem on get the text from other window Pin
kbyiu2-Mar-03 22:07
kbyiu2-Mar-03 22:07 
Generalgprof on windows Pin
PrashantJ2-Mar-03 19:58
PrashantJ2-Mar-03 19:58 
GeneralRe: gprof on windows Pin
Neville Franks2-Mar-03 21:49
Neville Franks2-Mar-03 21:49 
GeneralRe: gprof on windows Pin
Johnny ²2-Mar-03 21:50
Johnny ²2-Mar-03 21:50 
GeneralRe: gprof on windows Pin
Steve S3-Mar-03 0:51
Steve S3-Mar-03 0:51 
GeneralError with ShellExecute(),WinExec() & CreateProcess() Pin
Hemant kulkarni2-Mar-03 19:36
Hemant kulkarni2-Mar-03 19:36 
GeneralRe: Error with ShellExecute(),WinExec() & CreateProcess() Pin
KarstenK2-Mar-03 21:31
mveKarstenK2-Mar-03 21:31 
GeneralRe: Error with ShellExecute(),WinExec() & CreateProcess() Pin
Neville Franks2-Mar-03 21:53
Neville Franks2-Mar-03 21:53 

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.