LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2003, 05:12 AM   #1
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Rep: Reputation: 15
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
 
Old 12-12-2003, 10:15 AM   #2
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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)
 
Old 12-12-2003, 02:17 PM   #3
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
did you mean create your own packets including the headers?
 
Old 12-12-2003, 03:06 PM   #4
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
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.
 
Old 12-12-2003, 03:07 PM   #5
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
also, does anyone know the equivelant unix functions. Thanks
 
Old 12-12-2003, 03:24 PM   #6
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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...
 
Old 12-12-2003, 03:28 PM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
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.
 
Old 12-12-2003, 03:52 PM   #8
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
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?
 
Old 12-12-2003, 05:19 PM   #9
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
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.
 
Old 12-12-2003, 05:35 PM   #10
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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).
 
Old 12-13-2003, 12:38 AM   #11
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
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.
 
Old 12-13-2003, 12:13 PM   #12
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
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.
 
Old 12-13-2003, 02:32 PM   #13
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
I read the whole thing and I put together a program that talks to another one on my mac using sockets.
 
Old 12-14-2003, 05:21 PM   #14
ooagentbender
Member
 
Registered: Sep 2003
Distribution: Vector Linux
Posts: 110

Original Poster
Rep: Reputation: 15
so how do I put together packets?
 
Old 12-14-2003, 10:06 PM   #15
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
edit.

Last edited by infamous41md; 12-14-2003 at 10:08 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
UDP packets dont seem to go out! linuxspartan Linux - Networking 2 11-06-2004 09:55 PM
udp packets m00t00 Programming 1 11-04-2004 09:20 PM
encapsulating TCP packets in UDP packets... yoshi95 Programming 3 06-03-2004 02:53 PM
How to receive UDP and ICMP packets, by one UDP socket(PMTUD) myself_rajat Linux - Networking 0 05-28-2004 05:43 AM
UDP packets going nowhere jylhar Programming 0 01-05-2004 05:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration