Skip to content

Commit

Permalink
stop with error on multiple non-matching mdns responses
Browse files Browse the repository at this point in the history
  • Loading branch information
john30 committed Oct 22, 2024
1 parent 254517f commit 3a9ee44
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
9 changes: 7 additions & 2 deletions src/ebusd/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,14 @@ int main(int argc, char* argv[], char* envp[]) {
return EINVAL;
}
if (ret > 1) {
logWrite(lf_main, ll_notice,
"found several devices from \"%s\", better limit to the desired one using e.g. \"mdns:%s\"", device,
ip = inet_ntoa(address.address);
logWrite(lf_main, ll_info, "discovered another device with ID %s and device string %s:%s",
address.id, address.proto, ip);
logWrite(lf_main, ll_error,
"found several devices from \"%s\". use e.g. \"--device=mdns:%s\" to limit it to the desired one", device,
address.id);
cleanup();
return EINVAL;
}
ip = inet_ntoa(address.address);
char *mdnsDevice = reinterpret_cast<char*>(malloc(4*4+3+1)); // ens:xxx.xxx.xxx.xxx
Expand Down
29 changes: 19 additions & 10 deletions src/lib/utils/tcpsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,32 +453,41 @@ int resolveMdnsOneShot(const char* url, mdns_oneshot_t *result, mdns_oneshot_t *
0x8000 | // unicast response bit
DNS_CLASS_AA);
len += sizeof(dns_question_t);
ssize_t done = sendto(sock, record, len, 0, reinterpret_cast<sockaddr*>(&address), sizeof(address));
ssize_t ret = sendto(sock, record, len, 0, reinterpret_cast<sockaddr*>(&address), sizeof(address));
#ifdef DEBUG_MDNS
printf("mdns: sent %ld, err %d\n", done, errno);
printf("mdns: sent %ld, err %d\n", ret, errno);
#endif
fcntl(sock, F_SETFL, O_NONBLOCK);
bool found = false, foundMore = false;
size_t moreRemain = moreResults && moreCount && *moreCount > 0 ? *moreCount : 0;
if (moreRemain > 0) {
*moreCount = 0;
}
size_t done = 0;
#ifdef DEBUG_MDNS
socketaddress aaddr;
socklen_t aaddrlen = 0;
#endif
for (int i=0; i < (found ? 3 : 5); i++) { // up to 5 seconds, at least 3 seconds
int ret = socketPoll(sock, POLLIN, 1);
ret = socketPoll(sock, POLLIN, 1);
done = 0;
if (ret > 0 && (ret&POLLIN)) {
#ifdef DEBUG_MDNS
aaddrlen = sizeof(aaddr);
done = recvfrom(sock, record, sizeof(record), 0, reinterpret_cast<sockaddr*>(&aaddr), &aaddrlen);
ret = recvfrom(sock, record, sizeof(record), 0, reinterpret_cast<sockaddr*>(&aaddr), &aaddrlen);
#else
done = recv(sock, record, sizeof(record), 0);
ret = recv(sock, record, sizeof(record), 0);
#endif
}
if (done == 0 || (done < 0 && errno == EAGAIN) || done < sizeof(dns_query_t)) {
if (ret < 0) {
if (errno == EAGAIN) {
continue;
}
close(sock);
return -1;
}
done = (size_t)ret;
if (done < sizeof(dns_query_t)) {
continue;
}
dnsr = reinterpret_cast<dns_query_t*>(record);
Expand All @@ -494,8 +503,8 @@ int resolveMdnsOneShot(const char* url, mdns_oneshot_t *result, mdns_oneshot_t *
) {
continue;
}
int anCnt = ntohs(dnsr->anCount);
int arCnt = ntohs(dnsr->arCount);
uint16_t anCnt = ntohs(dnsr->anCount);
uint16_t arCnt = ntohs(dnsr->arCount);
if (anCnt < 1 || dnsr->nsCount || arCnt < 1) {
continue;
}
Expand Down Expand Up @@ -546,7 +555,7 @@ int resolveMdnsOneShot(const char* url, mdns_oneshot_t *result, mdns_oneshot_t *
break; // skip this one
}
len += sizeof(dns_answer_t);
int rdLen = ntohs(a->rdLength);
uint16_t rdLen = ntohs(a->rdLength);
#ifdef DEBUG_MDNS
printf(" rd %d @%2.2x = ", rdLen, len);
for (int i=0; i < rdLen && len+i < done; i++) {
Expand Down Expand Up @@ -577,7 +586,7 @@ int resolveMdnsOneShot(const char* url, mdns_oneshot_t *result, mdns_oneshot_t *
sep = sep ? strchr(sep2+1, '=') : nullptr;
}
if (sep && strncmp(sep2+1, "proto", sep-sep2-1) == 0 && (
pos == sep+1+sizeof(mdns_oneshot_t::proto)-1-name
pos == (size_t)(sep-name)+1+sizeof(mdns_oneshot_t::proto)-1
|| strchr(sep+1, '.') == sep+1+sizeof(mdns_oneshot_t::proto)-1)) {
memcpy(proto, sep+1, sizeof(mdns_oneshot_t::proto)-1);
}
Expand Down

0 comments on commit 3a9ee44

Please sign in to comment.