YAZ 5.38.1
tcpip.c File Reference

Implements TCP/IP + SSL COMSTACK. More...

#include <yaz/yconfig.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <yaz/thread_create.h>
#include <yaz/log.h>
#include <yaz/wrbuf.h>
#include <yaz/base64.h>
#include "comstack-p.h"
#include <yaz/tcpip.h>
#include <yaz/snprintf.h>
#include <yaz/zgdu.h>

Go to the source code of this file.

Data Structures

struct  tcpip_state

Macros

#define RESOLVER_THREAD   1
#define YAZ_SOCKLEN_T   int
#define CS_TCPIP_BUFCHUNK   4096

Typedefs

typedef struct tcpip_state tcpip_state

Functions

static void 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_connect (COMSTACK h, void *address)
static int tcpip_more (COMSTACK h)
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)
static int tcpip_set_blocking (COMSTACK p, int blocking)
struct addrinfo * tcpip_getaddrinfo (const char *str, const char *port, int *ipv6_only)
static COMSTACK tcpip_accept (COMSTACK h)
static const char * tcpip_addrstr (COMSTACK h)
static void * tcpip_straddr (COMSTACK h, const char *str)
static int tcpip_init (void)
static struct tcpip_state * tcpip_state_create (void)
static void tcpip_state_destroy (tcpip_state *sp)
COMSTACK tcpip_type (int s, int flags, int protocol, void *vp)
static void cs_set_error (COMSTACK cs, int error, const char *details)
static void connect_and_bind (COMSTACK p, const char *connect_host, const char *connect_auth, const char *bind_host)
COMSTACK yaz_tcpip_create3 (int s, int flags, int protocol, const char *connect_host, const char *connect_auth, const char *bind_host)
COMSTACK yaz_tcpip_create2 (int s, int flags, int protocol, const char *connect_host, const char *bind_host)
COMSTACK yaz_tcpip_create (int s, int flags, int protocol, const char *connect_host)
COMSTACK ssl_type (int s, int flags, int protocol, void *vp)
COMSTACK yaz_ssl_create (int s, int flags, int protocol, const char *connect_host, const char *connect_auth, const char *bind_host)
static void parse_host_port (const char *host_port, char *tmp, size_t tmp_sz, const char **host, const char **port)
static struct addrinfo * create_net_socket (COMSTACK h)
static int cont_connect (COMSTACK h)
void cs_print_session_info (COMSTACK cs)
 Prints TLS peer certificate information to standard output.
void * cs_get_ssl (COMSTACK cs)
 Legacy OpenSSL session accessor.
int cs_set_ssl_ctx (COMSTACK cs, void *ctx)
 Legacy OpenSSL context setter.
int cs_set_ssl_certificate_file (COMSTACK cs, const char *fname)
 Selects certificate and private key files for a TLS endpoint.
int cs_get_peer_certificate_x509 (COMSTACK cs, char **buf, int *len)
 Gets a human-readable description of the first peer X.509 certificate.
int yaz_tcpip_set_head_only (COMSTACK cs, int head_only)
const char * yaz_tcpip_get_error_details (COMSTACK cs)

Variables

static int log_level = 0

Detailed Description

Implements TCP/IP + SSL COMSTACK.

Definition in file tcpip.c.

Macro Definition Documentation

◆ CS_TCPIP_BUFCHUNK

#define CS_TCPIP_BUFCHUNK   4096

Definition at line 1207 of file tcpip.c.

Referenced by tcpip_get().

◆ RESOLVER_THREAD

#define RESOLVER_THREAD   1

Definition at line 72 of file tcpip.c.

◆ YAZ_SOCKLEN_T

#define YAZ_SOCKLEN_T   int

Definition at line 104 of file tcpip.c.

Typedef Documentation

◆ tcpip_state

typedef struct tcpip_state tcpip_state

Function Documentation

◆ connect_and_bind()

void connect_and_bind ( COMSTACK p,
const char * connect_host,
const char * connect_auth,
const char * bind_host )
static

◆ cont_connect()

◆ create_net_socket()

◆ cs_get_peer_certificate_x509()

int cs_get_peer_certificate_x509 ( COMSTACK cs,
char ** buf,
int * len )

Gets a human-readable description of the first peer X.509 certificate.

Parameters
csendpoint with an established TLS session
bufrequired output pointer to allocated, NUL-terminated text
lenrequired output pointer to text length, excluding the terminator
Returns
1 on success, 0 if unavailable; outputs are unchanged on failure

Requires GnuTLS certificate printing support. Free *buf with xfree after success. The result is descriptive text, not a PEM-encoded certificate.

Definition at line 1733 of file tcpip.c.

References tcpip_state::buf, comstack::cprivate, cs_get_peer_certificate_x509(), ssl_type(), comstack::type, and xstrdup.

Referenced by cs_get_peer_certificate_x509(), and get_cert().

◆ cs_get_ssl()

void * cs_get_ssl ( COMSTACK cs)

Legacy OpenSSL session accessor.

Parameters
csendpoint
Returns
NULL
Deprecated
OpenSSL support has been removed; this function is a no-op.

