Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a function to parse a srv reply with ttl #393

Closed
wants to merge 51 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
7a26cb6
add a function to parse a srv reply with ttl
kylebevans Jan 25, 2021
4c9f2b6
remove print statements
kylebevans Jan 25, 2021
ab66778
make ares_parse_srv_reply a wrapper for cares_parse_srv_reply
kylebevans Jan 30, 2021
73d8bf5
return const char* from cares_srv_reply_get_host
kylebevans Jan 30, 2021
eb34dbe
use ares_strdup in ares_parse_srv_reply to copy host string
kylebevans Jan 31, 2021
a86be7e
add new structs and parse functions to support TTL for these RRs:
kylebevans Mar 10, 2021
ce230f7
check hostent->h_addr_list[0] before free
kylebevans Mar 10, 2021
7078113
Merge branch 'master' into kylebevans-ttl
kylebevans Mar 10, 2021
3d06d06
fix loop off by one for freeing h_aliases
kylebevans Mar 10, 2021
a6c585c
modify ptr parsing to use the last hostname as h_name
kylebevans Mar 11, 2021
b55ccb9
use ptr_curr instead of ptr_out to get h_name
kylebevans Mar 11, 2021
08de712
fix memory leaks
kylebevans Mar 13, 2021
7eae6d8
remove debug prints and fix unrelated Container test leak
kylebevans Mar 13, 2021
a82fed6
Merge branch 'main' into kylebevans-ttl
kylebevans Nov 7, 2021
3179732
406 - Validate hostnames in DNS responses
kylebevans Nov 7, 2021
1e03b81
fix a missed a conflict in ares.h
kylebevans Nov 7, 2021
d36bec7
add is_hostname parameter back in one spot
kylebevans Nov 7, 2021
2814894
change struct cares_ to just cares_ for declarations
kylebevans Nov 7, 2021
7fcb191
change ttl in ares_uri_reply to be unsigned int
kylebevans Nov 7, 2021
487d072
change ARES_DATATYPE_C prefix to CARES_DATATYPE_ prefix
kylebevans Nov 7, 2021
97143cc
return const from cares_get_next functions
kylebevans Nov 12, 2021
699a9cd
create cares_memdup function as sugar for malloc/memcpy pattern
kylebevans Nov 12, 2021
3286a5d
save progress
kylebevans Dec 29, 2021
aed6a71
save progress
kylebevans Dec 29, 2021
baf30b8
save progress
kylebevans Dec 30, 2021
6983bc3
add free routine for container
kylebevans Dec 30, 2021
39c8e55
add malloc routine for container
kylebevans Dec 31, 2021
385a3fe
change cares_parse_srv_reply to use container
kylebevans Dec 31, 2021
697b9fd
change ares_parse_srv_reply to use container
kylebevans Dec 31, 2021
de092f3
fix errors and warnings
kylebevans Dec 31, 2021
9583762
fix errors and warnings
kylebevans Dec 31, 2021
e22efd5
add debugging
kylebevans Jan 1, 2022
f349105
fix crashes and add debugging
kylebevans Jan 2, 2022
867e3f7
fix crashes
kylebevans Jan 2, 2022
0f5ba50
remove print statements
kylebevans Jan 2, 2022
9268be0
Add build/test instructions for asan
kylebevans Jan 2, 2022
13aec44
update build/test instructions for asan
kylebevans Jan 2, 2022
86daca3
update build/test instructions for asan
kylebevans Jan 2, 2022
9f7b8b9
update build/test instructions for asan
kylebevans Jan 2, 2022
487cb0c
attempt to fix leak
kylebevans Jan 2, 2022
84d87cb
attempt to fix leak
kylebevans Jan 2, 2022
f131cf2
add missing break statement
kylebevans Jan 2, 2022
8bca0fa
free srv_host on error
kylebevans Jan 2, 2022
979f715
add debugging
kylebevans Jan 2, 2022
183a999
fix leak if no srv rr type
kylebevans Jan 3, 2022
e247b6e
add fuzzing loop instrution
kylebevans Jan 3, 2022
e1bb47d
fix typo for cares_free_container.h
kylebevans Jan 3, 2022
73248f7
remove debugging print statements
kylebevans Jan 3, 2022
337db1d
Update instructions for lsan
kylebevans Jan 4, 2022
be37e6a
Update instructions for lsan
kylebevans Jan 4, 2022
e82ff03
change srv to csrv
kylebevans Jan 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix errors and warnings
  • Loading branch information
