Hatem Mashaqi
Hatem Mashaqi

Reputation: 608

Sendto returns -1 and errno 22 (Invalid argument), when set the multicast outgoing interface by ip_mreqn

I faced an issue, when I try to send to multicast group by setting the intended outgoing interface by the code bellow, Actually when the condition is TRUE (if(config.enable_if == 1)) the sendto system call returns error Invalid Argument, but if the condition was False sendto send data and doesn't generate any error.

Please Anyone has an idea, or should I modify anything in my code?

    /* Create a datagram socket on which to send. */
    sd = socket(AF_INET, SOCK_DGRAM, 0);

    /* Set local interface for outbound multicast datagrams. */
    /* The IP address specified must be associated with a local */
    /* multicast capable interface. */
    if(config.enable_if == 1){
       mreqn.imr_ifindex = if_nametoindex("eth3");
       rc = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (void *)&mreqn, sizeof(mreqn));
     }

    /* Initialize the group sockaddr structure with a */
    /* group address of dynamic address and port dynamic port. */
    memset((char *) &groupSock, 0, sizeof(groupSock));
    groupSock.sin_family = AF_INET;
    groupSock.sin_addr.s_addr = inet_addr(config.mip);
    groupSock.sin_port = htons(config.port);


    /* Send a message to the multicast group specified by the*/
    /* groupSock sockaddr structure. */

    rc = sendto(sd, (const char *)& databuf, datalen, 0, (const struct sockaddr*)&groupSock, sizeof (struct sockaddr));
    printf("errno %d\n",errno);

Upvotes: 1

Views: 6220

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409166

One reason sendto fails is because you pass it a data pointer it does not expect. If you have char* databuf and you then do &databuf you get the address of the pointer, i.e. a pointer to a pointer, of type char**. If you remove the cast (which is not needed) then you will get at least a warning or maybe even an error when compiling.

Upvotes: 1

Related Questions