Sending many packets Ethernet simultaneously

Sending many packets Ethernet simultaneously - C++ - Programmation

Marsh Posté le 07-05-2011 à 14:18:42    

Hello
 
i'am developping an application with Visual C++ 2010 but i have a problem.
i need to send many packets Ethernet simultaneously from application layer to physical layer (of course with construction of every packet in UDP,IP, and MAC layers), and in Mac layer there is a scheduler that arranges packets (coming from uper layer) in the same line to be send to the physical layer.
 
So i need to know how can i do this?
there is use of threads or sockets?  
i think sockets can be used only for communication between two hosts not in the same host (my case).
 
i hope some one can help me. thank you

Reply

Marsh Posté le 07-05-2011 à 14:18:42   

Reply

Marsh Posté le 07-05-2011 à 15:23:53    

Citation :

i think sockets can be used only for communication between two hosts not in the same host (my case)

No, you can have a client and a server on the same host.
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 07-05-2011 à 22:00:17    

how can i use it in my case? who is the server and who is the client?  
for exemple i want to send 3 messages simultaneously from application layer through 3 ports udp to the transport layer so i have to use 3 sockets?  

Reply

Marsh Posté le 07-05-2011 à 23:33:26    

If you don't understand what is a server and a client, how can you expect to program socket code?
 
You want to communicate data between two applications, each one on a machine (it may be the same machine or not)
One software will create a socket and will listen it, waiting for input to arrive. It is named "the server".
It must be running and listening, else, nothing will happen.
The other software will create a socket, try to connect it to the server socket, and if successful, will send some data to the server, to announce its arrival. It is named "the client".
Then data exchange can take place (following a common high level protocol)
 
What is the final destination of your three messages? in term of IP and Ports. The same IP and ports? Same IP and three different ports? This is very unclear from what you wrote.
 
A+,
 
 


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 09-05-2011 à 12:35:21    

thank you for your time gilou,i will explain to you my application. i really need your help so please be patient with me.
 
i read about socket but i don't know if i can use it in my application.
i will explain to you what i want to develop:
i want to send packets from application layer via many UDP ports simultaneously, and in UDP layer the header will be added and all calculs done.after, packets passed to IP layer which add the ip header and do calculs (it is a single ip destination adress not mutlicast adress).then packets passed to Mac layer where is a scheduler (MUX) which regulate all Mac frames to one voice (une seule voix ou une seule ligne) ready to be sent to physical layer with winpcap librairies. in the other hand there is a receiver (in my case i want to implement it in the same host with the transmitter) who will capture these frames from the physical layer with winpcap libraries and passes them to MAC layer where is a scheduler (DEMUX) then to ip layer to UDP layer to application layer.  
so i think about threads to use one transmit thread who will sends frames to the physical layer and receiver thread who will receive frames from physical layer to application layer.
but sockets, i thought that are used only in udp layer to udp layer of the server.
 
Cordialment

Reply

Marsh Posté le 09-05-2011 à 16:29:02    

Citation :

i want to send packets from application layer via many UDP ports simultaneously

OK

Citation :

and in UDP layer the header will be added and all calculs done.after, packets passed to IP layer which add the ip header and do calculs (it is a single ip destination adress not mutlicast adress).then packets passed to Mac layer where is a scheduler (MUX) which regulate all Mac frames to one voice (une seule voix ou une seule ligne) ready to be sent to physical layer with winpcap librairies.

This is basic stuff that we don't have to care about in socket programming.
 
A basic udp client on unix  
- creates a socket with a socket() call with the information on the socket type  
- allocates a struct sockaddr data structure with the information on the server address
- sends its data ( which is in a buffer) using the sendto() call
and that's all
 
A very minimal UDP client has the following structure:
( this is unix/linux code )

Code :
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <sys/types.h>
  4. #include <sys/socket.h>
  5. #include <netinet/in.h>
  6. #include <netdb.h>  // for gethostbyname
  7. int main(int argc, char *argv[])
  8. {
  9.     // these may be parameters
  10.     char *host = "localhost"; // or the machine name, IP...
  11.     char *port = "1043"; // a port value
  12.     // socket creation
  13.     int sd;  // socket descriptor
  14.     sd = socket(AF_INET, SOCK_DGRAM, 0);
  15.     if (sd < 0) {
  16.         perror("sock_init" );
  17.         exit(1);
  18.     }
  19.     // socket adress  structure initialisation
  20.     struct sockaddr_in in_name;
  21.     struct hostent *hptr;
  22.     memset((char *) &in_name, 0, sizeof(in_name));
  23.     in_name.sin_family = AF_INET;
  24.     in_name.sin_port = htons(port);
  25.     hptr = gethostbyname(host);
  26.     memcpy(&(in_name.sin_in_addr.s_addr), hptr->h_addr, hptr->h_length);
  27.    
  28.     // sending the data
  29.     char *data[256]; // must be less than 65507  
  30.     strcpy(data, "Hello world!" );
  31.     if (sendto(sd, data, strlen(data), &in_name, sizeof(in_name)) < 0) {
  32.         perror("sendto failed" );
  33.         close(sd);
  34.         exit(1);
  35.     }
  36.     close(sd);
  37.     exit(0);
  38. }


 
