Nagios  4.4.6
Dev docs for Nagios core and neb-module hackers
nsock.h
Go to the documentation of this file.
1 #ifndef LIBNAGIOS_NSOCK_H_INCLUDED
2 #define LIBNAGIOS_NSOCK_H_INCLUDED
3 #include <errno.h>
4 
5 /**
6  * @file nsock.h
7  * @brief Nagios socket helper library
8  *
9  * This is a pretty stupid library, but since so many addons and
10  * now Nagios core itself makes use of sockets, we might as well
11  * have some simple wrappers for it that handle the most common
12  * cases.
13  *
14  * @{
15  */
16 
17 #define NSOCK_EBIND (-1) /**< failed to bind() */
18 #define NSOCK_ELISTEN (-2) /**< failed to listen() */
19 #define NSOCK_ESOCKET (-3) /**< failed to socket() */
20 #define NSOCK_EUNLINK (-4) /**< failed to unlink() */
21 #define NSOCK_ECONNECT (-5) /**< failed to connect() */
22 #define NSOCK_EFCNTL (-6) /**< failed to fcntl() */
23 #define NSOCK_EINVAL (-EINVAL) /**< -22, normally */
24 
25 /* flags for the various create calls */
26 #define NSOCK_TCP (1 << 0) /**< use tcp mode */
27 #define NSOCK_UDP (1 << 1) /**< use udp mode */
28 #define NSOCK_UNLINK (1 << 2) /**< unlink existing path (only nsock_unix) */
29 #define NSOCK_REUSE (1 << 2) /**< reuse existing address */
30 #define NSOCK_CONNECT (1 << 3) /**< connect rather than create */
31 #define NSOCK_BLOCK (1 << 4) /**< socket should be in blocking mode */
32 
33 /**
34  * Grab an error string relating to nsock_unix()
35  * @param code The error code return by the nsock library
36  * @return An error string describing the error
37  */
38 extern const char *nsock_strerror(int code);
39 
40 /**
41  * Create or connect to a unix socket
42  * To control permissions on sockets when NSOCK_LISTEN is specified,
43  * callers will have to modify their umask() before (and possibly
44  * after) the nsock_unix() call.
45  *
46  * @param path The path to connect to or create
47  * @param flags Various options controlling the mode of the socket
48  * @return An NSOCK_E macro on errors, the created socket on success
49  */
50 extern int nsock_unix(const char *path, unsigned int flags);
51 
52 /**
53  * Write a nul-terminated message to the socket pointed to by sd.
54  * This isn't quite the same as dprintf(), which doesn't include
55  * the terminating nul byte.
56  * @note This function may block, so poll(2) for writability
57  * @param sd The socket to write to
58  * @param fmt The format string
59  * @return Whatever write() returns
60  */
61 extern int nsock_printf_nul(int sd, const char *fmt, ...)
62  __attribute__((__format__(__printf__, 2, 3)));
63 
64 /**
65  * Write a printf()-formatted string to the socket pointed to by sd.
66  * This is identical to dprintf(), which is unfortunately GNU only.
67  * @note This function may block, so poll(2) for writability
68  * @param sd The socket to write to
69  * @param fmt The format string
70  * @return Whatever write() returns
71  */
72 extern int nsock_printf(int sd, const char *fmt, ...)
73  __attribute__((__format__(__printf__, 2, 3)));
74 
75 /** @} */
76 #endif /* LIBNAGIOS_NSOCK_H_INCLUDED */
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,...
nsock_printf
int int nsock_printf(int sd, const char *fmt,...) __attribute__((__format__(__printf__
Write a printf()-formatted string to the socket pointed to by sd.
nsock_strerror
const char * nsock_strerror(int code)
Grab an error string relating to nsock_unix()
nsock_printf_nul
int nsock_printf_nul(int sd, const char *fmt,...) __attribute__((__format__(__printf__
Write a nul-terminated message to the socket pointed to by sd.