[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 | |
---|
[eded1f7] | 47 | #include "ssl_client.h" |
---|
| 48 | |
---|
[b7d3cc34] | 49 | void strip_linefeed(gchar *text) |
---|
| 50 | { |
---|
| 51 | int i, j; |
---|
| 52 | gchar *text2 = g_malloc(strlen(text) + 1); |
---|
| 53 | |
---|
| 54 | for (i = 0, j = 0; text[i]; i++) |
---|
| 55 | if (text[i] != '\r') |
---|
| 56 | text2[j++] = text[i]; |
---|
| 57 | text2[j] = '\0'; |
---|
| 58 | |
---|
| 59 | strcpy(text, text2); |
---|
| 60 | g_free(text2); |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | char *normalize(const char *s) |
---|
| 64 | { |
---|
| 65 | static char buf[BUF_LEN]; |
---|
| 66 | char *t, *u; |
---|
| 67 | int x = 0; |
---|
| 68 | |
---|
| 69 | g_return_val_if_fail((s != NULL), NULL); |
---|
| 70 | |
---|
| 71 | u = t = g_strdup(s); |
---|
| 72 | |
---|
| 73 | strcpy(t, s); |
---|
| 74 | g_strdown(t); |
---|
| 75 | |
---|
| 76 | while (*t && (x < BUF_LEN - 1)) { |
---|
| 77 | if (*t != ' ') { |
---|
| 78 | buf[x] = *t; |
---|
| 79 | x++; |
---|
| 80 | } |
---|
| 81 | t++; |
---|
| 82 | } |
---|
| 83 | buf[x] = '\0'; |
---|
| 84 | g_free(u); |
---|
| 85 | return buf; |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | time_t get_time(int year, int month, int day, int hour, int min, int sec) |
---|
| 89 | { |
---|
| 90 | struct tm tm; |
---|
| 91 | |
---|
| 92 | tm.tm_year = year - 1900; |
---|
| 93 | tm.tm_mon = month - 1; |
---|
| 94 | tm.tm_mday = day; |
---|
| 95 | tm.tm_hour = hour; |
---|
| 96 | tm.tm_min = min; |
---|
| 97 | tm.tm_sec = sec >= 0 ? sec : time(NULL) % 60; |
---|
| 98 | return mktime(&tm); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | typedef struct htmlentity |
---|
| 102 | { |
---|
[51fdc45] | 103 | char code[7]; |
---|
| 104 | char is[3]; |
---|
[b7d3cc34] | 105 | } htmlentity_t; |
---|
| 106 | |
---|
[39cc341] | 107 | static const htmlentity_t ent[] = |
---|
[b7d3cc34] | 108 | { |
---|
[39cc341] | 109 | { "lt", "<" }, |
---|
| 110 | { "gt", ">" }, |
---|
| 111 | { "amp", "&" }, |
---|
| 112 | { "quot", "\"" }, |
---|
| 113 | { "aacute", "á" }, |
---|
| 114 | { "eacute", "é" }, |
---|
| 115 | { "iacute", "é" }, |
---|
| 116 | { "oacute", "ó" }, |
---|
| 117 | { "uacute", "ú" }, |
---|
| 118 | { "agrave", "à" }, |
---|
| 119 | { "egrave", "è" }, |
---|
| 120 | { "igrave", "ì" }, |
---|
| 121 | { "ograve", "ò" }, |
---|
| 122 | { "ugrave", "ù" }, |
---|
| 123 | { "acirc", "â" }, |
---|
| 124 | { "ecirc", "ê" }, |
---|
| 125 | { "icirc", "î" }, |
---|
| 126 | { "ocirc", "ô" }, |
---|
| 127 | { "ucirc", "û" }, |
---|
| 128 | { "auml", "ä" }, |
---|
| 129 | { "euml", "ë" }, |
---|
| 130 | { "iuml", "ï" }, |
---|
| 131 | { "ouml", "ö" }, |
---|
| 132 | { "uuml", "ü" }, |
---|
| 133 | { "nbsp", " " }, |
---|
| 134 | { "", "" } |
---|
[b7d3cc34] | 135 | }; |
---|
| 136 | |
---|
| 137 | void strip_html( char *in ) |
---|
| 138 | { |
---|
| 139 | char *start = in; |
---|
| 140 | char *out = g_malloc( strlen( in ) + 1 ); |
---|
| 141 | char *s = out, *cs; |
---|
| 142 | int i, matched; |
---|
| 143 | |
---|
| 144 | memset( out, 0, strlen( in ) + 1 ); |
---|
| 145 | |
---|
| 146 | while( *in ) |
---|
| 147 | { |
---|
| 148 | if( *in == '<' && ( isalpha( *(in+1) ) || *(in+1) == '/' ) ) |
---|
| 149 | { |
---|
| 150 | /* If in points at a < and in+1 points at a letter or a slash, this is probably |
---|
| 151 | a HTML-tag. Try to find a closing > and continue there. If the > can't be |
---|
| 152 | found, assume that it wasn't a HTML-tag after all. */ |
---|
| 153 | |
---|
| 154 | cs = in; |
---|
| 155 | |
---|
| 156 | while( *in && *in != '>' ) |
---|
| 157 | in ++; |
---|
| 158 | |
---|
| 159 | if( *in ) |
---|
| 160 | { |
---|
| 161 | if( g_strncasecmp( cs+1, "br", 2) == 0 ) |
---|
| 162 | *(s++) = '\n'; |
---|
| 163 | in ++; |
---|
| 164 | } |
---|
| 165 | else |
---|
| 166 | { |
---|
| 167 | in = cs; |
---|
| 168 | *(s++) = *(in++); |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | else if( *in == '&' ) |
---|
| 172 | { |
---|
| 173 | cs = ++in; |
---|
| 174 | while( *in && isalpha( *in ) ) |
---|
| 175 | in ++; |
---|
| 176 | |
---|
| 177 | if( *in == ';' ) in ++; |
---|
| 178 | matched = 0; |
---|
| 179 | |
---|
| 180 | for( i = 0; *ent[i].code; i ++ ) |
---|
| 181 | if( g_strncasecmp( ent[i].code, cs, strlen( ent[i].code ) ) == 0 ) |
---|
| 182 | { |
---|
[39cc341] | 183 | int j; |
---|
| 184 | |
---|
| 185 | for( j = 0; ent[i].is[j]; j ++ ) |
---|
| 186 | *(s++) = ent[i].is[j]; |
---|
| 187 | |
---|
[b7d3cc34] | 188 | matched = 1; |
---|
| 189 | break; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | /* None of the entities were matched, so return the string */ |
---|
| 193 | if( !matched ) |
---|
| 194 | { |
---|
| 195 | in = cs - 1; |
---|
| 196 | *(s++) = *(in++); |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | else |
---|
| 200 | { |
---|
| 201 | *(s++) = *(in++); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | strcpy( start, out ); |
---|
| 206 | g_free( out ); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | char *escape_html( const char *html ) |
---|
| 210 | { |
---|
| 211 | const char *c = html; |
---|
| 212 | GString *ret; |
---|
| 213 | char *str; |
---|
| 214 | |
---|
| 215 | if( html == NULL ) |
---|
| 216 | return( NULL ); |
---|
| 217 | |
---|
| 218 | ret = g_string_new( "" ); |
---|
| 219 | |
---|
| 220 | while( *c ) |
---|
| 221 | { |
---|
| 222 | switch( *c ) |
---|
| 223 | { |
---|
| 224 | case '&': |
---|
| 225 | ret = g_string_append( ret, "&" ); |
---|
| 226 | break; |
---|
| 227 | case '<': |
---|
| 228 | ret = g_string_append( ret, "<" ); |
---|
| 229 | break; |
---|
| 230 | case '>': |
---|
| 231 | ret = g_string_append( ret, ">" ); |
---|
| 232 | break; |
---|
| 233 | case '"': |
---|
| 234 | ret = g_string_append( ret, """ ); |
---|
| 235 | break; |
---|
| 236 | default: |
---|
| 237 | ret = g_string_append_c( ret, *c ); |
---|
| 238 | } |
---|
| 239 | c ++; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | str = ret->str; |
---|
| 243 | g_string_free( ret, FALSE ); |
---|
| 244 | return( str ); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | void info_string_append(GString *str, char *newline, char *name, char *value) |
---|
| 248 | { |
---|
| 249 | if( value && value[0] ) |
---|
| 250 | g_string_sprintfa( str, "%s%s: %s", newline, name, value ); |
---|
| 251 | } |
---|
[c88999c] | 252 | |
---|
| 253 | /* Decode%20a%20file%20name */ |
---|
| 254 | void http_decode( char *s ) |
---|
| 255 | { |
---|
| 256 | char *t; |
---|
| 257 | int i, j, k; |
---|
| 258 | |
---|
| 259 | t = g_new( char, strlen( s ) + 1 ); |
---|
| 260 | |
---|
| 261 | for( i = j = 0; s[i]; i ++, j ++ ) |
---|
| 262 | { |
---|
| 263 | if( s[i] == '%' ) |
---|
| 264 | { |
---|
| 265 | if( sscanf( s + i + 1, "%2x", &k ) ) |
---|
| 266 | { |
---|
| 267 | t[j] = k; |
---|
| 268 | i += 2; |
---|
| 269 | } |
---|
| 270 | else |
---|
| 271 | { |
---|
| 272 | *t = 0; |
---|
| 273 | break; |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | else |
---|
| 277 | { |
---|
| 278 | t[j] = s[i]; |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | t[j] = 0; |
---|
| 282 | |
---|
| 283 | strcpy( s, t ); |
---|
| 284 | g_free( t ); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | /* Warning: This one explodes the string. Worst-cases can make the string 3x its original size! */ |
---|
| 288 | /* This fuction is safe, but make sure you call it safely as well! */ |
---|
| 289 | void http_encode( char *s ) |
---|
| 290 | { |
---|
| 291 | char *t; |
---|
| 292 | int i, j; |
---|
| 293 | |
---|
| 294 | t = g_strdup( s ); |
---|
| 295 | |
---|
| 296 | for( i = j = 0; t[i]; i ++, j ++ ) |
---|
| 297 | { |
---|
[dd8d4c5] | 298 | /* if( t[i] <= ' ' || ((unsigned char *)t)[i] >= 128 || t[i] == '%' ) */ |
---|
| 299 | if( !isalnum( t[i] ) ) |
---|
[c88999c] | 300 | { |
---|
| 301 | sprintf( s + j, "%%%02X", ((unsigned char*)t)[i] ); |
---|
| 302 | j += 2; |
---|
| 303 | } |
---|
| 304 | else |
---|
| 305 | { |
---|
| 306 | s[j] = t[i]; |
---|
| 307 | } |
---|
| 308 | } |
---|
| 309 | s[j] = 0; |
---|
| 310 | |
---|
| 311 | g_free( t ); |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | /* Strip newlines from a string. Modifies the string passed to it. */ |
---|
| 315 | char *strip_newlines( char *source ) |
---|
| 316 | { |
---|
| 317 | int i; |
---|
| 318 | |
---|
| 319 | for( i = 0; source[i] != '\0'; i ++ ) |
---|
| 320 | if( source[i] == '\n' || source[i] == '\r' ) |
---|
| 321 | source[i] = ' '; |
---|
| 322 | |
---|
| 323 | return source; |
---|
| 324 | } |
---|
[2a6ca4f] | 325 | |
---|
| 326 | /* Wrap an IPv4 address into IPv6 space. Not thread-safe... */ |
---|
| 327 | char *ipv6_wrap( char *src ) |
---|
| 328 | { |
---|
| 329 | static char dst[64]; |
---|
| 330 | int i; |
---|
| 331 | |
---|
| 332 | for( i = 0; src[i]; i ++ ) |
---|
| 333 | if( ( src[i] < '0' || src[i] > '9' ) && src[i] != '.' ) |
---|
| 334 | break; |
---|
| 335 | |
---|
| 336 | /* Hmm, it's not even an IP... */ |
---|
| 337 | if( src[i] ) |
---|
| 338 | return src; |
---|
| 339 | |
---|
| 340 | g_snprintf( dst, sizeof( dst ), "::ffff:%s", src ); |
---|
| 341 | |
---|
| 342 | return dst; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | /* Unwrap an IPv4 address into IPv6 space. Thread-safe, because it's very simple. :-) */ |
---|
| 346 | char *ipv6_unwrap( char *src ) |
---|
| 347 | { |
---|
| 348 | int i; |
---|
| 349 | |
---|
| 350 | if( g_strncasecmp( src, "::ffff:", 7 ) != 0 ) |
---|
| 351 | return src; |
---|
| 352 | |
---|
| 353 | for( i = 7; src[i]; i ++ ) |
---|
| 354 | if( ( src[i] < '0' || src[i] > '9' ) && src[i] != '.' ) |
---|
| 355 | break; |
---|
| 356 | |
---|
| 357 | /* Hmm, it's not even an IP... */ |
---|
| 358 | if( src[i] ) |
---|
| 359 | return src; |
---|
| 360 | |
---|
| 361 | return ( src + 7 ); |
---|
| 362 | } |
---|
[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 | } |
---|
[d444c09] | 547 | |
---|
| 548 | /* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */ |
---|
| 549 | char *word_wrap( char *msg, int line_len ) |
---|
| 550 | { |
---|
| 551 | GString *ret = g_string_sized_new( strlen( msg ) + 16 ); |
---|
| 552 | |
---|
| 553 | while( strlen( msg ) > line_len ) |
---|
| 554 | { |
---|
| 555 | int i; |
---|
| 556 | |
---|
| 557 | /* First try to find out if there's a newline already. Don't |
---|
| 558 | want to add more splits than necessary. */ |
---|
| 559 | for( i = line_len; i > 0 && msg[i] != '\n'; i -- ); |
---|
| 560 | if( msg[i] == '\n' ) |
---|
| 561 | { |
---|
| 562 | g_string_append_len( ret, msg, i + 1 ); |
---|
| 563 | msg += i + 1; |
---|
| 564 | continue; |
---|
| 565 | } |
---|
| 566 | |
---|
| 567 | for( i = line_len; i > 0; i -- ) |
---|
| 568 | { |
---|
| 569 | if( msg[i] == '-' ) |
---|
| 570 | { |
---|
| 571 | g_string_append_len( ret, msg, i + 1 ); |
---|
| 572 | g_string_append_c( ret, '\n' ); |
---|
| 573 | msg += i + 1; |
---|
| 574 | break; |
---|
| 575 | } |
---|
| 576 | else if( msg[i] == ' ' ) |
---|
| 577 | { |
---|
| 578 | g_string_append_len( ret, msg, i ); |
---|
| 579 | g_string_append_c( ret, '\n' ); |
---|
| 580 | msg += i + 1; |
---|
| 581 | break; |
---|
| 582 | } |
---|
| 583 | } |
---|
| 584 | if( i == 0 ) |
---|
| 585 | { |
---|
| 586 | g_string_append_len( ret, msg, line_len ); |
---|
| 587 | g_string_append_c( ret, '\n' ); |
---|
| 588 | msg += line_len; |
---|
| 589 | } |
---|
| 590 | } |
---|
| 591 | g_string_append( ret, msg ); |
---|
| 592 | |
---|
| 593 | return g_string_free( ret, FALSE ); |
---|
| 594 | } |
---|
[eded1f7] | 595 | |
---|
| 596 | gboolean ssl_sockerr_again( void *ssl ) |
---|
| 597 | { |
---|
| 598 | if( ssl ) |
---|
| 599 | return ssl_errno == SSL_AGAIN; |
---|
| 600 | else |
---|
| 601 | return sockerr_again(); |
---|
| 602 | } |
---|