1#include "includes.h" 2 3extern bool AllowDebugChange; 4 5static int ServerFD= -1; 6static bool RootPort = false; 7 8/**************************************************************************** 9 Open the socket communication. 10**************************************************************************/ 11 12static bool open_mysockets(void) 13{ 14 struct sockaddr_storage ss; 15 const char *sock_addr = lp_socket_address(); 16 17 if (!interpret_string_addr(&ss, sock_addr, 18 AI_NUMERICHOST|AI_PASSIVE)) { 19 DEBUG(0,("open_mysockets: unable to get socket address " 20 "from string %s", sock_addr)); 21 return false; 22 } 23 24 ServerFD = open_socket_in( SOCK_DGRAM, 25 (RootPort ? 137 : 0), 26 (RootPort ? 0 : 3), 27 &ss, true ); 28 29 if (ServerFD == -1) { 30 return false; 31 } 32 33 set_socket_options( ServerFD, "SO_BROADCAST" ); 34 35 DEBUG(3, ("Socket opened.\n")); 36 return true; 37} 38 39/**************************************************************************** 40 Do a node status query. 41****************************************************************************/ 42static bool do_mynode_status(int fd, 43 const char *name, 44 int type, 45 struct sockaddr_storage *pss, 46 char** hostname) 47{ 48 struct nmb_name nname; 49 int count, i, j; 50 NODE_STATUS_STRUCT *status; 51 struct node_status_extra extra; 52 char addr[INET6_ADDRSTRLEN]; 53 bool result = false; 54 55 print_sockaddr(addr, sizeof(addr), pss); 56 make_nmb_name(&nname, name, type); 57 status = node_status_query(fd, &nname, pss, &count, &extra, 500); 58 if (status) { 59 if(count>0){ 60 int i; 61 for(i=0; i<count; i++){ 62 //fprintf(stderr, "%s#%02x: flags = 0x%02x\n", status[i].name, status[i].type, status[i].flags); 63 if(status[i].type==0x20){ 64 *hostname = SMB_STRDUP(status[i].name); 65 //fprintf(stderr, "Looking up status of [%s], host name = [%s], [%02x]\n\n", addr,*hostname, status[i].type); 66 result = true; 67 break; 68 } 69 } 70 //*hostname = SMB_STRDUP(status[0].name); 71 //fprintf(stderr, "Looking up status of [%s], host name = [%s] %d, %x\n\n", addr,*hostname, count, status[0].type); 72 //result = true; 73 } 74 SAFE_FREE(status); 75 } 76 77 return result; 78} 79 80static bool query_hostname_byip(const char *lookup_ip, char** hostname) 81{ 82 load_interfaces(); 83 84 if (!open_mysockets()) { 85 return false; 86 } 87 88 bool res; 89 char *p; 90 unsigned int lookup_type = 0x00; 91 struct in_addr ip; 92 static bool lookup_by_ip = True; 93 94 p = SMB_STRDUP(lookup_ip); 95 96 struct sockaddr_storage ss; 97 ip = interpret_addr2(p); 98 in_addr_to_sockaddr_storage(&ss, ip); 99 fstrcpy((char*)p,"*"); 100 101 //fprintf(stderr, "enter query_hostname_byip %s\n", lookup_ip); 102 res = do_mynode_status(ServerFD, p, lookup_type, &ss, hostname); 103 //fprintf(stderr, "leave query_hostname_byip %s\n", lookup_ip); 104 105 close(ServerFD); 106 107 SAFE_FREE(p); 108 109 return res; 110 111 /* 112 struct sockaddr_storage ss; 113 ip = interpret_addr2(lookup_ip); 114 in_addr_to_sockaddr_storage(&ss, ip); 115 fstrcpy((char*)lookup_ip,"*"); 116 return do_mynode_status(ServerFD, lookup_ip, lookup_type, &ss, hostname); 117 */ 118} 119