I’m new using sockets.
I must implement under linux environment, in user space, the LACP protocol. Each computer sends periodically for each ethernet interface a control message, a structure LACPDUs.
What would be the best family of sockets to get them to communicate(RAW, PACKET, TCP, UDP)? The socket send / receive must be of the same type?
My application already sends well the LACPDUs, but the application on the other side does not receive them ( I was capturing with wireshark, it captures the packets, but dont reach the application).
This is how i created the sockets:
Send paquet: (this works ok, even without binding)
int sock, sent;
struct sockaddr sa;
if (sock = socket(AF_INET, SOCK_PACKET, htons(ETH_P_SLOW))<0) //sockfd = socket(int socket_family, int socket_type, int protocol);
{perror("error socketsalida\n");
exit(EXIT_FAILURE);}
sa.sa_family = AF_INET;
strcpy(sa.sa_data, iface);
if((sent = sendto(sock, data, len, 0, &sa, sizeof(sa))) <= 0)
{perror("error sendto\n");
exit(EXIT_FAILURE);}
close(sock);
Receive packet: (this doesnt work)
int received, sockrec;
struct sockaddr sa;
struct LACPDU buffer;
socklen_t addrlen = sizeof (sa);
sockrec = socket(AF_INET, SOCK_PACKET, htons(ETH_P_SLOW));
if (sockrec<0) {perror("Error receiver socket\n");exit(EXIT_FAILURE);}
if ((received = recvfrom(sockrec, &buffer, BUFLEN, 0, (struct sockaddr *)&sa, &addrlen)) < 0)
{perror("Errorrecvfrom\n");exit(EXIT_FAILURE);}
close (sockrec);
Any ideea? Thanks.
Since you need to send Layer 2 packets, packet sockets are the correct choice. Beware that the
AF_INET/SOCK_PACKET-combination you use has been deprecated a long time ago. UseAF_PACKETas the socket domain and eitherSOCK_RAWorSOCK_DGRAMas the type. Using thesendto/recvfrom-calls is the right way to go about this, but your initializion is mostly wrong.I suggest you take a look at existing code (like this) that uses packet sockets to see how it can be done. I also recommend a look at the manpage
packet(7)(see here).