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
Unix domain sockets

Support for unixdomain sockets using sockets interface. More...

Files

file  unixsock.c
 Attach a thread to a unix socket start a new thread on connect.
 

Data Structures

struct  unixserv_sockthread
 Unix socket server data structure. More...
 
struct  unixclient_sockthread
 Unix socket client data structure. More...
 

Functions

struct fwsocketunixsocket_server (const char *sock, int protocol, int mask, socketrecv read, void *data)
 Create and run UNIX server socket thread. More...
 
struct fwsocketunixsocket_client (const char *sock, int protocol, socketrecv read, void *data)
 Create a client thread on the socket. More...
 

Detailed Description

Support for unixdomain sockets using sockets interface.

A thread is started on the sockect and will start a new client thread on each connection with the socket and data reference.

Function Documentation

struct fwsocket* unixsocket_client ( const char *  sock,
int  protocol,
socketrecv  read,
void *  data 
)

Create a client thread on the socket.

It is not recomended to use SOCK_DGRAM as it requires a socket endpoint [inode] created this is done in /tmp using the basename of the socket and 6 random chars. this file is set to have no permissions as we only need the inode.

Parameters
sockPath to UNIX socket
protocolEither SOCK_STREAM or SOCK_DGRAM, SOCK_STREAM is recomended.
readCall back to call when read is ready.
dataReference to data to be returned in read callback.
Returns
Socket file descriptor

Definition at line 310 of file unixsock.c.

References fwsocket::addr, fwsocket::flags, make_socket(), objref(), objunref(), fwsocket::sock, SOCK_FLAG_UNIX, strlenzero(), and sockstruct::un.

Referenced by unixsocktest().

310  {
311  struct fwsocket *fws;
312  union sockstruct caddr, *saddr;
313  char *temp = NULL;
314  const char *tmpsock;
315  int salen;
316  mode_t omask;
317 
318  /*Create a UNIX socket structure*/
319  if (!(fws = make_socket(PF_UNIX, protocol, 0, NULL))) {
320  return NULL;
321  }
322 
323  /* bind my endpoint to temp file*/
324  if (protocol == SOCK_DGRAM) {
325  /*yip i want only a inode here folks*/
326  omask = umask(S_IXUSR | S_IRUSR | S_IWUSR | S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH | S_IXOTH);
327  tmpsock = basename((char*)sock);
328  temp = tempnam(NULL, tmpsock);
329  if (strlenzero(temp)) {
330  if (temp) {
331  free(temp);
332  }
333  objunref(fws);
334  return NULL;
335  }
336 
337  /*Allocate address and connect to the client*/
338  salen = sizeof(caddr.un);
339  memset(&caddr.un, 0, salen);
340  caddr.un.sun_family = PF_UNIX;
341  strncpy((char *)caddr.un.sun_path, temp, sizeof(caddr.un.sun_path) -1);
342 
343  if (bind(fws->sock, (struct sockaddr *)&caddr.un, salen)) {
344  /*reset umask*/
345  umask(omask);
346  if (temp) {
347  if (!strlenzero(temp)) {
348  unlink(temp);
349  }
350  free(temp);
351  }
352  objunref(fws);
353  return NULL;
354  }
355  /*reset umask*/
356  umask(omask);
357  }
358 
359  /*Allocate address and connect to the server*/
360  saddr = &fws->addr;
361  salen = sizeof(saddr->un);
362  memset(&saddr->un, 0, salen);
363  saddr->un.sun_family = PF_UNIX;
364  strncpy((char *)saddr->un.sun_path, sock, sizeof(saddr->un.sun_path) -1);
365 
366  if (connect(fws->sock, (struct sockaddr *)&saddr->un, salen)) {
367  if (temp) {
368  if (!strlenzero(temp)) {
369  unlink(temp);
370  }
371  free(temp);
372  }
373  objunref(fws);
374  return NULL;
375  }
376 
377  fws->flags |= SOCK_FLAG_UNIX;
378  if (!(new_unixclientthread(fws, temp, read, data))) {
379  if (temp) {
380  if (!strlenzero(temp)) {
381  unlink(temp);
382  }
383  free(temp);
384  }
385  objunref(fws);
386  return NULL;
387  }
388 
389  return (objref(fws)) ? fws : NULL;
390 }
int strlenzero(const char *str)
Check if a string is zero length.
Definition: util.c:341
union sockstruct addr
system socket data structure.
Definition: dtsapp.h:143
struct sockaddr_un un
Unix sockets.
Definition: dtsapp.h:85
int objref(void *data)
Reference a object.
Definition: refobj.c:153
Socket data structure.
Definition: dtsapp.h:131
int sock
Socket FD.
Definition: dtsapp.h:133
struct fwsocket * make_socket(int family, int type, int proto, void *ssl)
Allocate a socket structure and return reference.
Definition: socket.c:120
Socket union describing all address types.
Definition: dtsapp.h:80
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
struct fwsocket* unixsocket_server ( const char *  sock,
int  protocol,
int  mask,
socketrecv  read,
void *  data 
)

Create and run UNIX server socket thread.

Parameters
sockPath to UNIX socket.
protocolProtocol number.
maskUmask for the socket.
readCallback to call when there is data available.
dataData reference to pass to read callback.
Returns
Reference to a socket

Definition at line 277 of file unixsock.c.

References unixserv_sockthread::data, framework_mkthread(), make_socket(), unixserv_sockthread::mask, objalloc(), objref(), objunref(), unixserv_sockthread::protocol, unixserv_sockthread::read, unixserv_sockthread::sock, and unixserv_sockthread::sockpath.

Referenced by unixsocktest().

277  {
278  struct unixserv_sockthread *unsock;
279 
280  if (!(unsock = objalloc(sizeof(*unsock), free_unixserv))) {
281  return NULL;
282  }
283 
284  strncpy(unsock->sockpath, sock, UNIX_PATH_MAX);
285  unsock->mask = mask;
286  unsock->read = read;
287  unsock->protocol = protocol;
288  unsock->data = (objref(data)) ? data : NULL;
289 
290  /*Create a UNIX socket structure*/
291  if (!(unsock->sock = make_socket(PF_UNIX, protocol, 0, NULL))) {
292  objunref(unsock);
293  return NULL;
294  }
295 
296  framework_mkthread(unsock_serv, NULL, NULL, unsock, 0);
297  return (objref(unsock->sock)) ? unsock->sock : NULL;
298 }
int protocol
Socket protocol.
Definition: unixsock.c:54
char sockpath[UNIX_PATH_MAX+1]
Socket path.
Definition: unixsock.c:50
int mask
Socket umask.
Definition: unixsock.c:52
int objref(void *data)
Reference a object.
Definition: refobj.c:153
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
struct fwsocket * sock
Socket reference.
Definition: unixsock.c:48
void * data
Data reference passed to callback.
Definition: unixsock.c:59
struct thread_pvt * framework_mkthread(threadfunc, threadcleanup, threadsighandler, void *data, int flags)
create a thread result must be unreferenced
Definition: thread.c:387
Unix socket server data structure.
Definition: unixsock.c:46
socketrecv read
Thread to begin on client connect.
Definition: unixsock.c:57
struct fwsocket * make_socket(int family, int type, int proto, void *ssl)
Allocate a socket structure and return reference.
Definition: socket.c:120
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184