It sends the string "Hello world!" (non null terminated or else, change strlen(data) by strlen(data)+1 in the sendto call) to a server listening on the corresponding port on the local machine.
Of course, this will fail if you dont have a server running and listening incoming data on the port.
There may be typos in the previous code, I did not try to compile it (I dont have a unix/linus OS installed currently)
 
The windows equivalent code should be:

Code :
  1. #include <winsock2.h>
  2. #include <string.h>
  3. #pragma comment(lib, "ws2_32.lib" )
  4. int main()
  5. {
  6.     WSADATA WSAData;
  7.     // these may be parameters
  8.     char *host = "127.0.0.1"; // 127.0.0.1 is the local host, or use the machine name, IP...
  9.     char *port = "1043"; //
  10.     // windows specific socket initialisation
  11.     WSAStartup(MAKEWORD(2,0), &WSAData); // use version 2.0 of winsock
  12.     // socket creation
  13.     SOCKET sd;
  14.     sd = socket(AF_INET, SOCK_DGRAM, 0);
  15.     // socket adress  structure initialisation
  16.     SOCKADDR_IN in_name;
  17.     in_name.sin_family = AF_INET;
  18.     in_name.sin_port = htons(port);
  19.     in_name.sin_addr.s_addr = inet_addr(host);
  20.     // sending the data
  21.     char *data[256]; // must be less than 65507  
  22.     strcpy(data, "Hello world!" );
  23.     sendto(sd, data, strlen(data), &in_name, sizeof(in_name));
  24.     closesocket(sd);
  25.     // windows specific socket cleanup
  26.     WSACleanup();
  27.     return 0;
  28. }


 
You want to use three UDP ports, fine, just do as previously described with three different ports (and each within its own thread if you want concurrency in your program).
 
A+,


Message édité par gilou le 09-05-2011 à 17:24:29

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 10-05-2011 à 13:17:09    

thank you Gilou. i'm working on windows platform. i will start to develop like you said to me without threads,if it works i will make concurrency in my program. so,i will recap what i have understood: i must have one server for each UDP port which will receive the message (we are now in transport layer,no?).and after i will build my packet with headers and send it with another socket (in this case udp layer is the client) to the Mac layer (the server)?? in this level,i send packet with winpcap librairies to the Ethernet interface (according to its device).  
so did i understood well? if not, please explain to me more.
im sorry to bother you but i really need your help,i have to finish this project before two weeks and you see i'm still having much problems :(

Reply

Marsh Posté le 10-05-2011 à 13:45:18    

Citation :

i will build my packet with headers

Do you mean UDP packets? Why would you do that?

 

When you do this:
sd = socket(AF_INET, SOCK_DGRAM, 0);
You're telling the system that the data that you will write in the socket has to be encapsulated as a UDP packet.

 
Citation :

i must have one server for each UDP port which will receive the message

Or you must have a server listening on more than one port, here also, multithreading helps.

 

I don't know what you're trying to do, but if you have never programmed in client/server through sockets, or never programmed a multithreading application, I doubt that 2 weeks is enough to have a working program free of bugs.

 

A+,

 


Message édité par gilou le 10-05-2011 à 13:45:49

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 10-05-2011 à 15:29:20    


i want to send raw packets from application layer to physical layer.when passing by the transport layer i have to add UDP header ,in network layer i add IP header,in data link layer i add MAC layer.so my raw packets will be built to be frames ready for sending with winpcap librairies to physical layer.
 
i have developped a part of code that builds frames and adds all headers and calculs checksum.but my problem is not here.
my problem that i want to run this part of code with three udp ports numbers simultaneously,thats why i thought about threads and sockets. i hope you can understand my problem.please if you have a solution.
 
 
 
 


Message édité par sloumanaw le 10-05-2011 à 15:44:34
Reply

Marsh Posté le 10-05-2011 à 15:45:02    

Citation :

i want to send raw packets from application layer

If you do all that by hand, you dont need sockets at all!
All you have to do is to buid up your packets (eventually in parallel threads) and send them using pcap_sendpacket or pcap_sendqueue_transmit.
UDP port number is just a value that you set in the UDP packet header, this should not require any specific handling.
A+,


Message édité par gilou le 10-05-2011 à 15:46:58

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 10-05-2011 à 15:45:02   

Reply

Marsh Posté le 10-05-2011 à 15:46:01    

i will do my best to finish this program in two weeks.it will be hard but i hope i will have good results

Reply

Marsh Posté le 10-05-2011 à 15:51:51    

