Link to home
Start Free TrialLog in
Avatar of raghu_vs
raghu_vs

asked on

socket programming

Hi
I am writing a packet generator, to test our local firewall.
Idea is to generate xxx number(say 1000) of DHCP (all kinds, including request, reply packets) packets.

For this I created a struct to hold the DHCP packet format data.

My question is:
Can I simply do sendto(socketid, &struct, sizeof(struct))
i.e. can I just push the stucture thru the socket (socketid).
Will this amount to DHCP packets being generated and sent?


Thanks
Raghu
Avatar of Paul Maker
Paul Maker
Flag of United Kingdom of Great Britain and Northern Ireland image

you can but this will just test the IP side of your firewall, remeber there are lots of other protocols such as IPX etc that are now programmed through sockets. although tcp ip is the most common
Avatar of raghu_vs
raghu_vs

ASKER

Hi
I am sorry..but I didnt quite understand ur reply.
I just want to test if the firewall is able to see dhcp packets generated on port 67/68 by my client application.

I have created a UDP socket on port 67 and 68 (DHCP)


Thanks
Raghu
Hi
I am sorry..but I didnt quite understand ur reply.
I just want to test if the firewall is able to see dhcp packets generated on port 67/68 by my client application.

I have created a UDP socket on port 67 and 68 (DHCP)


Thanks
Raghu
Hi
I am sorry..but I didnt quite understand ur reply.
I just want to test if the firewall is able to see dhcp packets generated on port 67/68 by my client application.

I have created a UDP socket on port 67 and 68 (DHCP)


Thanks
Raghu
sorry for the repeats...the page probably was not loaded properly!!
You have the right idea, but don't use structs. Use unions instead. Read up on unions in any good C book. Also, read up on sockets- you'll need the AF_INET type of socket. Google "Unix Network Programming".

Good luck and enjoy- it's fun stuff! :)


Cheers,
Dan.
ASKER CERTIFIED SOLUTION
Avatar of bkrahmer
bkrahmer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can send a struct through a socket but when I did this in my socket programming, I've noticed that using struct will always send data in size that is a multiple of 4.

In the following case, sizeof(struct data) will be 4:

struct data{
  char c;
  }

In this case also, sizeof(struct data) will be 4.

struct data{
  char c[2];
  }

In the following case, sizeof(struct data) will be 8.

struct data{
  char c[7];
  }

And so on...

So you will have to ignore these extra bytes on the receiving end.

With some error-checking, of course, using struct can prove to be robust. But as suggested by danieljng, just use union. =)
YoungJi, incorrect assumptions again, my friend.  IIRC, the sizeof operator on a struct will return at least the size of a pointer.  On some platforms, this is 32 bits, but not all!  Packing directives can also change the behavior.  You are best off not making assumptions.

brian
I realize I forgot to mention that implementation is platform-dependent. But the question simply asks whether sending struct will be OK, so I was just suggesting possible errors.
It's because you are not writing the correct code.  If you want to send a TCP packet with a one byte payload, you may.  Personally, I would specially form the structure so that I made no assumptions and that all of my data types were correct on all platforms I was working with.  Secondly, I would not use sizeof(struct) for the size passed to sendto, I would use the specific size of the data to be sent.

I was merely pointing out that 'sizeof(struct data) will be 4' is a false generalization.  Such false generalizations are very dangerous to use around inexperienced programmers.  That is one way people learn bad habits (like programming in C for example.) :)

brian
>>Personally, I would specially form the structure so that I made no assumptions and that all of my data types were correct on all platforms I was working with.  

... so sending struct is OK.

>>Secondly, I would not use sizeof(struct) for the size passed to sendto, I would use the specific size of the data to be sent.

Try receiving this:

struct data{
   char c;
   int i;
   char s[12];
   }

Maybe you will see what I mean.
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by bkrahmer [grade B] (asker's original question not fully answered).

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer