Click here to Skip to main content
12,457,226 members (57,844 online)
Click here to Skip to main content

Stats

54.6K views
767 downloads
16 bookmarked
Posted

IDNA resolver client

, 30 Mar 2004
Using non-ASCII in DNS name lookups.
#ifndef _IDNA_H
#define _IDNA_H

#include <stdlib.h>
#include <windows.h>
#include <winsock.h>

#ifndef MAX_HOST_LEN
#define MAX_HOST_LEN  256
#endif

#ifndef MAX_HOST_LABELS
#define MAX_HOST_LABELS  8
#endif


#if defined(_MSC_VER) && !defined(__cplusplus)
  #define MS_CDECL _cdecl  /* var-arg functions cannot be _fastcall */
#else
  #define MS_CDECL
#endif

#ifdef __cplusplus
extern "C" {
#endif

extern int _idna_debug, _idna_errno, _idna_winnls_errno;

extern int (MS_CDECL *_idna_printf) (const char *fmt, ...)
  #if defined(__GNUC__) && (__GNUC__ >= 3)
    __attribute__((format(printf,1,2)))
  #endif
  ;

enum IDNA_errors {
     IDNAERR_OK = 0,
     IDNAERR_NOT_INIT,
     IDNAERR_PUNYCODE_BASE,
     IDNAERR_PUNYCODE_BAD_INPUT,
     IDNAERR_PUNYCODE_BIG_OUTBUF,
     IDNAERR_PUNYCODE_OVERFLOW,
     IDNAERR_PUNY_ENCODE,
     IDNAERR_ILL_CODEPAGE,
     IDNAERR_WINNLS               /* specific error in _idna_winnls_errno */
   };

BOOL        IDNA_init (WORD code_page);
const char *IDNA_strerror (int err);

BOOL IDNA_convert_to_ACE   (char *name, size_t *size);
BOOL IDNA_convert_from_ACE (char *name, size_t *size);
UINT IDNA_GetCodePage      (void);

#ifdef __cplusplus

  typedef int (*MS_CDECL IDNA_debug_func) (const char *fmt, ...);

  class CIDNA_convert
  {
    public:
      CIDNA_convert (WORD code_page = 0)
      {
        IDNA_init (code_page);
        m_buf[0] = '\0';
      }

      inline const char *convert_to_ACE (const char *name, int *error = NULL)
      {
	strncpy (m_buf, name, sizeof(m_buf)-1);
	m_buf [sizeof(m_buf)-1] = '\0';

        size_t size = sizeof (m_buf);
        BOOL   rc   = IDNA_convert_to_ACE (m_buf, &size);

        if (error)
           *error = _idna_errno;
        return (rc ? m_buf : NULL);
      }

      inline const char *convert_from_ACE (const char *name, int *error = NULL)
      {
	strncpy (m_buf, name, sizeof(m_buf)-1);
	m_buf [sizeof(m_buf)-1] = '\0';

        size_t size = strlen (m_buf);
        BOOL   rc   = IDNA_convert_from_ACE (m_buf, &size);

        if (error)
           *error = _idna_errno;
        return (rc ? m_buf : NULL);
      }

      inline BOOL is_ACE (void)
      {
        return (strstr(m_buf,"xn--") ? TRUE : FALSE);
      }

      inline void set_debug (int level, IDNA_debug_func func)
      {
	_idna_debug = level;
 	_idna_printf = func;
      }

    private:
      char m_buf [2*MAX_HOST_LEN];  /* A conservative estimate */
  };

  class CIDNA_resolver : public CIDNA_convert
  {
    public:
      CIDNA_resolver (WORD code_page = 0) : CIDNA_convert (code_page)  {}
      struct hostent *gethostbyname (const char *name);
      struct hostent *gethostbyaddress (const char *addr_name, int size, int af);
  };
}
#endif  /* __cplusplus */
#endif  /* _IDNA_H */

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Gisle Vanem
Norway Norway
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160826.1 | Last Updated 31 Mar 2004
Article Copyright 2004 by Gisle Vanem
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid