473,434 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

convert argv[] to u_int32_t and checking it.

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.

Mar 1 '06 #1
5 3810
"The Dark Free Soul" <th*************@gmail.com> wrote:
/* 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
strto*() - for unsigned longs, strtoul(). You need <stdlib.h> for those,
as well.
/* 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)
_is_ ISO C, but only C99, and only when <stdint.h> has been #included
and that type is defined for your implementation.
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
Mar 1 '06 #2
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

Mar 1 '06 #3

ma***********@gmail.com wrote:
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.

--
BR, Vladimir

Mar 1 '06 #4
#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

Mar 5 '06 #5
"The Dark Free Soul" <th*************@gmail.com> writes:
#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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Brad Moore | last post by:
Hey all, I'm getting the following compiler error from my code. I was wondering if anyone could help me understand the concept behind it (I actually did try and compile this degenerate...
9
by: mahurshi | last post by:
i have a quick question i am putting a debug flag in my program (i really dont need this feature, but i figured it might be useful when i get into trouble) so i want to check if argv is the...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
65
by: kyle.tk | last post by:
I am trying to write a function to convert an ipv4 address that is held in the string char *ip to its long value equivalent. Here is what I have right now, but I can't seem to get it to work. ...
11
by: cybernerdsx2 | last post by:
Hi, I have a program that check for the hex value that passes in from the command line. Let say user type in c:\demo.exe 3c and in the program, I #define SOME_CMD 0x3c. So how can I...
1
by: akk003 | last post by:
Hi, I have to convert to a hexadecimal file. I'am new to file handling so kindly review my code and tell me why does it crash! //As inputs, I use: // hexdump_c.cpp // gif.txt /* after this,...
17
by: Matt | last post by:
Hello. I've got a very strange problem. Basically I have a programme where I wish to view all the strings in the argv array so I can see what arguments are being passed to the programme. ...
7
by: amrich2000 | last post by:
Hi, I have a c++ code in which i have combined 2 u_int32_t data into a single u_int64_t data initially. Now, i need to split this u_int64_t data into their 2 corresponding u_int_32_t data?(I need...
22
by: xiao | last post by:
Can I fullfill this task? Using fred and fwrite?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.