X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=src%2Ftcpip.c;h=6d69d2f0dbf042b2d107d99de8b29820dcd60033;hb=6b152d04e55e9d23fc2dd0c948929ad5e2bd4c05;hp=db0e834b77352e191623f4c308233aae8cde9cb5;hpb=fb6d99a0c7e07d9cc4a315c447deaf6564a85505;p=yaz-moved-to-github.git diff --git a/src/tcpip.c b/src/tcpip.c index db0e834..6d69d2f 100644 --- a/src/tcpip.c +++ b/src/tcpip.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2005, Index Data ApS * See the file LICENSE for details. * - * $Id: tcpip.c,v 1.16 2005-06-25 15:46:06 adam Exp $ + * $Id: tcpip.c,v 1.22 2006-08-30 12:55:12 adam Exp $ */ /** * \file tcpip.c @@ -187,6 +187,7 @@ COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp) p->f_addrstr = tcpip_addrstr; p->f_straddr = tcpip_straddr; p->f_set_blocking = tcpip_set_blocking; + p->max_recv_bytes = 5000000; p->state = new_socket ? CS_ST_UNBND : CS_ST_IDLE; /* state of line */ p->event = CS_NONE; @@ -240,9 +241,22 @@ int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add, int default_port) { struct hostent *hp; +#if HAVE_GETHOSTBYNAME_R + struct hostent h; + char hbuf[1024]; + int h_error; +#endif char *p, buf[512]; short int port = default_port; - unsigned tmpadd; +#ifdef WIN32 + unsigned long tmpadd; +#else + in_addr_t tmpadd; +#endif + +#ifndef INADDR_NONE +#define INADDR_NONE -1 +#endif if (!tcpip_init ()) return 0; @@ -259,12 +273,26 @@ int tcpip_strtoaddr_ex(const char *str, struct sockaddr_in *add, } add->sin_port = htons(port); if (!strcmp("@", buf)) + { add->sin_addr.s_addr = INADDR_ANY; + } + else if ((tmpadd = inet_addr(buf)) != INADDR_NONE) + { + memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr)); + } +#if HAVE_GETHOSTBYNAME_R + else if (gethostbyname_r(buf, &h, hbuf, sizeof(hbuf), &hp, &h_error) == 0) + { + memcpy(&add->sin_addr.s_addr, *hp->h_addr_list, + sizeof(struct in_addr)); + } +#else else if ((hp = gethostbyname(buf))) + { memcpy(&add->sin_addr.s_addr, *hp->h_addr_list, sizeof(struct in_addr)); - else if ((tmpadd = (unsigned) inet_addr(buf)) != 0) - memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr)); + } +#endif else return 0; return 1; @@ -490,8 +518,7 @@ static int tcpip_bind(COMSTACK h, void *address, int mode) if (sp->ctx_alloc) { int res; - res = SSL_CTX_use_certificate_file (sp->ctx, sp->cert_fname, - SSL_FILETYPE_PEM); + res = SSL_CTX_use_certificate_chain_file(sp->ctx, sp->cert_fname); if (res <= 0) { ERR_print_errors_fp(stderr); @@ -534,7 +561,8 @@ static int tcpip_bind(COMSTACK h, void *address, int mode) h->cerrno = CSYSERR; return -1; } - if (mode == CS_SERVER && listen(h->iofile, 3) < 0) + /* Allow a maximum-sized backlog of waiting-to-connect clients */ + if (mode == CS_SERVER && listen(h->iofile, SOMAXCONN) < 0) { h->cerrno = CSYSERR; return -1; @@ -751,11 +779,17 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize) if (!*bufsize) { if (!(*buf = (char *)xmalloc(*bufsize = CS_TCPIP_BUFCHUNK))) + { + h->cerrno = CSYSERR; return -1; + } } else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK) if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2))) + { + h->cerrno = CSYSERR; return -1; + } #ifdef __sun__ yaz_set_errno( 0 ); /* unfortunatly, sun sometimes forgets to set errno in recv @@ -800,6 +834,11 @@ int tcpip_get(COMSTACK h, char **buf, int *bufsize) else if (!res) return hasread; hasread += res; + if (hasread > h->max_recv_bytes) + { + h->cerrno = CSBUFSIZE; + return -1; + } } TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n", hasread, berlen));