Send a lookip NOT_FOUND reply if a lookup yields no results

This commit is contained in:
Martin Willi 2012-10-09 11:16:07 +02:00
parent f6fb2b98e9
commit 9d422bb1b0
3 changed files with 32 additions and 11 deletions

View File

@ -102,6 +102,9 @@ static int receive(int fd, int block)
case LOOKIP_ENTRY: case LOOKIP_ENTRY:
label = "lookup:"; label = "lookup:";
break; break;
case LOOKIP_NOT_FOUND:
label = "not found:";
break;
case LOOKIP_NOTIFY_UP: case LOOKIP_NOTIFY_UP:
label = "up:"; label = "up:";
break; break;
@ -117,7 +120,7 @@ static int receive(int fd, int block)
resp.id[sizeof(resp.id) - 1] = '\0'; resp.id[sizeof(resp.id) - 1] = '\0';
resp.name[sizeof(resp.name) - 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); label, resp.vip, resp.ip, resp.name, resp.id);
} }
} }

View File

@ -40,19 +40,21 @@ enum {
/** request a dump of all entries */ /** request a dump of all entries */
LOOKIP_DUMP = 1, LOOKIP_DUMP = 1,
/** lookup a specific virtual IP */ /** lookup a specific virtual IP */
LOOKIP_LOOKUP = 2, LOOKIP_LOOKUP,
/** reply message for DUMP and 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 */ /** register for notifications about new virtual IPs */
LOOKIP_REGISTER_UP = 4, LOOKIP_REGISTER_UP,
/** register for notifications about virtual IPs released */ /** register for notifications about virtual IPs released */
LOOKIP_REGISTER_DOWN = 5, LOOKIP_REGISTER_DOWN,
/** notify reply message for REGISTER_UP */ /** notify reply message for REGISTER_UP */
LOOKIP_NOTIFY_UP = 6, LOOKIP_NOTIFY_UP,
/** notify reply message for REGISTER_DOWN */ /** notify reply message for REGISTER_DOWN */
LOOKIP_NOTIFY_DOWN = 7, LOOKIP_NOTIFY_DOWN,
/** end of request batch */ /** end of request batch */
LOOKIP_END = 8, LOOKIP_END,
}; };
/** /**
@ -72,7 +74,9 @@ struct lookip_request_t {
/** /**
* Response message sent to client. * 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 { struct lookip_response_t {
/** response message type */ /** response message type */

View File

@ -188,6 +188,7 @@ static void query(private_lookip_socket_t *this, int fd, lookip_request_t *req)
.type = LOOKIP_ENTRY, .type = LOOKIP_ENTRY,
}; };
host_t *vip = NULL; host_t *vip = NULL;
int matches = 0;
if (req) if (req)
{ /* lookup */ { /* 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); vip = host_create_from_string(req->vip, 0);
if (vip) if (vip)
{ {
this->listener->lookup(this->listener, vip, matches = this->listener->lookup(this->listener, vip,
(void*)listener_cb, &entry); (void*)listener_cb, &entry);
vip->destroy(vip); 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 else
{ /* dump */ { /* dump */