• C

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
raghu_vsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

makerpCommented:
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
0
raghu_vsAuthor Commented:
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
0
raghu_vsAuthor Commented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

raghu_vsAuthor Commented:
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
0
raghu_vsAuthor Commented:
sorry for the repeats...the page probably was not loaded properly!!
0
danieljngCommented:
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.
0
bkrahmerCommented:
What are you really trying to test?  The default configuration for most (all?) routers is to drop DHCP packets.  What kind of results do you expect to see?  

Writing a packet generator that would generate all permutations of legal DHCP packets could be quasi-interesting, but it would be hard to interpret the results, as I wouldn't expect to get any replies from your firewall.  

brian
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
YoungJiCommented:
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. =)
0
bkrahmerCommented:
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
0
YoungJiCommented:
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.
0
bkrahmerCommented:
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
0
YoungJiCommented:
>>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.
0
jmcgOwnerCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.