#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include <netinet/ether.h>
#include <linux/if_packet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h> //for sockets
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include <linux/if_ether.h> //for ETH_P_802_2
#include <linux/if_packet.h> //for sockaddr_ll
#include <linux/filter.h>
#include <sys/types.h> //for sendto
#include "stp_bpdu.h"
struct bpdu_format
{
int bridge_id;
int port_id;
};
int main ()
{
struct bpdu_format bpdu;
struct sockaddr_in sin;
struct in_addr localInterface;
int fd;
bpdu.bridge_id=6969;
bpdu.port_id=04;
fd = socket(PF_PACKET, (int)SOCK_RAW, htons(ETH_P_802_2)); /* open socket */
if(fd==-1)
perror("socket failed to open\n");
else
printf("socket successfully created\n");
memset((char *) &sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr("239.168.140.22");
localInterface.s_addr = inet_addr("192.168.15.69");
if(setsockopt(fd,SOL_SOCKET, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0)
{
perror("Setting local interface error\n");
exit(1);
}
else
printf("Setting the local interface...OK\n");
if(sendto(fd,(struct bpdu *)&bpdu, sizeof(struct bpdu_format), 0, (struct sockaddr *)&sin, sizeof(sin))<0)
{
perror("Sending message error");
}
else
printf("Sending message...OK\n");
close(fd);
return 0;
}
output:
socket successfully created....
Setting the local interface...OK
Sending message error: Invalid argument
Hi Pradeep,
please see the attached client and server examples. You can check here how to create and use sockets.
Regards,
Carlos
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi
I think you need to check the input parameters of sendto function.
I guess , you need to check the socket type, one socket type is PF_PACKET, the other is AF_INET,
fd = socket(PF_PACKET, (int)SOCK_RAW, htons(ETH_P_802_2))
....
sin.sin_family = AF_INET;
Regards
Daniel