static int test_mdns_start(char *hostname, char *netif_name) { if (mdnsd_start(hostname, netif_name) != 0) { fprintf(stderr, "ERROR: |%s| fail to execute mdnsd_start() \n", __FUNCTION__); return -1; } printf("mdnsd_start() OK. \n"); return 0; }
int main(int argc, char *argv[]) { // create host entries char *hostname = "some-random-host.local"; struct mdnsd *svr = mdnsd_start(); if (svr == NULL) { printf("mdnsd_start() error\n"); return 1; } printf("mdnsd_start OK. press ENTER to add hostname & service\n"); getchar(); mdnsd_set_hostname(svr, hostname, inet_addr("192.168.0.29")); struct rr_entry *a2_e = NULL; a2_e = rr_create_a(create_nlabel(hostname), inet_addr("192.168.0.31")); mdnsd_add_rr(svr, a2_e); struct rr_entry *aaaa_e = NULL; struct addrinfo hints; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_INET6; hints.ai_flags = AI_NUMERICHOST; struct addrinfo* results; getaddrinfo( "fe80::e2f8:47ff:fe20:28e0", NULL, &hints, &results); struct sockaddr_in6* addr = (struct sockaddr_in6*)results->ai_addr; struct in6_addr v6addr = addr->sin6_addr; freeaddrinfo(results); aaaa_e = rr_create_aaaa(create_nlabel(hostname), &v6addr); mdnsd_add_rr(svr, aaaa_e); const char *txt[] = { "path=/mywebsite", NULL }; struct mdns_service *svc = mdnsd_register_svc(svr, "My Website", "_http._tcp.local", 8080, NULL, txt); mdns_service_destroy(svc); printf("added service and hostname. press ENTER to exit\n"); getchar(); mdnsd_stop(svr); return 0; }
static int mdns_tinysvcmdns_register(char *apname, int port) { struct ifaddrs *ifalist; struct ifaddrs *ifa; svr = mdnsd_start(); if (svr == NULL) { warn("tinysvcmdns: mdnsd_start() failed"); return -1; } // Thanks to Paul Lietar for this // room for name + .local + NULL char hostname[100 + 6]; gethostname(hostname, 99); // according to POSIX, this may be truncated without a final NULL ! hostname[99] = 0; // will not work if the hostname doesn't end in .local char *hostend = hostname + strlen(hostname); if ((strlen(hostname) < strlen(".local")) || (strcmp(hostend - 6, ".local") != 0)) { strcat(hostname, ".local"); } if (getifaddrs(&ifalist) < 0) { warn("tinysvcmdns: getifaddrs() failed"); return -1; } ifa = ifalist; // Look for an ipv4/ipv6 non-loopback interface to use as the main one. for (ifa = ifalist; ifa != NULL; ifa = ifa->ifa_next) { if (!(ifa->ifa_flags & IFF_LOOPBACK) && ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { uint32_t main_ip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; mdnsd_set_hostname(svr, hostname, main_ip); // TTL should be 120 seconds break; } else if (!(ifa->ifa_flags & IFF_LOOPBACK) && ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { struct in6_addr *addr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; mdnsd_set_hostname_v6(svr, hostname, addr); // TTL should be 120 seconds break; } } if (ifa == NULL) { warn("tinysvcmdns: no non-loopback ipv4 or ipv6 interface found"); return -1; } // Skip the first one, it was already added by set_hostname for (ifa = ifa->ifa_next; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_flags & IFF_LOOPBACK) // Skip loop-back interfaces continue; switch (ifa->ifa_addr->sa_family) { case AF_INET: { // ipv4 uint32_t ip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; struct rr_entry *a_e = rr_create_a(create_nlabel(hostname), ip); // TTL should be 120 seconds mdnsd_add_rr(svr, a_e); } break; case AF_INET6: { // ipv6 struct in6_addr *addr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; struct rr_entry *aaaa_e = rr_create_aaaa(create_nlabel(hostname), addr); // TTL should be 120 seconds mdnsd_add_rr(svr, aaaa_e); } break; } } freeifaddrs(ifa); char *txtwithoutmetadata[] = {MDNS_RECORD_WITHOUT_METADATA, NULL}; #ifdef CONFIG_METADATA char *txtwithmetadata[] = {MDNS_RECORD_WITH_METADATA, NULL}; #endif char **txt; #ifdef CONFIG_METADATA if (config.metadata_enabled) txt = txtwithmetadata; else #endif txt = txtwithoutmetadata; struct mdns_service *svc = mdnsd_register_svc(svr, apname, "_raop._tcp.local", port, NULL, (const char **)txt); // TTL should be 75 minutes, i.e. 4500 seconds mdns_service_destroy(svc); return 0; }
static int mdns_tinysvcmdns_register(char *apname, int port) { struct ifaddrs *ifalist; struct ifaddrs *ifa; svr = mdnsd_start(); if (svr == NULL) { warn("tinysvcmdns: mdnsd_start() failed"); return -1; } // room for name + .local + NULL char hostname[100 + 6]; gethostname(hostname, 99); // according to POSIX, this may be truncated without a final NULL ! hostname[99] = 0; // will not work on iOS if the hostname doesn't end in .local strcat(hostname, ".local"); if (getifaddrs(&ifalist) < 0) { warn("tinysvcmdns: getifaddrs() failed"); return -1; } ifa = ifalist; // Look for an ipv4/ipv6 non-loopback interface to use as the main one. for (ifa = ifalist; ifa != NULL; ifa = ifa->ifa_next) { if (!(ifa->ifa_flags & IFF_LOOPBACK) && ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { uint32_t main_ip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; mdnsd_set_hostname(svr, hostname, main_ip); break; } else if (!(ifa->ifa_flags & IFF_LOOPBACK) && ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) { struct in6_addr *addr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; mdnsd_set_hostname_v6(svr, hostname, addr); break; } } if (ifa == NULL) { warn("tinysvcmdns: no non-loopback ipv4 or ipv6 interface found"); return -1; } // Skip the first one, it was already added by set_hostname for (ifa = ifa->ifa_next; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_flags & IFF_LOOPBACK) // Skip loop-back interfaces continue; switch (ifa->ifa_addr->sa_family) { case AF_INET: { // ipv4 uint32_t ip = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr; struct rr_entry *a_e = rr_create_a(create_nlabel(hostname), ip); mdnsd_add_rr(svr, a_e); } break; case AF_INET6: { // ipv6 struct in6_addr *addr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr; struct rr_entry *aaaa_e = rr_create_aaaa(create_nlabel(hostname), addr); mdnsd_add_rr(svr, aaaa_e); } break; } } freeifaddrs(ifa); const char *txt[] = { MDNS_RECORD, NULL }; struct mdns_service *svc = mdnsd_register_svc(svr, apname, "_raop._tcp.local", port, NULL, txt); mdns_service_destroy(svc); return 0; }
tenuWifiMode localParseEventLoop(ParseClientInternal *parseClient) { tenuWifiMode wifiStatus = M2M_WIFI_MODE_UNKNOWN; while (m2m_wifi_handle_events(NULL) != M2M_SUCCESS) { } #ifdef ENABLE_MDNS if(gTimerCbMdns == 1) { gTimerCbMdns = 0; printf("Send mDNS====\r\n"); mdnsd_send_response(); } #endif switch (gParseMode) { case M2M_WIFI_MODE_UNKNOWN: break; case M2M_WIFI_MODE_AP_ENABLE: { uint8 macAddr[6] = {0,}; uint8_t macInfo[2] = {0,}; m2m_wifi_get_mac_address(macAddr); macInfo[0] = macAddr[4]; macInfo[1] = macAddr[5]; deviceConfigAddKey( 0, "applicationId", "App ID", NULL ); deviceConfigAddKey( 0, "clientKey", "Client Key", NULL ); deviceConfigAddKey( 0, "installationId", "Installation ID", NULL ); deviceConfigAddKey( 0, "sessionToken", "Session Token", NULL ); deviceConfigAddKey( 0, "deviceName", "Device Name", NULL ); /* AP Mode : Enable*/ fluffy_wifi_ap_mode(1, macInfo); gParseMode = M2M_WIFI_MODE_AP_PROV; wifiStatus = M2M_WIFI_MODE_AP_PROV; break; } case M2M_WIFI_MODE_AP_PROV: { /* Provision AP or HTTP */ fluffy_trans_ap_provision(); gParseMode = M2M_WIFI_MODE_AP; wifiStatus = M2M_WIFI_MODE_AP; break; } case M2M_WIFI_MODE_AP: { if (fluffy_get_ap_mode_completed()) { printf("WIFI : Complete AP Provision\r\n"); nm_bsp_sleep(DELAY_FOR_MODE_CHANGE); /* AP Mode : Disable*/ fluffy_trans_socket_close(); fluffy_wifi_ap_mode(0, NULL); nm_bsp_sleep(DELAY_FOR_MODE_CHANGE); gParseMode = M2M_WIFI_MODE_STA_ENABLE; wifiStatus = M2M_WIFI_MODE_STA_ENABLE; } break; } // AP mode for provisioning //////////////////////////////////////////////////////// // Station mode for connecting parse server (request & push) case M2M_WIFI_MODE_STA_ENABLE: { /* printf( "applicationId = %s\r\n", deviceConfigGetValue( 0, "applicationId" ) ); printf( "clientKey = %s\r\n", deviceConfigGetValue( 0, "clientKey" ) ); printf( "installationId = %s\r\n", deviceConfigGetValue( 0, "installationId" ) ); printf( "deviceId = %s\r\n", deviceConfigGetValue( 0, "sessionToken" ) ); printf( "name = %s\r\n", deviceConfigGetValue( 0, "deviceName" ) ); */ /* Station Mode : Enable */ fluffy_wifi_stat_mode(1); gParseMode = M2M_WIFI_MODE_STA_CON; wifiStatus = M2M_WIFI_MODE_STA_CON; break; } case M2M_WIFI_MODE_STA_CON: { if (fluffy_get_wifi_connected()) { #ifdef ENABLE_MDNS if ( !mdnsd_start( deviceConfigGetValue( 0, "deviceName" ) ) ) { printf("Error, mDNS Start\r\n"); } tcc_enable_callback(&tempTccModule, TCC_CALLBACK_CHANNEL_3); #endif gParseMode = M2M_WIFI_MODE_STA; wifiStatus = M2M_WIFI_MODE_STA; } break; } case M2M_WIFI_MODE_STA: { if( parseClient && parseClient->isStartPushService ) { parseClient->isStartPushService = 0; fluffy_trans_sta_socket(); } wifiStatus = M2M_WIFI_MODE_STA; break; } default: { printf("Unknown WIFI Mode : %d\r\n", gParseMode); wifiStatus = gParseMode; break; } } return wifiStatus; }