Search code examples
csocketsunixnetworkingethernet

Raw sockets in C, isn't connect redundant?


I'm writing a simple program that creates an ethernet I frame and sends it through an interface to the specified MAC.

As i have read, the process for connecting to a socket in UNIX goes a bit like:

int sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
struct sockaddr_ll sll;
/* populate sll with the target and interface info */
connect(sockfd, (struct sockaddr*)&sll, sizeof(sll));
write(sockfd, stuff, sizeof(stuff));
close(sockfd)

The thing is, for me, stuff is a valid eth frame already containing everything needed to send a packet to its destination. Isn't the connect step redundant then? What am I missing?

Have a nice day.


Solution

  • Not only is the connect "redundant", it is an error -- according to the Linux man page:

    The connect(2) operation is not supported on packet sockets.

    So the connect is probably failing but not actually doing anything. Since you ignore the return value of connect, you don't notice the failure.