ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I'm trying to send a TCP segment .When I try and send the segemtn and print out the values on the client side evrything seems ot be properly filled but when I start EThereal and have a look at the packet it shows "Malformed Packet" and stops immediately after looking at TCP sport and TCP dest port . EVen the values it takes for the source and est port are wrong . AFter a bit of work I found otu that the values its taking is the hex equivalent of the entire segment which is wrong .Heres what I'm talking about:-
Code:
memcpy(segment, h.conn->seg->ttcp_hdr, 20);
memcpy(segment+20, h.conn->seg->options, strlen((char *)h.conn->seg->options)
);
memcpy(segment+20+strlen((char *)h.conn->seg->options), h.conn->seg->buff, st
rlen((char *)h.conn->seg->buff));
int n = sendto(sockfd, &segment, strlen((char *)segment), 0, toAddr, toAddrle
n);
//cout<<"N is "<<n<<"and segment is "<<segment<<endl;
printf("Segment is %x",segment); //THE OUTPUT HERE IS Segment is 8058e68 which is what is getting displayed in ethereal.....
for(int i=0; i < 43; i++)
{
unsigned char *name = new unsigned char;
memcpy(name, segment+i, i);
printf("\nbuffer in cli is %c, %x, %s", *(segment+i), *(segment+i), n
ame);
delete name;
}
Otherwise the segment prints perfectly...any ideas.....I can give anyone whois intrstd the whole code...
Thnx
Arvind
Distribution: At home: Arch, OpenBSD, Solaris. At work: CentOS, Debian, Ubuntu
Posts: 3,625
Rep:
I suppose it could be a byte ordering problem. IIRC network byte ordering is big endian and if you're on an x86 processor the native byte ordering is little endian. Do the values look "backwards" (i.e. the bytes in the value are reversed?)? If so, it's byte ordering.
You should look to htonl, ntohl, and friends for byte order conversion.
Your code also has a problem with heap corruption.
unsigned char *name = new unsigned char;
memcpy(name, segment+i, i);
You are allocating space for only 1 char, but copying 'i' chars into it. Which is a number from 0-42.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.