1 /*****************************************************************************
2 *
3 * Monitoring Plugins net utilities include file
4 *
5 * License: GPL
6 * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
7 * Copyright (c) 2003-2007 Monitoring Plugins Development Team
8 *
9 * Description:
10 *
11 * This file contains common include files and function definitions
12 * used in many of the plugins.
13 *
14 *
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 *
28 *
29 *****************************************************************************/
30 
31 #ifndef _NETUTILS_H_
32 #define _NETUTILS_H_
33 
34 #include "common.h"
35 #include "utils.h"
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <netdb.h>
39 
40 #ifdef HAVE_SYS_UN_H
41 # include <sys/un.h>
42 # ifndef UNIX_PATH_MAX
43    /* linux uses this, on sun it's hard-coded at 108 without a define, on BSD at 104 */
44 #  define UNIX_PATH_MAX 104
45 # endif /* UNIX_PATH_MAX */
46 #endif /* HAVE_SYS_UN_H */
47 
48 #ifndef HOST_MAX_BYTES
49 # define HOST_MAX_BYTES 255
50 #endif
51 
52 /* process_request and wrapper macros */
53 #define process_tcp_request(addr, port, sbuf, rbuf, rsize) \
54 	process_request(addr, port, IPPROTO_TCP, sbuf, rbuf, rsize)
55 #define process_udp_request(addr, port, sbuf, rbuf, rsize) \
56 	process_request(addr, port, IPPROTO_UDP, sbuf, rbuf, rsize)
57 int process_tcp_request2 (const char *address, int port,
58   const char *sbuffer, char *rbuffer, int rsize);
59 int process_request (const char *address, int port, int proto,
60   const char *sbuffer, char *rbuffer, int rsize);
61 
62 /* my_connect and wrapper macros */
63 #define my_tcp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_TCP)
64 #define my_udp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_UDP)
65 int np_net_connect(const char *address, int port, int *sd, int proto);
66 
67 /* send_request and wrapper macros */
68 #define send_tcp_request(s, sbuf, rbuf, rsize) \
69 	send_request(s, IPPROTO_TCP, sbuf, rbuf, rsize)
70 #define send_udp_request(s, sbuf, rbuf, rsize) \
71 	send_request(s, IPPROTO_UDP, sbuf, rbuf, rsize)
72 int send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size);
73 
74 
75 /* "is_*" wrapper macros and functions */
76 int is_host (const char *);
77 int is_addr (const char *);
78 int dns_lookup (const char *, struct sockaddr_storage *, int);
79 void host_or_die(const char *str);
80 #define resolve_host_or_addr(addr, family) dns_lookup(addr, NULL, family)
81 #define is_inet_addr(addr) resolve_host_or_addr(addr, AF_INET)
82 #ifdef USE_IPV6
83 #  define is_inet6_addr(addr) resolve_host_or_addr(addr, AF_INET6)
84 #  define is_hostname(addr) resolve_host_or_addr(addr, address_family)
85 #else
86 #  define is_hostname(addr) resolve_host_or_addr(addr, AF_INET)
87 #endif
88 
89 extern unsigned int socket_timeout;
90 extern unsigned int socket_timeout_state;
91 extern int econn_refuse_state;
92 extern int was_refused;
93 extern int address_family;
94 
95 RETSIGTYPE socket_timeout_alarm_handler (int) __attribute__((noreturn));
96 
97 /* SSL-Related functionality */
98 #ifdef HAVE_SSL
99 #  define MP_SSLv2 1
100 #  define MP_SSLv3 2
101 #  define MP_TLSv1 3
102 #  define MP_TLSv1_1 4
103 #  define MP_TLSv1_2 5
104 #  define MP_SSLv2_OR_NEWER 6
105 #  define MP_SSLv3_OR_NEWER 7
106 #  define MP_TLSv1_OR_NEWER 8
107 #  define MP_TLSv1_1_OR_NEWER 9
108 #  define MP_TLSv1_2_OR_NEWER 10
109 /* maybe this could be merged with the above np_net_connect, via some flags */
110 int np_net_ssl_init(int sd);
111 int np_net_ssl_init_with_hostname(int sd, char *host_name);
112 int np_net_ssl_init_with_hostname_and_version(int sd, char *host_name, int version);
113 int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int version, char *cert, char *privkey);
114 void np_net_ssl_cleanup();
115 int np_net_ssl_write(const void *buf, int num);
116 int np_net_ssl_read(void *buf, int num);
117 int np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit);
118 #endif /* HAVE_SSL */
119 
120 #endif /* _NETUTILS_H_ */
121