ok then.i think you understand exactly what i want to do :)
i have prepared the code to build my packets and i know about winpcap functions. my only problem is how can i use threads? do i have to use one thread to transmit or many threads as the number of UDP port used? means one thread for each port? and please if you have some links that can help me. thank soooo much gilou for your time

Reply

Marsh Posté le 10-05-2011 à 15:54:45    

[:clem104:2]


---------------
brisez les rêves des gens, il en restera toujours quelque chose...  -- laissez moi troller sur discu !
Reply

Marsh Posté le 10-05-2011 à 15:56:10    

if UDP port number is just a value in UDP header so how can we send raw packet from application layer to UDP layer without port.it can be communication port or SAP port.each communication port is assigned to an UDP port.

Reply

Marsh Posté le 10-05-2011 à 15:56:14    

Writing raw UDP packets is easy, the hard part will be multitasking.
If you have a lot of incoming data that you need to split and schedule for sending through UDP (eg real time voice processing), with some data dropping when some delay for sending is exceeded, this may be tricky.  
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 10-05-2011 à 16:00:27    

You dont send raw packet from application layer to UDP layer. :heink:  
If your packet is built in your application layer as a UDP packet, with IP pseudo header, you give it to the raw layer (ie the wincap library), as it is a raw packet ready for sending.
A+,


Message édité par gilou le 10-05-2011 à 16:00:54

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 10-05-2011 à 16:07:04    

raw packets will be send to transport layer via communication ports,each one is assigned to one UDP port. i will not use so many incoming data to split or schedule for sending through UDP.i will only have some tests with this project so i think i need some handling between ports that all. i need your help in the use of multithread. so please can you explain to me how can i use threads? do i have to use one thread to transmit or many threads as the number of UDP ports (communication ports) used? means one thread for each port?

Reply

Marsh Posté le 10-05-2011 à 16:08:27    

so i do not need even communication ports?

Reply

Marsh Posté le 10-05-2011 à 16:11:25    

in the specifications of the project there is communication ports between udp layer and application layer.if it is not necessary i will not use it. if it will be more simple without so i wil no use :)

Reply

Marsh Posté le 10-05-2011 à 16:15:17    

sloumanaw a écrit :

in the specifications of the project there is communication ports between udp layer and application layer.if it is not necessary i will not use it. if it will be more simple without so i wil no use :)

Communication ports? That does not makes any sense.
What is your UDP layer in your specification? some independant software or service?
A+,
 


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 10-05-2011 à 16:25:09    

my UDP layer is the same for Ethernet and model OSI. but communication ports is another service. but i don't have problem with it.i only have problem with the use of threads in multithreading application.so please can you help my only in this point, i know that i asked you so many questions but please how can i use threads? do i have to use one thread to transmit or many threads as the number of UDP port used? means one thread for each port? and please if you have some links that can help me.
thak you so much.

Reply

Marsh Posté le 10-05-2011 à 16:35:27    

Citation :

my UDP layer is the same for Ethernet and model OSI. but communication ports is another service.

This does not tell me anything. My question was clear.
What do you call your UDP layer?
Is it a set of functions in your software? Is it a service running in the background? or else?
 
No I can't help you on the multitasking aspect, as a correct usage will depend on the kind and rate of data you have to packetize, if you can drop packets, etc etc, and I have no idea about it.
A+,
 


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 10-05-2011 à 16:44:25    

my UDP layer is a set of functions in my software.
thank you very much for your time gilou,i will try to cherche about multithreading applications :)

Reply

Marsh Posté le 10-05-2011 à 16:57:47    

Citation :

my UDP layer is a set of functions in my software

Therefore, speaking of port does not make sense,as you will communicate data to the function through a buffer or a buffer address.
A+,


---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 10-05-2011 à 17:03:33    

yes you are right :)

Reply

Marsh Posté le 10-05-2011 à 17:08:23    

please another question gilou,if we want to send these packets with a rate.  means BAG=2ms Bandwidth Allocation Gap, between every two consecutive frames

Reply

Marsh Posté le 10-05-2011 à 17:58:40    

It seems (winpcap is unclear on this) that the timestamp value is taken into account when the sync parameter of pcap_sendqueue_transmit is set:

Citation :

sync determines if the send operation must be synchronized: if it is non-zero, the packets are sent respecting the timestamps, otherwise they are sent as fast as possible.


This is pretty unclear, so you will have to find out how exactly it works.
But if you add packets in a queue with timesamps incremented by the BAG, this may work.
A+,


Message édité par gilou le 10-05-2011 à 17:59:00

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
Reply

Marsh Posté le 10-05-2011 à 18:47:30    

ok thank you i will try it :)

Reply

Marsh Posté le    

Reply

Sujets relatifs:

Leave a Replay

Make sure you enter the(*)required information where indicate.HTML code is not allowed