Send a lookip NOT_FOUND reply if a lookup yields no results
authorMartin Willi <martin@revosec.ch>
Tue, 9 Oct 2012 09:16:07 +0000 (11:16 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 24 Oct 2012 09:43:34 +0000 (11:43 +0200)
src/libcharon/plugins/lookip/lookip.c
src/libcharon/plugins/lookip/lookip_msg.h
src/libcharon/plugins/lookip/lookip_socket.c

index 7f5dc8a..4637e7b 100644 (file)
@@ -102,6 +102,9 @@ static int receive(int fd, int block)
                        case LOOKIP_ENTRY:
                                label = "lookup:";
                                break;
+                       case LOOKIP_NOT_FOUND:
+                               label = "not found:";
+                               break;
                        case LOOKIP_NOTIFY_UP:
                                label = "up:";
                                break;
@@ -117,7 +120,7 @@ static int receive(int fd, int block)
                resp.id[sizeof(resp.id) - 1] = '\0';
                resp.name[sizeof(resp.name) - 1] = '\0';
 
-               printf("%-8s %16s %16s %20s %s\n",
+               printf("%-12s %16s %16s %20s %s\n",
                           label, resp.vip, resp.ip, resp.name, resp.id);
        }
 }
index 86ea42f..337d1c4 100644 (file)
@@ -40,19 +40,21 @@ enum {
        /** request a dump of all entries */
        LOOKIP_DUMP = 1,
        /** lookup a specific virtual IP */
-       LOOKIP_LOOKUP = 2,
+       LOOKIP_LOOKUP,
        /** reply message for DUMP and LOOKUP */
-       LOOKIP_ENTRY = 3,
+       LOOKIP_ENTRY,
+       /** reply message for LOOKUP if no such IP found */
+       LOOKIP_NOT_FOUND,
        /** register for notifications about new virtual IPs */
-       LOOKIP_REGISTER_UP = 4,
+       LOOKIP_REGISTER_UP,
        /** register for notifications about virtual IPs released */
-       LOOKIP_REGISTER_DOWN = 5,
+       LOOKIP_REGISTER_DOWN,
        /** notify reply message for REGISTER_UP */
-       LOOKIP_NOTIFY_UP = 6,
+       LOOKIP_NOTIFY_UP,
        /** notify reply message for REGISTER_DOWN */
-       LOOKIP_NOTIFY_DOWN = 7,
+       LOOKIP_NOTIFY_DOWN,
        /** end of request batch */
-       LOOKIP_END = 8,
+       LOOKIP_END,
 };
 
 /**
@@ -72,7 +74,9 @@ struct lookip_request_t {
 /**
  * Response message sent to client.
  *
- * Valid response message types are ENTRY and NOTIFY_UP/DOWN.
+ * Valid response message types are ENTRY, NOT_FOUND and NOTIFY_UP/DOWN.
+ *
+ * All fields are set in all messages, except in NOT_FOUND: Only vip is set.
  */
 struct lookip_response_t {
        /** response message type */
index 66eafd3..5c6e0e3 100644 (file)
@@ -188,6 +188,7 @@ static void query(private_lookip_socket_t *this, int fd, lookip_request_t *req)
                .type = LOOKIP_ENTRY,
        };
        host_t *vip = NULL;
+       int matches = 0;
 
        if (req)
        {       /* lookup */
@@ -195,10 +196,23 @@ static void query(private_lookip_socket_t *this, int fd, lookip_request_t *req)
                vip = host_create_from_string(req->vip, 0);
                if (vip)
                {
-                       this->listener->lookup(this->listener, vip,
-                                                                  (void*)listener_cb, &entry);
+                       matches = this->listener->lookup(this->listener, vip,
+                                                                                        (void*)listener_cb, &entry);
                        vip->destroy(vip);
                }
+               if (matches == 0)
+               {
+                       lookip_response_t resp = {
+                               .type = LOOKIP_NOT_FOUND,
+                       };
+
+                       snprintf(resp.vip, sizeof(resp.vip), "%s", req->vip);
+                       if (send(fd, &resp, sizeof(resp), 0) < 0)
+                       {
+                               DBG1(DBG_CFG, "sending lookip not-found failed: %s",
+                                        strerror(errno));
+                       }
+               }
        }
        else
        {       /* dump */