Ticket #1098: srv_lookup_fix_fixed.patch

File srv_lookup_fix_fixed.patch, 9.3 KB (added by dx <dx@…>, at 2014-01-09T13:48:43Z)

Two minor fixes to the patch above for situations when ns_parse.c isn't needed

  • configure

    diff -Naur a/configure b/configure
    a b RESOLV_TESTCODE=' 
    332332
    333333int main()
    334334{
     335
     336        res_query( NULL, 0, 0, NULL, 0);
     337        dn_expand( NULL, NULL, NULL, NULL, 0);
     338        dn_skipname( NULL, NULL);
     339}
     340'
     341RESOLV_NS_TESTCODE='
     342#include <sys/types.h>
     343#include <netinet/in.h>
     344#include <arpa/nameser.h>
     345#include <resolv.h>
     346
     347int main()
     348{
    335349        ns_initparse( NULL, 0, NULL );
    336350        ns_parserr( NULL, ns_s_an, 0, NULL );
    337351}
    detect_resolv_dynamic() 
    341355{
    342356        case "$arch" in
    343357        FreeBSD )
    344                 # In FreeBSD res_* routines are present in libc.so
     358                # In FreeBSD res_*/dn_* routines are present in libc.so
     359                LIBRESOLV=;;
     360        CYGWIN* )
     361                # In Cygwin res_*/dn_* routines are present in libc.so
    345362                LIBRESOLV=;;
    346363        * )
    347364                LIBRESOLV=-lresolv;;
    detect_resolv_static() 
    376393        return $ret
    377394}
    378395
     396detect_resolv_ns_dynamic()
     397{
     398        case "$arch" in
     399        FreeBSD )
     400                # In FreeBSD ns_ routines are present in libc.so
     401                LIBRESOLV=;;
     402        * )
     403                LIBRESOLV=-lresolv;;
     404        esac
     405        TMPFILE=$(mktemp /tmp/bitlbee-configure.XXXXXX)
     406        ret=1
     407        echo "$RESOLV_NS_TESTCODE" | $CC -o $TMPFILE -x c - $LIBRESOLV >/dev/null 2>/dev/null
     408        if [ "$?" = "0" ]; then
     409                ret=0
     410        fi
     411
     412        rm -f $TMPFILE
     413        return $ret
     414}
     415
     416detect_resolv_ns_static()
     417{
     418        TMPFILE=$(mktemp /tmp/bitlbee-configure.XXXXXX)
     419        ret=1
     420        for i in $systemlibdirs; do
     421                if [ -f $i/libresolv.a ]; then
     422                        echo "$RESOLV_NS_TESTCODE" | $CC -o $TMPFILE -x c - -Wl,$i/libresolv.a >/dev/null 2>/dev/null
     423                        if [ "$?" = "0" ]; then
     424                                ret=0
     425                        fi
     426                fi
     427        done
     428
     429        rm -f $TMPFILE
     430        return $ret
     431}
     432
    379433if [ "$ssl" = "auto" ]; then
    380434        detect_gnutls
    381435        if [ "$ret" = "0" ]; then
    echo 'SSL_CLIENT=ssl_'$ssl'.o' >> Makefile.settings 
    433487
    434488if detect_resolv_dynamic || detect_resolv_static; then
    435489        echo '#define HAVE_RESOLV_A' >> config.h
     490    if detect_resolv_ns_dynamic || detect_resolv_ns_static; then
     491            echo '#define HAVE_RESOLV_A_WITH_NS' >> config.h
     492    fi
     493else
     494    echo 'Insuffcient resolv routines Jabber server must be set explicitly'
    436495fi
    437496
     497
    438498STORAGES="xml"
    439499
    440500for i in $STORAGES; do
  • lib/Makefile

    diff -Naur a/lib/Makefile b/lib/Makefile
    a b _SRCDIR_ := $(_SRCDIR_)lib/ 
    1212endif
    1313
    1414# [SH] Program variables
    15 objects = arc.o base64.o $(EVENT_HANDLER) ftutil.o http_client.o ini.o json.o json_util.o md5.o misc.o oauth.o oauth2.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o
     15objects = arc.o base64.o $(EVENT_HANDLER) ftutil.o http_client.o ini.o json.o json_util.o md5.o misc.o oauth.o oauth2.o proxy.o sha1.o $(SSL_CLIENT) url.o xmltree.o ns_parse.o
    1616
    1717LFLAGS += -r
    1818
  • lib/misc.c

    diff -Naur a/lib/misc.c b/lib/misc.c
    a b struct ns_srv_reply **srv_lookup( char *service, char *protocol, char *domain ) 
    537537        n = 0;
    538538        while( ns_parserr( &nsh, ns_s_an, n, &rr ) == 0 )
    539539        {
    540                 size = ns_rr_rdlen( rr );
     540                char name[NS_MAXDNAME];
     541
     542                if( ns_rr_rdlen( rr ) < 7)
     543                    break;
     544
    541545                buf = ns_rr_rdata( rr );
    542546               
    543                 len = 0;
    544                 for( i = 6; i < size && buf[i]; i += buf[i] + 1 )
    545                         len += buf[i] + 1;
    546                
    547                 if( i > size )
     547                if( dn_expand(querybuf, querybuf + size, &buf[6], name, NS_MAXDNAME) == -1 )
    548548                        break;
     549
     550                len = strlen(name) + 1;
    549551               
    550552                reply = g_malloc( sizeof( struct ns_srv_reply ) + len );
    551                 memcpy( reply->name, buf + 7, len );
    552                
    553                 for( i = buf[6]; i < len && buf[7+i]; i += buf[7+i] + 1 )
    554                         reply->name[i] = '.';
    555                
    556                 if( i > len )
    557                 {
    558                         g_free( reply );
    559                         break;
    560                 }
     553                memcpy( reply->name, name, len );
    561554               
    562555                reply->prio = ( buf[0] << 8 ) | buf[1];
    563556                reply->weight = ( buf[2] << 8 ) | buf[3];
  • new file lib/ns_parse.c

    diff -Naur a/lib/ns_parse.c b/lib/ns_parse.c
    - +  
     1/*
     2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
     3 * Copyright (c) 1996,1999 by Internet Software Consortium.
     4 *
     5 * Permission to use, copy, modify, and distribute this software for any
     6 * purpose with or without fee is hereby granted, provided that the above
     7 * copyright notice and this permission notice appear in all copies.
     8 *
     9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
     10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     11 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
     12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
     15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     16 */
     17
     18#include "bitlbee.h"
     19
     20#ifndef lint
     21static const char rcsid[] = "$Id: ns_parse.c,v 1.10 2009/01/23 19:59:16 each Exp $";
     22#endif
     23
     24#ifdef HAVE_RESOLV_A
     25#ifndef HAVE_RESOLV_A_WITH_NS
     26/* Import. */
     27
     28
     29#include <sys/types.h>
     30
     31#include <netinet/in.h>
     32#include <arpa/nameser.h>
     33
     34#include <errno.h>
     35#include <resolv.h>
     36#include <string.h>
     37
     38
     39/* Forward. */
     40
     41static void     setsection(ns_msg *msg, ns_sect sect);
     42
     43/* Macros. */
     44
     45#if !defined(SOLARIS2) || defined(__COVERITY__)
     46#define RETERR(err) do { errno = (err); return (-1); } while (0)
     47#else
     48#define RETERR(err) \
     49        do { errno = (err); if (errno == errno) return (-1); } while (0)
     50#endif
     51
     52#define PARSE_FMT_PRESO 0       /* Parse using presentation-format names */
     53#define PARSE_FMT_WIRE 1        /* Parse using network-format names */
     54
     55/* Public. */
     56
     57/* These need to be in the same order as the nres.h:ns_flag enum. */
     58struct _ns_flagdata _ns_flagdata[16] = {
     59        { 0x8000, 15 },         /*%< qr. */
     60        { 0x7800, 11 },         /*%< opcode. */
     61        { 0x0400, 10 },         /*%< aa. */
     62        { 0x0200, 9 },          /*%< tc. */
     63        { 0x0100, 8 },          /*%< rd. */
     64        { 0x0080, 7 },          /*%< ra. */
     65        { 0x0040, 6 },          /*%< z. */
     66        { 0x0020, 5 },          /*%< ad. */
     67        { 0x0010, 4 },          /*%< cd. */
     68        { 0x000f, 0 },          /*%< rcode. */
     69        { 0x0000, 0 },          /*%< expansion (1/6). */
     70        { 0x0000, 0 },          /*%< expansion (2/6). */
     71        { 0x0000, 0 },          /*%< expansion (3/6). */
     72        { 0x0000, 0 },          /*%< expansion (4/6). */
     73        { 0x0000, 0 },          /*%< expansion (5/6). */
     74        { 0x0000, 0 },          /*%< expansion (6/6). */
     75};
     76
     77int ns_msg_getflag(ns_msg handle, int flag) {
     78        return(((handle)._flags & _ns_flagdata[flag].mask) >> _ns_flagdata[flag].shift);
     79}
     80
     81int
     82ns_skiprr(const u_char *ptr, const u_char *eom, ns_sect section, int count) {
     83        const u_char *optr = ptr;
     84
     85        for ((void)NULL; count > 0; count--) {
     86                int b, rdlength;
     87
     88                b = dn_skipname(ptr, eom);
     89                if (b < 0)
     90                        RETERR(EMSGSIZE);
     91                ptr += b/*Name*/ + NS_INT16SZ/*Type*/ + NS_INT16SZ/*Class*/;
     92                if (section != ns_s_qd) {
     93                        if (ptr + NS_INT32SZ + NS_INT16SZ > eom)
     94                                RETERR(EMSGSIZE);
     95                        ptr += NS_INT32SZ/*TTL*/;
     96                        NS_GET16(rdlength, ptr);
     97                        ptr += rdlength/*RData*/;
     98                }
     99        }
     100        if (ptr > eom)
     101                RETERR(EMSGSIZE);
     102        return (ptr - optr);
     103}
     104
     105int
     106ns_initparse(const u_char *msg, int msglen, ns_msg *handle) {
     107        const u_char *eom = msg + msglen;
     108        int i;
     109
     110        handle->_msg = msg;
     111        handle->_eom = eom;
     112        if (msg + NS_INT16SZ > eom)
     113                RETERR(EMSGSIZE);
     114        NS_GET16(handle->_id, msg);
     115        if (msg + NS_INT16SZ > eom)
     116                RETERR(EMSGSIZE);
     117        NS_GET16(handle->_flags, msg);
     118        for (i = 0; i < ns_s_max; i++) {
     119                if (msg + NS_INT16SZ > eom)
     120                        RETERR(EMSGSIZE);
     121                NS_GET16(handle->_counts[i], msg);
     122        }
     123        for (i = 0; i < ns_s_max; i++)
     124                if (handle->_counts[i] == 0)
     125                        handle->_sections[i] = NULL;
     126                else {
     127                        int b = ns_skiprr(msg, eom, (ns_sect)i,
     128                                          handle->_counts[i]);
     129
     130                        if (b < 0)
     131                                return (-1);
     132                        handle->_sections[i] = msg;
     133                        msg += b;
     134                }
     135        if (msg != eom)
     136                RETERR(EMSGSIZE);
     137        setsection(handle, ns_s_max);
     138        return (0);
     139}
     140
     141int
     142ns_parserr(ns_msg *handle, ns_sect section, int rrnum, ns_rr *rr) {
     143        int b;
     144        int tmp;
     145
     146        /* Make section right. */
     147        tmp = section;
     148        if (tmp < 0 || section >= ns_s_max)
     149                RETERR(ENODEV);
     150        if (section != handle->_sect)
     151                setsection(handle, section);
     152
     153        /* Make rrnum right. */
     154        if (rrnum == -1)
     155                rrnum = handle->_rrnum;
     156        if (rrnum < 0 || rrnum >= handle->_counts[(int)section])
     157                RETERR(ENODEV);
     158        if (rrnum < handle->_rrnum)
     159                setsection(handle, section);
     160        if (rrnum > handle->_rrnum) {
     161                b = ns_skiprr(handle->_msg_ptr, handle->_eom, section,
     162                              rrnum - handle->_rrnum);
     163
     164                if (b < 0)
     165                        return (-1);
     166                handle->_msg_ptr += b;
     167                handle->_rrnum = rrnum;
     168        }
     169
     170        /* Do the parse. */
     171        b = dn_expand(handle->_msg, handle->_eom,
     172                      handle->_msg_ptr, rr->name, NS_MAXDNAME);
     173        if (b < 0)
     174                return (-1);
     175        handle->_msg_ptr += b;
     176        if (handle->_msg_ptr + NS_INT16SZ + NS_INT16SZ > handle->_eom)
     177                RETERR(EMSGSIZE);
     178        NS_GET16(rr->type, handle->_msg_ptr);
     179        NS_GET16(rr->rr_class, handle->_msg_ptr);
     180        if (section == ns_s_qd) {
     181                rr->ttl = 0;
     182                rr->rdlength = 0;
     183                rr->rdata = NULL;
     184        } else {
     185                if (handle->_msg_ptr + NS_INT32SZ + NS_INT16SZ > handle->_eom)
     186                        RETERR(EMSGSIZE);
     187                NS_GET32(rr->ttl, handle->_msg_ptr);
     188                NS_GET16(rr->rdlength, handle->_msg_ptr);
     189                if (handle->_msg_ptr + rr->rdlength > handle->_eom)
     190                        RETERR(EMSGSIZE);
     191                rr->rdata = handle->_msg_ptr;
     192                handle->_msg_ptr += rr->rdlength;
     193        }
     194        if (++handle->_rrnum > handle->_counts[(int)section])
     195                setsection(handle, (ns_sect)((int)section + 1));
     196
     197        /* All done. */
     198        return (0);
     199}
     200
     201/* Private. */
     202
     203static void
     204setsection(ns_msg *msg, ns_sect sect) {
     205        msg->_sect = sect;
     206        if (sect == ns_s_max) {
     207                msg->_rrnum = -1;
     208                msg->_msg_ptr = NULL;
     209        } else {
     210                msg->_rrnum = 0;
     211                msg->_msg_ptr = msg->_sections[(int)sect];
     212        }
     213}
     214
     215/*! \file */
     216#endif
     217#endif