diff --git a/mdns.c b/mdns.c index 14912b3..43127bd 100644 --- a/mdns.c +++ b/mdns.c @@ -132,11 +132,10 @@ char *nlabel_to_str(const uint8_t *name) { // does NOT uncompress the field, so it could be as small as 2 bytes // or 1 for the root static size_t label_len(uint8_t *pkt_buf, size_t pkt_len, size_t off) { - uint8_t *p; uint8_t *e = pkt_buf + pkt_len; size_t len = 0; - for (p = pkt_buf + off; p < e; p++) { + for (uint8_t *p = pkt_buf + off; p < e; p++) { if (*p == 0) { return len + 1; } else if ((*p & 0xC0) == 0xC0) { @@ -295,7 +294,6 @@ const char *rr_get_type_name(enum rr_type type) { } void rr_entry_destroy(struct rr_entry *rr) { - struct rr_data_txt *txt_rec; assert(rr); // check rr_type and free data elements @@ -306,8 +304,8 @@ void rr_entry_destroy(struct rr_entry *rr) { // don't free entry break; - case RR_TXT: - txt_rec = &rr->data.TXT; + case RR_TXT: { + struct rr_data_txt *txt_rec = &rr->data.TXT; while (txt_rec) { struct rr_data_txt *next = txt_rec->next; if (txt_rec->txt) @@ -320,6 +318,7 @@ void rr_entry_destroy(struct rr_entry *rr) { txt_rec = next; } break; + } case RR_SRV: if (rr->data.SRV.target) @@ -342,9 +341,7 @@ void rr_entry_destroy(struct rr_entry *rr) { // destroys an RR list (and optionally, items) void rr_list_destroy(struct rr_list *rr, char destroy_items) { - struct rr_list *rr_next; - - for (; rr; rr = rr_next) { + for (struct rr_list *rr_next; rr; rr = rr_next) { rr_next = rr->next; if (destroy_items) rr_entry_destroy(rr->e); @@ -359,8 +356,8 @@ int rr_list_count(struct rr_list *rr) { } struct rr_entry *rr_list_remove(struct rr_list **rr_head, struct rr_entry *rr) { - struct rr_list *le = *rr_head, *pe = NULL; - for (; le; le = le->next) { + struct rr_list *pe = NULL; + for (struct rr_list *le = *rr_head; le; le = le->next) { if (le->e == rr) { if (pe == NULL) { *rr_head = le->next; @@ -389,8 +386,8 @@ int rr_list_append(struct rr_list **rr_head, struct rr_entry *rr) { if (*rr_head == NULL) { *rr_head = node; } else { - struct rr_list *e = *rr_head, *taile; - for (; e; e = e->next) { + struct rr_list *taile = NULL; + for (struct rr_list *e = *rr_head; e; e = e->next) { // already in list - don't add if (e->e == rr) { MDNS_FREE(node); @@ -618,8 +615,7 @@ struct rr_group *rr_group_find(struct rr_group* g, uint8_t *name) { } struct rr_entry *rr_entry_find(struct rr_list *rr_list, uint8_t *name, uint16_t type) { - struct rr_list *rr = rr_list; - for (; rr; rr = rr->next) { + for (struct rr_list *rr = rr_list; rr; rr = rr->next) { if (rr->e->type == type && cmp_nlabel(rr->e->name, name) == 0) return rr->e; } @@ -629,8 +625,7 @@ struct rr_entry *rr_entry_find(struct rr_list *rr_list, uint8_t *name, uint16_t // looks for a matching entry in rr_list // if entry is a PTR, we need to check if the PTR target also matches struct rr_entry *rr_entry_match(struct rr_list *rr_list, struct rr_entry *entry) { - struct rr_list *rr = rr_list; - for (; rr; rr = rr->next) { + for (struct rr_list *rr = rr_list; rr; rr = rr->next) { if (rr->e->type == entry->type && cmp_nlabel(rr->e->name, entry->name) == 0) { if (entry->type != RR_PTR) { return rr->e; @@ -644,9 +639,7 @@ struct rr_entry *rr_entry_match(struct rr_list *rr_list, struct rr_entry *entry) } void rr_group_destroy(struct rr_group *group) { - struct rr_group *g = group; - - while (g) { + for (struct rr_group *g = group; g; ) { struct rr_group *nextg = g->next; free(g->name); rr_list_destroy(g->rr, 1); diff --git a/mdnsd.c b/mdnsd.c index 84f13c4..b6cc6ee 100644 --- a/mdnsd.c +++ b/mdnsd.c @@ -216,20 +216,11 @@ static void print_rr_entry(struct rr_entry *rr_e) static void print_cache(struct mdnsd *svr) { - struct rr_group *group = svr->cache; - struct rr_list *list = NULL; - struct rr_entry *entry = NULL; - char *pname = NULL; - DEBUG_PRINTF("\n"); DEBUG_PRINTF(" Multicast DNS Cache\n"); - for (; group; group = group->next) { - if (group->name) { - pname = nlabel_to_str(group->name); - } else { - pname = NULL; - } + for (struct rr_group *group = svr->cache; group; group = group->next) { + char *pname = group->name? nlabel_to_str(group->name): NULL; DEBUG_PRINTF("==================================================\n"); DEBUG_PRINTF(" Group: %s\n", pname ? pname : "Unknown"); @@ -238,9 +229,8 @@ static void print_cache(struct mdnsd *svr) MDNS_FREE(pname); } - list = group->rr; - for (; list; list = list->next) { - entry = list->e; + for (struct rr_list *list = group->rr; list; list = list->next) { + struct rr_entry *entry = list->e; if (entry) { print_rr_entry(entry); } @@ -281,9 +271,7 @@ done: static char *get_service_type_without_subtype(char *name) { - char *str = NULL; - - str = strstr(name, MDNS_CHECK_SUBTYPE_STR); + char *str = strstr(name, MDNS_CHECK_SUBTYPE_STR); if (str) { str += strlen(MDNS_CHECK_SUBTYPE_STR); } else { @@ -297,21 +285,16 @@ static char *get_service_type_without_subtype(char *name) static int lookup_hostname(struct mdnsd *svr, char *hostname) { int result = -1; - struct rr_group *group = svr->cache; - struct rr_list *list = NULL; - struct rr_entry *entry = NULL; int b_found = 0; - char *e_name; - int ret; pthread_mutex_lock(&svr->data_lock); - for (; group; group = group->next) { - list = group->rr; - for (; list; list = list->next) { - entry = list->e; + for (struct rr_group *group = svr->cache; group; group = group->next) { + for (struct rr_list *list = group->rr; list; list = list->next) { + struct rr_entry *entry = list->e; + int ret; if (entry && entry->name) { - e_name = nlabel_to_str(entry->name); + char *e_name = nlabel_to_str(entry->name); ret = strncmp(e_name, hostname, strlen(hostname)); MDNS_FREE(e_name); } else { @@ -339,26 +322,22 @@ static int lookup_hostname(struct mdnsd *svr, char *hostname) } #endif /* !defined MDNS_NO_RESPONDER_SUPPORT */ -static int lookup_hostname_to_addr(struct mdnsd *svr, char *hostname, int *ipaddr) +static int lookup_hostname_to_addr(struct mdnsd *svr, char *hostname, uint32_t *ipaddr) { int result = -1; - struct rr_group *group = svr->cache; - struct rr_list *list = NULL; - struct rr_entry *entry = NULL; int b_found = 0; - char *e_name; - int ret; update_cache(svr); pthread_mutex_lock(&svr->data_lock); - for (; group; group = group->next) { - list = group->rr; - for (; list; list = list->next) { - entry = list->e; + for (struct rr_group *group = svr->cache; group; group = group->next) { + for (struct rr_list *list = group->rr; list; list = list->next) { + struct rr_entry *entry = list->e; if (entry && (entry->type == RR_A)) { // currently, support only ipv4 + int ret; if (entry->name) { + char *e_name; e_name = nlabel_to_str(entry->name); ret = strncmp(e_name, hostname, strlen(hostname)); MDNS_FREE(e_name); @@ -405,10 +384,8 @@ static int lookup_service(struct mdnsd *svr, char *type, struct mdns_service_inf MDNS_FREE(type_nlabel); if (ptr_grp) { - struct rr_list *list = ptr_grp->rr; - struct rr_entry *entry = NULL; - for (; list; list = list->next) { - entry = list->e; + for (struct rr_list *list = ptr_grp->rr; list; list = list->next) { + struct rr_entry *entry = list->e; if (entry && (entry->type == RR_PTR)) { if (entry->data.PTR.name) { /* SRV's name */ struct rr_group *srv_grp = rr_group_find(svr->cache, @@ -608,13 +585,12 @@ static ssize_t send_packet(int fd, const void *data, size_t len, int domain) { static int populate_query(struct mdnsd *svr, struct rr_list **rr_head) { int num_qns = 0; - struct rr_entry *qn_e = NULL; // check if we have the records pthread_mutex_lock(&svr->data_lock); while (svr->query) { - qn_e = rr_list_remove(&svr->query, svr->query->e); + struct rr_entry *qn_e = rr_list_remove(&svr->query, svr->query->e); if (qn_e == NULL) { break; } @@ -636,8 +612,8 @@ static int populate_probe(struct mdnsd *svr, struct rr_list **rr_head) // check if we have the records pthread_mutex_lock(&svr->data_lock); - for (struct rr_entry *qn_e = NULL; svr->probe; ) { - qn_e = rr_list_remove(&svr->probe, svr->probe->e); + while (svr->probe) { + struct rr_entry *qn_e = rr_list_remove(&svr->probe, svr->probe->e); if (qn_e == NULL) { break; } @@ -954,8 +930,8 @@ static int process_mdns_pkt(struct mdnsd *svr, struct mdns_pkt *pkt, struct mdns } // remove our replies if they were already in their answers - struct rr_list *ans = NULL, *prev_ans = NULL; - for (ans = reply->rr_ans; ans; ) { + struct rr_list *prev_ans = NULL; + for (struct rr_list *ans = reply->rr_ans; ans; ) { struct rr_list *next_ans = ans->next; struct rr_entry *known_ans = rr_entry_match(pkt->rr_ans, ans->e); @@ -1500,11 +1476,11 @@ static int mdnsd_set_host_info(struct mdnsd *svr, const char *hostname, uint32_t domain = check_mdns_domain(hostname); if (domain == MDNS_DOMAIN_LOCAL) { - snprintf(mdns_suffix, sizeof(mdns_suffix), MDNS_SUFFIX_LOCAL); + snprintf(mdns_suffix, sizeof(mdns_suffix)-1, MDNS_SUFFIX_LOCAL); } #if defined(CONFIG_NETUTILS_MDNS_XMDNS) else if (domain == MDNS_DOMAIN_SITE) { - snprintf(mdns_suffix, sizeof(mdns_suffix), MDNS_SUFFIX_SITE); + snprintf(mdns_suffix, sizeof(mdns_suffix)-1, MDNS_SUFFIX_SITE); } #endif else { @@ -2233,11 +2209,10 @@ void mdnsd_add_rr(struct mdnsd *svr, struct rr_entry *rr) { * On success, 0 is returned. On failure, a negative value is returned. * ****************************************************************************/ -int mdnsd_resolve_hostname(char *hostname, int *ipaddr) +int mdnsd_resolve_hostname(char *hostname, uint32_t *ipaddr) { int result = -1; struct timeval old_time, cur_time; - unsigned int time_diff; int try_count; int domain; @@ -2295,13 +2270,7 @@ int mdnsd_resolve_hostname(char *hostname, int *ipaddr) try_count = 0; TIME_GET(old_time); - while (1) { - TIME_GET(cur_time); - time_diff = TIME_DIFF_USEC(old_time, cur_time); - if (time_diff > (MDNS_HOSTNAME_RESOLVER_TIMEOUT_MSEC * 1000)) { - break; - } - + do { if (try_count < MDNS_HOSTNAME_RESOLVER_MAX_TRY_COUNT) { struct rr_entry *a_e = NULL; struct rr_entry *aaaa_e = NULL; @@ -2327,7 +2296,8 @@ int mdnsd_resolve_hostname(char *hostname, int *ipaddr) result = 0; break; } - } + } while ((TIME_GET(cur_time),TIME_DIFF_USEC(old_time, cur_time)) <= (MDNS_HOSTNAME_RESOLVER_TIMEOUT_MSEC * 1000)); + #ifdef MDNS_NO_RESPONDER_SUPPORT out_with_context: @@ -2448,13 +2418,7 @@ int mdnsd_discover_service(char *service_type, int discover_time_msec, struct md try_count = 0; TIME_GET(old_time); - while (1) { - TIME_GET(cur_time); - time_diff = TIME_DIFF_USEC(old_time, cur_time); - if (time_diff > (discover_time_msec * 1000)) { - break; - } - + do { if (try_count < MDNS_HOSTNAME_RESOLVER_MAX_TRY_COUNT) { ptr_e = qn_create(create_nlabel(service_type_str), RR_PTR, 0); @@ -2469,7 +2433,7 @@ int mdnsd_discover_service(char *service_type, int discover_time_msec, struct md } usleep(MDNS_SERVICE_DISCOVERY_WAIT_TIME_MSEC * 1000); - } + } while ((TIME_GET(cur_time),TIME_DIFF_USEC(old_time, cur_time)) <= (discover_time_msec * 1000)); if (lookup_service(g_svr, service_type_str, g_service_list, num_of_services) == 0) { *service_list = g_service_list; diff --git a/mdnsd.h b/mdnsd.h index b24ed9d..f4dc9bf 100644 --- a/mdnsd.h +++ b/mdnsd.h @@ -129,7 +129,7 @@ void mdnsd_add_rr(struct mdnsd *svr, struct rr_entry *rr); * On success, 0 is returned. On failure, a negative value is returned. * */ -int mdnsd_resolve_hostname(char *hostname, int *ipaddr); +int mdnsd_resolve_hostname(char *hostname, uint32_t *ipaddr); /** * mdnsd_discover_service() discovers services with the given service type string