static int tcpip_close(COMSTACK h);
static int tcpip_put(COMSTACK h, char *buf, int size);
static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
static int tcpip_close(COMSTACK h);
static int tcpip_put(COMSTACK h, char *buf, int size);
static int tcpip_get(COMSTACK h, char **buf, int *bufsize);
static int tcpip_rcvconnect(COMSTACK h);
static int tcpip_bind(COMSTACK h, void *address, int mode);
static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
static int tcpip_rcvconnect(COMSTACK h);
static int tcpip_bind(COMSTACK h, void *address, int mode);
static int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
- int (*check_ip)(void *cd, const char *a, int len, int type),
- void *cd);
+ int (*check_ip)(void *cd, const char *a, int len, int type),
+ void *cd);
SSL_CTX *ctx; /* current CTX. */
SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
SSL *ssl;
SSL_CTX *ctx; /* current CTX. */
SSL_CTX *ctx_alloc; /* If =ctx it is owned by CS. If 0 it is not owned */
SSL *ssl;
COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
{
COMSTACK p;
COMSTACK tcpip_type(int s, int blocking, int protocol, void *vp)
{
COMSTACK p;
p->timeout = COMSTACK_DEFAULT_TIMEOUT;
TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
p->timeout = COMSTACK_DEFAULT_TIMEOUT;
TRC(fprintf(stderr, "Created new TCPIP comstack\n"));
COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
{
COMSTACK ssl_type(int s, int blocking, int protocol, void *vp)
{
COMSTACK p;
p = tcpip_type (s, blocking, protocol, 0);
if (!p)
COMSTACK p;
p = tcpip_type (s, blocking, protocol, 0);
if (!p)
add->sin_addr.s_addr = INADDR_ANY;
else if ((hp = gethostbyname(buf)))
memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
add->sin_addr.s_addr = INADDR_ANY;
else if ((hp = gethostbyname(buf)))
memcpy(&add->sin_addr.s_addr, *hp->h_addr_list,
else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
else
else if ((tmpadd = (unsigned) inet_addr(buf)) != 0)
memcpy(&add->sin_addr.s_addr, &tmpadd, sizeof(struct in_addr));
else
static struct sockaddr_in add;
if (!tcpip_strtoaddr_ex (str, &add, 210))
static struct sockaddr_in add;
if (!tcpip_strtoaddr_ex (str, &add, 210))
tcpip_state *sp = (tcpip_state *)h->cprivate;
return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
tcpip_state *sp = (tcpip_state *)h->cprivate;
return sp->altlen && (*sp->complete)((unsigned char *) sp->altbuf,
recbuflen *= 10; /* lets be optimistic */
if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
{
recbuflen *= 10; /* lets be optimistic */
if ( setsockopt(h->iofile, SOL_SOCKET, SO_RCVBUF, (void *)&recbuflen, rbufsize ) < 0 )
{
- SSL_load_error_strings();
- SSLeay_add_all_algorithms();
-
- sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
- if (!sp->ctx)
- {
- h->cerrno = CSERRORSSL;
- return -1;
- }
+ SSL_load_error_strings();
+ SSLeay_add_all_algorithms();
+
+ sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
+ if (!sp->ctx)
+ {
+ h->cerrno = CSERRORSSL;
+ return -1;
+ }
- int res;
-
- if (!sp->ssl)
- {
- sp->ssl = SSL_new (sp->ctx);
- SSL_set_fd (sp->ssl, h->iofile);
- }
- res = SSL_connect (sp->ssl);
- if (res <= 0)
- {
- int err = SSL_get_error(sp->ssl, res);
- if (err == SSL_ERROR_WANT_READ)
- {
- h->io_pending = CS_WANT_READ;
- return 1;
- }
- if (err == SSL_ERROR_WANT_WRITE)
- {
- h->io_pending = CS_WANT_WRITE;
- return 1;
- }
- h->cerrno = CSERRORSSL;
- return -1;
- }
+ int res;
+
+ if (!sp->ssl)
+ {
+ sp->ssl = SSL_new (sp->ctx);
+ SSL_set_fd (sp->ssl, h->iofile);
+ }
+ res = SSL_connect (sp->ssl);
+ if (res <= 0)
+ {
+ int err = SSL_get_error(sp->ssl, res);
+ if (err == SSL_ERROR_WANT_READ)
+ {
+ h->io_pending = CS_WANT_READ;
+ return 1;
+ }
+ if (err == SSL_ERROR_WANT_WRITE)
+ {
+ h->io_pending = CS_WANT_WRITE;
+ return 1;
+ }
+ h->cerrno = CSERRORSSL;
+ return -1;
+ }
- SSL_load_error_strings();
- SSLeay_add_all_algorithms();
-
- sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
- if (!sp->ctx)
- {
- h->cerrno = CSERRORSSL;
- return -1;
- }
+ SSL_load_error_strings();
+ SSLeay_add_all_algorithms();
+
+ sp->ctx = sp->ctx_alloc = SSL_CTX_new (SSLv23_method());
+ if (!sp->ctx)
+ {
+ h->cerrno = CSERRORSSL;
+ return -1;
+ }
- if (sp->ctx_alloc)
- {
- int res;
- res = SSL_CTX_use_certificate_file (sp->ctx, CERTF,
- SSL_FILETYPE_PEM);
- if (res <= 0)
- {
- ERR_print_errors_fp(stderr);
- exit (2);
- }
- res = SSL_CTX_use_PrivateKey_file (sp->ctx, KEYF,
- SSL_FILETYPE_PEM);
- if (res <= 0)
- {
- ERR_print_errors_fp(stderr);
- exit (3);
- }
- res = SSL_CTX_check_private_key (sp->ctx);
- if (res <= 0)
- {
- ERR_print_errors_fp(stderr);
- exit(5);
- }
- }
- TRC (fprintf (stderr, "ssl_bind\n"));
+ if (sp->ctx_alloc)
+ {
+ int res;
+ res = SSL_CTX_use_certificate_file (sp->ctx, sp->cert_fname,
+ SSL_FILETYPE_PEM);
+ if (res <= 0)
+ {
+ ERR_print_errors_fp(stderr);
+ exit (2);
+ }
+ res = SSL_CTX_use_PrivateKey_file (sp->ctx, sp->cert_fname,
+ SSL_FILETYPE_PEM);
+ if (res <= 0)
+ {
+ ERR_print_errors_fp(stderr);
+ exit (3);
+ }
+ res = SSL_CTX_check_private_key (sp->ctx);
+ if (res <= 0)
+ {
+ ERR_print_errors_fp(stderr);
+ exit(5);
+ }
+ }
+ TRC (fprintf (stderr, "ssl_bind\n"));
}
#else
TRC (fprintf (stderr, "tcpip_bind\n"));
#endif
#ifndef WIN32
if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
}
#else
TRC (fprintf (stderr, "tcpip_bind\n"));
#endif
#ifndef WIN32
if (setsockopt(h->iofile, SOL_SOCKET, SO_REUSEADDR, (char*)
}
int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
}
int tcpip_listen(COMSTACK h, char *raddr, int *addrlen,
- int (*check_ip)(void *cd, const char *a, int len, int t),
- void *cd)
+ int (*check_ip)(void *cd, const char *a, int len, int t),
+ void *cd)
TRC(fprintf(stderr, "tcpip_accept\n"));
if (h->state == CS_ST_INCON)
{
TRC(fprintf(stderr, "tcpip_accept\n"));
if (h->state == CS_ST_INCON)
{
- h->newfd = -1;
- return 0;
- }
- memcpy(cnew, h, sizeof(*h));
- cnew->iofile = h->newfd;
- cnew->io_pending = 0;
- if (!(state = (tcpip_state *)
- (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
- {
- h->cerrno = CSYSERR;
- if (h->newfd != -1)
- {
+ h->newfd = -1;
+ return 0;
+ }
+ memcpy(cnew, h, sizeof(*h));
+ cnew->iofile = h->newfd;
+ cnew->io_pending = 0;
+ if (!(state = (tcpip_state *)
+ (cnew->cprivate = xmalloc(sizeof(tcpip_state)))))
+ {
+ h->cerrno = CSYSERR;
+ if (h->newfd != -1)
+ {
- h->newfd = -1;
- }
- xfree (cnew);
- xfree (state);
- return 0;
- }
- h->newfd = -1;
- state->altbuf = 0;
- state->altsize = state->altlen = 0;
- state->towrite = state->written = -1;
- state->complete = st->complete;
- cnew->state = CS_ST_ACCEPT;
- h->state = CS_ST_IDLE;
-
+ h->newfd = -1;
+ }
+ xfree (cnew);
+ xfree (state);
+ return 0;
+ }
+ h->newfd = -1;
+ state->altbuf = 0;
+ state->altsize = state->altlen = 0;
+ state->towrite = state->written = -1;
+ state->complete = st->complete;
+ cnew->state = CS_ST_ACCEPT;
+ h->state = CS_ST_IDLE;
+
- state->ctx = st->ctx;
- state->ctx_alloc = 0;
- state->ssl = st->ssl;
- if (state->ctx)
- {
- state->ssl = SSL_new (state->ctx);
- SSL_set_fd (state->ssl, cnew->iofile);
- }
+ state->ctx = st->ctx;
+ state->ctx_alloc = 0;
+ state->ssl = st->ssl;
+ if (state->ctx)
+ {
+ state->ssl = SSL_new (state->ctx);
+ SSL_set_fd (state->ssl, cnew->iofile);
+ }
- tcpip_state *state = (tcpip_state *)h->cprivate;
- if (state->ctx)
- {
- int res = SSL_accept (state->ssl);
- TRC(fprintf(stderr, "SSL_accept\n"));
- if (res <= 0)
- {
- int err = SSL_get_error(state->ssl, res);
- if (err == SSL_ERROR_WANT_READ)
- {
- h->io_pending = CS_WANT_READ;
- return h;
- }
- if (err == SSL_ERROR_WANT_WRITE)
- {
- h->io_pending = CS_WANT_WRITE;
- return h;
- }
- cs_close (h);
- return 0;
- }
- }
+ tcpip_state *state = (tcpip_state *)h->cprivate;
+ if (state->ctx)
+ {
+ int res = SSL_accept (state->ssl);
+ TRC(fprintf(stderr, "SSL_accept\n"));
+ if (res <= 0)
+ {
+ int err = SSL_get_error(state->ssl, res);
+ if (err == SSL_ERROR_WANT_READ)
+ {
+ h->io_pending = CS_WANT_READ;
+ return h;
+ }
+ if (err == SSL_ERROR_WANT_WRITE)
+ {
+ h->io_pending = CS_WANT_WRITE;
+ return h;
+ }
+ cs_close (h);
+ return 0;
+ }
+ }
- yaz_set_errno( 0 );
- /* unfortunatly, sun sometimes forgets to set errno in recv
- when EWOULDBLOCK etc. would be required (res = -1) */
+ yaz_set_errno( 0 );
+ /* unfortunatly, sun sometimes forgets to set errno in recv
+ when EWOULDBLOCK etc. would be required (res = -1) */
- res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
- TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
- if (res < 0)
- {
- TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
- strerror(yaz_errno())));
+ res = recv(h->iofile, *buf + hasread, CS_TCPIP_BUFCHUNK, 0);
+ TRC(fprintf(stderr, " recv res=%d, hasread=%d\n", res, hasread));
+ if (res < 0)
+ {
+ TRC(fprintf(stderr, " recv errno=%d, (%s)\n", yaz_errno(),
+ strerror(yaz_errno())));
hasread += res;
}
TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
hasread += res;
}
TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
return -1;
else if (*bufsize - hasread < CS_TCPIP_BUFCHUNK)
if (!(*buf =(char *)xrealloc(*buf, *bufsize *= 2)))
return -1;
- res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
- TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
- if (res <= 0)
- {
- int ssl_err = SSL_get_error(sp->ssl, res);
- if (ssl_err == SSL_ERROR_WANT_READ)
- {
- h->io_pending = CS_WANT_READ;
- break;
- }
- if (ssl_err == SSL_ERROR_WANT_WRITE)
- {
- h->io_pending = CS_WANT_WRITE;
- break;
- }
- if (res == 0)
- return 0;
- h->cerrno = CSERRORSSL;
- return -1;
- }
- hasread += res;
+ res = SSL_read (sp->ssl, *buf + hasread, CS_TCPIP_BUFCHUNK);
+ TRC(fprintf(stderr, " SSL_read res=%d, hasread=%d\n", res, hasread));
+ if (res <= 0)
+ {
+ int ssl_err = SSL_get_error(sp->ssl, res);
+ if (ssl_err == SSL_ERROR_WANT_READ)
+ {
+ h->io_pending = CS_WANT_READ;
+ break;
+ }
+ if (ssl_err == SSL_ERROR_WANT_WRITE)
+ {
+ h->io_pending = CS_WANT_WRITE;
+ break;
+ }
+ if (res == 0)
+ return 0;
+ h->cerrno = CSERRORSSL;
+ return -1;
+ }
+ hasread += res;
}
TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
hasread, berlen));
}
TRC (fprintf (stderr, " Out of read loop with hasread=%d, berlen=%d\n",
hasread, berlen));
- )
- {
- TRC(fprintf(stderr, " Flow control stop\n"));
- h->io_pending = CS_WANT_WRITE;
- return 1;
- }
- h->cerrno = CSYSERR;
- return -1;
- }
- state->written += res;
- TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
- res, state->written, size));
+ )
+ {
+ TRC(fprintf(stderr, " Flow control stop\n"));
+ h->io_pending = CS_WANT_WRITE;
+ return 1;
+ }
+ h->cerrno = CSYSERR;
+ return -1;
+ }
+ state->written += res;
+ TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
+ res, state->written, size));
}
state->towrite = state->written = -1;
TRC(fprintf(stderr, " Ok\n"));
}
state->towrite = state->written = -1;
TRC(fprintf(stderr, " Ok\n"));
- res = SSL_write (state->ssl, buf + state->written,
- size - state->written);
- if (res <= 0)
- {
- int ssl_err = SSL_get_error(state->ssl, res);
- if (ssl_err == SSL_ERROR_WANT_READ)
- {
- h->io_pending = CS_WANT_READ;
- return 1;
- }
- if (ssl_err == SSL_ERROR_WANT_WRITE)
- {
- h->io_pending = CS_WANT_WRITE;
- return 1;
- }
- h->cerrno = CSERRORSSL;
- return -1;
- }
- state->written += res;
- TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
- res, state->written, size));
+ res = SSL_write (state->ssl, buf + state->written,
+ size - state->written);
+ if (res <= 0)
+ {
+ int ssl_err = SSL_get_error(state->ssl, res);
+ if (ssl_err == SSL_ERROR_WANT_READ)
+ {
+ h->io_pending = CS_WANT_READ;
+ return 1;
+ }
+ if (ssl_err == SSL_ERROR_WANT_WRITE)
+ {
+ h->io_pending = CS_WANT_WRITE;
+ return 1;
+ }
+ h->cerrno = CSERRORSSL;
+ return -1;
+ }
+ state->written += res;
+ TRC(fprintf(stderr, " Wrote %d, written=%d, nbytes=%d\n",
+ res, state->written, size));
}
state->towrite = state->written = -1;
TRC(fprintf(stderr, " Ok\n"));
}
state->towrite = state->written = -1;
TRC(fprintf(stderr, " Ok\n"));
if (h->protocol == PROTO_HTTP)
sprintf(buf, "http:%s", r);
else
if (h->protocol == PROTO_HTTP)
sprintf(buf, "http:%s", r);
else
else
flag = flag | O_NONBLOCK;
if (fcntl(p->iofile, F_SETFL, flag) < 0)
else
flag = flag | O_NONBLOCK;
if (fcntl(p->iofile, F_SETFL, flag) < 0)
- state = (struct tcpip_state *) cs->cprivate;
- return state->ssl;
+ sp = (struct tcpip_state *) cs->cprivate;
+ return sp->ssl;
+}
+
+int cs_set_ssl_certificate_file(COMSTACK cs, const char *fname)
+{
+ struct tcpip_state *sp;
+ if (!cs || cs->type != ssl_type)
+ return 0;
+ sp = (struct tcpip_state *) cs->cprivate;
+ strncpy(sp->cert_fname, fname, sizeof(sp->cert_fname)-1);
+ sp->cert_fname[sizeof(sp->cert_fname)-1] = '\0';
+ return 1;
- X509 *server_cert = SSL_get_peer_certificate (ssl);
- if (server_cert)
- {
- BIO *bio = BIO_new(BIO_s_mem());
- char *pem_buf;
- /* get PEM buffer in memory */
- PEM_write_bio_X509(bio, server_cert);
- *len = BIO_get_mem_data(bio, &pem_buf);
- *buf = xmalloc(*len);
- memcpy(*buf, pem_buf, *len);
- BIO_free(bio);
- return 1;
- }
+ X509 *server_cert = SSL_get_peer_certificate (ssl);
+ if (server_cert)
+ {
+ BIO *bio = BIO_new(BIO_s_mem());
+ char *pem_buf;
+ /* get PEM buffer in memory */
+ PEM_write_bio_X509(bio, server_cert);
+ *len = BIO_get_mem_data(bio, &pem_buf);
+ *buf = (char *) xmalloc(*len);
+ memcpy(*buf, pem_buf, *len);
+ BIO_free(bio);
+ return 1;
+ }