DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Network socket interface

Allocate and initialise a socket for use as a client or server. More...

Modules

 SSL socket support
 TLSv1 SSLv2 SSLv3 DTLSv1 support.
 
 Unix domain sockets
 Support for unixdomain sockets using sockets interface.
 
 Multicast sockets
 Support for multicast sockets either IPv4 or IPv6.
 

Files

file  socket.c
 Allocate and initialise a socket for use as a client or server.
 
file  sslutil.c
 TLSv1 SSLv2 SSLv3 DTLSv1 support.
 

Data Structures

union  sockstruct
 Socket union describing all address types. More...
 
struct  fwsocket
 Socket data structure. More...
 
struct  socket_handler
 Socket handling thread data. More...
 

Typedefs

typedef void(* socketrecv )(struct fwsocket *, void *)
 Callback function to register with a socket that will be called when there is data available. More...
 

Enumerations

enum  sock_flags {
  SOCK_FLAG_BIND = 1 << 0, SOCK_FLAG_CLOSE = 1 << 1, SOCK_FLAG_SSL = 1 << 2, SOCK_FLAG_UNIX = 1 << 3,
  SOCK_FLAG_MCAST = 1 << 4
}
 Socket flags controling a socket. More...
 

Functions

void close_socket (struct fwsocket *sock)
 Mark the socket for closure and release the reference. More...
 
struct fwsocketmake_socket (int family, int type, int proto, void *ssl)
 Allocate a socket structure and return reference. More...
 
struct fwsocketaccept_socket (struct fwsocket *sock)
 Create and return a socket structure from accept() More...
 
struct fwsocketsockconnect (int family, int stype, int proto, const char *ipaddr, const char *port, void *ssl)
 Generic client socket. More...
 
struct fwsocketudpconnect (const char *ipaddr, const char *port, void *ssl)
 UDP Socket client. More...
 
struct fwsockettcpconnect (const char *ipaddr, const char *port, void *ssl)
 TCP Socket client. More...
 
struct fwsocketsockbind (int family, int stype, int proto, const char *ipaddr, const char *port, void *ssl, int backlog)
 Generic server socket. More...
 
struct fwsocketudpbind (const char *ipaddr, const char *port, void *ssl)
 UDP server socket. More...
 
struct fwsockettcpbind (const char *ipaddr, const char *port, void *ssl, int backlog)
 Generic server socket. More...
 
void socketserver (struct fwsocket *sock, socketrecv read, socketrecv acceptfunc, threadcleanup cleanup, void *data)
 Create a server thread with a socket that has been created with sockbind udpbind or tcpbind. More...
 
void socketclient (struct fwsocket *sock, void *data, socketrecv read, threadcleanup cleanup)
 Create a server thread with a socket that has been created with sockbind udpbind or tcpbind. More...
 
const char * sockaddr2ip (union sockstruct *addr, char *buff, int blen)
 Return the ip address of a sockstruct addr. More...
 
int socketread_d (struct fwsocket *sock, void *buf, int num, union sockstruct *addr)
 Read from a socket into a buffer. More...
 
int socketread (struct fwsocket *sock, void *buf, int num)
 Read from a socket into a buffer. More...
 
int socketwrite_d (struct fwsocket *sock, const void *buf, int num, union sockstruct *addr)
 Write a buffer to a socket. More...
 
int socketwrite (struct fwsocket *sock, const void *buf, int num)
 Write a buffer to a socket. More...
 

Detailed Description

Allocate and initialise a socket for use as a client or server.

See Also
Socket Interface
Socket Example (Echo Server/Client)

Typedef Documentation

typedef void(* socketrecv)(struct fwsocket *, void *)

Callback function to register with a socket that will be called when there is data available.

Parameters
sockSocket structure data arrived on.
dataReference to data held by client/server thread.

Definition at line 259 of file dtsapp.h.

Enumeration Type Documentation

enum sock_flags

Socket flags controling a socket.

Enumerator
SOCK_FLAG_BIND 

The socket has been bound and awaiting connections.

SOCK_FLAG_CLOSE 

The socket is going away stop processing in its thread.