Definition at line 1680 of file tcpip.c.

References cs_get_ssl().

Referenced by cs_get_ssl().

◆ cs_print_session_info()

void cs_print_session_info ( COMSTACK cs)

Prints TLS peer certificate information to standard output.

Parameters
csendpoint

Does nothing for other transports or when no TLS session is available.

Definition at line 1638 of file tcpip.c.

References comstack::cprivate, cs_print_session_info(), ssl_type(), and comstack::type.

Referenced by cs_print_session_info().

◆ cs_set_error()

◆ cs_set_ssl_certificate_file()

int cs_set_ssl_certificate_file ( COMSTACK cs,
const char * fname )

Selects certificate and private key files for a TLS endpoint.

Parameters
csendpoint, or NULL
fnamefilename containing both certificate and key, or "certificate-file,key-file"; NULL or empty disables the default file
Returns
1 if filenames were stored, 0 for an unsupported endpoint

Call before binding or connecting. Filenames are copied; success does not mean the files have been loaded or validated. This overrides the default server certificate filename yaz.pem.

Definition at line 1698 of file tcpip.c.

References comstack::cprivate, cs_set_ssl_certificate_file(), ssl_type(), and comstack::type.

Referenced by add_listener(), cs_set_ssl_certificate_file(), do_connect_host(), and yaz_url_exec().

◆ cs_set_ssl_ctx()

int cs_set_ssl_ctx ( COMSTACK cs,
void * ctx )

Legacy OpenSSL context setter.

Parameters
csendpoint, or NULL
ctxignored legacy context pointer
Returns
1 for a TLS endpoint with GnuTLS support, otherwise 0
Deprecated
OpenSSL support has been removed; no context is installed.

Definition at line 1686 of file tcpip.c.

References cs_set_ssl_ctx(), ssl_type(), and comstack::type.

Referenced by cs_set_ssl_ctx().

◆ parse_host_port()

void parse_host_port ( const char * host_port,
char * tmp,
size_t tmp_sz,
const char ** host,
const char ** port )
static

Definition at line 432 of file tcpip.c.

References parse_host_port().

Referenced by parse_host_port(), tcpip_getaddrinfo(), and tcpip_rcvconnect().

◆ ssl_type()

◆ tcpip_accept()

◆ tcpip_addrstr()

◆ tcpip_bind()

int tcpip_bind ( COMSTACK h,
void * address,
int mode )
static

◆ tcpip_close()

void tcpip_close ( COMSTACK h)
static

◆ tcpip_connect()

◆ tcpip_get()

◆ tcpip_getaddrinfo()

struct addrinfo * tcpip_getaddrinfo ( const char * str,
const char * port,
int * ipv6_only )

Definition at line 471 of file tcpip.c.

References parse_host_port(), and tcpip_getaddrinfo().

Referenced by create_net_socket(), tcpip_getaddrinfo(), and tcpip_straddr().

◆ tcpip_init()

int tcpip_init ( void )
static

Definition at line 154 of file tcpip.c.

References log_level, tcpip_init(), yaz_init_globals(), and yaz_log_module_level().

Referenced by tcpip_init(), tcpip_straddr(), and tcpip_type().

◆ tcpip_listen()

int tcpip_listen ( COMSTACK h,
char * raddr,
int * addrlen,
int(* check_ip )(void *cd, const char *a, int len, int type),
void * cd )
static

◆ tcpip_more()

int tcpip_more ( COMSTACK h)
static

◆ tcpip_put()

◆ tcpip_rcvconnect()

◆ tcpip_set_blocking()

int tcpip_set_blocking ( COMSTACK p,
int blocking )
static

◆ tcpip_state_create()

◆ tcpip_state_destroy()

◆ tcpip_straddr()

◆ tcpip_type()

◆ yaz_ssl_create()

COMSTACK yaz_ssl_create ( int s,
int flags,
int protocol,
const char * connect_host,
const char * connect_auth,
const char * bind_host )

Definition at line 401 of file tcpip.c.

References connect_and_bind(), ssl_type(), and yaz_ssl_create().

Referenced by cs_create_host2(), and yaz_ssl_create().

◆ yaz_tcpip_create()

COMSTACK yaz_tcpip_create ( int s,
int flags,
int protocol,
const char * connect_host )

Definition at line 346 of file tcpip.c.

References tcpip_state::connect_host, yaz_tcpip_create(), and yaz_tcpip_create2().

Referenced by yaz_tcpip_create().

◆ yaz_tcpip_create2()

COMSTACK yaz_tcpip_create2 ( int s,
int flags,
int protocol,
const char * connect_host,
const char * bind_host )

◆ yaz_tcpip_create3()

COMSTACK yaz_tcpip_create3 ( int s,
int flags,
int protocol,
const char * connect_host,
const char * connect_auth,
const char * bind_host )

◆ yaz_tcpip_get_error_details()

const char * yaz_tcpip_get_error_details ( COMSTACK cs)

◆ yaz_tcpip_set_head_only()

Variable Documentation

◆ log_level

int log_level = 0
static

Definition at line 152 of file tcpip.c.