getrouters() changed so that a router ignores its own entry in the router list

svn:r34
This commit is contained in:
Matej Pjafjar 2002-07-10 12:35:59 +00:00
parent ce934e4974
commit f07ade3046
3 changed files with 70 additions and 13 deletions

View File

@ -302,7 +302,7 @@ int do_main_loop(void) {
int i; int i;
/* load the routers file */ /* load the routers file */
router_array = getrouters(options[RouterFile].r.str,&rarray_len); router_array = getrouters(options[RouterFile].r.str,&rarray_len, options[ORPort].r.i);
if (!router_array) if (!router_array)
{ {
log(LOG_ERR,"Error loading router list."); log(LOG_ERR,"Error loading router list.");

View File

@ -549,7 +549,7 @@ tracked_onion_t *id_tracked_onion(unsigned char *onion, uint32_t onionlen, track
/********************************* routers.c ***************************/ /********************************* routers.c ***************************/
routerinfo_t **getrouters(char *routerfile, size_t *listlenp); routerinfo_t **getrouters(char *routerfile, size_t *listlenp, uint16_t or_listenport);
void delete_routerlist(routerinfo_t *list); void delete_routerlist(routerinfo_t *list);
/* create an NULL-terminated array of pointers pointing to elements of a router list */ /* create an NULL-terminated array of pointers pointing to elements of a router list */
routerinfo_t **make_rarray(routerinfo_t* list, size_t *listlenp); routerinfo_t **make_rarray(routerinfo_t* list, size_t *listlenp);

View File

@ -10,6 +10,42 @@
#include "or.h" #include "or.h"
/* private function, to determine whether the current entry in the router list is actually us */
static int routers_is_us(uint32_t or_address, uint16_t or_listenport, uint16_t my_or_listenport)
{
/* local host information */
char localhostname[512];
struct hostent *localhost;
char *addr = NULL;
int i = 0;
/* obtain local host information */
if (gethostname(localhostname,512) < 0) {
log(LOG_ERR,"Error obtaining local hostname.");
return -1;
}
localhost = gethostbyname(localhostname);
if (!localhost) {
log(LOG_ERR,"Error obtaining local host info.");
return -1;
}
/* check host addresses for a match with or_address above */
addr = localhost->h_addr_list[i++]; /* set to the first local address */
while(addr)
{
if (!memcmp((void *)&or_address, (void *)addr, sizeof(uint32_t))) { /* addresses match */
if (or_listenport == htons(my_or_listenport)) /* ports also match */
return 1;
}
addr = localhost->h_addr_list[i++];
}
return 0;
}
/* delete a list of routers from memory */ /* delete a list of routers from memory */
void delete_routerlist(routerinfo_t *list) void delete_routerlist(routerinfo_t *list)
{ {
@ -76,7 +112,7 @@ routerinfo_t **make_rarray(routerinfo_t* list, size_t *len)
} }
/* load the router list */ /* load the router list */
routerinfo_t **getrouters(char *routerfile, size_t *lenp) routerinfo_t **getrouters(char *routerfile, size_t *lenp, uint16_t or_listenport)
{ {
int retval = 0; int retval = 0;
char *retp = NULL; char *retp = NULL;
@ -273,6 +309,10 @@ routerinfo_t **getrouters(char *routerfile, size_t *lenp)
delete_routerlist(routerlist); delete_routerlist(routerlist);
return NULL; return NULL;
} }
/* check that this router doesn't actually represent us */
retval = routers_is_us(router->addr, router->or_port, or_listenport);
if (!retval) { /* this isn't us, continue */
router->next = NULL; router->next = NULL;
/* save the entry into the routerlist linked list */ /* save the entry into the routerlist linked list */
if (!routerlist) /* this is the first entry */ if (!routerlist) /* this is the first entry */
@ -281,6 +321,23 @@ routerinfo_t **getrouters(char *routerfile, size_t *lenp)
lastrouter->next = (void *)router; lastrouter->next = (void *)router;
lastrouter = router; lastrouter = router;
} }
else if (retval == 1) /* this is us, ignore */
{
log(LOG_DEBUG,"getrouters(): This entry is actually me. Ignoring.");
free((void *)router->address);
RSA_free(router->pkey);
free((void *)router);
}
else /* routers_is_us() returned an error */
{
free((void *)router->address);
RSA_free(router->pkey);
free((void *)router);
fclose(rf);
delete_routerlist(routerlist);
return NULL;
}
}
} }
else /* maximum link utilisation is zero */ else /* maximum link utilisation is zero */
{ {