2

For example I am having two different segment IP of same server 172.17.8.90 172.19.8.100. Now, I want my program to send multicast from only 172.19.8.100 this ip. Currently my program is sending multicast from 172.17.8.90 which is not allowed to send multicast because of this, i want multicast data from specific IP

#include <stdio.h>      /* for fprintf() */
#include <sys/socket.h> /* for socket(), connect(), send(), and recv() */
#include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for sleep() */

void DieWithError(char *errorMessage)  /* External error handling function */
{
     fprintf(stderr,"Dieing baby\n");
}

int main(int argc, char *argv[])
{
  int sock;                         /* Socket */
  struct sockaddr_in multicastAddr; /* Multicast address */
  char *multicastIP;                /* IP Multicast address */
  unsigned short multicastPort;     /* Server port */
  char *sendString;                 /* String to multicast */
  unsigned char multicastTTL;       /* TTL of multicast packets */
  unsigned int sendStringLen;       /* Length of string to multicast */
  if ((argc < 4) || (argc > 5))         /* Test for correct number of parameters */
  {
     fprintf(stderr,"Usage:  %s <Multicast Address> <Port> <Send String> [<TTL>]\n",
     argv[0]);
     exit(1);
  }
  multicastIP = argv[1];            /* First arg:  multicast IP address */ 
  multicastPort = atoi(argv[2]);    /* Second arg:  multicast port */
  sendString = argv[3];             /* Third arg:  String to multicast */
  if (argc == 5)                     /* Is TTL specified on command-line? */
    multicastTTL = atoi(argv[4]);  /* Command-line specified TTL */
  else
    multicastTTL = 1;              /* Default TTL = 1 */
  /* Create socket for sending/receiving datagrams */
  if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
     DieWithError("socket() failed");
  /* Set TTL of multicast packet */
  if (setsockopt(sock, IPPROTO_IP, IP_MULTICAST_TTL, (void *) &multicastTTL, 
      sizeof(multicastTTL)) < 0)
      DieWithError("setsockopt() failed");
 /* Construct local address structure */
  memset(&multicastAddr, 0, sizeof(multicastAddr));   /* Zero out structure */
  multicastAddr.sin_family = AF_INET;                 /* Internet address family */
  multicastAddr.sin_addr.s_addr = inet_addr(multicastIP);/* Multicast IP address */
  multicastAddr.sin_port = htons(multicastPort);         /* Multicast port */
  sendStringLen = strlen(sendString);  /* Find length of sendString */
  for (;;) /* Run forever */
  {
    /* Multicast sendString in datagram to clients every 3 seconds */
    if (sendto(sock, sendString, sendStringLen, 0, (struct sockaddr *) 
          &multicastAddr, sizeof(multicastAddr)) != sendStringLen)
       DieWithError("sendto() sent a different number of bytes than expected");
       sleep(3);
  }
}
  • 1
    So bind the socket to that IP then. – Cheatah Feb 08 '20 at 10:37
  • You need to `bind()` your socket to 172.19.8.100 before entering your `sendto()` loop – Remy Lebeau Feb 08 '20 at 18:27
  • Yes i can use bind but for binding the socket i need to assign ip address "multicastAddr.sin_addr.s_addr" in this variable but in my current program i m assigning multicast ip to "multicastAddr.sin_addr.s_addr" in this variable. I m new to socket program can u please explain me how can i do it. – Nitesh Prabhat Feb 10 '20 at 06:55

1 Answers1

1

When sending out multicast packets, the OS will by default chose one particular interface to send from. To change the outgoing interface, you need to set the IP_MULTICAST_IF option:

struct in_addr multi_out;
multi_out.s_addr = inet_addr("172.19.8.100");
if (setsockopt(sock,IPPROTO_IP,IP_MULTICAST_IF, (char *)&multi_out, sizeof(multi_out)) == -1) {
    perror("error setting IP_MULTICAST_IF option");
    exit(1);
}

Also be sure to use the perror function as above for error checking to see why a particular library call failed.

dbush
  • 205,898
  • 23
  • 218
  • 273