SOCK_FLAG_SSL 

SSL has been requested on this socket dont allow clear read/send.

SOCK_FLAG_UNIX 

UNIX Domain Socket.

SOCK_FLAG_MCAST 

Multicast Socket.

Definition at line 102 of file dtsapp.h.

102  {
104  SOCK_FLAG_BIND = 1 << 0,
106  SOCK_FLAG_CLOSE = 1 << 1,
108  SOCK_FLAG_SSL = 1 << 2,
110  SOCK_FLAG_UNIX = 1 << 3,
112  SOCK_FLAG_MCAST = 1 << 4
113 };
Multicast Socket.
Definition: dtsapp.h:112
SSL has been requested on this socket dont allow clear read/send.
Definition: dtsapp.h:108
The socket has been bound and awaiting connections.
Definition: dtsapp.h:104
UNIX Domain Socket.
Definition: dtsapp.h:110
The socket is going away stop processing in its thread.
Definition: dtsapp.h:106

Function Documentation

struct fwsocket* accept_socket ( struct fwsocket sock)

Create and return a socket structure from accept()

Parameters
sockReference to the socket its accepted on.
Returns
Reference to new socket.

Definition at line 144 of file socket.c.

References fwsocket::addr, objalloc(), objlock(), objunlock(), objunref(), fwsocket::proto, sockstruct::sa, fwsocket::sock, fwsocket::ssl, tlsaccept(), and fwsocket::type.

144  {
145  struct fwsocket *si;
146  socklen_t salen = sizeof(si->addr);
147 
148  if (!(si = objalloc(sizeof(*si),clean_fwsocket))) {
149  return NULL;
150  }
151 
152  objlock(sock);
153  if ((si->sock = accept(sock->sock, &si->addr.sa, &salen)) < 0) {
154  objunlock(sock);
155  objunref(si);
156  return NULL;
157  }
158 
159  si->type = sock->type;
160  si->proto = sock->proto;
161 
162  if (sock->ssl) {
163  tlsaccept(si, sock->ssl);
164  }
165  objunlock(sock);
166 
167  return (si);
168 }
union sockstruct addr
system socket data structure.
Definition: dtsapp.h:143
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
Socket data structure.
Definition: dtsapp.h:131
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int sock
Socket FD.
Definition: dtsapp.h:133
void tlsaccept(struct fwsocket *sock, struct ssldata *orig)
Create SSL session for new connection.
Definition: sslutil.c:382
int proto
Socket protocol.
Definition: dtsapp.h:135
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
struct sockaddr sa
Base socket addr structure.
Definition: dtsapp.h:82
int type
Socket type.
Definition: dtsapp.h:137
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
void close_socket ( struct fwsocket sock)

Mark the socket for closure and release the reference.

Parameters
sockSocket to close.

Definition at line 79 of file socket.c.

References objunref(), setflag, and SOCK_FLAG_CLOSE.

Referenced by socktest(), and unixsocktest().

79  {
80  if (sock) {
81  setflag(sock, SOCK_FLAG_CLOSE);
82  objunref(sock);
83  }
84 }
#define setflag(obj, flag)
Atomically set a flag in the flags field of a referenced object.
Definition: dtsapp.h:925
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
The socket is going away stop processing in its thread.
Definition: dtsapp.h:106
struct fwsocket* make_socket ( int  family,
int  type,
int  proto,
void *  ssl 
)

Allocate a socket structure and return reference.

The socket FD is assined by a call to socket.

Warning
This function should not be called directly.
Parameters
familyProtocol family.
typeSocket type.
protoProtocol to be used.
sslSSL structure to associate with the socket.
Returns
Reference to socket structure holding a FD.

Definition at line 120 of file socket.c.

References objalloc(), objunref(), fwsocket::proto, fwsocket::sock, fwsocket::ssl, and fwsocket::type.

Referenced by dtls_listenssl(), mcast_socket(), unixsocket_client(), and unixsocket_server().

