convert argv[] to u_int32_t and checking it.

  • Thread starter The Dark Free Soul
  • Start date
T

The Dark Free Soul

Moin everybody.
I'm writing a little application to take confidence with socket raw and
tcp protocol over ipv4.
According to tcp protocol sequence number must be a 32bit log number.
I do this (code follows) to read the sequence number as argument of the
main and to assign the value to the tcp.seq:

/* i get argv[5] from main(argc, argv[]) */
unsigned long seqn;
seqn = atoi(argv[5]);

/* in tcp header tcp.seq is a u_int32_t
* then i use htonl() to transform it to u_int32_t */
tcp.seq = htonl(seqn);

Then i build the tcp/ip packet and send it to my localhost. Sniffing
the packet with a network sniffer (ethereal or tcpdump) i see my
packet, everything is ok but the sequence number is completly wrong
(and terribly big, value "1" become comes around 36000000 or more).
I can't figure out where i'm wrong.
Many many thanks in advance to everybody will reply me.
 
R

Richard Bos

The Dark Free Soul said:
/* i get argv[5] from main(argc, argv[]) */
unsigned long seqn;
seqn = atoi(argv[5]);

This is the on-topic bit of your problem. Are you absolutely certain
that argv[5] contains the required number? Did you print it? Did you
remember to #include <stdlib.h> for atoi()? Did you print the converted
unsigned long as well? _Was_ it correct?

BTW, atoi() is rarely the best function to use, and using it on
unchecked CLPs is definitely a bad idea. For one thing, it causes
undefined behaviour on overflow; for another, it doesn't report back
when it finds something else than a number. A better idea is to use
/* in tcp header tcp.seq is a u_int32_t
* then i use htonl() to transform it to u_int32_t */
tcp.seq = htonl(seqn);

This bit is off-topic. ISO C does not define a htonl() function, and we
can't tell which standard your htonl() came from, if any. Also, you
haven't shown the actual declaration of tcp.seq, only what your comment
says it is; and you haven't shown the definition of an u_int32_t, which
isn't an ISO C type, either. uint32_t (note lack of first underscore)
Then i build the tcp/ip packet and send it to my localhost. Sniffing
the packet with a network sniffer (ethereal or tcpdump) i see my
packet, everything is ok but the sequence number is completly wrong
(and terribly big, value "1" become comes around 36000000 or more).

And this is far off-topic; for one, it may not even be a problem with
your code, but with the way you use the sniffer.

Richard
 
M

manochavishal

Hi,

I would appreciate if words like 'BTW' are expanded to their original
form.
Its really tough for some of us to really figure out what does these
words mean.

Cheers
Vishal
 
V

Vladimir S. Oka

Hi,

I would appreciate if words like 'BTW' are expanded to their original
form.
Its really tough for some of us to really figure out what does these
words mean.

GIYF ;-)

Some have a veeery long history, and have become second nature for
many.

You could also have a look into Jargon File
(http://www.catb.org/jargon/). It is both fun to read and instructive.
 
T

The Dark Free Soul

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define IPHDRSIZE sizeof(struct iphdr)
#define TCPHDRSIZE sizeof(struct tcphdr)

/* Some code... */
int main(int argc,char **argv)
{
unsigned long seqn, ackn;
struct tcphdr tcp;
seqn = strtoul(argv[5], NULL, 10);
ackn = strtoul(argv[6], NULL, 10);
tcp.seq = htonl(seqn);
tcp.ack_seq = htonl(ackn);
printf("Sequence number : %lu\n", tcp.seq);
printf("Sequence number : %lu\n", seqn);
printf("Acknowledgement number : %lu\n", tcp.ack_seq);
printf("Acknowledgement number : %lu\n", ackn);
return 0;
}

I run my app with argv[5] = 49 and argv[6] = 1.
Printf works fine (we assume the imput is numberic and correct), but if
sniffing the packet with ethereal i get:
sequence number = 1683153356
acknowledgment number = 1687969411

Any idea?? I'm getting crazy! Many many thanks in advance.

TDFS
 
K

Keith Thompson

The Dark Free Soul said:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <sys/socket.h>
#include <arpa/inet.h>

Either modify your program so it uses only standard C headers (of the
ones you use, only <stdio.h>, <stdlib.h>, <string.h>, and <time.h> are
defined in the C standard), or post to comp.unix.programmer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top