src/mk_mdns_client.c
static int mk_create_mcast_enpoint(char *ipv4)
{
    int sd = -1;
    int rc = -1;
    int ttl = 32;
    struct sockaddr_in addr = {0};
    struct ip_mreq ipm = {0};

    if ((!ipv4) || (strlen(ipv4) == 0)) {
       // MK_LOG(LOG_ERR, "%s() ipv4 is empty \n", __func__);
       addr.sin_addr.s_addr = htonl(INADDR_ANY);
    }
    else {
      addr.sin_addr.s_addr = inet_addr(ipv4);
    }
    ipm.imr_interface.s_addr = addr.sin_addr.s_addr;

    /*
     * create an endpoint for communication 
     */
    sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sd < 0) {
       MK_LOG(LOG_ERR, "%s() socket() failed. err=%d(%s) \n", __func__,errno,strerror(errno));
       return -1;
    }

    /*
     * bind a name to the socket
     */
    addr.sin_family = AF_INET;
    rc = bind(sd, (struct sockaddr *)&addr, sizeof(addr));
    if (rc < 0) {
       MK_LOG(LOG_ERR, "%s() bind() failed. err=%d(%s) \n", __func__,errno,strerror(errno));
       close(sd);
       return -1;
    }

// The following are needed only if we need to be a multicast listener

    /* 
     * Set the local device for a multicast socket -- using IP_MULTICAST_IF socket option
     */
     rc = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, &addr.sin_addr, sizeof(struct in_addr));
     if (rc < 0) {
        MK_LOG(LOG_ERR, "%s() set IP_MULTICAST_IF failed. err=%d(%s) \n", __func__,errno,strerror(errno));
        close(sd);
        return -1;
     }

    /* 
     * Join to a multicast group -- using IP_ADD_MEMBERSHIP socket option
     */
     ipm.imr_multiaddr.s_addr = inet_addr(MK_MDNS_MULTICAST_ADDR);
     rc = setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &ipm, sizeof(struct ip_mreq));
     if (rc < 0) {
        MK_LOG(LOG_ERR, "%s() set IP_ADD_MEMBERSHIP failed. err=%d(%s) \n", __func__,errno,strerror(errno));
        close(sd);
        return -1;
     }

    /*
     *  Set  the time-to-live value of outgoing multicast packets for this socket
     *  TTL of 1 are restricted to the same subnet
     *  TTL of 32 are restricted to the same site
     */
     rc = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl,sizeof(ttl));
     if (rc < 0) {
        MK_LOG(LOG_ERR, "%s() set IP_MULTICAST_TTL failed. err=%d(%s) \n", __func__,errno,strerror(errno));
        close(sd);
        return -1;
     }

     MK_LOG(LOG_INFO, "%s() multicast socket=%d setup success \n", __func__,sd);

     return sd;
}
/*
  mk_create_mcast_enpoint() creates a mdns multicast udp socket enpoint for 
  communication and returns the socket descriptor on success and -1 on failure
*/
mk_create_mcast_enpoint() creates a mdns multicast udp socket enpoint for
communication and returns the socket descriptor on success and -1 on failure
mk_create_mcast_enpoint () creates a mdns multicast udp socket enpoint for communication and returns the socket descriptor on success and - 1 on failure
static int mk_create_mcast_enpoint(char *ipv4)
{
    int sd = -1;
    int rc = -1;
    int ttl = 32;
    struct sockaddr_in addr = {0};
    struct ip_mreq ipm = {0};
    if ((!ipv4) || (strlen(ipv4) == 0)) {
       addr.sin_addr.s_addr = htonl(INADDR_ANY);
    }
    else {
      addr.sin_addr.s_addr = inet_addr(ipv4);
    }
    ipm.imr_interface.s_addr = addr.sin_addr.s_addr;
    sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
    if (sd < 0) {
       MK_LOG(LOG_ERR, "%s() socket() failed. err=%d(%s) \n", __func__,errno,strerror(errno));
       return -1;
    }
    addr.sin_family = AF_INET;
    rc = bind(sd, (struct sockaddr *)&addr, sizeof(addr));
    if (rc < 0) {
       MK_LOG(LOG_ERR, "%s() bind() failed. err=%d(%s) \n", __func__,errno,strerror(errno));
       close(sd);
       return -1;
    }
     rc = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, &addr.sin_addr, sizeof(struct in_addr));
     if (rc < 0) {
        MK_LOG(LOG_ERR, "%s() set IP_MULTICAST_IF failed. err=%d(%s) \n", __func__,errno,strerror(errno));
        close(sd);
        return -1;
     }
     ipm.imr_multiaddr.s_addr = inet_addr(MK_MDNS_MULTICAST_ADDR);
     rc = setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &ipm, sizeof(struct ip_mreq));
     if (rc < 0) {
        MK_LOG(LOG_ERR, "%s() set IP_ADD_MEMBERSHIP failed. err=%d(%s) \n", __func__,errno,strerror(errno));
        close(sd);
        return -1;
     }
     rc = setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL, (char *)&ttl,sizeof(ttl));
     if (rc < 0) {
        MK_LOG(LOG_ERR, "%s() set IP_MULTICAST_TTL failed. err=%d(%s) \n", __func__,errno,strerror(errno));
        close(sd);
        return -1;
     }
     MK_LOG(LOG_INFO, "%s() multicast socket=%d setup success \n", __func__,sd);
     return sd;
}
static int mk_create_mcast_enpoint ( char * ipv4 ) { int sd = -1 ; int rc = -1 ; int ttl = 32 ; struct sockaddr_in addr = { 0 } ; struct ip_mreq ipm = { 0 } ; if ( ( ! ipv4 ) || ( strlen ( ipv4 ) == 0 ) ) { addr . sin_addr . s_addr = htonl ( INADDR_ANY ) ; } else { addr . sin_addr . s_addr = inet_addr ( ipv4 ) ; } ipm . imr_interface . s_addr = addr . sin_addr . s_addr ; sd = socket ( AF_INET , SOCK_DGRAM , IPPROTO_IP ) ; if ( sd < 0 ) { MK_LOG ( LOG_ERR , " \n " , __func__ , errno , strerror ( errno ) ) ; return -1 ; } addr . sin_family = AF_INET ; rc = bind ( sd , ( struct sockaddr * ) & addr , sizeof ( addr ) ) ; if ( rc < 0 ) { MK_LOG ( LOG_ERR , " \n " , __func__ , errno , strerror ( errno ) ) ; close ( sd ) ; return -1 ; } rc = setsockopt ( sd , IPPROTO_IP , IP_MULTICAST_IF , & addr . sin_addr , sizeof ( struct in_addr ) ) ; if ( rc < 0 ) { MK_LOG ( LOG_ERR , " \n " , __func__ , errno , strerror ( errno ) ) ; close ( sd ) ; return -1 ; } ipm . imr_multiaddr . s_addr = inet_addr ( MK_MDNS_MULTICAST_ADDR ) ; rc = setsockopt ( sd , IPPROTO_IP , IP_ADD_MEMBERSHIP , & ipm , sizeof ( struct ip_mreq ) ) ; if ( rc < 0 ) { MK_LOG ( LOG_ERR , " \n " , __func__ , errno , strerror ( errno ) ) ; close ( sd ) ; return -1 ; } rc = setsockopt ( sd , IPPROTO_IP , IP_MULTICAST_TTL , ( char * ) & ttl , sizeof ( ttl ) ) ; if ( rc < 0 ) { MK_LOG ( LOG_ERR , " \n " , __func__ , errno , strerror ( errno ) ) ; close ( sd ) ; return -1 ; } MK_LOG ( LOG_INFO , " \n " , __func__ , sd ) ; return sd ; }
mk_create_mcast_enpoint() creates a mdns multicast udp socket enpoint for
communication and returns the socket descriptor on success and -1 on failure
mk_create_mcast_enpoint () creates a mdns multicast udp socket enpoint for communication and returns the socket descriptor on success and - 1 on failure
// MK_LOG(LOG_ERR, "%s() ipv4 is empty \n", __func__); /*
     * create an endpoint for communication 
     */ /*
     * bind a name to the socket
     */ // The following are needed only if we need to be a multicast listener /* 
     * Set the local device for a multicast socket -- using IP_MULTICAST_IF socket option
     */ /* 
     * Join to a multicast group -- using IP_ADD_MEMBERSHIP socket option
     */ /*
     *  Set  the time-to-live value of outgoing multicast packets for this socket
     *  TTL of 1 are restricted to the same subnet
     *  TTL of 32 are restricted to the same site
     */