kylebevans committed Dec 31, 2021
commit 9583762c4e4b0619f28ea0cdbf7f8b40f3e284e2
4 changes: 2 additions & 2 deletions include/ares.h
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ CARES_EXTERN const cares_srv_reply*
cares_srv_reply_container_get_first(const cares_srv_reply_container* container);

CARES_EXTERN const cares_srv_reply*
cares_srv_reply_container_get_next(const cares_srv_reply_container* container);
cares_srv_reply_container_get_next(cares_srv_reply_container* container);

CARES_EXTERN const cares_srv_reply*
cares_srv_reply_container_get_last(const cares_srv_reply_container* container);
Expand All @@ -784,7 +784,7 @@ cares_srv_reply_container_get_count(const cares_srv_reply_container* container);
CARES_EXTERN unsigned int
cares_srv_reply_container_get_curr(const cares_srv_reply_container* container);

CARES_EXTERN bool
CARES_EXTERN int
cares_srv_reply_container_at_end(const cares_srv_reply_container* container);

CARES_EXTERN const char*
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ares_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ struct cares_srv_reply {
};

struct cares_srv_reply_container {
cares_srv_reply *replies;
cares_srv_reply **replies;
unsigned int curr;
unsigned int count;
};
Expand Down Expand Up @@ -534,7 +534,7 @@ void cares_srv_reply_set_ttl(cares_srv_reply* srv_reply,
const unsigned int ttl);

void cares_srv_reply_container_set_replies(cares_srv_reply_container* container,
cares_srv_reply* replies);
cares_srv_reply** replies);

void cares_srv_reply_container_set_curr(cares_srv_reply_container* container,
const unsigned int index);
Expand Down
23 changes: 12 additions & 11 deletions src/lib/cares_container.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,37 @@
const cares_srv_reply*
cares_srv_reply_container_get_first(const cares_srv_reply_container* container)
{
return &container[0];
return container->replies[0];
}

const cares_srv_reply*
cares_srv_reply_container_get_next(const cares_srv_reply_container* container)
cares_srv_reply_container_get_next(cares_srv_reply_container* container)
{
if (cares_srv_reply_container_get_count(container) == 0)
{
return container->replies;
return container->replies[0];
}

if (container->curr == cares_srv_reply_container_get_count(container) - 1)
{
return &container->replies[cares_srv_reply_container_get_count(container) - 1];
return container->replies[cares_srv_reply_container_get_count(container) - 1];
}

cares_srv_reply_container_set_curr(container,
cares_srv_reply_container_get_curr(container) + 1);

return &container->replies[container->curr];
return container->replies[container->curr];
}

const cares_srv_reply*
cares_srv_reply_container_get_last(const cares_srv_reply_container* container)
{
if (cares_srv_reply_container_get_count(container) == 0)
{
return container->replies;
return container->replies[0];
}

return &container->replies[cares_srv_reply_container_get_count(container) - 1];
return container->replies[cares_srv_reply_container_get_count(container) - 1];
}

unsigned int
Expand All @@ -64,14 +64,15 @@ cares_srv_reply_container_get_curr(const cares_srv_reply_container* container)
return container->curr;
}

bool
int
cares_srv_reply_container_at_end(const cares_srv_reply_container* container)
{
return cares_srv_reply_get_curr(container) == cares_srv_reply_get_count(container);
return cares_srv_reply_container_get_curr(container) ==
cares_srv_reply_container_get_count(container);
}

void cares_srv_reply_container_set_replies(cares_srv_reply_container* container,
cares_srv_reply* replies)
cares_srv_reply** replies)
{
container->replies = replies;
}
Expand All @@ -86,4 +87,4 @@ void cares_srv_reply_container_set_count(cares_srv_reply_container* container,
const unsigned int count)
{
container->count = count;
}
}
27 changes: 21 additions & 6 deletions src/lib/cares_free_container.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,52 @@
* without express or implied warranty.
*/

#include <stddef.h>
#include "ares_setup.h"
#include "ares.h"
#include "ares_private.h"
#include "cares_free_container.h"
#include "ares_data.h"

void cares_free_container(void *containerptr)
{
if (containerptr == NULL) {
return;
}

struct cares_container *ptr;
unsigned int count;

ptr = (void *)((char *)containerptr - offsetof(struct cares_container, container));

if (ptr->mark != ARES_DATATYPE_MARK)
return;

for (int i = 0; i < ptr->count; ++i)
switch (ptr->type)
{
case CARES_CONTAINER_SRV_REPLY_CONTAINER:
count = ptr->container.srv_container.count;
break;

default:
return;
}

for (unsigned int i = 0; i < count; ++i)
{
curr = ptr->container->replies;
switch (ptr->type)
{
case CARES_CONTAINER_SRV_REPLY_CONTAINER:
ares_free_data((cares_srv_reply *)curr[i]);
ares_free_data(ptr->container.srv_container.replies[i]);
if (i == count - 1)
ares_free(ptr->container.srv_container.replies);
break;

default:
return;
}
}

ares_free(ptr->container->replies);
ares_free(ptr->container);
ares_free(ptr);
}

