[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[1719464] | 4 | * Copyright 2002-2006 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 5 | \********************************************************************/ |
---|
| 6 | |
---|
| 7 | /* |
---|
[c88999c] | 8 | * Various utility functions. Some are copied from Gaim to support the |
---|
| 9 | * IM-modules, most are from BitlBee. |
---|
[b7d3cc34] | 10 | * |
---|
| 11 | * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> |
---|
| 12 | * (and possibly other members of the Gaim team) |
---|
[1719464] | 13 | * Copyright 2002-2006 Wilmer van der Gaast <wilmer@gaast.net> |
---|
[b7d3cc34] | 14 | */ |
---|
| 15 | |
---|
| 16 | /* |
---|
| 17 | This program is free software; you can redistribute it and/or modify |
---|
| 18 | it under the terms of the GNU General Public License as published by |
---|
| 19 | the Free Software Foundation; either version 2 of the License, or |
---|
| 20 | (at your option) any later version. |
---|
| 21 | |
---|
| 22 | This program is distributed in the hope that it will be useful, |
---|
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 25 | GNU General Public License for more details. |
---|
| 26 | |
---|
| 27 | You should have received a copy of the GNU General Public License with |
---|
| 28 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
| 29 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
| 30 | Suite 330, Boston, MA 02111-1307 USA |
---|
| 31 | */ |
---|
| 32 | |
---|
| 33 | #define BITLBEE_CORE |
---|
| 34 | #include "nogaim.h" |
---|
| 35 | #include <stdio.h> |
---|
| 36 | #include <stdlib.h> |
---|
| 37 | #include <string.h> |
---|
[dd8d4c5] | 38 | #include <ctype.h> |
---|
[b7d3cc34] | 39 | #include <glib.h> |
---|
| 40 | #include <time.h> |
---|
| 41 | |
---|
[36e9f62] | 42 | #ifdef HAVE_RESOLV_A |
---|
| 43 | #include <arpa/nameser.h> |
---|
| 44 | #include <resolv.h> |
---|
| 45 | #endif |
---|
| 46 | |
---|
[b7d3cc34] | 47 | void strip_linefeed(gchar *text) |
---|
| 48 | { |
---|
| 49 | int i, j; |
---|
| 50 | gchar *text2 = g_malloc(strlen(text) + 1); |
---|
| 51 | |
---|
| 52 | for (i = 0, j = 0; text[i]; i++) |
---|
| 53 | if (text[i] != '\r') |
---|
| 54 | text2[j++] = text[i]; |
---|
| 55 | text2[j] = '\0'; |
---|
| 56 | |
---|
| 57 | strcpy(text, text2); |
---|
| 58 | g_free(text2); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | char *normalize(const char *s) |
---|
| 62 | { |
---|
| 63 | static char buf[BUF_LEN]; |
---|
| 64 | char *t, *u; |
---|
| 65 | int x = 0; |
---|
| 66 | |
---|
| 67 | g_return_val_if_fail((s != NULL), NULL); |
---|
| 68 | |
---|
| 69 | u = t = g_strdup(s); |
---|
| 70 | |
---|
| 71 | strcpy(t, s); |
---|
| 72 | g_strdown(t); |
---|
| 73 | |
---|
| 74 | while (*t && (x < BUF_LEN - 1)) { |
---|
| 75 | if (*t != ' ') { |
---|
| 76 | buf[x] = *t; |
---|
| 77 | x++; |
---|
| 78 | } |
---|
| 79 | t++; |
---|
| 80 | } |
---|
| 81 | buf[x] = '\0'; |
---|
| 82 | g_free(u); |
---|
| 83 | return buf; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | time_t get_time(int year, int month, int day, int hour, int min, int sec) |
---|
| 87 | { |
---|
| 88 | struct tm tm; |
---|
| 89 | |
---|
| 90 | tm.tm_year = year - 1900; |
---|
| 91 | tm.tm_mon = month - 1; |
---|
| 92 | tm.tm_mday = day; |
---|
| 93 | tm.tm_hour = hour; |
---|
| 94 | tm.tm_min = min; |
---|
| 95 | tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; |
---|
| 96 | return mktime(&tm); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | typedef struct htmlentity |
---|
| 100 | { |
---|
[51fdc45] | 101 | char code[7]; |
---|
| 102 | char is[3]; |
---|
[b7d3cc34] | 103 | } htmlentity_t; |
---|
| 104 | |
---|
[39cc341] | 105 | static const htmlentity_t ent[] = |
---|
[b7d3cc34] | 106 | { |
---|
[39cc341] | 107 | { "lt", "<" }, |
---|
| 108 | { "gt", ">" }, |
---|
| 109 | { "amp", "&" }, |
---|
| 110 | { "quot", "\"" }, |
---|
| 111 | { "aacute", "á" }, |
---|
| 112 | { "eacute", "é" }, |
---|
| 113 | { "iacute", "é" }, |
---|
| 114 | { "oacute", "ó" }, |
---|
| 115 | { "uacute", "ú" }, |
---|
| 116 | { "agrave", "à" }, |
---|
| 117 | { "egrave", "è" }, |
---|
| 118 | { "igrave", "ì" }, |
---|
| 119 | { "ograve", "ò" }, |
---|
| 120 | { "ugrave", "ù" }, |
---|
| 121 | { "acirc", "â" }, |
---|
| 122 | { "ecirc", "ê" }, |
---|
| 123 | { "icirc", "î" }, |
---|
| 124 | { "ocirc", "ô" }, |
---|
| 125 | { "ucirc", "û" }, |
---|
| 126 | { "auml", "ä" }, |
---|
| 127 | { "euml", "ë" }, |
---|
| 128 | { "iuml", "ï" }, |
---|
| 129 | { "ouml", "ö" }, |
---|
| 130 | { "uuml", "ü" }, |
---|
| 131 | { "nbsp", " " }, |
---|
| 132 | { "", "" } |
---|
[b7d3cc34] | 133 | }; |
---|
| 134 | |
---|
| 135 | void strip_html( char *in ) |
---|
| 136 | { |
---|
| 137 | char *start = in; |
---|
| 138 | char *out = g_malloc( strlen( in ) + 1 ); |
---|
| 139 | char *s = out, *cs; |
---|
| 140 | int i, matched; |
---|
| 141 | |
---|
| 142 | memset( out, 0, strlen( in ) + 1 ); |
---|
| 143 | |
---|
| 144 | while( *in ) |
---|
| 145 | { |
---|
| 146 | if( *in == '<' && ( isalpha( *(in+1) ) || *(in+1) == '/' ) ) |
---|
| 147 | { |
---|
| 148 | /* If in points at a < and in+1 points at a letter or a slash, this is probably |
---|
| 149 | a HTML-tag. Try to find a closing > and continue there. If the > can't be |
---|
| 150 | found, assume that it wasn't a HTML-tag after all. */ |
---|
| 151 | |
---|
| 152 | cs = in; |
---|
| 153 | |
---|
| 154 | while( *in && *in != '>' ) |
---|
| 155 | in ++; |
---|
| 156 | |
---|
| 157 | if( *in ) |
---|
| 158 | { |
---|
| 159 | if( g_strncasecmp( cs+1, "br", 2) == 0 ) |
---|
| 160 | *(s++) = '\n'; |
---|
| 161 | in ++; |
---|
| 162 | } |
---|
| 163 | else |
---|
| 164 | { |
---|
| 165 | in = cs; |
---|
| 166 | *(s++) = *(in++); |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | else if( *in == '&' ) |
---|
| 170 | { |
---|
| 171 | cs = ++in; |
---|
| 172 | while( *in && isalpha( *in ) ) |
---|
| 173 | in ++; |
---|
| 174 | |
---|
| 175 | if( *in == ';' ) in ++; |
---|
| 176 | matched = 0; |
---|
| 177 | |
---|
| 178 | for( i = 0; *ent[i].code; i ++ ) |
---|
| 179 | if( g_strncasecmp( ent[i].code, cs, strlen( ent[i].code ) ) == 0 ) |
---|
| 180 | { |
---|
[39cc341] | 181 | int j; |
---|
| 182 | |
---|
| 183 | for( j = 0; ent[i].is[j]; j ++ ) |
---|
| 184 | *(s++) = ent[i].is[j]; |
---|
| 185 | |
---|
[b7d3cc34] | 186 | matched = 1; |
---|
| 187 | break; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /* None of the entities were matched, so return the string */ |
---|
| 191 | if( !matched ) |
---|
| 192 | { |
---|
| 193 | in = cs - 1; |
---|
| 194 | *(s++) = *(in++); |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | else |
---|
| 198 | { |
---|
| 199 | *(s++) = *(in++); |
---|
| 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | strcpy( start, out ); |
---|
| 204 | g_free( out ); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | char *escape_html( const char *html ) |
---|
| 208 | { |
---|
| 209 | const char *c = html; |
---|
| 210 | GString *ret; |
---|
| 211 | char *str; |
---|
| 212 | |
---|
| 213 | if( html == NULL ) |
---|
| 214 | return( NULL ); |
---|
| 215 | |
---|
| 216 | ret = g_string_new( "" ); |
---|
| 217 | |
---|
| 218 | while( *c ) |
---|
| 219 | { |
---|
| 220 | switch( *c ) |
---|
| 221 | { |
---|
| 222 | case '&': |
---|
| 223 | ret = g_string_append( ret, "&" ); |
---|
| 224 | break; |
---|
| 225 | case '<': |
---|
| 226 | ret = g_string_append( ret, "<" ); |
---|
| 227 | break; |
---|
| 228 | case '>': |
---|
| 229 | ret = g_string_append( ret, ">" ); |
---|
| 230 | break; |
---|
| 231 | case '"': |
---|
| 232 | ret = g_string_append( ret, """ ); |
---|
| 233 | break; |
---|
| 234 | default: |
---|
| 235 | ret = g_string_append_c( ret, *c ); |
---|
| 236 | } |
---|
| 237 | c ++; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | str = ret->str; |
---|
| 241 | g_string_free( ret, FALSE ); |
---|
| 242 | return( str ); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | void info_string_append(GString *str, char *newline, char *name, char *value) |
---|
| 246 | { |
---|
| 247 | if( value && value[0] ) |
---|
| 248 | g_string_sprintfa( str, "%s%s: %s", newline, name, value ); |
---|
| 249 | } |
---|
[c88999c] | 250 | |
---|
| 251 | /* Decode%20a%20file%20name */ |
---|
| 252 | void http_decode( char *s ) |
---|
| 253 | { |
---|
| 254 | char *t; |
---|
| 255 | int i, j, k; |
---|
| 256 | |
---|
| 257 | t = g_new( char, strlen( s ) + 1 ); |
---|
| 258 | |
---|
| 259 | for( i = j = 0; s[i]; i ++, j ++ ) |
---|
| 260 | { |
---|
| 261 | if( s[i] == '%' ) |
---|
| 262 | { |
---|
| 263 | if( sscanf( s + i + 1, "%2x", &k ) ) |
---|
| 264 | { |
---|
| 265 | t[j] = k; |
---|
| 266 | i += 2; |
---|
| 267 | } |
---|
| 268 | else |
---|
| 269 | { |
---|
| 270 | *t = 0; |
---|
| 271 | break; |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | else |
---|
| 275 | { |
---|
| 276 | t[j] = s[i]; |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | t[j] = 0; |
---|
| 280 | |
---|
| 281 | strcpy( s, t ); |
---|
| 282 | g_free( t ); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | /* Warning: This one explodes the string. Worst-cases can make the string 3x its original size! */ |
---|
| 286 | /* This fuction is safe, but make sure you call it safely as well! */ |
---|
| 287 | void http_encode( char *s ) |
---|
| 288 | { |
---|
| 289 | char *t; |
---|
| 290 | int i, j; |
---|
| 291 | |
---|
| 292 | t = g_strdup( s ); |
---|
| 293 | |
---|
| 294 | for( i = j = 0; t[i]; i ++, j ++ ) |
---|
| 295 | { |
---|
[dd8d4c5] | 296 | /* if( t[i] <= ' ' || ((unsigned char *)t)[i] >= 128 || t[i] == '%' ) */ |
---|
| 297 | if( !isalnum( t[i] ) ) |
---|
[c88999c] | 298 | { |
---|
| 299 | sprintf( s + j, "%%%02X", ((unsigned char*)t)[i] ); |
---|
| 300 | j += 2; |
---|
| 301 | } |
---|
| 302 | else |
---|
| 303 | { |
---|
| 304 | s[j] = t[i]; |
---|
| 305 | } |
---|
| 306 | } |
---|
| 307 | s[j] = 0; |
---|
| 308 | |
---|
| 309 | g_free( t ); |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | /* Strip newlines from a string. Modifies the string passed to it. */ |
---|
| 313 | char *strip_newlines( char *source ) |
---|
| 314 | { |
---|
| 315 | int i; |
---|
| 316 | |
---|
| 317 | for( i = 0; source[i] != '\0'; i ++ ) |
---|
| 318 | if( source[i] == '\n' || source[i] == '\r' ) |
---|
| 319 | source[i] = ' '; |
---|
| 320 | |
---|
| 321 | return source; |
---|
| 322 | } |
---|
[2a6ca4f] | 323 | |
---|
| 324 | #ifdef IPV6 |
---|
| 325 | /* Wrap an IPv4 address into IPv6 space. Not thread-safe... */ |
---|
| 326 | char *ipv6_wrap( char *src ) |
---|
| 327 | { |
---|
| 328 | static char dst[64]; |
---|
| 329 | int i; |
---|
| 330 | |
---|
| 331 | for( i = 0; src[i]; i ++ ) |
---|
| 332 | if( ( src[i] < '0' || src[i] > '9' ) && src[i] != '.' ) |
---|
| 333 | break; |
---|
| 334 | |
---|
| 335 | /* Hmm, it's not even an IP... */ |
---|
| 336 | if( src[i] ) |
---|
| 337 | return src; |
---|
| 338 | |
---|
| 339 | g_snprintf( dst, sizeof( dst ), "::ffff:%s", src ); |
---|
| 340 | |
---|
| 341 | return dst; |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | /* Unwrap an IPv4 address into IPv6 space. Thread-safe, because it's very simple. :-) */ |
---|
| 345 | char *ipv6_unwrap( char *src ) |
---|
| 346 | { |
---|
| 347 | int i; |
---|
| 348 | |
---|
| 349 | if( g_strncasecmp( src, "::ffff:", 7 ) != 0 ) |
---|
| 350 | return src; |
---|
| 351 | |
---|
| 352 | for( i = 7; src[i]; i ++ ) |
---|
| 353 | if( ( src[i] < '0' || src[i] > '9' ) && src[i] != '.' ) |
---|
| 354 | break; |
---|
| 355 | |
---|
| 356 | /* Hmm, it's not even an IP... */ |
---|
| 357 | if( src[i] ) |
---|
| 358 | return src; |
---|
| 359 | |
---|
| 360 | return ( src + 7 ); |
---|
| 361 | } |
---|
| 362 | #endif |
---|
[e27661d] | 363 | |
---|
| 364 | /* Convert from one charset to another. |
---|
| 365 | |
---|
| 366 | from_cs, to_cs: Source and destination charsets |
---|
| 367 | src, dst: Source and destination strings |
---|
| 368 | size: Size if src. 0 == use strlen(). strlen() is not reliable for UNICODE/UTF16 strings though. |
---|
| 369 | maxbuf: Maximum number of bytes to write to dst |
---|
| 370 | |
---|
| 371 | Returns the number of bytes written to maxbuf or -1 on an error. |
---|
| 372 | */ |
---|
| 373 | signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf ) |
---|
| 374 | { |
---|
[574af7e] | 375 | GIConv cd; |
---|
[e27661d] | 376 | size_t res; |
---|
| 377 | size_t inbytesleft, outbytesleft; |
---|
| 378 | char *inbuf = src; |
---|
| 379 | char *outbuf = dst; |
---|
| 380 | |
---|
[574af7e] | 381 | cd = g_iconv_open( to_cs, from_cs ); |
---|
| 382 | if( cd == (GIConv) -1 ) |
---|
[e27661d] | 383 | return( -1 ); |
---|
| 384 | |
---|
| 385 | inbytesleft = size ? size : strlen( src ); |
---|
| 386 | outbytesleft = maxbuf - 1; |
---|
[574af7e] | 387 | res = g_iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft ); |
---|
[e27661d] | 388 | *outbuf = '\0'; |
---|
[574af7e] | 389 | g_iconv_close( cd ); |
---|
[e27661d] | 390 | |
---|
| 391 | if( res == (size_t) -1 ) |
---|
| 392 | return( -1 ); |
---|
| 393 | else |
---|
| 394 | return( outbuf - dst ); |
---|
| 395 | } |
---|
| 396 | |
---|
[1719464] | 397 | /* A pretty reliable random number generator. Tries to use the /dev/random |
---|
| 398 | devices first, and falls back to the random number generator from libc |
---|
| 399 | when it fails. Opens randomizer devices with O_NONBLOCK to make sure a |
---|
| 400 | lack of entropy won't halt BitlBee. */ |
---|
| 401 | void random_bytes( unsigned char *buf, int count ) |
---|
[e27661d] | 402 | { |
---|
[1719464] | 403 | static int use_dev = -1; |
---|
| 404 | |
---|
| 405 | /* Actually this probing code isn't really necessary, is it? */ |
---|
| 406 | if( use_dev == -1 ) |
---|
| 407 | { |
---|
| 408 | if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 ) |
---|
| 409 | use_dev = 1; |
---|
| 410 | else |
---|
| 411 | { |
---|
| 412 | use_dev = 0; |
---|
| 413 | srand( ( getpid() << 16 ) ^ time( NULL ) ); |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | |
---|
| 417 | if( use_dev ) |
---|
| 418 | { |
---|
| 419 | int fd; |
---|
| 420 | |
---|
| 421 | /* At least on Linux, /dev/random can block if there's not |
---|
| 422 | enough entropy. We really don't want that, so if it can't |
---|
| 423 | give anything, use /dev/urandom instead. */ |
---|
| 424 | if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 ) |
---|
| 425 | if( read( fd, buf, count ) == count ) |
---|
| 426 | { |
---|
| 427 | close( fd ); |
---|
| 428 | return; |
---|
| 429 | } |
---|
| 430 | close( fd ); |
---|
| 431 | |
---|
| 432 | /* urandom isn't supposed to block at all, but just to be |
---|
| 433 | sure. If it blocks, we'll disable use_dev and use the libc |
---|
| 434 | randomizer instead. */ |
---|
| 435 | if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 ) |
---|
| 436 | if( read( fd, buf, count ) == count ) |
---|
| 437 | { |
---|
| 438 | close( fd ); |
---|
| 439 | return; |
---|
| 440 | } |
---|
| 441 | close( fd ); |
---|
| 442 | |
---|
| 443 | /* If /dev/random blocks once, we'll still try to use it |
---|
| 444 | again next time. If /dev/urandom also fails for some |
---|
| 445 | reason, stick with libc during this session. */ |
---|
| 446 | |
---|
| 447 | use_dev = 0; |
---|
| 448 | srand( ( getpid() << 16 ) ^ time( NULL ) ); |
---|
| 449 | } |
---|
| 450 | |
---|
| 451 | if( !use_dev ) |
---|
| 452 | { |
---|
| 453 | int i; |
---|
| 454 | |
---|
| 455 | /* Possibly the LSB of rand() isn't very random on some |
---|
| 456 | platforms. Seems okay on at least Linux and OSX though. */ |
---|
| 457 | for( i = 0; i < count; i ++ ) |
---|
| 458 | buf[i] = rand() & 0xff; |
---|
| 459 | } |
---|
[e27661d] | 460 | } |
---|
[5100caa] | 461 | |
---|
| 462 | int is_bool( char *value ) |
---|
| 463 | { |
---|
| 464 | if( *value == 0 ) |
---|
| 465 | return 0; |
---|
| 466 | |
---|
| 467 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
| 468 | return 1; |
---|
| 469 | if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
| 470 | return 1; |
---|
| 471 | |
---|
| 472 | while( *value ) |
---|
| 473 | if( !isdigit( *value ) ) |
---|
| 474 | return 0; |
---|
| 475 | else |
---|
| 476 | value ++; |
---|
| 477 | |
---|
| 478 | return 1; |
---|
| 479 | } |
---|
| 480 | |
---|
| 481 | int bool2int( char *value ) |
---|
| 482 | { |
---|
| 483 | int i; |
---|
| 484 | |
---|
| 485 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
| 486 | return 1; |
---|
| 487 | if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
| 488 | return 0; |
---|
| 489 | |
---|
| 490 | if( sscanf( value, "%d", &i ) == 1 ) |
---|
| 491 | return i; |
---|
| 492 | |
---|
| 493 | return 0; |
---|
| 494 | } |
---|
[36e9f62] | 495 | |
---|
| 496 | struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain ) |
---|
| 497 | { |
---|
| 498 | struct ns_srv_reply *reply = NULL; |
---|
| 499 | #ifdef HAVE_RESOLV_A |
---|
| 500 | char name[1024]; |
---|
| 501 | unsigned char querybuf[1024]; |
---|
| 502 | const unsigned char *buf; |
---|
| 503 | ns_msg nsh; |
---|
| 504 | ns_rr rr; |
---|
| 505 | int i, len, size; |
---|
| 506 | |
---|
| 507 | g_snprintf( name, sizeof( name ), "_%s._%s.%s", service, protocol, domain ); |
---|
| 508 | |
---|
| 509 | if( ( size = res_query( name, ns_c_in, ns_t_srv, querybuf, sizeof( querybuf ) ) ) <= 0 ) |
---|
| 510 | return NULL; |
---|
| 511 | |
---|
| 512 | if( ns_initparse( querybuf, size, &nsh ) != 0 ) |
---|
| 513 | return NULL; |
---|
| 514 | |
---|
| 515 | if( ns_parserr( &nsh, ns_s_an, 0, &rr ) != 0 ) |
---|
| 516 | return NULL; |
---|
| 517 | |
---|
| 518 | size = ns_rr_rdlen( rr ); |
---|
| 519 | buf = ns_rr_rdata( rr ); |
---|
| 520 | |
---|
| 521 | len = 0; |
---|
| 522 | for( i = 6; i < size && buf[i]; i += buf[i] + 1 ) |
---|
| 523 | len += buf[i] + 1; |
---|
| 524 | |
---|
| 525 | if( i > size ) |
---|
| 526 | return NULL; |
---|
| 527 | |
---|
| 528 | reply = g_malloc( sizeof( struct ns_srv_reply ) + len ); |
---|
| 529 | memcpy( reply->name, buf + 7, len ); |
---|
| 530 | |
---|
| 531 | for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 ) |
---|
| 532 | reply->name[i] = '.'; |
---|
| 533 | |
---|
| 534 | if( i > len ) |
---|
| 535 | { |
---|
| 536 | g_free( reply ); |
---|
| 537 | return NULL; |
---|
| 538 | } |
---|
| 539 | |
---|
| 540 | reply->prio = ( buf[0] << 8 ) | buf[1]; |
---|
| 541 | reply->weight = ( buf[2] << 8 ) | buf[3]; |
---|
| 542 | reply->port = ( buf[4] << 8 ) | buf[5]; |
---|
| 543 | #endif |
---|
| 544 | |
---|
| 545 | return reply; |
---|
| 546 | } |
---|