120  {
121  struct fwsocket *si;
122 
123  if (!(si = objalloc(sizeof(*si),clean_fwsocket))) {
124  return NULL;
125  }
126 
127  if ((si->sock = socket(family, type, proto)) < 0) {
128  objunref(si);
129  return NULL;
130  };
131 
132  if (ssl) {
133  si->ssl = ssl;
134  }
135  si->type = type;
136  si->proto = proto;
137 
138  return (si);
139 }
Socket data structure.
Definition: dtsapp.h:131
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
int sock
Socket FD.
Definition: dtsapp.h:133
int proto
Socket protocol.
Definition: dtsapp.h:135
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
int type
Socket type.
Definition: dtsapp.h:137
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
const char* sockaddr2ip ( union sockstruct addr,
char *  buff,
int  blen 
)

Return the ip address of a sockstruct addr.

Parameters
addrSocketstruct to return the address for.
buffBuffer the IP will be copied too.
blenBuffer length.
Returns
a pointer to buff.

Definition at line 504 of file socket.c.

References inet_ntop(), sockstruct::sa4, sockstruct::sa6, and sockstruct::ss.

504  {
505  if (!buff) {
506  return NULL;
507  }
508 
509  switch (addr->ss.ss_family) {
510  case PF_INET:
511  inet_ntop(PF_INET, &addr->sa4.sin_addr, buff, blen);
512  break;
513  case PF_INET6:
514  inet_ntop(PF_INET6, &addr->sa6.sin6_addr, buff, blen);
515  break;
516  }
517  return buff;
518 }
struct sockaddr_in sa4
IPv4 socket addr structure.
Definition: dtsapp.h:88
struct sockaddr_storage ss
Sockaddr storage is a &quot;magic&quot; struct been able to hold IPv4 or IPv6.
Definition: dtsapp.h:92
const char * inet_ntop(int af, const void *src, char *dest, socklen_t size)
Win32 implementation of inet_ntop.
Definition: winiface.cpp:43
struct sockaddr_in6 sa6
IPv6 socket addr structure.
Definition: dtsapp.h:90
struct fwsocket* sockbind ( int  family,
int  stype,
int  proto,
const char *  ipaddr,
const char *  port,
void *  ssl,
int  backlog 
)

Generic server socket.

See Also
udpbind
tcpbind
Parameters
familyProtocol family.
stypeSocket type.
protoSocket protocol.
ipaddrIpaddr to connect too.
portPort to connect too.
sslSSL structure to associate with socket.
backlogConnection backlog passed to listen.
Returns
Reference to socket structure.

Definition at line 290 of file socket.c.

290  {
291  return(_opensocket(family, stype, proto, ipaddr, port, ssl, 1, backlog));
292 }
int proto
Socket protocol.
Definition: dtsapp.h:135
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
struct fwsocket* sockconnect ( int  family,
int  stype,
int  proto,
const char *  ipaddr,
const char *  port,
void *  ssl 
)

Generic client socket.

See Also
udpconnect
tcpconnect
Parameters
familyProtocol family.
stypeSocket type.
protoSocket protocol.
ipaddrIpaddr to connect too.
portPort to connect too.
sslSSL structure to associate with socket.
Returns
Reference to socket structure.

Definition at line 250 of file socket.c.

250  {
251  return(_opensocket(family, stype, proto, ipaddr, port, ssl, 0, 0));
252 }
int proto
Socket protocol.
Definition: dtsapp.h:135
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
void socketclient ( struct fwsocket sock,
void *  data,
socketrecv  read,
threadcleanup  cleanup 
)

Create a server thread with a socket that has been created with sockbind udpbind or tcpbind.

See Also
sockclient
threadcleanup
socketrecv
Parameters
sockReference to a bound socket.
datato send to the callbacks in paramaters.
readCallback to handle data when ready to read.
cleanupThread cleanup function for when the socket closes.

Definition at line 493 of file socket.c.

References startsslclient().

Referenced by socktest().

493  {
494  startsslclient(sock);
495 
496  _start_socket_handler(sock, read, NULL, cleanup, data);
497 }
void startsslclient(struct fwsocket *sock)
Start SSL on a client socket.
Definition: sslutil.c:811
int socketread ( struct fwsocket sock,
void *  buf,
int  num 
)

Read from a socket into a buffer.

