From 7d31fef29eefca30a8b8be668eebf1694e4df4a5 Mon Sep 17 00:00:00 2001 From: Bogdan-Andrei Iancu Date: Tue, 17 Dec 2024 12:50:24 +0200 Subject: [PATCH] [dns_cache] Fix restoring IP array in hostend struct 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. --- modules/dns_cache/dns_cache.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/dns_cache/dns_cache.c b/modules/dns_cache/dns_cache.c index d9a4a95ff7..2964568cae 100644 --- a/modules/dns_cache/dns_cache.c +++ b/modules/dns_cache/dns_cache.c @@ -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) @@ -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_noh_aliases[i])+1+sizeof(int); alias_no++; @@ -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_noh_length; addr_no++; } @@ -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)); @@ -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); @@ -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) @@ -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) { @@ -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)); @@ -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; }