[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 | |
---|
[d52111a] | 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 | |
---|
[c88999c] | 247 | /* Decode%20a%20file%20name */ |
---|
| 248 | void http_decode( char *s ) |
---|
| 249 | { |
---|
| 250 | char *t; |
---|
| 251 | int i, j, k; |
---|
| 252 | |
---|
| 253 | t = g_new( char, strlen( s ) + 1 ); |
---|
| 254 | |
---|
| 255 | for( i = j = 0; s[i]; i ++, j ++ ) |
---|
| 256 | { |
---|
| 257 | if( s[i] == '%' ) |
---|
| 258 | { |
---|
| 259 | if( sscanf( s + i + 1, "%2x", &k ) ) |
---|
| 260 | { |
---|
| 261 | t[j] = k; |
---|
| 262 | i += 2; |
---|
| 263 | } |
---|
| 264 | else |
---|
| 265 | { |
---|
| 266 | *t = 0; |
---|
| 267 | break; |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | else |
---|
| 271 | { |
---|
| 272 | t[j] = s[i]; |
---|
| 273 | } |
---|
| 274 | } |
---|
| 275 | t[j] = 0; |
---|
| 276 | |
---|
| 277 | strcpy( s, t ); |
---|
| 278 | g_free( t ); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | /* Warning: This one explodes the string. Worst-cases can make the string 3x its original size! */ |
---|
| 282 | /* This fuction is safe, but make sure you call it safely as well! */ |
---|
| 283 | void http_encode( char *s ) |
---|
| 284 | { |
---|
| 285 | char *t; |
---|
| 286 | int i, j; |
---|
| 287 | |
---|
| 288 | t = g_strdup( s ); |
---|
| 289 | |
---|
| 290 | for( i = j = 0; t[i]; i ++, j ++ ) |
---|
| 291 | { |
---|
[dd8d4c5] | 292 | /* if( t[i] <= ' ' || ((unsigned char *)t)[i] >= 128 || t[i] == '%' ) */ |
---|
| 293 | if( !isalnum( t[i] ) ) |
---|
[c88999c] | 294 | { |
---|
| 295 | sprintf( s + j, "%%%02X", ((unsigned char*)t)[i] ); |
---|
| 296 | j += 2; |
---|
| 297 | } |
---|
| 298 | else |
---|
| 299 | { |
---|
| 300 | s[j] = t[i]; |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | s[j] = 0; |
---|
| 304 | |
---|
| 305 | g_free( t ); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | /* Strip newlines from a string. Modifies the string passed to it. */ |
---|
| 309 | char *strip_newlines( char *source ) |
---|
| 310 | { |
---|
| 311 | int i; |
---|
| 312 | |
---|
| 313 | for( i = 0; source[i] != '\0'; i ++ ) |
---|
| 314 | if( source[i] == '\n' || source[i] == '\r' ) |
---|
| 315 | source[i] = ' '; |
---|
| 316 | |
---|
| 317 | return source; |
---|
| 318 | } |
---|
[2a6ca4f] | 319 | |
---|
| 320 | /* Wrap an IPv4 address into IPv6 space. Not thread-safe... */ |
---|
| 321 | char *ipv6_wrap( char *src ) |
---|
| 322 | { |
---|
| 323 | static char dst[64]; |
---|
| 324 | int i; |
---|
| 325 | |
---|
| 326 | for( i = 0; src[i]; i ++ ) |
---|
| 327 | if( ( src[i] < '0' || src[i] > '9' ) && src[i] != '.' ) |
---|
| 328 | break; |
---|
| 329 | |
---|
| 330 | /* Hmm, it's not even an IP... */ |
---|
| 331 | if( src[i] ) |
---|
| 332 | return src; |
---|
| 333 | |
---|
| 334 | g_snprintf( dst, sizeof( dst ), "::ffff:%s", src ); |
---|
| 335 | |
---|
| 336 | return dst; |
---|
| 337 | } |
---|
| 338 | |
---|
| 339 | /* Unwrap an IPv4 address into IPv6 space. Thread-safe, because it's very simple. :-) */ |
---|
| 340 | char *ipv6_unwrap( char *src ) |
---|
| 341 | { |
---|
| 342 | int i; |
---|
| 343 | |
---|
| 344 | if( g_strncasecmp( src, "::ffff:", 7 ) != 0 ) |
---|
| 345 | return src; |
---|
| 346 | |
---|
| 347 | for( i = 7; src[i]; i ++ ) |
---|
| 348 | if( ( src[i] < '0' || src[i] > '9' ) && src[i] != '.' ) |
---|
| 349 | break; |
---|
| 350 | |
---|
| 351 | /* Hmm, it's not even an IP... */ |
---|
| 352 | if( src[i] ) |
---|
| 353 | return src; |
---|
| 354 | |
---|
| 355 | return ( src + 7 ); |
---|
| 356 | } |
---|
[e27661d] | 357 | |
---|
| 358 | /* Convert from one charset to another. |
---|
| 359 | |
---|
| 360 | from_cs, to_cs: Source and destination charsets |
---|
| 361 | src, dst: Source and destination strings |
---|
| 362 | size: Size if src. 0 == use strlen(). strlen() is not reliable for UNICODE/UTF16 strings though. |
---|
| 363 | maxbuf: Maximum number of bytes to write to dst |
---|
| 364 | |
---|
| 365 | Returns the number of bytes written to maxbuf or -1 on an error. |
---|
| 366 | */ |
---|
| 367 | signed int do_iconv( char *from_cs, char *to_cs, char *src, char *dst, size_t size, size_t maxbuf ) |
---|
| 368 | { |
---|
[574af7e] | 369 | GIConv cd; |
---|
[e27661d] | 370 | size_t res; |
---|
| 371 | size_t inbytesleft, outbytesleft; |
---|
| 372 | char *inbuf = src; |
---|
| 373 | char *outbuf = dst; |
---|
| 374 | |
---|
[574af7e] | 375 | cd = g_iconv_open( to_cs, from_cs ); |
---|
| 376 | if( cd == (GIConv) -1 ) |
---|
[e27661d] | 377 | return( -1 ); |
---|
| 378 | |
---|
| 379 | inbytesleft = size ? size : strlen( src ); |
---|
| 380 | outbytesleft = maxbuf - 1; |
---|
[574af7e] | 381 | res = g_iconv( cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft ); |
---|
[e27661d] | 382 | *outbuf = '\0'; |
---|
[574af7e] | 383 | g_iconv_close( cd ); |
---|
[e27661d] | 384 | |
---|
| 385 | if( res == (size_t) -1 ) |
---|
| 386 | return( -1 ); |
---|
| 387 | else |
---|
| 388 | return( outbuf - dst ); |
---|
| 389 | } |
---|
| 390 | |
---|
[1719464] | 391 | /* A pretty reliable random number generator. Tries to use the /dev/random |
---|
| 392 | devices first, and falls back to the random number generator from libc |
---|
| 393 | when it fails. Opens randomizer devices with O_NONBLOCK to make sure a |
---|
| 394 | lack of entropy won't halt BitlBee. */ |
---|
| 395 | void random_bytes( unsigned char *buf, int count ) |
---|
[e27661d] | 396 | { |
---|
[1719464] | 397 | static int use_dev = -1; |
---|
| 398 | |
---|
| 399 | /* Actually this probing code isn't really necessary, is it? */ |
---|
| 400 | if( use_dev == -1 ) |
---|
| 401 | { |
---|
| 402 | if( access( "/dev/random", R_OK ) == 0 || access( "/dev/urandom", R_OK ) == 0 ) |
---|
| 403 | use_dev = 1; |
---|
| 404 | else |
---|
| 405 | { |
---|
| 406 | use_dev = 0; |
---|
| 407 | srand( ( getpid() << 16 ) ^ time( NULL ) ); |
---|
| 408 | } |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | if( use_dev ) |
---|
| 412 | { |
---|
| 413 | int fd; |
---|
| 414 | |
---|
| 415 | /* At least on Linux, /dev/random can block if there's not |
---|
| 416 | enough entropy. We really don't want that, so if it can't |
---|
| 417 | give anything, use /dev/urandom instead. */ |
---|
| 418 | if( ( fd = open( "/dev/random", O_RDONLY | O_NONBLOCK ) ) >= 0 ) |
---|
| 419 | if( read( fd, buf, count ) == count ) |
---|
| 420 | { |
---|
| 421 | close( fd ); |
---|
| 422 | return; |
---|
| 423 | } |
---|
| 424 | close( fd ); |
---|
| 425 | |
---|
| 426 | /* urandom isn't supposed to block at all, but just to be |
---|
| 427 | sure. If it blocks, we'll disable use_dev and use the libc |
---|
| 428 | randomizer instead. */ |
---|
| 429 | if( ( fd = open( "/dev/urandom", O_RDONLY | O_NONBLOCK ) ) >= 0 ) |
---|
| 430 | if( read( fd, buf, count ) == count ) |
---|
| 431 | { |
---|
| 432 | close( fd ); |
---|
| 433 | return; |
---|
| 434 | } |
---|
| 435 | close( fd ); |
---|
| 436 | |
---|
| 437 | /* If /dev/random blocks once, we'll still try to use it |
---|
| 438 | again next time. If /dev/urandom also fails for some |
---|
| 439 | reason, stick with libc during this session. */ |
---|
| 440 | |
---|
| 441 | use_dev = 0; |
---|
| 442 | srand( ( getpid() << 16 ) ^ time( NULL ) ); |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | if( !use_dev ) |
---|
| 446 | { |
---|
| 447 | int i; |
---|
| 448 | |
---|
| 449 | /* Possibly the LSB of rand() isn't very random on some |
---|
| 450 | platforms. Seems okay on at least Linux and OSX though. */ |
---|
| 451 | for( i = 0; i < count; i ++ ) |
---|
| 452 | buf[i] = rand() & 0xff; |
---|
| 453 | } |
---|
[e27661d] | 454 | } |
---|
[5100caa] | 455 | |
---|
| 456 | int is_bool( char *value ) |
---|
| 457 | { |
---|
| 458 | if( *value == 0 ) |
---|
| 459 | return 0; |
---|
| 460 | |
---|
| 461 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
| 462 | return 1; |
---|
| 463 | if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
| 464 | return 1; |
---|
| 465 | |
---|
| 466 | while( *value ) |
---|
| 467 | if( !isdigit( *value ) ) |
---|
| 468 | return 0; |
---|
| 469 | else |
---|
| 470 | value ++; |
---|
| 471 | |
---|
| 472 | return 1; |
---|
| 473 | } |
---|
| 474 | |
---|
| 475 | int bool2int( char *value ) |
---|
| 476 | { |
---|
| 477 | int i; |
---|
| 478 | |
---|
| 479 | if( ( g_strcasecmp( value, "true" ) == 0 ) || ( g_strcasecmp( value, "yes" ) == 0 ) || ( g_strcasecmp( value, "on" ) == 0 ) ) |
---|
| 480 | return 1; |
---|
| 481 | if( ( g_strcasecmp( value, "false" ) == 0 ) || ( g_strcasecmp( value, "no" ) == 0 ) || ( g_strcasecmp( value, "off" ) == 0 ) ) |
---|
| 482 | return 0; |
---|
| 483 | |
---|
| 484 | if( sscanf( value, "%d", &i ) == 1 ) |
---|
| 485 | return i; |
---|
| 486 | |
---|
| 487 | return 0; |
---|
| 488 | } |
---|
[36e9f62] | 489 | |
---|
| 490 | struct ns_srv_reply *srv_lookup( char *service, char *protocol, char *domain ) |
---|
| 491 | { |
---|
| 492 | struct ns_srv_reply *reply = NULL; |
---|
| 493 | #ifdef HAVE_RESOLV_A |
---|
| 494 | char name[1024]; |
---|
| 495 | unsigned char querybuf[1024]; |
---|
| 496 | const unsigned char *buf; |
---|
| 497 | ns_msg nsh; |
---|
| 498 | ns_rr rr; |
---|
| 499 | int i, len, size; |
---|
| 500 | |
---|
| 501 | g_snprintf( name, sizeof( name ), "_%s._%s.%s", service, protocol, domain ); |
---|
| 502 | |
---|
| 503 | if( ( size = res_query( name, ns_c_in, ns_t_srv, querybuf, sizeof( querybuf ) ) ) <= 0 ) |
---|
| 504 | return NULL; |
---|
| 505 | |
---|
| 506 | if( ns_initparse( querybuf, size, &nsh ) != 0 ) |
---|
| 507 | return NULL; |
---|
| 508 | |
---|
| 509 | if( ns_parserr( &nsh, ns_s_an, 0, &rr ) != 0 ) |
---|
| 510 | return NULL; |
---|
| 511 | |
---|
| 512 | size = ns_rr_rdlen( rr ); |
---|
| 513 | buf = ns_rr_rdata( rr ); |
---|
| 514 | |
---|
| 515 | len = 0; |
---|
| 516 | for( i = 6; i < size && buf[i]; i += buf[i] + 1 ) |
---|
| 517 | len += buf[i] + 1; |
---|
| 518 | |
---|
| 519 | if( i > size ) |
---|
| 520 | return NULL; |
---|
| 521 | |
---|
| 522 | reply = g_malloc( sizeof( struct ns_srv_reply ) + len ); |
---|
| 523 | memcpy( reply->name, buf + 7, len ); |
---|
| 524 | |
---|
| 525 | for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 ) |
---|
| 526 | reply->name[i] = '.'; |
---|
| 527 | |
---|
| 528 | if( i > len ) |
---|
| 529 | { |
---|
| 530 | g_free( reply ); |
---|
| 531 | return NULL; |
---|
| 532 | } |
---|
| 533 | |
---|
| 534 | reply->prio = ( buf[0] << 8 ) | buf[1]; |
---|
| 535 | reply->weight = ( buf[2] << 8 ) | buf[3]; |
---|
| 536 | reply->port = ( buf[4] << 8 ) | buf[5]; |
---|
| 537 | #endif |
---|
| 538 | |
---|
| 539 | return reply; |
---|
| 540 | } |
---|
[d444c09] | 541 | |
---|
| 542 | /* Word wrapping. Yes, I know this isn't UTF-8 clean. I'm willing to take the risk. */ |
---|
| 543 | char *word_wrap( char *msg, int line_len ) |
---|
| 544 | { |
---|
| 545 | GString *ret = g_string_sized_new( strlen( msg ) + 16 ); |
---|
| 546 | |
---|
| 547 | while( strlen( msg ) > line_len ) |
---|
| 548 | { |
---|
| 549 | int i; |
---|
| 550 | |
---|
| 551 | /* First try to find out if there's a newline already. Don't |
---|
| 552 | want to add more splits than necessary. */ |
---|
| 553 | for( i = line_len; i > 0 && msg[i] != '\n'; i -- ); |
---|
| 554 | if( msg[i] == '\n' ) |
---|
| 555 | { |
---|
| 556 | g_string_append_len( ret, msg, i + 1 ); |
---|
| 557 | msg += i + 1; |
---|
| 558 | continue; |
---|
| 559 | } |
---|
| 560 | |
---|
| 561 | for( i = line_len; i > 0; i -- ) |
---|
| 562 | { |
---|
| 563 | if( msg[i] == '-' ) |
---|
| 564 | { |
---|
| 565 | g_string_append_len( ret, msg, i + 1 ); |
---|
| 566 | g_string_append_c( ret, '\n' ); |
---|
| 567 | msg += i + 1; |
---|
| 568 | break; |
---|
| 569 | } |
---|
| 570 | else if( msg[i] == ' ' ) |
---|
| 571 | { |
---|
| 572 | g_string_append_len( ret, msg, i ); |
---|
| 573 | g_string_append_c( ret, '\n' ); |
---|
| 574 | msg += i + 1; |
---|
| 575 | break; |
---|
| 576 | } |
---|
| 577 | } |
---|
| 578 | if( i == 0 ) |
---|
| 579 | { |
---|
| 580 | g_string_append_len( ret, msg, line_len ); |
---|
| 581 | g_string_append_c( ret, '\n' ); |
---|
| 582 | msg += line_len; |
---|
| 583 | } |
---|
| 584 | } |
---|
| 585 | g_string_append( ret, msg ); |
---|
| 586 | |
---|
| 587 | return g_string_free( ret, FALSE ); |
---|
| 588 | } |
---|
[d52111a] | 589 | |
---|
| 590 | gboolean ssl_sockerr_again( void *ssl ) |
---|
| 591 | { |
---|
| 592 | if( ssl ) |
---|
| 593 | return ssl_errno == SSL_AGAIN; |
---|
| 594 | else |
---|
| 595 | return sockerr_again(); |
---|
| 596 | } |
---|