gethostbyname和gethostbyaddr

1、gethostbyname函數原型

#include <netdb.h>

struct hostent *gethostbyname(const char *ghostname); 返回:成功返回非空指針,出錯爲NULL且設置h_errno

2、hostent結構

struct hostent { char  *h_name;        /* official name of host */
    char  **h_aliases;    /* pointer to array of pointers to alias name */
    int     h_addrtype;   /* host address type: AF_INET */
    int     h_length;     /* length of address: 4 */
    char  **h_addr_list;  /* ptr to array of ptrs with IPv4 addrs */ }; 

3、關於全局總體變量h_errno

  當gethostbyname發生錯誤時,它不設置errno變量,而是將全局變量h_errno設置爲<netdb.h>中定義的下列常值之一:socket

  (1)HOST_NOT_FOUND;函數

  (2)TRY_AGAIN;ui

  (3)NO_RECOVERY;spa

  (4)NO_DATA(等同於NO_ADDRESS)指針

  NO_DATA錯誤表示指定的名字有效,可是它沒有A記錄code

4、gethostbyname例子

#include    <stdio.h> #include <netdb.h> #include <stdlib.h> #include <sys/socket.h> #include <arpa/inet.h>

#define     INET_ADDRSTRLEN     16

void err_ret(const char *, ...); void err_msg(const char *, ...); int main(int argc, char **argv) { char    *ptr, **pptr; char str[INET_ADDRSTRLEN]; struct hostent    *hptr; while (--argc > 0) { ptr = *++argv; if ( (hptr = gethostbyname(ptr)) == NULL) { err_msg("gethostbyname error for host: %s: %s", ptr, hstrerror(h_errno)); continue; } printf("official hostname: %s\n", hptr->h_name); for (pptr = hptr->h_aliases; *pptr != NULL; pptr++) { printf("\talias: %s\n", *pptr); } switch (hptr->h_addrtype) { case AF_INET: { pptr = hptr->h_addr_list; for ( ; *pptr != NULL; pptr++) { printf("\taddress: %s\n", inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str))); } break; } default: { err_ret("unknown address type"); break; } } } exit(0); }
#include    <stdio.h> #include <errno.h> #include <stdlib.h> #include <string.h> #include <stdarg.h>        /* ANSI C header file */ #include <syslog.h>        /* for syslog() */

#define     MAXLINE     4096

int        daemon_proc;        /* set nonzero by daemon_init() */

static void    err_doit(int, int, const char *, va_list); /* Nonfatal error related to system call * Print message and return */

void err_ret(const char *fmt, ...) { va_list ap; va_start(ap, fmt); err_doit(1, LOG_INFO, fmt, ap); va_end(ap); return; } /* Fatal error related to system call * Print message and terminate */

void err_sys(const char *fmt, ...) { va_list ap; va_start(ap, fmt); err_doit(1, LOG_ERR, fmt, ap); va_end(ap); exit(1); } /* Fatal error related to system call * Print message, dump core, and terminate */

void err_dump(const char *fmt, ...) { va_list ap; va_start(ap, fmt); err_doit(1, LOG_ERR, fmt, ap); va_end(ap); abort(); /* dump core and terminate */ exit(1);        /* shouldn't get here */ } /* Nonfatal error unrelated to system call * Print message and return */

void err_msg(const char *fmt, ...) { va_list ap; va_start(ap, fmt); err_doit(0, LOG_INFO, fmt, ap); va_end(ap); return; } /* Fatal error unrelated to system call * Print message and terminate */

void err_quit(const char *fmt, ...) { va_list ap; va_start(ap, fmt); err_doit(0, LOG_ERR, fmt, ap); va_end(ap); exit(1); } /* Print message and return to caller * Caller specifies "errnoflag" and "level" */

static void err_doit(int errnoflag, int level, const char *fmt, va_list ap) { int errno_save, n; char    buf[MAXLINE + 1]; errno_save = errno;        /* value caller might want printed */ #ifdef HAVE_VSNPRINTF vsnprintf(buf, MAXLINE, fmt, ap); /* safe */
#else vsprintf(buf, fmt, ap); /* not safe */
#endif n = strlen(buf); if (errnoflag) snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save)); strcat(buf, "\n"); if (daemon_proc) { syslog(level, buf); } else { fflush(stdout); /* in case stdout and stderr are the same */ fputs(buf, stderr); fflush(stderr); } return; }

5、gethostbyaddr函數原型

#include <netdb.h>

struct hostent *gethostbyaddr(const struct in_addr *, socklen_t, int family); 返回:成功爲非空指針,不然爲NULL且設置h_errno 功能:由一個二進制IP地址找到相應的主機名
相關文章
相關標籤/搜索