/* Find all address associated with the NetBIOS name */ ssize_t SMBResolveNetBIOSName(const char * hostName, uint8_t nodeType, uint32_t timeout, struct sockaddr_storage ** results) { int32_t outCount = 0; *results = SMBResolveNetBIOSNameEx(hostName, nodeType, NULL, timeout, NULL, &outCount); /* We didn't time out, report to the calling process that we had an error */ if (errno && (errno != EHOSTUNREACH)) { return -1; } return outCount; }
int cmd_lookup(int argc, char *argv[]) { char *hostname; int opt; struct sockaddr_storage *startAddr = NULL, *listAddr = NULL; const char *winsServer = NULL; int32_t ii, count = 0; struct sockaddr_storage respAddr; struct sockaddr_in *in4 = NULL; struct sockaddr_in6 *in6 = NULL; char addrStr[INET6_ADDRSTRLEN+1]; uint8_t nodeType = kNetBIOSFileServerService; Boolean escapeNames= FALSE; bzero(&respAddr, sizeof(respAddr)); if (argc < 2) lookup_usage(); while ((opt = getopt(argc, argv, "ew:t:")) != EOF) { switch(opt) { case 'e': escapeNames = TRUE; break; case 'w': winsServer = optarg; break; case 't': errno = 0; nodeType = (uint8_t)strtol(optarg, NULL, 0); if (errno) errx(EX_DATAERR, "invalid value for node type"); break; default: lookup_usage(); /*NOTREACHED*/ } } if (optind >= argc) lookup_usage(); hostname = getHostName(argv[argc - 1], escapeNames); if (!hostname) { err(EX_OSERR, "failed to resolve %s", argv[argc - 1]); } startAddr = listAddr = SMBResolveNetBIOSNameEx(hostname, nodeType, winsServer, 0, &respAddr, &count); if (startAddr == NULL) { err(EX_NOHOST, "unable to resolve %s", hostname); } if (respAddr.ss_family == AF_INET) { in4 = (struct sockaddr_in *)&respAddr; inet_ntop(respAddr.ss_family, &in4->sin_addr, addrStr, sizeof(addrStr)); } else if (respAddr.ss_family == AF_INET6) { in6 = (struct sockaddr_in6 *)&respAddr; inet_ntop(respAddr.ss_family, &in6->sin6_addr, addrStr, sizeof(addrStr)); } else { strcpy(addrStr, "unknown address family"); } fprintf(stdout, "Got response from %s\n", addrStr); for (ii=0; ii < count; ii++) { if (listAddr->ss_family == AF_INET) { in4 = (struct sockaddr_in *)listAddr; inet_ntop(listAddr->ss_family, &in4->sin_addr, addrStr, sizeof(addrStr)); } else if (respAddr.ss_family == AF_INET6) { in6 = (struct sockaddr_in6 *)listAddr; inet_ntop(respAddr.ss_family, &in6->sin6_addr, addrStr, sizeof(addrStr)); } else { strcpy(addrStr, "unknown address family"); } fprintf(stdout, "IP address of %s: %s\n", hostname, addrStr); listAddr++; } if (startAddr) { free(startAddr); } if (hostname) { free(hostname); } return 0; }