There are 2 functions each for reading and writing data to a socket.

Connected (client) sockets (Including UDP [SOCK_DGRAM])
Use of socketwrite and socketread is acceptable.
UDP (SOCK_DGRAM) servers.
These require use of socketread_d and socketwrite_d the exception is DTLS connections that use there own routines and either works.
Parameters
sockSocket structure to read from.
bufBuffer to fill.
numSize of the buffer.
Returns
Number of bytes read or -1 on error 0 will indicate connection closed.

Definition at line 489 of file sslutil.c.

References socketread_d().

Referenced by client_func().

489  {
490  return (socketread_d(sock, buf, num, NULL));
491 }
int socketread_d(struct fwsocket *sock, void *buf, int num, union sockstruct *addr)
Read from a socket into a buffer.
Definition: sslutil.c:406
int socketread_d ( struct fwsocket sock,
void *  buf,
int  num,
union sockstruct addr 
)

Read from a socket into a buffer.

There are 2 functions each for reading and writing data to a socket.

Connected (client) sockets (Including UDP [SOCK_DGRAM])
Use of socketwrite and socketread is acceptable.
UDP (SOCK_DGRAM) servers.
These require use of socketread_d and socketwrite_d the exception is DTLS connections that use there own routines and either works.
Parameters
sockSocket structure to read from.
bufBuffer to fill.
numSize of the buffer.
addrAddr structure to fill remote address in.
Returns
Number of bytes read or -1 on error 0 will indicate connection closed.

Definition at line 406 of file sslutil.c.

References fwsocket::flags, objlock(), objunlock(), objunref(), sockstruct::sa, fwsocket::sock, SOCK_FLAG_CLOSE, SOCK_FLAG_SSL, ssldata::ssl, fwsocket::ssl, testflag, and fwsocket::type.

Referenced by server_func(), and socketread().

406  {
407  struct ssldata *ssl = sock->ssl;
408  socklen_t salen = sizeof(*addr);
409  int ret, err, syserr;
410 
411  if (!ssl && !testflag(sock, SOCK_FLAG_SSL)) {
412  objlock(sock);
413  if (addr && (sock->type == SOCK_DGRAM)) {
414  ret = recvfrom(sock->sock, buf, num, 0, &addr->sa, &salen);
415  } else {
416 #ifndef __WIN32
417  ret = read(sock->sock, buf, num);
418 #else
419  ret = recv(sock->sock, buf, num, 0);
420 #endif
421  }
422  if (ret == 0) {
423  sock->flags |= SOCK_FLAG_CLOSE;
424  }
425  objunlock(sock);
426  return (ret);
427  } else if (!ssl) {
428  return -1;
429  }
430 
431  objlock(ssl);
432  /* ive been shutdown*/
433  if (!ssl->ssl) {
434  objunlock(ssl);
435  return (-1);
436  }
437  ret = SSL_read(ssl->ssl, buf, num);
438  err = SSL_get_error(ssl->ssl, ret);
439  if (ret == 0) {
440  sock->flags |= SOCK_FLAG_CLOSE;
441  }
442  objunlock(ssl);
443  switch (err) {
444  case SSL_ERROR_NONE:
445  break;
446  case SSL_ERROR_WANT_X509_LOOKUP:
447  printf("Want X509\n");
448  break;
449  case SSL_ERROR_WANT_READ:
450  printf("Read Want Read\n");
451  break;
452  case SSL_ERROR_WANT_WRITE:
453  printf("Read Want write\n");
454  break;
455  case SSL_ERROR_ZERO_RETURN:
456  case SSL_ERROR_SSL:
457  objlock(sock);
458  objunref(sock->ssl);
459  sock->ssl = NULL;
460  objunlock(sock);
461  break;
462  case SSL_ERROR_SYSCALL:
463  syserr = ERR_get_error();
464  if (syserr || (!syserr && (ret == -1))) {
465  printf("R syscall %i %i\n", syserr, ret);
466  }
467  break;
468  default
469  :
470  printf("other\n");
471  break;
472  }
473 
474  return (ret);
475 }
SSL data structure for enabling encryption on sockets.
Definition: sslutil.c:66
#define testflag(obj, flag)
Atomically test a flag in the flags field of a referenced object.
Definition: dtsapp.h:932
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
int sock
Socket FD.
Definition: dtsapp.h:133
SSL has been requested on this socket dont allow clear read/send.
Definition: dtsapp.h:108
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
struct sockaddr sa
Base socket addr structure.
Definition: dtsapp.h:82
int type
Socket type.
Definition: dtsapp.h:137
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
enum sock_flags flags
Socket control flags.
Definition: dtsapp.h:140
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
The socket is going away stop processing in its thread.
Definition: dtsapp.h:106
void socketserver ( struct fwsocket sock,
socketrecv  read,
socketrecv  acceptfunc,
threadcleanup  cleanup,
void *  data 
)

