Use new split function and strcmpstart correctly

svn:r2327
This commit is contained in:
Nick Mathewson 2004-09-02 18:39:59 +00:00
parent 4c799ae731
commit bda41ba3fd
3 changed files with 45 additions and 59 deletions

View File

@ -154,7 +154,8 @@ static int config_compare(struct config_line_t *c, const char *key, config_type_
case CONFIG_TYPE_CSV: case CONFIG_TYPE_CSV:
if(*(smartlist_t**)arg == NULL) if(*(smartlist_t**)arg == NULL)
*(smartlist_t**)arg = smartlist_create(); *(smartlist_t**)arg = smartlist_create();
smartlist_split_string(*(smartlist_t**)arg, c->value, ",", 1); smartlist_split_string(*(smartlist_t**)arg, c->value, ",",
SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
break; break;
case CONFIG_TYPE_LINELIST: case CONFIG_TYPE_LINELIST:
/* Note: this reverses the order that the lines appear in. That's /* Note: this reverses the order that the lines appear in. That's
@ -980,9 +981,7 @@ config_parse_exit_policy(struct config_line_t *cfg,
struct exit_policy_t **dest) struct exit_policy_t **dest)
{ {
struct exit_policy_t **nextp; struct exit_policy_t **nextp;
char *e, *s; smartlist_t *entries;
int last=0;
char line[1024];
if (!cfg) if (!cfg)
return; return;
@ -990,30 +989,22 @@ config_parse_exit_policy(struct config_line_t *cfg,
while (*nextp) while (*nextp)
nextp = &((*nextp)->next); nextp = &((*nextp)->next);
entries = smartlist_create();
for (; cfg; cfg = cfg->next) { for (; cfg; cfg = cfg->next) {
s = cfg->value; smartlist_split_string(entries,cfg->value,",",SPLIT_SKIP_SPACE,0);
for (;;) { SMARTLIST_FOREACH(entries, const char *, ent, {
e = strchr(s,','); log_fn(LOG_DEBUG,"Adding new entry '%s'",ent);
if(!e) { *nextp = router_parse_exit_policy_from_string(ent);
last = 1;
strncpy(line,s,1023);
} else {
memcpy(line,s, ((e-s)<1023)?(e-s):1023);
line[e-s] = 0;
}
line[1023]=0;
log_fn(LOG_DEBUG,"Adding new entry '%s'",line);
*nextp = router_parse_exit_policy_from_string(line);
if(*nextp) { if(*nextp) {
nextp = &((*nextp)->next); nextp = &((*nextp)->next);
} else { } else {
log_fn(LOG_WARN,"Malformed exit policy %s; skipping.", line); log_fn(LOG_WARN,"Malformed exit policy %s; skipping.", ent);
} }
if (last) });
break; SMARTLIST_FOREACH(entries, char *, ent, tor_free(ent));
s = e+1; smartlist_clear(entries);
}
} }
smartlist_free(entries);
} }
void exit_policy_free(struct exit_policy_t *p) { void exit_policy_free(struct exit_policy_t *p) {

View File

@ -155,25 +155,22 @@ int all_directory_servers_down(void) {
void void
add_nickname_list_to_smartlist(smartlist_t *sl, const char *list, int warn_if_down) add_nickname_list_to_smartlist(smartlist_t *sl, const char *list, int warn_if_down)
{ {
const char *start,*end;
char nick[MAX_HEX_NICKNAME_LEN+1];
routerinfo_t *router; routerinfo_t *router;
smartlist_t *nickname_list;
tor_assert(sl); tor_assert(sl);
tor_assert(list); tor_assert(list);
while(isspace((int)*list) || *list==',') list++; nickname_list = smartlist_create();
start = list; smartlist_split_string(nickname_list, list, ",",
while(*start) { SPLIT_SKIP_SPACE|SPLIT_IGNORE_BLANK, 0);
end=start; while(*end && !isspace((int)*end) && *end != ',') end++;
if (end-start > MAX_HEX_NICKNAME_LEN) { SMARTLIST_FOREACH(nickname_list, const char *, nick, {
if (strlen(nick) > MAX_HEX_NICKNAME_LEN) {
log_fn(LOG_WARN,"Nickname too long; skipping"); log_fn(LOG_WARN,"Nickname too long; skipping");
start = end;
continue; continue;
} }
memcpy(nick,start,end-start);
nick[end-start] = 0; /* null terminate it */
router = router_get_by_nickname(nick); router = router_get_by_nickname(nick);
if (router) { if (router) {
if (router->is_running) if (router->is_running)
@ -184,9 +181,9 @@ add_nickname_list_to_smartlist(smartlist_t *sl, const char *list, int warn_if_do
} else } else
log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO, log_fn(has_fetched_directory ? LOG_WARN : LOG_INFO,
"Nickname list includes '%s' which isn't a known router.",nick); "Nickname list includes '%s' which isn't a known router.",nick);
while(isspace((int)*end) || *end==',') end++; });
start = end; SMARTLIST_FOREACH(nickname_list, char *, nick, tor_free(nick));
} smartlist_free(nickname_list);
} }
/** Add every router from our routerlist that is currently running to /** Add every router from our routerlist that is currently running to

View File

@ -172,7 +172,7 @@ get_recommended_software_from_directory(const char *str)
const char *cp = str, *eol; const char *cp = str, *eol;
int len = strlen(REC); int len = strlen(REC);
cp = str; cp = str;
if (strncmp(str, REC, len)==0) { if (strcmpstart(str, REC)==0) {
cp += len; cp += len;
} else { } else {
cp = strstr(str, "\n"REC); cp = strstr(str, "\n"REC);
@ -196,10 +196,10 @@ get_recommended_software_from_directory(const char *str)
/* static */ int is_obsolete_version(const char *myversion, /* static */ int is_obsolete_version(const char *myversion,
const char *versionlist) { const char *versionlist) {
const char *vl; const char *vl;
char *version, *comma, *cp;
tor_version_t mine, other; tor_version_t mine, other;
int found_newer = 0, r; int found_newer = 0, r, ret;
static int warned_too_new=0; static int warned_too_new=0;
smartlist_t *version_sl;
vl = versionlist; vl = versionlist;
@ -209,14 +209,11 @@ get_recommended_software_from_directory(const char *str)
log_fn(LOG_ERR, "I couldn't parse my own version (%s)", myversion); log_fn(LOG_ERR, "I couldn't parse my own version (%s)", myversion);
tor_assert(0); tor_assert(0);
} }
version_sl = smartlist_create();
smartlist_split_string(version_sl, versionlist, ",", SPLIT_SKIP_SPACE, 0);
for(;;) { SMARTLIST_FOREACH(version_sl, const char *, cp, {
comma = strchr(vl, ','); if (!strcmpstart(cp, "Tor "))
version = tor_strndup(vl, comma?(comma-vl):strlen(vl));
cp = version;
while (isspace((int)*cp))
++cp;
if (!strncmp(cp, "Tor ", 4))
cp += 4; cp += 4;
if (tor_version_parse(cp, &other)) { if (tor_version_parse(cp, &other)) {
@ -224,28 +221,29 @@ get_recommended_software_from_directory(const char *str)
} else { } else {
r = tor_version_compare(&mine, &other); r = tor_version_compare(&mine, &other);
if (r==0) { if (r==0) {
tor_free(version); ret = 0;
return 0; /* It's a match. */ goto done;
} else if (r<0) { } else if (r<0) {
found_newer = 1; found_newer = 1;
} }
} }
tor_free(version); });
if (comma)
vl = comma+1;
else
break;
}
if (!found_newer) { if (!found_newer) {
if (!warned_too_new) { if (!warned_too_new) {
log_fn(LOG_WARN, "This version of Tor (%s) is newer than any on the recommended list (%s)", log_fn(LOG_WARN, "This version of Tor (%s) is newer than any on the recommended list (%s)",
myversion, versionlist); myversion, versionlist);
warned_too_new=1; warned_too_new=1;
} }
return 0; ret = 0;
} else { } else {
return 1; ret = 1;
} }
done:
SMARTLIST_FOREACH(version_sl, char *, version, tor_free(version));
smartlist_free(version_sl);
return ret;
} }
/* Return 0 if myversion is supported; else log a message and return /* Return 0 if myversion is supported; else log a message and return
@ -1162,13 +1160,13 @@ get_next_token(const char **s, where_syntax where) {
} }
} }
*s = eat_whitespace(*s); *s = eat_whitespace(*s);
if (strncmp(*s, "-----BEGIN ", 11)) { if (strcmpstart(*s, "-----BEGIN ")) {
goto done_tokenizing; goto done_tokenizing;
} }
obstart = *s; obstart = *s;
*s += 11; /* length of "-----BEGIN ". */ *s += 11; /* length of "-----BEGIN ". */
next = strchr(*s, '\n'); next = strchr(*s, '\n');
if (next-*s < 6 || strncmp(next-5, "-----\n", 6)) { if (next-*s < 6 || strcmpstart(next-5, "-----\n")) {
RET_ERR("Malformed object: bad begin line"); RET_ERR("Malformed object: bad begin line");
} }
tok->object_type = tor_strndup(*s, next-*s-5); tok->object_type = tor_strndup(*s, next-*s-5);
@ -1178,7 +1176,7 @@ get_next_token(const char **s, where_syntax where) {
RET_ERR("Malformed object: missing end line"); RET_ERR("Malformed object: missing end line");
} }
if (!strcmp(tok->object_type, "RSA PUBLIC KEY")) { if (!strcmp(tok->object_type, "RSA PUBLIC KEY")) {
if (strncmp(next, "-----END RSA PUBLIC KEY-----\n", 29)) if (strcmpstart(next, "-----END RSA PUBLIC KEY-----\n"))
RET_ERR("Malformed object: mismatched end line"); RET_ERR("Malformed object: mismatched end line");
next = strchr(next,'\n')+1; next = strchr(next,'\n')+1;
tok->key = crypto_new_pk_env(); tok->key = crypto_new_pk_env();
@ -1194,7 +1192,7 @@ get_next_token(const char **s, where_syntax where) {
tok->object_size = i; tok->object_size = i;
*s = next + 9; /* length of "-----END ". */ *s = next + 9; /* length of "-----END ". */
i = strlen(tok->object_type); i = strlen(tok->object_type);
if (strncmp(*s, tok->object_type, i) || strncmp(*s+i, "-----\n", 6)) { if (strncmp(*s, tok->object_type, i) || strcmpstart(*s+i, "-----\n")) {
RET_ERR("Malformed object: mismatched end tag"); RET_ERR("Malformed object: mismatched end tag");
} }
*s += i+6; *s += i+6;