// If this node has a leaf, we will check if it matches
if (__loc_database_node_is_leaf(node_v1)) {
r = __loc_database_lookup_handle_leaf(db, address, network, network_address, level, node_v1);
- if (r <= 0)
+ if (r < 0)
return r;
}
- return 1;
+ // Return no error - even if nothing was found
+ return 0;
}
LOC_EXPORT int loc_database_lookup(struct loc_database* db,
}
// Create a network object
- r = create_network(L, network);
- loc_network_unref(network);
+ if (network) {
+ r = create_network(L, network);
+ loc_network_unref(network);
+ }
return r;
}
static PyObject* Database_lookup(DatabaseObject* self, PyObject* args) {
struct loc_network* network = NULL;
const char* address = NULL;
+ int r;
if (!PyArg_ParseTuple(args, "s", &address))
return NULL;
// Try to retrieve a matching network
- int r = loc_database_lookup_from_string(self->db, address, &network);
+ r = loc_database_lookup_from_string(self->db, address, &network);
+ if (r) {
+ // Handle any errors
+ switch (errno) {
+ case EINVAL:
+ PyErr_Format(PyExc_ValueError, "Invalid IP address: %s", address);
- // We got a network
- if (r == 0) {
- PyObject* obj = new_network(&NetworkType, network);
- loc_network_unref(network);
+ default:
+ PyErr_SetFromErrno(PyExc_OSError);
+ }
- return obj;
+ return NULL;
}
// Nothing found
- if (!errno)
+ if (!network)
Py_RETURN_NONE;
- // Handle any errors
- switch (errno) {
- case EINVAL:
- PyErr_Format(PyExc_ValueError, "Invalid IP address: %s", address);
-
- default:
- PyErr_SetFromErrno(PyExc_OSError);
- }
+ // We got a network
+ PyObject* obj = new_network(&NetworkType, network);
+ loc_network_unref(network);
- return NULL;
+ return obj;
}
static PyObject* new_database_enumerator(PyTypeObject* type, struct loc_database_enumerator* enumerator) {
// Lookup an address outside the subnet
err = loc_database_lookup_from_string(db, "2001:db8:fffe:1::", &network1);
- if (err == 0) {
+ if (err || network1) {
fprintf(stderr, "Could look up 2001:db8:fffe:1::, but I shouldn't\n");
exit(EXIT_FAILURE);
}