- short *port_ptr)
- {
- char *h,*p;
-
- h=str;
- p=strchr(str,':');
- if (p == NULL)
- {
- BIO_printf(bio_err,"no port defined\n");
- return(0);
- }
- *(p++)='\0';
-
- if ((ip != NULL) && !host_ip(str,ip))
- goto err;
- if (host_ptr != NULL) *host_ptr=h;
-
- if (!extract_port(p,port_ptr))
- goto err;
- return(1);
-err:
- return(0);
- }
-
-static int host_ip(char *str, unsigned char ip[4])
- {
- unsigned int in[4];
- int i;
-
- if (sscanf(str,"%u.%u.%u.%u",&(in[0]),&(in[1]),&(in[2]),&(in[3])) == 4)
- {
- for (i=0; i<4; i++)
- if (in[i] > 255)
- {
- BIO_printf(bio_err,"invalid IP address\n");
- goto err;
- }
- ip[0]=in[0];
- ip[1]=in[1];
- ip[2]=in[2];
- ip[3]=in[3];
- }
- else
- { /* do a gethostbyname */
- struct hostent *he;
-
- if (!ssl_sock_init()) return(0);
-
- he=GetHostByName(str);
- if (he == NULL)
- {
- BIO_printf(bio_err,"gethostbyname failure\n");
- goto err;
- }
- /* cast to short because of win16 winsock definition */
- if ((short)he->h_addrtype != AF_INET)
- {
- BIO_printf(bio_err,"gethostbyname addr is not AF_INET\n");
- return(0);
- }
- ip[0]=he->h_addr_list[0][0];
- ip[1]=he->h_addr_list[0][1];
- ip[2]=he->h_addr_list[0][2];
- ip[3]=he->h_addr_list[0][3];
- }
- return(1);
-err:
- return(0);
- }
-
-int extract_port(char *str, short *port_ptr)
- {
- int i;
- struct servent *s;
-
- i=atoi(str);
- if (i != 0)
- *port_ptr=(unsigned short)i;
- else
- {
- s=getservbyname(str,"tcp");
- if (s == NULL)
- {
- BIO_printf(bio_err,"getservbyname failure for %s\n",str);
- return(0);
- }
- *port_ptr=ntohs((unsigned short)s->s_port);
- }
- return(1);
- }
-
-#define GHBN_NUM 4
-static struct ghbn_cache_st
- {
- char name[128];
- struct hostent ent;
- unsigned long order;
- } ghbn_cache[GHBN_NUM];
-
-static unsigned long ghbn_hits=0L;
-static unsigned long ghbn_miss=0L;
-
-static struct hostent *GetHostByName(char *name)
- {
- struct hostent *ret;
- int i,lowi=0;
- unsigned long low= (unsigned long)-1;
-
- for (i=0; i<GHBN_NUM; i++)
- {
- if (low > ghbn_cache[i].order)
- {
- low=ghbn_cache[i].order;
- lowi=i;
- }
- if (ghbn_cache[i].order > 0)
- {
- if (strncmp(name,ghbn_cache[i].name,128) == 0)
- break;
- }
- }
- if (i == GHBN_NUM) /* no hit*/
- {
- ghbn_miss++;
- ret=gethostbyname(name);
- if (ret == NULL) return(NULL);
- /* else add to cache */
- if(strlen(name) < sizeof ghbn_cache[0].name)
- {
- strcpy(ghbn_cache[lowi].name,name);
- memcpy((char *)&(ghbn_cache[lowi].ent),ret,sizeof(struct hostent));
- ghbn_cache[lowi].order=ghbn_miss+ghbn_hits;
- }
- return(ret);
- }
- else
- {
- ghbn_hits++;
- ret= &(ghbn_cache[i].ent);
- ghbn_cache[i].order=ghbn_miss+ghbn_hits;
- return(ret);
- }
- }
+ unsigned short *port_ptr)
+{
+ char *h, *p;
+
+ h = str;
+ p = strchr(str, ':');
+ if (p == NULL) {
+ BIO_printf(bio_err, "no port defined\n");
+ return (0);
+ }
+ *(p++) = '\0';
+
+ if ((ip != NULL) && !host_ip(str, ip))
+ goto err;
+ if (host_ptr != NULL)
+ *host_ptr = h;
+
+ if (!extract_port(p, port_ptr))
+ goto err;
+ return (1);
+ err:
+ return (0);
+}
+
+static int host_ip(const char *str, unsigned char ip[4])
+{
+ unsigned int in[4];
+ int i;
+
+ if (sscanf(str, "%u.%u.%u.%u", &(in[0]), &(in[1]), &(in[2]), &(in[3])) ==
+ 4) {
+ for (i = 0; i < 4; i++)
+ if (in[i] > 255) {
+ BIO_printf(bio_err, "invalid IP address\n");
+ goto err;
+ }
+ ip[0] = in[0];
+ ip[1] = in[1];
+ ip[2] = in[2];
+ ip[3] = in[3];
+ } else { /* do a gethostbyname */
+ struct hostent *he;
+
+ if (!ssl_sock_init())
+ return (0);
+
+ he = gethostbyname(str);
+ if (he == NULL) {
+ BIO_printf(bio_err, "gethostbyname failure\n");
+ goto err;
+ }
+ if (he->h_addrtype != AF_INET) {
+ BIO_printf(bio_err, "gethostbyname addr is not AF_INET\n");
+ return (0);
+ }
+ ip[0] = he->h_addr_list[0][0];
+ ip[1] = he->h_addr_list[0][1];
+ ip[2] = he->h_addr_list[0][2];
+ ip[3] = he->h_addr_list[0][3];
+ }
+ return (1);
+ err:
+ return (0);
+}
+
+int extract_port(const char *str, unsigned short *port_ptr)
+{
+ int i;
+ struct servent *s;
+
+ i = atoi(str);
+ if (i != 0)
+ *port_ptr = (unsigned short)i;
+ else {
+ s = getservbyname(str, "tcp");
+ if (s == NULL) {
+ BIO_printf(bio_err, "getservbyname failure for %s\n", str);
+ return (0);
+ }
+ *port_ptr = ntohs((unsigned short)s->s_port);
+ }
+ return (1);
+}