LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Manufacturing UDP packets, how in the heck? (https://www.linuxquestions.org/questions/programming-9/manufacturing-udp-packets-how-in-the-heck-125167/)

ooagentbender 12-12-2003 05:12 AM

Manufacturing UDP packets, how in the heck?
 
I was just wondering how I would go about making a UDP packet and sending it out, I know C and C++ well and I know unix well enough to learn how if need be. Can anyone help me out here, I am trying to learn about computers and heres something I don't know about. Heck I would like to know how to make a packet period, not just UDP.

thanks

deiussum 12-12-2003 10:15 AM

A "packet" is kind of a loose term for a set of data. About the easiest way to create a "packet" is to create a struct. To send it out via UDP, you should look into sockets.

Just to give you an idea of where to start looking, here's a quick pseudo-code example:

Code:

typedef struct _packet
{
    int iSomeValue;
    int iSomeOtherValue;
} Packet;

int main()
{
  int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  SOCKADDR_IN addr;
  Packet p;

  // Setup a port to bind to
  addr.sin_family = AF_INET;
  addr.sin_addr.S_un.S_addr = INADDR_ANY;
  addr.sin_port = htons(9998);

  bind(sock, (SOCKADDR*)&addr, sizeof(SOCKADDR_IN));

  p.iSomeValue = 0;
  p.iSomeOtherValue = 1;

  // Define an address to send to
  addr.sin_addr.S_un.S_addr = inet_addr("192.168.1.167");
  addr.sin_port = htons(1812);
 
  // Send our packet to 192.168.1.167:1812
  sendto(sock, (char*)(void*)&packet, sizeof(Packet), (SOCKADDR*)&addr, sizeof(SOCKADDR_IN));

  return 0;
}

Note: Some of the above code was taken from a Windows app I wrote, so the constants might be a bit different. (For example, you probably need to use sockaddr_in rather than SOCKADDR_IN)

infamous41md 12-12-2003 02:17 PM

did you mean create your own packets including the headers?

ooagentbender 12-12-2003 03:06 PM

yes I meant creating your own packets including the headers. Where would I find documentation for all the functions that where called in that code. I get the feeling that when it comes to alot of that stuff its one of those situations in programing where it would take to long to go into all the function calls and you just have to take them ob faith.

ooagentbender 12-12-2003 03:07 PM

also, does anyone know the equivelant unix functions. Thanks

deiussum 12-12-2003 03:24 PM

To find info on the functions I used above use:

man socket
man 2 bind
man sendto
man inet_addr
man htons

That should give you some information to get started with socket programming...

deiussum 12-12-2003 03:28 PM

I know it may be taboo to recommend Microsoft references here, but you can also take a look at the Winsock documentation at MSDN. Winsock is derived from Berkley sockets, so it is pretty close to the same calls you would use on Unix. Not everything is exactly the same, but it's usually easy enough to figure out the differences.

ooagentbender 12-12-2003 03:52 PM

good stuff, and those are standardized so that when send a packet to a windows machine from unix it will know what to do correct?

ooagentbender 12-12-2003 05:19 PM

ok nother quick question while this thread is open how do you start a chargen service in lunix or unix? open a new one on say port 15000 or something like that.

infamous41md 12-12-2003 05:35 PM

i think you have alot of learning to do b4 you create your own packets, have a look here first
http://www.ecst.csuchico.edu/~beej/guide/net/ and learn about the sockets interface. a great book is unix network programming vol I by W Richard Stevens. after reading that you'll have a solid grasp of whats going on and can move onto advanced stuff like raw sockets(that is how you create your own packets).

ooagentbender 12-13-2003 12:38 AM

dood that sight is what I have been looking for a long time seriously it rocks big time. Anyway, so now my question is: when you call the send(...) functions I take it you would use a struct that would be pointed to by *msg, but in that struct if I wanted to send something like SSDP packet or something like it. Like here:

NOTIFY * HTTP/1.1
HOST: 239.255.255.250:1900
CACHE-CONTROL: max-age=1
LOCATION: URL
NT: urn: schemas-upnp-org:device:InternetGatewayDevice:1
NTS: ssdp:alive
SERVER: EEYE/2001 UPnP/1.0 PASSITON/1.1
USN: uuid:EEYE


how does that translate into a packet that I can make. Anyone have an places or sites where I can learn to put that info into a packet.

thanks you guys have been awesome.

infamous41md 12-13-2003 12:13 PM

eheh you gotta learn how sockets work before you go crafting your own packets. read the whole tutorial and i promise u'll understand after.

ooagentbender 12-13-2003 02:32 PM

I read the whole thing and I put together a program that talks to another one on my mac using sockets.

ooagentbender 12-14-2003 05:21 PM

so how do I put together packets?

infamous41md 12-14-2003 10:06 PM

edit.


All times are GMT -5. The time now is 08:06 AM.