diff options
author | Jose Bollo <jose.bollo@iot.bzh> | 2019-10-30 17:15:23 +0100 |
---|---|---|
committer | Jose Bollo <jose.bollo@iot.bzh> | 2019-11-04 12:03:31 +0100 |
commit | 195d93caaec446b2cbca2059b41803498a0b30c9 (patch) | |
tree | 31e9ec5ca099d7261e9fd82e9008ab64eb3b82af /src/cyn.c | |
parent | 3ab6b486ca043d237b89fe530cb10ca89b489344 (diff) |
Improve management of agent names
Signed-off-by: Jose Bollo <jose.bollo@iot.bzh>
Diffstat (limited to 'src/cyn.c')
-rw-r--r-- | src/cyn.c | 39 |
1 files changed, 26 insertions, 13 deletions
@@ -367,17 +367,27 @@ static struct agent * search_agent( const char *name, - size_t length, + uint8_t length, struct agent ***ppprev ) { struct agent *it, **pprev; pprev = &agents; - while((it = *pprev) - && ((uint8_t)length != it->len || memcmp(it->name, name, length))) + it = agents; + while(it && length > it->len) { pprev = &it->next; + it = it->next; + } + while(it && length == it->len) { + if (!memcmp(it->name, name, length)) { + *ppprev = pprev; + return it; + } + pprev = &it->next; + it = it->next; + } *ppprev = pprev; - return it; + return NULL; } /** @@ -392,12 +402,15 @@ struct agent* required_agent( const char *value ) { - struct agent **pprev; + struct agent **pprev, *agent; size_t length; for (length = 0 ; length <= UINT8_MAX && value[length] ; length++) - if (value[length] == AGENT_SEPARATOR_CHARACTER) - return search_agent(value, length, &pprev); + if (value[length] == AGENT_SEPARATOR_CHARACTER) { + agent = search_agent(value, (uint8_t)length, &pprev); + if (agent) + return agent; + } return NULL; } @@ -565,7 +578,7 @@ cyn_agent_add( void *closure ) { struct agent *agent, **pprev; - size_t length; + uint8_t length; /* compute and check name */ length = agent_check_name(name); @@ -578,16 +591,16 @@ cyn_agent_add( return -EEXIST; /* allocates the memory */ - agent = malloc(sizeof *agent + 1 + length); + agent = malloc(sizeof *agent + 1 + (size_t)length); if (!agent) return -ENOMEM; /* initialize the agent */ - agent->next = 0; agent->agent_cb = agent_cb; agent->closure = closure; - agent->len = (uint8_t)length; - memcpy(agent->name, name, length + 1); + agent->len = length; + memcpy(agent->name, name, 1 + (size_t)length); + agent->next = *pprev; *pprev = agent; return 0; @@ -599,7 +612,7 @@ cyn_agent_remove_by_name( const char *name ) { struct agent *agent, **pprev; - size_t length; + uint8_t length; /* compute and check name length */ length = agent_check_name(name); |