This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

res_search problem


I'm trying to port some code which uses res_search() to look up an SRV
record.   The attached code works on Linux, Solaris, HP-UX, Darwin, and 
Irix, but doesn't work on Cygwin.   I'm not a DNS expert but this test
case is derived from code at:

http://www.libspf.org/doxygen/html/dns_8c-source.html
http://www.waider.ie/hacks/workshop/c/rvp/librvp-0.9.tar.gz

Am I missing something fundamental, or is res_search not working?  (In
ethereal I can see the question being sent and the correct response coming
back - but the correct string does not seem to be in the buffer returned
from res_search().)   Can someone point to an example of using 
res_search() under Cygwin?

(If you want to try this yourself, you'll need to set srvname to the 
name of a valid SRV record at your site)


#################################################################
/* srv_rec.c - test whether we can look up on SRV record */

/* Build with 
	gcc -o srv_rec srv_rec.c -lresolv
*/

#include <netdb.h>
#include <iconv.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>
#if defined(__linux__) || defined(__APPLE_CC__)
# include <arpa/nameser_compat.h>
#endif
#include <resolv.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>


/* record type: 2 bytes
   record class: 2 bytes
   TTL: 4 bytes
   data length: 2 bytes */
typedef struct _rechdr {
  unsigned short type;
  unsigned short class;
  unsigned int ttl;
  unsigned short length;
} rechdr;

/*
   priority: 2 bytes
   weight: 2 bytes
   port: 2 bytes
   target: remainder
*/
typedef struct _srv {
  unsigned short priority;
  unsigned short weight;
  unsigned short port;
  unsigned char *target;
} srv_t;

/* DNS service record */
typedef struct _srvrec {
  /* realistically this should be a list of
     { host, port, priority, weight } structures */
  char *host;
  int port;
  time_t expiry; /* time at which we'll need to requery */
} srvrec;

#ifndef T_SRV
#define T_SRV 33
#endif

#ifndef HFIXEDSZ
#define HFIXEDSZ 12
#endif

main() {

  char *srvname = "_rvp._tcp.lucent.com";
  HEADER *hdr;
  unsigned char answer[1024];
  char name[1024];
  int len = 0;
  srvrec *retval = NULL;

  bzero( answer, 1024 );
  bzero( name, 1024 );

  _res.options |= RES_DEBUG;

  if (( len = res_search( srvname, C_IN, T_SRV, answer, sizeof( answer )))
      != -1 ) {
    unsigned char *blob;
    int i, l;

    hdr = (HEADER *)answer;

    if ( ntohs( hdr->ancount ) == 0 ) {
      printf( __FUNCTION__, "res_search: no records found\n" );
      exit(1);
    }

    blob = &answer[HFIXEDSZ];

      blob += dn_skipname(blob,answer+len) + QFIXEDSZ;


    for ( i = 0; i < ntohs( hdr->ancount ); i++ ) {
      rechdr *header;
      srv_t *server;

      l = dn_expand( answer, answer + len, blob, name, 1024 );
      if ( l < 0 ) {
        printf( __FUNCTION__, "dn_expand failed (2)\n" );
        exit(3);
      }

      blob += l;

      header = (rechdr *)&blob[0];
      server = (srv_t *)&blob[10]; /* errr. magic number. */

      l = dn_expand( answer, answer + len, blob + 16, name, 1024 );
      if ( l < 0 ) {
        printf( __FUNCTION__, "dn_expand failed (3)\n" );
        exit(4);
      }

      printf("Host name from SRV record is %s\n",name);
      printf("Port from SRV record is %d\n", ntohs( server->port ));

    }
  }
}

-- 
Peter Fales
Lucent Technologies
Member of Technical Staff
2000 Lucent Lane
Room: 1C-436
Naperville, IL 60566-7033
Email: psfales@lucent.com
Phone: 630 979 8031

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]