Expand Down Expand Up @@ -70,4 +85,4 @@ void *cares_malloc_container(cares_container_type type)
ptr->type = type;

return &ptr->container;
}
}
5 changes: 2 additions & 3 deletions src/lib/cares_free_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

#include "ares_private.h"
#include "ares_data.h"

typedef enum {
CARES_CONTAINER_SRV_REPLY_CONTAINER,
Expand All @@ -22,10 +21,10 @@ typedef enum {

struct cares_container {
cares_container_type type; /* Actual data type identifier. */
unsigned int mark; /* Private ares_data signature. */
unsigned int mark; /* Private ares_data signature. */
union {
struct cares_srv_reply_container srv_container;
} container;
};

void *cares_malloc_container(cares_container_type type);
void *cares_malloc_container(cares_container_type type);
11 changes: 5 additions & 6 deletions src/lib/cares_parse_srv_reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ int
cares_parse_srv_reply (const unsigned char *abuf, int alen,
cares_srv_reply_container **srv_out)
{
unsigned int qdcount, ancount, i;
unsigned int qdcount, ancount, i, count;
const unsigned char *aptr, *vptr;
int status, rr_type, rr_class, rr_len;
unsigned int rr_ttl;
Expand All @@ -49,8 +49,7 @@ cares_parse_srv_reply (const unsigned char *abuf, int alen,
// cares_srv_reply *srv_head = NULL;
// cares_srv_reply *srv_last = NULL;
cares_srv_reply *srv_curr;
cares_srv_reply *srv_replies = NULL;
int count;
cares_srv_reply **srv_replies = NULL;

/* Set *srv_out to NULL for all failure cases. */
*srv_out = NULL;
Expand All @@ -72,7 +71,7 @@ cares_parse_srv_reply (const unsigned char *abuf, int alen,
return ARES_ENODATA;

cares_srv_reply_container_set_count(*srv_out, ancount);
srv_replies = ares_malloc(ancount * sizeof(*srv_replies));
*srv_replies = ares_malloc(ancount * sizeof(**srv_replies));

/* Expand the name from the question, and skip past the question. */
aptr = abuf + HFIXEDSZ;
Expand Down Expand Up @@ -145,7 +144,7 @@ cares_parse_srv_reply (const unsigned char *abuf, int alen,
if (status != ARES_SUCCESS)
break;
cares_srv_reply_set_host(srv_curr, srv_host);
srv_replies[i] = *srv_curr;
srv_replies[i] = srv_curr;
}
else if (rr_type != T_CNAME)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain why T_CNAME is excluded? A short comment in the code would be great.
(Same for CAA, MX, NAPTR, NS, PTR, SRV, TXT)

{
Expand Down Expand Up @@ -181,6 +180,6 @@ cares_parse_srv_reply (const unsigned char *abuf, int alen,
}

/* everything looks fine, return the data */
*srv_out->replies = srv_replies;
(*srv_out)->replies = srv_replies;
return ARES_SUCCESS;
}
20 changes: 10 additions & 10 deletions src/lib/cares_reply.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
#include "string.h"


const cares_srv_reply*
cares_srv_reply_get_next(const cares_srv_reply* srv_reply)
{
return srv_reply->next;
}
// const cares_srv_reply*
// cares_srv_reply_get_next(const cares_srv_reply* srv_reply)
// {
// return srv_reply->next;
// }

const char* cares_srv_reply_get_host(const cares_srv_reply* srv_reply)
{
Expand Down Expand Up @@ -51,11 +51,11 @@ unsigned int cares_srv_reply_get_ttl(const cares_srv_reply* srv_reply)
return srv_reply->ttl;
}

void cares_srv_reply_set_next(cares_srv_reply* srv_reply,
cares_srv_reply* next)
{
srv_reply->next = next;
}
// void cares_srv_reply_set_next(cares_srv_reply* srv_reply,
// cares_srv_reply* next)
// {
// srv_reply->next = next;
// }

void cares_srv_reply_set_host(cares_srv_reply* srv_reply, char* host)
{
Expand Down