| 157 | static unsigned char *he_buf=NULL; |
| 158 | static int he_buf_len=0; |
| 159 | static char* serialize_he_rdata(struct hostent *he,int *buf_len,int do_encoding) |
| 160 | { |
| 161 | unsigned char *p; |
| 162 | int i,len=0,needed_len=0,base64_len=0,alias_no=0,addr_no=0; |
| 163 | |
| 164 | /* addr_type, name_len, alias_no, addr_no */ |
| 165 | len+=sizeof(int)*4; |
| 166 | |
| 167 | /* compute needed buffer length */ |
| 168 | if (he->h_name) |
| 169 | len+=strlen(he->h_name)+1; |
| 170 | |
| 171 | if (he->h_aliases) |
| 172 | for (i=0;he->h_aliases[i]&&alias_no<MAXALIASES-1;i++) { |
| 173 | /* integer with len + len bytes of alias */ |
| 174 | len+=strlen(he->h_aliases[i])+1+sizeof(int); |
| 175 | alias_no++; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | i=0; |
| 180 | if (he->h_addr_list) |
| 181 | for (i=0;he->h_addr_list[i]&&addr_no<MAXADDRS-1;i++) { |
| 182 | len+=he->h_length; |
| 183 | addr_no++; |
| 184 | } |
| 185 | |
| 186 | if (do_encoding) { |
| 187 | /* backend does not support binary values - allocate continuous buffer |
| 188 | for encoding */ |
| 189 | base64_len = calc_base64_encode_len(len); |
| 190 | needed_len=len+base64_len; |
| 191 | } else |
| 192 | needed_len = len; |
| 193 | |
| 194 | if (he_buf == NULL || needed_len > he_buf_len) { |
| 195 | /* realloc if not enough space */ |
| 196 | he_buf = pkg_realloc(he_buf,needed_len); |
| 197 | if (he_buf == NULL) { |
| 198 | LM_ERR("No more pkg\n"); |
| 199 | return NULL; |
| 200 | } |
| 201 | he_buf_len = needed_len; |
| 202 | } |
| 203 | |
| 204 | p = he_buf; |
| 205 | |
| 206 | /* copy address type */ |
| 207 | memcpy(p,&he->h_addrtype,sizeof(int)); |
| 208 | p+=sizeof(int); |
| 209 | |
| 210 | if (he->h_name) { |
| 211 | /* copy h_name len */ |
| 212 | len=strlen(he->h_name)+1; |
| 213 | memcpy(p,&len,sizeof(int)); |
| 214 | p+=sizeof(int); |
| 215 | /* copy h_name */ |
| 216 | memcpy(p,he->h_name,len); |
no test coverage detected