ei_resolve.c

来自「OTP是开放电信平台的简称」· C语言 代码 · 共 632 行 · 第 1/2 页

C
632
字号
      errno = ERANGE;#endif      *h_errnop = 0;    }  }    else {    /* failure - lookup */#ifdef __WIN32__    *h_errnop = WSAGetLastError();#else    *h_errnop = h_errno;#endif  }#ifdef _REENTRANT  /* === END critical section === */  ei_mutex_unlock(ei_gethost_sem);#endif /* _REENTRANT */  return rval;}static struct hostent *my_gethostbyaddr_r(const char *addr,					  int length, 					  int type, 					  struct hostent *hostp,					  char *buffer,  					  int buflen, 					  int *h_errnop){  struct hostent dest;  struct hostent *src;  struct hostent *rval = NULL;  /* FIXME this should have been done in 'erl'_init()? */  if (!ei_resolve_initialized) ei_init_resolve();#ifdef _REENTRANT  /* === BEGIN critical section === */  if (ei_mutex_lock(ei_gethost_sem,0) != 0) {    *h_errnop = NO_RECOVERY;    return NULL;  }#endif /* _REENTRANT */  /* lookup the data */  if ((src = ei_gethostbyaddr(addr,length,type))) {    /* copy to caller's buffer */    if (!copy_hostent(&dest,src,buffer,buflen)) {      /* success */      *hostp = dest;                      *h_errnop = 0;      rval = hostp;    }    else {      /* failure - buffer size */#ifdef __WIN32__      SetLastError(ERROR_INSUFFICIENT_BUFFER);#else      errno = ERANGE;#endif      *h_errnop = 0;    }  }  else {    /* failure - lookup */#ifdef __WIN32__    *h_errnop = WSAGetLastError();#else    *h_errnop = h_errno;#endif  }#ifdef _REENTRANT  /* === END critical section === */  ei_mutex_unlock(ei_gethost_sem);#endif /* _REENTRANT */  return rval;}#endif /* !HAVE_GETHOSTBYNAME_R */#ifdef __WIN32__struct hostent *ei_gethostbyname(const char *name){    return gethostbyname(name);}struct hostent *ei_gethostbyaddr(const char *addr, int len, int type){    return gethostbyaddr(addr, len, type);}#elif VXWORKS/* these are a couple of substitutes for the real thing when we run on * stock vxworks (i.e. no sens). * * len and type are ignored, but we make up some reasonable values and * insert them */static struct hostent *my_gethostbyname(const char *name){  /* FIXME problem for threaded ? */  static struct hostent h;  static char hostname[EI_MAXHOSTNAMELEN+1];  static char *aliases[1] = {NULL};  static char *addrp[2] = {NULL,NULL};  static unsigned long addr = 0;  strcpy(hostname,name);  if ((addr = (unsigned long)hostGetByName(hostname)) == ERROR) {    h_errno = HOST_NOT_FOUND;    return NULL;  }  h_errno = 0;  h.h_name = hostname;  h.h_aliases = aliases;  h.h_length = 4;  h.h_addrtype = AF_INET;  addrp[0] = (char *)&addr;  h.h_addr_list = addrp;    return &h;}static struct hostent *my_gethostbyaddr(const char *addr, int len, int type){  /* FIXME problem for threaded ? */  static struct hostent h;  static char hostname[EI_MAXHOSTNAMELEN+1];  static char *aliases[1] = { NULL };  static unsigned long inaddr;  static char *addrp[2] = {(char *)&inaddr, NULL};  memmove(&inaddr,addr,sizeof(inaddr));    if ((hostGetByAddr(inaddr,hostname)) == ERROR) {    h_errno = HOST_NOT_FOUND;    return NULL;  }    h_errno = 0;  h.h_name = hostname;  h.h_aliases = aliases;  h.h_length = 4;  h.h_addrtype = AF_INET;  h.h_addr_list = addrp;  return &h;}/* use sens functions for these, if found. */struct hostent *ei_gethostbyname(const char *name){  struct hostent *h = NULL;    if (!sens_gethostbyname) {    h = my_gethostbyname(name);  }  else {    /* FIXME problem for threaded ? */    static char buf[1024];    h = sens_gethostbyname(name,buf,1024);  }  return h;}struct hostent *ei_gethostbyaddr(const char *addr, int len, int type){  struct hostent *h = NULL;    if (!sens_gethostbyaddr) {     h = my_gethostbyaddr(addr,len,type);  }  else {    /* FIXME problem for threaded ? */    static char buf[1024];    h = sens_gethostbyaddr(addr,buf,1024);  }  return h;}struct hostent *ei_gethostbyaddr_r(const char *addr,				int length, 				int type, 				struct hostent *hostp,				char *buffer,  				int buflen, 				int *h_errnop){  struct hostent *h = NULL;    /* use own func if sens function not available */  if (!sens_gethostbyaddr) {    h = my_gethostbyaddr_r(addr,length,type,hostp,buffer,buflen,h_errnop);  }  else {    if (!(h = sens_gethostbyaddr(addr,buffer,buflen))) {      /* sens returns status via errno */      *h_errnop = errno;     }    else {      *hostp = *h;      *h_errnop = 0;    }  }        return h;}struct hostent *ei_gethostbyname_r(const char *name, 				    struct hostent *hostp, 				    char *buffer, 				    int buflen, 				    int *h_errnop){  struct hostent *h = NULL;    /* use own func if sens function not available */  if (!sens_gethostbyname) {    h = my_gethostbyname_r(name,hostp,buffer,buflen,h_errnop);  }  else {    if (!(h = sens_gethostbyname(name,buffer,buflen))) {      /* sens returns status via errno */      *h_errnop = errno;     }    else {      *hostp = *h;      *h_errnop = 0;    }  }        return h;}#else /* unix of some kind */struct hostent *ei_gethostbyname(const char *name){    return gethostbyname(name);}struct hostent *ei_gethostbyaddr(const char *addr, int len, int type){    return gethostbyaddr(addr, len, type);}struct hostent *ei_gethostbyaddr_r(const char *addr,				int length, 				int type, 				struct hostent *hostp,				char *buffer,  				int buflen, 				int *h_errnop){#if (EI_THREADS == false)  /* threads disabled, no need to call reentrant function */  return gethostbyaddr(addr, length, type); #else#ifndef HAVE_GETHOSTBYNAME_R  return my_gethostbyaddr_r(addr,length,type,hostp,buffer,buflen,h_errnop);#else#ifdef __GLIBC__  struct hostent *result;  gethostbyaddr_r(addr, length, type, hostp, buffer, buflen, &result,		h_errnop);  return result;#else  return gethostbyaddr_r(addr,length,type,hostp,buffer,buflen,h_errnop);#endif#endif#endif}struct hostent *ei_gethostbyname_r(const char *name, 				    struct hostent *hostp, 				    char *buffer, 				    int buflen, 				    int *h_errnop){#ifndef _REENTRANT  /* threads disabled, no need to call reentrant function */  return gethostbyname(name);#else#ifndef HAVE_GETHOSTBYNAME_R  return my_gethostbyname_r(name,hostp,buffer,buflen,h_errnop);#else#ifdef __GLIBC__  struct hostent *result;  gethostbyname_r(name, hostp, buffer, buflen, &result, h_errnop);  return result;#else  return gethostbyname_r(name,hostp,buffer,buflen,h_errnop);#endif#endif#endif}#endif /* vxworks, win, unix */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?