Bond's TCP Library  1.0
Bond's TCP Client/Server Library
tcp Namespace Reference

A tcp client/server library for linux that supports openSSL and EPoll. More...

Classes

class  Client
 A blocking or non-blocking TCP client connection. More...
 
class  DataSocket
 Represents a buffered socket that can send and receive data using optional SSL encryption. More...
 
class  EPoll
 Encapsulates the EPoll interface. More...
 
class  Server
 Listens for TCP connections and establishes Sessions. More...
 
class  Session
 Represents a TCP connection accepted by the Server. More...
 
class  Socket
 Encapsulates a socket handle that is capable of recieving epoll events. More...
 
class  SSL
 Encapsulates an SSL connection data structure. More...
 
class  SSLContext
 Encapsulates an openSSL SSL_CTX record. More...
 

Enumerations

enum  SocketState {
  UNCONNECTED =0, LISTENING, CONNECTING, CONNECTED,
  DISCONNECTED
}
 Determines the state of a socket. More...
 
enum  SSLMode { CLIENT, SERVER }
 

Functions

ostream logstream (clog.rdbuf())
 
void setLogStream (ostream *os)
 Set the output stream used by the library for log, warning and error messages. More...
 
void error (string msg)
 Send an error message to the log stream.
 
void error (string label, string msg)
 Send a labelled error message to the log stream.
 
void warning (string msg)
 Send a warning message to the log stream.
 
void warning (string label, string msg)
 Send an labelled warning message to the log stream.
 
void log (string msg)
 Send an log message to the log stream.
 
void log (string label, string msg)
 Send a labelled log message to the log stream.
 
int getDomainFromHostAndPort (const char *host, const char *port, int def_domain=AF_INET)
 Tries to determine which address family to use from a host and port string. More...
 
void initSSLLibrary ()
 Initialize the openSSL library. More...
 
void freeSSLLibrary ()
 Free up resources created by the openSSL library. More...
 
void print_error_string (unsigned long err, const char *const label)
 
int printSSLErrors_cb (const char *str, size_t len, void *u)
 
void printSSLErrors ()
 This method logs openSSL errors to cerr.
 
int wildcmp (const char *wild, const char *string)
 Wildcard compare function. More...
 
void print_cn_name (const char *label, X509_NAME *const name)
 Prints the certificate common name to clog.
 
void print_san_name (const char *label, X509 *const cert)
 Prints the certificate subject alt name to clog.
 
int verify_callback (int preverify, X509_STORE_CTX *x509_ctx)
 Prints the certificate details to clog.
 
int ctx_password_callback (char *buf, int size, int rwflag, void *userdata)
 
int ssl_password_callback (char *buf, int size, int rwflag, void *userdata)
 

Variables

bool sslinitialized_ {false}
 

Detailed Description

A tcp client/server library for linux that supports openSSL and EPoll.

Enumeration Type Documentation

◆ SocketState

enum tcp::SocketState
strong

Determines the state of a socket.

Not all states are valid for every socket type.

Definition at line 58 of file tcpsocket.h.

58 {UNCONNECTED=0, LISTENING, CONNECTING, CONNECTED, DISCONNECTED};

Function Documentation

◆ freeSSLLibrary()

void tcp::freeSSLLibrary ( )

Free up resources created by the openSSL library.

Applications should call this method at application shutdown.

Definition at line 32 of file tcpssl.cpp.

33 {
34  ERR_free_strings();
35 }

◆ getDomainFromHostAndPort()

int tcp::getDomainFromHostAndPort ( const char *  host,
const char *  port,
int  def_domain = AF_INET 
)

Tries to determine which address family to use from a host and port string.

If host is other than a numeric address, the address family will be detemined through a canonical name lookup

Returns
AF_INET or AF_INET6 if an address family can be determined, AF_UNSPEC otherwise

Definition at line 357 of file tcpsocket.cpp.

358 {
359  struct addrinfo hints;
360  struct addrinfo *result;
361  int errorcode;
362  int domain;
363 
364  memset(&hints,0,sizeof(struct addrinfo));
365  hints.ai_family = AF_UNSPEC;
366  hints.ai_flags = AI_NUMERICHOST;
367  errorcode = getaddrinfo(host,nullptr,&hints,&result);
368  if (errorcode == 0) {
369  domain = result->ai_family;
370  } else {
371  domain = AF_UNSPEC;
372  }
373  if (domain == AF_UNSPEC) {
374  hints.ai_flags = AI_CANONNAME;
375  errorcode = getaddrinfo(host,port,&hints,&result);
376  if (errorcode == 0) {
377  domain = result->ai_family;
378  }
379  }
380  if (domain == AF_UNSPEC) {
381  return def_domain;
382  } else {
383  return domain;
384  }
385 }

◆ initSSLLibrary()

void tcp::initSSLLibrary ( )

Initialize the openSSL library.

Applications should call this method once at application startup. It must be called before any other openSSL library functions are called.

Definition at line 19 of file tcpssl.cpp.

20 {
21  if (!sslinitialized_) {
22  SSL_library_init();
23  OpenSSL_add_all_algorithms();
24  SSL_load_error_strings();
25  ERR_load_crypto_strings();
26  OpenSSL_add_all_ciphers();
27  sslinitialized_ = true;
28  clog << "OpenSSL library initialized" << endl;
29  }
30 }
Here is the caller graph for this function:

◆ setLogStream()

void tcp::setLogStream ( ostream *  os)

Set the output stream used by the library for log, warning and error messages.

Defaults to clog

Definition at line 17 of file tcpsocket.cpp.

17 { if (os) logstream.rdbuf(os->rdbuf()); }

◆ wildcmp()

int tcp::wildcmp ( const char *  wild,
const char *  string 
)

Wildcard compare function.

This compare function performs comparisons against a string using the * and ? wildcard characters.

Remarks
Used internally to validate that hostName matches a certificate subjectName. Exposed because it might be useful elsewhere in a program.

Definition at line 62 of file tcpssl.cpp.

62  {
63  // Written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A>
64  const char *cp = NULL, *mp = NULL;
65 
66  while ((*string) && (*wild != '*')) {
67  if ((*wild != *string) && (*wild != '?')) {
68  return 0;
69  }
70  wild++;
71  string++;
72  }
73 
74  while (*string) {
75  if (*wild == '*') {
76  if (!*++wild) {
77  return 1;
78  }
79  mp = wild;
80  cp = string+1;
81  } else if ((*wild == *string) || (*wild == '?')) {
82  wild++;
83  string++;
84  } else {
85  wild = mp;
86  string = cp++;
87  }
88  }
89 
90  while (*wild == '*') {
91  wild++;
92  }
93  return !*wild;
94 }
Here is the caller graph for this function: