Skip to content

Commit

Permalink
[dns_cache] Fix restoring IP array in hostend struct
Browse files Browse the repository at this point in the history
When restoring (from cache) a hostend structure, be sure you end the "aliases" and "addresses" arrays with a NULL element.
Without that NULL terminator, when doing DNS-based failover on a cached hostend, OpenSIPS will use garbage IPs from the addresses array.
  • Loading branch information
bogdan-iancu committed Dec 17, 2024
1 parent e6a0c98 commit 7d31fef
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions modules/dns_cache/dns_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ static void destroy(void)
static int rdata_struct_len=sizeof(struct rdata)-sizeof(void *) -
sizeof(struct rdata *);

#define MAXALIASES 36
#define MAXADDRS 36
static unsigned char *he_buf=NULL;
static int he_buf_len=0;
static char* serialize_he_rdata(struct hostent *he,int *buf_len,int do_encoding)
Expand All @@ -165,7 +167,7 @@ static char* serialize_he_rdata(struct hostent *he,int *buf_len,int do_encoding)
len+=strlen(he->h_name)+1;

if (he->h_aliases)
for (i=0;he->h_aliases[i];i++) {
for (i=0;he->h_aliases[i]&&alias_no<MAXALIASES-1;i++) {
/* integer with len + len bytes of alias */
len+=strlen(he->h_aliases[i])+1+sizeof(int);
alias_no++;
Expand All @@ -174,7 +176,7 @@ static char* serialize_he_rdata(struct hostent *he,int *buf_len,int do_encoding)

i=0;
if (he->h_addr_list)
for (i=0;he->h_addr_list[i];i++) {
for (i=0;he->h_addr_list[i]&&addr_no<MAXADDRS-1;i++) {
len+=he->h_length;
addr_no++;
}
Expand Down Expand Up @@ -219,7 +221,7 @@ static char* serialize_he_rdata(struct hostent *he,int *buf_len,int do_encoding)

/* copy aliases, if any */
if (he->h_aliases)
for (i=0;he->h_aliases[i];i++) {
for (i=0;he->h_aliases[i];i++) {
len=strlen(he->h_aliases[i])+1;
/* copy alias length */
memcpy(p,&len,sizeof(int));
Expand All @@ -235,7 +237,7 @@ static char* serialize_he_rdata(struct hostent *he,int *buf_len,int do_encoding)

/* copy addresses */
if (he->h_addr_list)
for (i=0;he->h_addr_list[i];i++) {
for (i=0;he->h_addr_list[i];i++) {
/* copy addreses. length will be known from the addrtype field */
len=he->h_length;
memcpy(p,he->h_addr_list[i],len);
Expand All @@ -261,8 +263,6 @@ static char* serialize_he_rdata(struct hostent *he,int *buf_len,int do_encoding)
static unsigned char *dec_he_buf=NULL;
static int dec_he_buf_len=0;
static struct hostent dec_global_he;
#define MAXALIASES 36
#define MAXADDRS 36
static char *h_addr_ptrs[MAXADDRS];
static char *host_aliases[MAXALIASES];
static struct hostent* deserialize_he_rdata(char *buff,int buf_len,int do_decoding)
Expand Down Expand Up @@ -291,10 +291,8 @@ static struct hostent* deserialize_he_rdata(char *buff,int buf_len,int do_decodi

/* set pointer in dec_global_he */
ap = host_aliases;
*ap = NULL;
dec_global_he.h_aliases = host_aliases;
hap = h_addr_ptrs;
*hap = NULL;
dec_global_he.h_addr_list = h_addr_ptrs;

if (do_decoding) {
Expand Down Expand Up @@ -331,6 +329,7 @@ static struct hostent* deserialize_he_rdata(char *buff,int buf_len,int do_decodi
*ap++ = (char *)p;
p+=len;
}
*ap = NULL;

/* get number of addresses */
memcpy(&addr_no,p,sizeof(int));
Expand All @@ -341,6 +340,7 @@ static struct hostent* deserialize_he_rdata(char *buff,int buf_len,int do_decodi
*hap++ = (char *)p;
p+=dec_global_he.h_length;
}
*hap = NULL;

return &dec_global_he;
}
Expand Down

0 comments on commit 7d31fef

Please sign in to comment.