Create a server thread with a socket that has been created with sockbind udpbind or tcpbind.

See Also
sockclient
threadcleanup
socketrecv
Parameters
sockReference to a bound socket.
readCallback to handle data when ready to read.
acceptfuncFunction to call on connection accept.
cleanupThread cleanup function for when the socket closes.
datato send to the callbacks in paramaters.

Definition at line 463 of file socket.c.

References fwsocket::children, create_bucketlist(), dtsl_serveropts(), fwsocket::flags, objlock(), objunlock(), fwsocket::ssl, and fwsocket::type.

Referenced by socktest().

464  {
465 
466  objlock(sock);
467  if (sock->flags & SOCK_FLAG_BIND) {
468  if (sock->ssl || !(sock->type == SOCK_DGRAM)) {
469  sock->children = create_bucketlist(6, hash_socket);
470  }
471  if (sock->ssl && (sock->type == SOCK_DGRAM)) {
472  objunlock(sock);
473  dtsl_serveropts(sock);
474  } else {
475  objunlock(sock);
476  }
477  } else {
478  objunlock(sock);
479  }
480  _start_socket_handler(sock, read, acceptfunc, cleanup, data);
481 }
void * create_bucketlist(int bitmask, blisthash hash_function)
Definition: refobj.c:356
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
The socket has been bound and awaiting connections.
Definition: dtsapp.h:104
struct bucket_list * children
We are the parent this is a list of spawn.
Definition: dtsapp.h:150
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
int type
Socket type.
Definition: dtsapp.h:137
void dtsl_serveropts(struct fwsocket *sock)
Start up the DTLSv1 Server.
Definition: sslutil.c:685
enum sock_flags flags
Socket control flags.
Definition: dtsapp.h:140
int socketwrite ( struct fwsocket sock,
const void *  buf,
int  num 
)

Write a buffer to a socket.

There are 2 functions each for reading and writing data to a socket.

Connected (client) sockets (Including UDP [SOCK_DGRAM])
Use of socketwrite and socketread is acceptable.
UDP (SOCK_DGRAM) servers.
These require use of socketread_d and socketwrite_d the exception is DTLS connections that use there own routines and either works.
Parameters
sockSocket structure to send data too.
bufBuffer to send.
numLengthe of the buffer.
Returns
Number of bytes written or -1 on error 0 will indicate some error in SSL.

Definition at line 629 of file sslutil.c.

References socketwrite_d().

Referenced by client_func(), and socktest().

629  {
630  return (socketwrite_d(sock, buf, num, NULL));
631 }
int socketwrite_d(struct fwsocket *sock, const void *buf, int num, union sockstruct *addr)
Write a buffer to a socket.
Definition: sslutil.c:508
int socketwrite_d ( struct fwsocket sock,
const void *  buf,
int  num,
union sockstruct addr 
)

Write a buffer to a socket.

There are 2 functions each for reading and writing data to a socket.

Connected (client) sockets (Including UDP [SOCK_DGRAM])
Use of socketwrite and socketread is acceptable.
UDP (SOCK_DGRAM) servers.
These require use of socketread_d and socketwrite_d the exception is DTLS connections that use there own routines and either works.
Todo:
implement send/sendto in WIN32
Parameters
sockSocket structure to send data too.
bufBuffer to send.
numLengthe of the buffer.
addrAddr structure to send the buffer too (SOCK_DGRAM) see notes.
Returns
Number of bytes written or -1 on error 0 will indicate some error in SSL.

