Click here to Skip to main content
16,005,491 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Need Help Auto-Locking NT4.0 Workstation Pin
geo_m4-May-03 23:40
geo_m4-May-03 23:40 
GeneralMeeting trouble when sending TCP package. Pin
George24-May-03 19:16
George24-May-03 19:16 
GeneralRe: Meeting trouble when sending TCP package. Pin
geo_m4-May-03 21:28
geo_m4-May-03 21:28 
GeneralRe: Meeting trouble when sending TCP package. Pin
George24-May-03 23:08
George24-May-03 23:08 
GeneralRe: Meeting trouble when sending TCP package. Pin
geo_m4-May-03 23:34
geo_m4-May-03 23:34 
GeneralRe: Meeting trouble when sending TCP package. Pin
George25-May-03 4:12
George25-May-03 4:12 
GeneralRe: Meeting trouble when sending TCP package. Pin
geo_m5-May-03 9:53
geo_m5-May-03 9:53 
GeneralRe: Meeting trouble when sending TCP package. Pin
George25-May-03 19:06
George25-May-03 19:06 
Thanks, geo_m buddy!

I have written a simple program to send SYN. It works good when the target machine port number is 25 (SMTP). But when I change the port number to 5678
(you can see this statement #define PORT 5678 from the source code), I find the target machine automatically sending back a RST ACK package.

I do not want this to happen. Smile | :) For I want to simulate TCP's three time shake hands procudure. If the target machine sending back the package automatically for me, I will have no work to do. Smile | :)

How can I disable the RST ACK package sending by the target machine? Can you help?

I have a snapshot of Ethereal which recording the communication procedures.
If you want it, I can send it to you. For I do not know how to post an attachment of the post. Smile | :)

Here are the source code:

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

#pragma comment(lib, "ws2_32.lib")

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) */

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

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);
}
#define PORT 5678

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

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 ps_hdr *pseudo_header = (struct ps_hdr *) (datagram + 8);

struct sockaddr_in sin;

sin.sin_family = AF_INET;
sin.sin_port = htons (PORT);
sin.sin_addr.s_addr = inet_addr ("210.73.46.79"); // TARGET ADDRESS MAKE SURE IT DOES EXIST
memset (datagram, 0, 4096); /* zero out the buffer */

pseudo_header->source_address = inet_addr ("210.73.46.75"); // YOUR ADDRESS, CAN BE SPOOFED
pseudo_header->dest_address = sin.sin_addr.s_addr;
pseudo_header->placeholder = 0;
pseudo_header->protocol = 6;
pseudo_header->tcp_length = htons(20);

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 = 5;
tcph->th_flags = 2; // SYN
tcph->th_win = htons(65535);
tcph->th_sum = 0;
tcph->th_urp = 0;

tcph->th_sum = checksum((unsigned short *) (datagram + 8), 32);

iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = htons(40);
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 ("210.73.46.75"); // YOUR ADDRESS, CAN BE SPOOFED
iph->ip_dst = sin.sin_addr.s_addr;

iph->ip_sum = checksum((unsigned short *) datagram, 20);

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

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

return 0;
}
--------


regards,
George
GeneralRe: Meeting trouble when sending TCP package. Pin
geo_m5-May-03 22:47
geo_m5-May-03 22:47 
GeneralRe: Meeting trouble when sending TCP package. Pin
George26-May-03 1:52
George26-May-03 1:52 
GeneralRe: Meeting trouble when sending TCP package. Pin
geo_m6-May-03 2:22
geo_m6-May-03 2:22 
GeneralRe: Meeting trouble when sending TCP package. Pin
George26-May-03 2:58
George26-May-03 2:58 
GeneralRe: Meeting trouble when sending TCP package. Pin
geo_m6-May-03 4:23
geo_m6-May-03 4:23 
GeneralRe: Meeting trouble when sending TCP package. Pin
George26-May-03 20:31
George26-May-03 20:31 
GeneralRe: Meeting trouble when sending TCP package. Pin
geo_m7-May-03 4:29
geo_m7-May-03 4:29 
GeneralRe: Meeting trouble when sending TCP package. Pin
George27-May-03 20:54
George27-May-03 20:54 
GeneralRe: Meeting trouble when sending TCP package. Pin
geo_m7-May-03 21:34
geo_m7-May-03 21:34 
GeneralRe: Meeting trouble when sending TCP package. Pin
George28-May-03 2:09
George28-May-03 2:09 
GeneralRe: Meeting trouble when sending TCP package. Pin
geo_m15-May-03 23:49
geo_m15-May-03 23:49 
QuestionHow to make the ComboBox's list box part horizontal scrollable?. Pin
psusong4-May-03 19:08
psusong4-May-03 19:08 
AnswerRe: How to make the ComboBox's list box part horizontal scrollable?. Pin
psusong4-May-03 19:14
psusong4-May-03 19:14 
GeneralI'd like to change some string that user selected by mouse in a HTML viewer Pin
Member 3487054-May-03 19:00
Member 3487054-May-03 19:00 
Generaldynamic dialog Pin
Member 3487054-May-03 18:59
Member 3487054-May-03 18:59 
GeneralRe: dynamic dialog Pin
Joan M4-May-03 22:04
professionalJoan M4-May-03 22:04 
GeneralRe: dynamic dialog Pin
Member 3487055-May-03 2:38
Member 3487055-May-03 2:38 

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.