Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

socket programming

Posted on 2003-03-26
14
Medium Priority
?
296 Views
Last Modified: 2008-03-06
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
0
Comment
Question by:raghu_vs
  • 4
  • 3
  • 3
  • +3
13 Comments
 
LVL 10

Expert Comment

by:makerp
ID: 8212621
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
 

Author Comment

by:raghu_vs
ID: 8214762
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
 

Author Comment

by:raghu_vs
ID: 8214764
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:raghu_vs
ID: 8214795
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
 

Author Comment

by:raghu_vs
ID: 8214799
sorry for the repeats...the page probably was not loaded properly!!
0
 

Expert Comment

by:danieljng
ID: 8215760
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
 
LVL 2

Accepted Solution

by:
bkrahmer earned 75 total points
ID: 8223060
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
 

Expert Comment

by:YoungJi
ID: 8237873
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
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8243011
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
 

Expert Comment

by:YoungJi
ID: 8243944
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
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8244041
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
 

Expert Comment

by:YoungJi
ID: 8244431
>>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
 
LVL 20

Expert Comment

by:jmcg
ID: 10194594
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question