Definition at line 508 of file sslutil.c.

References fwsocket::addr, fwsocket::flags, objlock(), objunlock(), objunref(), sockstruct::sa, setflag, fwsocket::sock, SOCK_FLAG_CLOSE, SOCK_FLAG_MCAST, SOCK_FLAG_SSL, SOCK_FLAG_UNIX, sockstruct::ss, ssldata::ssl, fwsocket::ssl, testflag, fwsocket::type, and sockstruct::un.

Referenced by server_func(), socketwrite(), and unixsocktest().

508  {
509  struct ssldata *ssl = (sock) ? sock->ssl : NULL;
510  int ret, err, syserr;
511 
512  if (!sock) {
513  return (-1);
514  }
515 
516  if (!ssl && !testflag(sock, SOCK_FLAG_SSL)) {
517  objlock(sock);
518  if (addr && (sock->type == SOCK_DGRAM)) {
519 #ifndef __WIN32
520  if (sock->flags & SOCK_FLAG_UNIX) {
521  ret = sendto(sock->sock, buf, num, MSG_NOSIGNAL, (const struct sockaddr *)&addr->un, sizeof(addr->un));
522  } else if (sock->flags & SOCK_FLAG_MCAST) {
523  ret = sendto(sock->sock, buf, num, MSG_NOSIGNAL, &sock->addr.sa, sizeof(sock->addr.ss));
524  } else {
525  ret = sendto(sock->sock, buf, num, MSG_NOSIGNAL, &addr->sa, sizeof(*addr));
526  }
527 #else
528  if (sock->flags & SOCK_FLAG_MCAST) {
529  ret = sendto(sock->sock, buf, num, 0, &sock->addr.sa, sizeof(sock->addr.ss));
530  } else {
531  ret = sendto(sock->sock, buf, num, 0, &addr->sa, sizeof(*addr));
532  }
533 #endif
534  } else {
535 #ifndef __WIN32
536  if (sock->flags & SOCK_FLAG_MCAST) {
537  ret = sendto(sock->sock, buf, num, MSG_NOSIGNAL, &sock->addr.sa, sizeof(sock->addr.ss));
538  } else {
539  ret = send(sock->sock, buf, num, MSG_NOSIGNAL);
540  }
541 #else
542  if (sock->flags & SOCK_FLAG_MCAST) {
543  ret = sendto(sock->sock, buf, num, 0, &sock->addr.sa, sizeof(sock->addr.ss));
544  } else {
545  ret = send(sock->sock, buf, num, 0);
546  }
547 #endif
548  }
549  if (ret == -1) {
550  switch(errno) {
551  case EBADF:
552  case EPIPE:
553 #ifndef __WIN32
554  case ENOTCONN:
555  case ENOTSOCK:
556 #endif
557  sock->flags |= SOCK_FLAG_CLOSE;
558  break;
559  }
560  }
561  objunlock(sock);
562  return (ret);
563  } else if (!ssl) {
564  return -1;
565  }
566 
567  if (ssl && ssl->ssl) {
568  objlock(ssl);
569  if (SSL_state(ssl->ssl) != SSL_ST_OK) {
570  objunlock(ssl);
571  return (SSL_ERROR_SSL);
572  }
573  ret = SSL_write(ssl->ssl, buf, num);
574  err = SSL_get_error(ssl->ssl, ret);
575  objunlock(ssl);
576  } else {
577  return -1;
578  }
579 
580  if (ret == -1) {
581  setflag(sock, SOCK_FLAG_CLOSE);
582  }
583 
584  switch(err) {
585  case SSL_ERROR_NONE:
586  break;
587  case SSL_ERROR_WANT_READ:
588  printf("Send Want Read\n");
589  break;
590  case SSL_ERROR_WANT_WRITE:
591  printf("Send Want write\n");
592  break;
593  case SSL_ERROR_WANT_X509_LOOKUP:
594  printf("Want X509\n");
595  break;
596  case SSL_ERROR_ZERO_RETURN:
597  case SSL_ERROR_SSL:
598  objlock(sock);
599  objunref(sock->ssl);
600  sock->ssl = NULL;
601  objunlock(sock);
602  break;
603  case SSL_ERROR_SYSCALL:
604  syserr = ERR_get_error();
605  if (syserr || (!syserr && (ret == -1))) {
606  printf("W syscall %i %i\n", syserr, ret);
607  }
608  break;
609  default:
610  printf("other\n");
611  break;
612  }
613 
614  return (ret);
615 }
SSL data structure for enabling encryption on sockets.
Definition: sslutil.c:66
union sockstruct addr
system socket data structure.
Definition: dtsapp.h:143
struct sockaddr_un un
Unix sockets.
Definition: dtsapp.h:85
#define testflag(obj, flag)
Atomically test a flag in the flags field of a referenced object.
Definition: dtsapp.h:932
int objlock(void *data)
Lock the reference.
Definition: refobj.c:269
Multicast Socket.
Definition: dtsapp.h:112
#define setflag(obj, flag)
Atomically set a flag in the flags field of a referenced object.
Definition: dtsapp.h:925
int sock
Socket FD.
Definition: dtsapp.h:133
SSL has been requested on this socket dont allow clear read/send.
Definition: dtsapp.h:108
struct ssldata * ssl
SSL structure for encryption.
Definition: dtsapp.h:146
int objunlock(void *data)
Unlock a reference.
Definition: refobj.c:301
struct sockaddr_storage ss
Sockaddr storage is a &quot;magic&quot; struct been able to hold IPv4 or IPv6.
Definition: dtsapp.h:92
struct sockaddr sa
Base socket addr structure.
Definition: dtsapp.h:82
int type
Socket type.
Definition: dtsapp.h:137
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
enum sock_flags flags
Socket control flags.
Definition: dtsapp.h:140
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
UNIX Domain Socket.
Definition: dtsapp.h:110
The socket is going away stop processing in its thread.
Definition: dtsapp.h:106
struct fwsocket* tcpbind ( const char *  ipaddr,
const char *  port,
void *  ssl,
int  backlog 
)

