Search code examples
csocketsnetwork-programmingraw-sockets

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


Solution

  • 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.