Explorar o código

allow netbios_ns_resolve to return an error code

This change also changes netbios_ns_resolve to distinguish between
an error and a non-reply from a host.
Reuben Hawkins %!s(int64=11) %!d(string=hai) anos
pai
achega
2a2bbfcbe8
Modificáronse 4 ficheiros con 19 adicións e 12 borrados
  1. 3 1
      dsm.c
  2. 1 1
      include/bdsm/netbios_ns.h
  3. 3 1
      lookup.c
  4. 12 9
      src/netbios_ns.c

+ 3 - 1
dsm.c

@@ -95,7 +95,9 @@ int main(int ac, char **av)
 
   ctx = bdsm_context_new();
   assert(ctx);
-  addr.sin_addr.s_addr = netbios_ns_resolve(ctx->ns, hoststr, NETBIOS_FILESERVER);
+  if (0 != netbios_ns_resolve(ctx->ns, hoststr, NETBIOS_FILESERVER, &addr.sin_addr.s_addr)) {
+    exit(-1);
+  }
   printf("%s's IP address is : %s\n", hoststr, inet_ntoa(addr.sin_addr));
 
   //netbios_ns_discover(ctx->ns);

+ 1 - 1
include/bdsm/netbios_ns.h

@@ -43,7 +43,7 @@ typedef struct {
 
 netbios_ns_t  *netbios_ns_new();
 void          netbios_ns_destroy(netbios_ns_t *ns);
-uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type);
+int           netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type, uint32_t * addr);
 int           netbios_ns_discover(netbios_ns_t *ns);
 int           netbios_ns_inverse(netbios_ns_t *ns, netbios_ns_entry_t *entry);
 

+ 3 - 1
lookup.c

@@ -43,7 +43,9 @@ int main(int ac, char **av)
     exit(1);
   }
 
-  addr.s_addr = netbios_ns_resolve(ctx->ns, av[1], NETBIOS_FILESERVER);
+  if (0 != netbios_ns_resolve(ctx->ns, av[1], NETBIOS_FILESERVER, &addr.s_addr)) {
+    exit(-1);
+  }
   if (!addr.s_addr)
   {
     fprintf(stderr, "Unable to perform name resolution for %s\n", av[1]);

+ 12 - 9
src/netbios_ns.c

@@ -158,7 +158,7 @@ ssize_t           netbios_ns_recv(int sock, void *buf, size_t buf_size,
     return (-1);
 }
 
-uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type)
+int      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type, uint32_t * addr)
 {
   struct timeval      timeout;
   netbios_query_t     *q;
@@ -173,7 +173,7 @@ uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type)
   assert(ns != NULL);
 
   if ((encoded_name = netbios_name_encode(name, 0, type)) == NULL)
-    return (0);
+    return (-1);
 
     // Prepare packet
   q = netbios_query_new(34 + 4, 1, NETBIOS_OP_NAME_QUERY);
@@ -192,7 +192,7 @@ uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type)
 
   // Let's send it
   if (!netbios_ns_send_query(ns, q, ip))
-    return (0);
+    return (-1);
   else if (BDSM_DEBUG)
     fprintf(stderr, "netbios_ns_resolve, name query sent for '%s' !\n", name);
 
@@ -203,16 +203,19 @@ uint32_t      netbios_ns_resolve(netbios_ns_t *ns, const char *name, char type)
   timeout.tv_usec = 420;
   recv = netbios_ns_recv(ns->socket, (void *)recv_buffer, 512, &timeout, 0, 0);
 
-  if (recv <= 0)
-    goto error;
+  if (recv < 0)
+    perror("netbios_ns_resolve: ");
+  else if (recv == 0 && BDSM_DEBUG)
+    fprintf(stderr, "netbios_ns_resolve, received no reply for '%s' !\n", name);
   else if (BDSM_DEBUG)
     fprintf(stderr, "netbios_ns_resolve, received a reply for '%s' !\n", name);
 
-  return (*(uint32_t *)(recv_buffer + recv - 4));
-
-  error:
-    perror("netbios_ns_resolve: ");
+  if (recv > 0) {
+    *addr = (*(uint32_t *)(recv_buffer + recv - 4));
     return (0);
+  }
+
+  return (-1);
 }
 
 int           netbios_ns_discover(netbios_ns_t *ns)