Generic server socket.

See Also
udpbind
sockbind
Parameters
ipaddrIpaddr to connect too.
portPort to connect too.
sslSSL structure to associate with socket.
backlogConnection backlog passed to listen.
Returns
Reference to socket structure.

Definition at line 315 of file socket.c.

Referenced by socktest().

315  {
316  return (_opensocket(PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, ipaddr, port, ssl, 1, backlog));
317 }
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
struct fwsocket* tcpconnect ( const char *  ipaddr,
const char *  port,
void *  ssl 
)

TCP Socket client.

See Also
sockconnect
udpconnect
Parameters
ipaddrIpaddr to connect too.
portPort to connect too.
sslSSL structure to associate with socket.
Returns
Reference to socket structure.

Definition at line 274 of file socket.c.

Referenced by socktest().

274  {
275  return (_opensocket(PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, ipaddr, port, ssl, 0, 0));
276 }
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
struct fwsocket* udpbind ( const char *  ipaddr,
const char *  port,
void *  ssl 
)

UDP server socket.

See Also
sockbind
tcpbind
Parameters
ipaddrIpaddr to connect too.
portPort to connect too.
sslSSL structure to associate with socket.
Returns
Reference to socket structure.

Definition at line 302 of file socket.c.

Referenced by socktest().

302  {
303  return (_opensocket(PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, ipaddr, port, ssl, 1, 0));
304 }
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70
struct fwsocket* udpconnect ( const char *  ipaddr,
const char *  port,
void *  ssl 
)

UDP Socket client.

See Also
sockconnect
tcpconnect
Parameters
ipaddrIpaddr to connect too.
portPort to connect too.
sslSSL structure to associate with socket.
Returns
Reference to socket structure.

Definition at line 262 of file socket.c.

Referenced by socktest().

262  {
263  return (_opensocket(PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, ipaddr, port, ssl, 0, 0));
264 }
SSL * ssl
OpenSSL ssl.
Definition: sslutil.c:70