How to set the MSS programatically.


 
Thread Tools Search this Thread
Top Forums Programming How to set the MSS programatically.
# 1  
Old 06-24-2004
How to set the MSS programatically.

I'm sending a SYN packet and now I want to specify the MSS (The maximum segment size option in a TCP packet ) through it.

How can I do that? Also, I'm not getting a SYN-ACK back from the remote host. TCPDUMP tells me that my packet is good and is a SYN. Then, why don't I get back the SYN-ACK? I know SYN-ACK is not received, since I check it with TCPdump.

Here is my code for sending the SYN
Quote:
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/time.h>/* struct timeval */
#include <unistd.h>
#include <string.h>
#include <time.h>

#define MYPORT 7777
#define REMOTEPORT 5555

int readn(int, void *, int);
unsigned short in_cksum(unsigned short *ptr, int nbytes);

int main(int argc, char * argv[])
{
int sock,sent, rcvd;
struct sockaddr_in sin;
unsigned short local_port;
unsigned short remote_port;
unsigned char protocol;
char * buffer;
char * in_buffer;
//char data[1452];
char *remote_ip_str, *local_ip_str;
int semantics = 0;
unsigned short buffer_size = 0;
const int tmp=1;
struct iphdr *ip_header;
struct tcphdr *tcp_header;

struct pseudo_header {
unsigned int source_address;
unsigned int dest_address;
unsigned char placeholder;
unsigned char protocol;
unsigned short tcp_length;
struct tcphdr tcp;
} pseudo;
/* Following two fields are added for calculating the checksum */

protocol = IPPROTO_TCP;
semantics = SOCK_RAW;

remote_ip_str="REMOTE_IP";
local_ip_str="MY_IP";
local_port = MYPORT;
remote_port = REMOTEPORT;

if((sock = socket(PF_INET, semantics, protocol)) < 0) {
perror("socket");
exit(1);
}

bzero((char *)& sin, sizeof(sin));
sin.sin_port = htons(local_port);

if ((bind(sock, (struct sockaddr *)& sin, sizeof(sin))) < 0) {
perror("bind");
exit(1);
}

setsockopt(sock, 0, IP_HDRINCL, &tmp, sizeof(tmp));

bzero((char *)& sin, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_port = htons(remote_port);
sin.sin_addr.s_addr = inet_addr(remote_ip_str);

buffer_size = sizeof(struct iphdr) + sizeof(struct tcphdr);

printf("buff %d\n", buffer_size);

srand(getpid());

buffer = (char *) malloc(buffer_size);

ip_header = (struct iphdr *) buffer;
ip_header->ihl = 5;
ip_header->version = 4;
ip_header->tos = 0;
ip_header->tot_len = htons(buffer_size);
ip_header->id = 0;
ip_header->ttl = 64;
ip_header->frag_off = 0x40;
ip_header->protocol = protocol;
ip_header->daddr = inet_addr(remote_ip_str);
ip_header->saddr = inet_addr(local_ip_str);
ip_header->check = in_cksum((unsigned short *)&ip_header, 20);

tcp_header=(struct tcphdr *)(buffer+sizeof(struct iphdr));
tcp_header->source = htons(local_port);
tcp_header->dest = htons(remote_port);
tcp_header->seq = rand()%time(NULL);
tcp_header->ack_seq = rand()%time(NULL);
tcp_header->res1 = 0;
tcp_header->window = htons(1000);
tcp_header->doff = 4;
tcp_header->syn = 1;
tcp_header->check = 0;

bzero(&pseudo, 12+sizeof(struct tcphdr));
pseudo.source_address = ip_header->saddr;
pseudo.dest_address = ip_header->daddr;
pseudo.placeholder = 0;
pseudo.protocol = IPPROTO_TCP;
pseudo.tcp_length = htons(sizeof( struct tcphdr));

bcopy((char *)tcp_header, (char *)&pseudo.tcp, sizeof(struct tcphdr));
tcp_header->check = in_cksum((unsigned short *)&pseudo,
12+ sizeof(struct tcphdr));

if((sent=sendto(sock, buffer, buffer_size, 0, (struct sockaddr *) &sin,
sizeof(sin))) < sizeof(buffer)) {
perror("sendto");
exit(1);
}
printf("Came here sent %d bytes \n",sent);
close(sock);

if((sock = socket(PF_INET, semantics, protocol)) < 0) {
perror("socket");
exit(1);
}
bzero((char *)& sin, sizeof(sin));
sin.sin_port = htons(local_port);
if ((bind(sock, (struct sockaddr *)& sin, sizeof(sin))) < 0) {
perror("bind");
exit(1);
}
return 0;
}

unsigned short in_cksum(u_short *buf, int nwords) {
unsigned long sum ;
u_short *w=buf;

for(sum=0; nwords > 0; nwords-=2)
sum += *w++;

sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~( sum & 0xffff ) ;
}
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SSH Programatically

Hi Experts, Need some help. I am trying below code programatically to login to ssh ip from a csv file. the below code logging into all servers and executing some command line syntax. But what I need is for example if i have 5 IP's ,if first one connects successfully and executes the... (3 Replies)
Discussion started by: onenessboy
3 Replies

2. IP Networking

TCP MSS Issue

Gretings! I have developed simple utility using Berkly sockets interface: So, what do I? $ ifconfig lo mtu 1500 $ make //run netcat $ ./target 127.0.0.1 5555 1460 1000000 #mss: 1460, loop sleep: 1s On other virtual terminal: $ netcat -l 127.0.0.1 -p 5555 On third virtual terminal: #... (1 Reply)
Discussion started by: denis4inet
1 Replies

3. HP-UX

[Solved] Not able to call a script from another programatically

Hi, I need to call a shell script from another. Let me explain you with an example. a1.sh and b1.sh are two scripts. a1.sh calls b1.sh internally. If i run a1.sh manually, it internally calls b1.sh but the problem is There is a process that calls this a1.sh at regular intervals,... (1 Reply)
Discussion started by: hadarapu
1 Replies

4. Solaris

Programatically read 'size' shown in top?

How can the 'size' of a process, that is shown by 'top', be read programatically? I'm fixing a memory leak in a large (20,000 lines) program. (The main.cpp is itself 7400 lines!). (3 Replies)
Discussion started by: douglaskbell
3 Replies

5. UNIX for Advanced & Expert Users

retrieve process state programatically

Assume I spawn a process on (csh) command line, like > du -a / >& /dev/null & which creates a process with id 1234. Now, I can suspend/resume that process with > kill -STOP 1234 > kill -CONT 1234 and can query the process state via 'jobs' or 'ps. How can I though query that state... (6 Replies)
Discussion started by: Andre_Merzky
6 Replies

6. Programming

Add IpAddress Through Programatically

Hi all, Can u give me some suggestions how to add Ip Address through Programmatically , Actually I done it in Windows But now I want same thing in Unix................................ Can Any body Help to me....... (6 Replies)
Discussion started by: ykmraju
6 Replies
Login or Register to Ask a Question