00001 #ifndef LIBNAGIOS_NSOCK_H_INCLUDED 00002 #define LIBNAGIOS_NSOCK_H_INCLUDED 00003 #include <errno.h> 00004 00005 /** 00006 * @file nsock.h 00007 * @brief Nagios socket helper library 00008 * 00009 * This is a pretty stupid library, but since so many addons and 00010 * now Nagios core itself makes use of sockets, we might as well 00011 * have some simple wrappers for it that handle the most common 00012 * cases. 00013 * 00014 * @{ 00015 */ 00016 00017 #define NSOCK_EBIND (-1) /**< failed to bind() */ 00018 #define NSOCK_ELISTEN (-2) /**< failed to listen() */ 00019 #define NSOCK_ESOCKET (-3) /**< failed to socket() */ 00020 #define NSOCK_EUNLINK (-4) /**< failed to unlink() */ 00021 #define NSOCK_ECONNECT (-5) /**< failed to connect() */ 00022 #define NSOCK_EFCNTL (-6) /**< failed to fcntl() */ 00023 #define NSOCK_EINVAL (-EINVAL) /**< -22, normally */ 00024 00025 /* flags for the various create calls */ 00026 #define NSOCK_TCP (1 << 0) /**< use tcp mode */ 00027 #define NSOCK_UDP (1 << 1) /**< use udp mode */ 00028 #define NSOCK_UNLINK (1 << 2) /**< unlink existing path (only nsock_unix) */ 00029 #define NSOCK_REUSE (1 << 2) /**< reuse existing address */ 00030 #define NSOCK_CONNECT (1 << 3) /**< connect rather than create */ 00031 #define NSOCK_BLOCK (1 << 4) /**< socket should be in blocking mode */ 00032 00033 /** 00034 * Grab an error string relating to nsock_unix() 00035 * @param code The error code return by the nsock library 00036 * @return An error string describing the error 00037 */ 00038 extern const char *nsock_strerror(int code); 00039 00040 /** 00041 * Create or connect to a unix socket 00042 * To control permissions on sockets when NSOCK_LISTEN is specified, 00043 * callers will have to modify their umask() before (and possibly 00044 * after) the nsock_unix() call. 00045 * 00046 * @param path The path to connect to or create 00047 * @param flags Various options controlling the mode of the socket 00048 * @return An NSOCK_E macro on errors, the created socket on succes 00049 */ 00050 extern int nsock_unix(const char *path, unsigned int flags); 00051 00052 /** 00053 * Write a nul-terminated message to the socket pointed to by sd. 00054 * This isn't quite the same as dprintf(), which doesn't include 00055 * the terminating nul byte. 00056 * @note This function may block, so poll(2) for writability 00057 * @param sd The socket to write to 00058 * @param fmt The format string 00059 * @return Whatever write() returns 00060 */ 00061 extern int nsock_printf_nul(int sd, const char *fmt, ...) 00062 __attribute__((__format__(__printf__, 2, 3))); 00063 00064 /** 00065 * Write a printf()-formatted string to the socket pointed to by sd. 00066 * This is identical to dprintf(), which is unfortunately GNU only. 00067 * @note This function may block, so poll(2) for writability 00068 * @param sd The socket to write to 00069 * @param fmt The format string 00070 * @return Whatever write() returns 00071 */ 00072 extern int nsock_printf(int sd, const char *fmt, ...) 00073 __attribute__((__format__(__printf__, 2, 3))); 00074 00075 /** @} */ 00076 #endif /* LIBNAGIOS_NSOCK_H_INCLUDED */