Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending raw eth packets. when should i close sockets?

I implement bridge by taking packets from nfqeueue , open socket to the eth card and send the packets (i have some logic in the middle).

i am new to cpp and low level , so i might ask stupid questions.

if i understand correct i shouldn't open-close the socket for every packet. i write my code based on this example - http://austinmarton.wordpress.com/2011/09/14/sending-raw-ethernet-packets-from-a-specific-interface-in-c-on-linux/

i open socket like this:

sockfd = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW))

my question are: 1. for how long the socket alive? 2. how can i check if the socket i still open? 3. how do i close it? i saw the shutdown , but i didnt know if this is the API? 4. can someone direct me to an example in production level. means handling socket exception ...

Thank you

like image 403
Avihai Marchiano Avatar asked Dec 02 '25 02:12

Avihai Marchiano


1 Answers

Keep that socket open as long as you have frames to send or receive. Close the socket with normal close(2). Here's another raw sockets tutorial for you - http://www.tenouk.com/Module43a.html

shutdown(2) only makes sense with TCP, not at all relevant here.

Edit 0:

There's no connection, you are talking ethernet here.

Do yourself a favor and read this book - TCP/IP Illustrated, Volume 1: The Protocols. by W. Richard Stevens - will save you ton of confusion.

like image 58
Nikolai Fetissov Avatar answered Dec 03 '25 18:12

Nikolai Fetissov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!