/* This file is part of the YAZ toolkit.
- * Copyright (C) 1995-2008 Index Data
+ * Copyright (C) 1995-2009 Index Data
* See the file LICENSE for details.
*/
/**
#include <yaz/comstack.h>
#include <yaz/tcpip.h>
-#include <yaz/nmem.h>
+#include <yaz/errno.h>
static int tcpip_close(COMSTACK h);
static int tcpip_put(COMSTACK h, char *buf, int size);
return 1;
}
-
#if HAVE_GETADDRINFO
void *tcpip_straddr(COMSTACK h, const char *str)
{
tcpip_state *sp = (tcpip_state *)h->cprivate;
const char *port = "210";
+ struct addrinfo *ai;
if (h->protocol == PROTO_HTTP)
port = "80";
if (!tcpip_init())
if (sp->ai && h->state == CS_ST_UNBND)
{
int s = -1;
- struct addrinfo *ai = sp->ai;
- for (; ai; ai = ai->ai_next)
+ /* try to make IPV6 socket first */
+ for (ai = sp->ai; ai; ai = ai->ai_next)
{
- s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
- if (s != -1)
- break;
+ if (ai->ai_family == AF_INET6)
+ {
+ s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (s != -1)
+ break;
+ }
+ }
+ if (s == -1)
+ {
+ /* no IPV6 could be made.. Try them all */
+ for (ai = sp->ai; ai; ai = ai->ai_next)
+ {
+ s = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
+ if (s != -1)
+ break;
+ }
}
if (s == -1)
return 0;
+ assert(ai);
h->iofile = s;
if (!tcpip_set_blocking(h, h->flags))
return 0;
}
- return sp->ai;
+ return ai;
}
#else
void *tcpip_straddr(COMSTACK h, const char *str)
int tcpip_connect(COMSTACK h, void *address)
{
#if HAVE_GETADDRINFO
+ struct addrinfo *ai = (struct addrinfo *) address;
tcpip_state *sp = (tcpip_state *)h->cprivate;
#else
struct sockaddr_in *add = (struct sockaddr_in *) address;
h->cerrno = CSOUTSTATE;
return -1;
}
-#if HAVE_GETADDRINFO
- if (sp->ai != (struct addrinfo *) address)
- {
- h->cerrno = CSOUTSTATE;
- return -1;
- }
-#endif
#ifdef __sun__
/* On Suns, you must set a bigger Receive Buffer BEFORE a call to connect
* This gives the connect a chance to negotiate with the other side
#endif
#if HAVE_GETADDRINFO
- r = connect(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
+ r = connect(h->iofile, ai->ai_addr, ai->ai_addrlen);
freeaddrinfo(sp->ai);
sp->ai = 0;
#else
gnutls_credentials_set (sp->session, GNUTLS_CRD_CERTIFICATE,
sp->cred_ptr->xcred);
- gnutls_transport_set_ptr(sp->session, (gnutls_transport_ptr_t) h->iofile);
-
+ /* cast to intermediate size_t to avoid GCC warning. */
+ gnutls_transport_set_ptr(sp->session,
+ (gnutls_transport_ptr_t)
+ (size_t) h->iofile);
res = gnutls_handshake(sp->session);
if (res < 0)
{
{
int r;
tcpip_state *sp = (tcpip_state *)h->cprivate;
-#if HAVE_GETADDRINFO
+#if HAVE_GETADDRINFO
+ struct addrinfo *ai = (struct addrinfo *) address;
#else
struct sockaddr *addr = (struct sockaddr *)address;
#endif
int one = 1;
#endif
-#if HAVE_GETADDRINFO
- if (sp->ai != (struct addrinfo *) address)
- {
- h->cerrno = CSOUTSTATE;
- return -1;
- }
-#endif
-
#if HAVE_GNUTLS_H
if (h->type == ssl_type && !sp->session)
{
#endif
tcpip_setsockopt(h->iofile);
#if HAVE_GETADDRINFO
- r = bind(h->iofile, sp->ai->ai_addr, sp->ai->ai_addrlen);
+ r = bind(h->iofile, ai->ai_addr, ai->ai_addrlen);
freeaddrinfo(sp->ai);
sp->ai = 0;
#else
h->cerrno = CSOUTSTATE;
return -1;
}
+#ifdef WIN32
+ h->newfd = accept(h->iofile, 0, 0);
+#else
h->newfd = accept(h->iofile, (struct sockaddr*)&addr, &len);
+#endif
if (h->newfd < 0)
{
if (
xfree(state);
return 0;
}
+ /* cast to intermediate size_t to avoid GCC warning. */
gnutls_transport_set_ptr(state->session,
- (gnutls_transport_ptr_t) cnew->iofile);
+ (gnutls_transport_ptr_t)
+ (size_t) cnew->iofile);
}
#elif HAVE_OPENSSL_SSL_H
state->ctx = st->ctx;
TRC(fprintf(stderr, "tcpip_get: bufsize=%d\n", *bufsize));
if (sp->altlen) /* switch buffers */
{
- TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
- (unsigned) sp->altbuf));
+ TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
+ sp->altbuf));
tmpc = *buf;
tmpi = *bufsize;
*buf = sp->altbuf;
h->cerrno = CSYSERR;
return -1;
}
- TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
- (unsigned) sp->altbuf));
+ TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
+ sp->altbuf));
memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
}
if (berlen < CS_TCPIP_BUFCHUNK - 1)
TRC(fprintf(stderr, "ssl_get: bufsize=%d\n", *bufsize));
if (sp->altlen) /* switch buffers */
{
- TRC(fprintf(stderr, " %d bytes in altbuf (0x%x)\n", sp->altlen,
- (unsigned) sp->altbuf));
+ TRC(fprintf(stderr, " %d bytes in altbuf (%p)\n", sp->altlen,
+ sp->altbuf));
tmpc = *buf;
tmpi = *bufsize;
*buf = sp->altbuf;
} else if (sp->altsize < req)
if (!(sp->altbuf =(char *)xrealloc(sp->altbuf, sp->altsize = req)))
return -1;
- TRC(fprintf(stderr, " Moving %d bytes to altbuf(0x%x)\n", tomove,
- (unsigned) sp->altbuf));
+ TRC(fprintf(stderr, " Moving %d bytes to altbuf(%p)\n", tomove,
+ sp->altbuf));
memcpy(sp->altbuf, *buf + berlen, sp->altlen = tomove);
}
if (berlen < CS_TCPIP_BUFCHUNK - 1)
/*
* Local variables:
* c-basic-offset: 4
+ * c-file-style: "Stroustrup"
* indent-tabs-mode: nil
* End:
* vim: shiftwidth=4 tabstop=8 expandtab