Nagios  4.4.6
Dev docs for Nagios core and neb-module hackers
nsock.h File Reference

Nagios socket helper library. More...

#include <errno.h>

Go to the source code of this file.

#define NSOCK_EBIND   (-1)
 failed to bind()
 
#define NSOCK_ELISTEN   (-2)
 failed to listen()
 
#define NSOCK_ESOCKET   (-3)
 failed to socket()
 
#define NSOCK_EUNLINK   (-4)
 failed to unlink()
 
#define NSOCK_ECONNECT   (-5)
 failed to connect()
 
#define NSOCK_EFCNTL   (-6)
 failed to fcntl()
 
#define NSOCK_EINVAL   (-EINVAL)
 -22, normally
 
#define NSOCK_TCP   (1 << 0)
 use tcp mode
 
#define NSOCK_UDP   (1 << 1)
 use udp mode
 
#define NSOCK_UNLINK   (1 << 2)
 unlink existing path (only nsock_unix)
 
#define NSOCK_REUSE   (1 << 2)
 reuse existing address
 
#define NSOCK_CONNECT   (1 << 3)
 connect rather than create
 
#define NSOCK_BLOCK   (1 << 4)
 socket should be in blocking mode
 
const char * nsock_strerror (int code)
 Grab an error string relating to nsock_unix() More...
 
int nsock_unix (const char *path, unsigned int flags)
 Create or connect to a unix socket To control permissions on sockets when NSOCK_LISTEN is specified, callers will have to modify their umask() before (and possibly after) the nsock_unix() call. More...
 
int nsock_printf_nul (int sd, const char *fmt,...) __attribute__((__format__(__printf__
 Write a nul-terminated message to the socket pointed to by sd. More...
 
int int nsock_printf (int sd, const char *fmt,...) __attribute__((__format__(__printf__
 Write a printf()-formatted string to the socket pointed to by sd. More...
 

Detailed Description

Nagios socket helper library.

This is a pretty stupid library, but since so many addons and now Nagios core itself makes use of sockets, we might as well have some simple wrappers for it that handle the most common cases.

Function Documentation

◆ nsock_printf()

int int nsock_printf ( int  sd,
const char *  fmt,
  ... 
)

Write a printf()-formatted string to the socket pointed to by sd.

This is identical to dprintf(), which is unfortunately GNU only.

Note
This function may block, so poll(2) for writability
Parameters
sdThe socket to write to
fmtThe format string
Returns
Whatever write() returns

◆ nsock_printf_nul()

int nsock_printf_nul ( int  sd,
const char *  fmt,
  ... 
)

Write a nul-terminated message to the socket pointed to by sd.

This isn't quite the same as dprintf(), which doesn't include the terminating nul byte.

Note
This function may block, so poll(2) for writability
Parameters
sdThe socket to write to
fmtThe format string
Returns
Whatever write() returns

◆ nsock_strerror()

const char* nsock_strerror ( int  code)

Grab an error string relating to nsock_unix()

Parameters
codeThe error code return by the nsock library
Returns
An error string describing the error

◆ nsock_unix()

int nsock_unix ( const char *  path,
unsigned int  flags 
)

Create or connect to a unix socket To control permissions on sockets when NSOCK_LISTEN is specified, callers will have to modify their umask() before (and possibly after) the nsock_unix() call.

Parameters
pathThe path to connect to or create
flagsVarious options controlling the mode of the socket
Returns
An NSOCK_E macro on errors, the created socket on success