AnsweredAssumed Answered

Iam facing a problem in sendto()....telling invalid arguments.

Question asked by Pradeep M P on Apr 21, 2016
Latest reply on Apr 26, 2016 by Carlos_Musich

#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

Outcomes