summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-09-15 15:49:26 +0200
committerLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2025-09-15 15:49:26 +0200
commit811da10fda11b1d0453453040647270700862d8f (patch)
treeb0060f09613b1cf088be92c9a6cf63b65db5dfc2
parentf855c5b5bbbc6d5436741fd8108be64825a3c76b (diff)
downloadmonitoring-plugins-811da10f.tar.gz
More refactoring
-rw-r--r--plugins-root/check_icmp.d/check_icmp_helpers.c4
-rw-r--r--plugins/check_load.c4
-rw-r--r--plugins/netutils.c224
-rw-r--r--plugins/netutils.h32
4 files changed, 139 insertions, 125 deletions
diff --git a/plugins-root/check_icmp.d/check_icmp_helpers.c b/plugins-root/check_icmp.d/check_icmp_helpers.c
index d56fbd8b..1b96a392 100644
--- a/plugins-root/check_icmp.d/check_icmp_helpers.c
+++ b/plugins-root/check_icmp.d/check_icmp_helpers.c
@@ -76,7 +76,7 @@ check_icmp_state check_icmp_state_init() {
76 76
77ping_target_create_wrapper ping_target_create(struct sockaddr_storage address) { 77ping_target_create_wrapper ping_target_create(struct sockaddr_storage address) {
78 ping_target_create_wrapper result = { 78 ping_target_create_wrapper result = {
79 .errorcode = OK, 79 .errorcode = 0,
80 }; 80 };
81 81
82 struct sockaddr_storage *tmp_addr = &address; 82 struct sockaddr_storage *tmp_addr = &address;
@@ -88,7 +88,7 @@ ping_target_create_wrapper ping_target_create(struct sockaddr_storage address) {
88 ((struct sockaddr_in *)tmp_addr)->sin_addr.s_addr == INADDR_ANY))) || 88 ((struct sockaddr_in *)tmp_addr)->sin_addr.s_addr == INADDR_ANY))) ||
89 (tmp_addr->ss_family == AF_INET6 && 89 (tmp_addr->ss_family == AF_INET6 &&
90 (((struct sockaddr_in6 *)tmp_addr)->sin6_addr.s6_addr == in6addr_any.s6_addr))) { 90 (((struct sockaddr_in6 *)tmp_addr)->sin6_addr.s6_addr == in6addr_any.s6_addr))) {
91 result.errorcode = ERROR; 91 result.errorcode = 1;
92 return result; 92 return result;
93 } 93 }
94 94
diff --git a/plugins/check_load.c b/plugins/check_load.c
index f7a6f7fd..644cd604 100644
--- a/plugins/check_load.c
+++ b/plugins/check_load.c
@@ -452,8 +452,8 @@ static top_processes_result print_top_consuming_processes(unsigned long n_procs_
452 top_processes_result result = { 452 top_processes_result result = {
453 .errorcode = OK, 453 .errorcode = OK,
454 }; 454 };
455 struct output chld_out; 455 output chld_out;
456 struct output chld_err; 456 output chld_err;
457 if (np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0) { 457 if (np_runcmd(PS_COMMAND, &chld_out, &chld_err, 0) != 0) {
458 fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND); 458 fprintf(stderr, _("'%s' exited with non-zero status.\n"), PS_COMMAND);
459 result.errorcode = ERROR; 459 result.errorcode = ERROR;
diff --git a/plugins/netutils.c b/plugins/netutils.c
index 92c53e4c..b4c6ff0a 100644
--- a/plugins/netutils.c
+++ b/plugins/netutils.c
@@ -30,13 +30,14 @@
30#include "common.h" 30#include "common.h"
31#include "output.h" 31#include "output.h"
32#include "states.h" 32#include "states.h"
33#include <sys/types.h>
33#include "netutils.h" 34#include "netutils.h"
34 35
35unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT; 36unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
36unsigned int socket_timeout_state = STATE_CRITICAL; 37mp_state_enum socket_timeout_state = STATE_CRITICAL;
37 38mp_state_enum econn_refuse_state = STATE_CRITICAL;
38int econn_refuse_state = STATE_CRITICAL;
39bool was_refused = false; 39bool was_refused = false;
40
40#if USE_IPV6 41#if USE_IPV6
41int address_family = AF_UNSPEC; 42int address_family = AF_UNSPEC;
42#else 43#else
@@ -63,39 +64,40 @@ void socket_timeout_alarm_handler(int sig) {
63/* connects to a host on a specified tcp port, sends a string, and gets a 64/* connects to a host on a specified tcp port, sends a string, and gets a
64 response. loops on select-recv until timeout or eof to get all of a 65 response. loops on select-recv until timeout or eof to get all of a
65 multi-packet answer */ 66 multi-packet answer */
66int process_tcp_request2(const char *server_address, int server_port, const char *send_buffer, 67mp_state_enum process_tcp_request2(const char *server_address, const int server_port,
67 char *recv_buffer, int recv_size) { 68 const char *send_buffer, char *recv_buffer,
69 const int recv_size) {
68 70
69 int result; 71 int socket;
70 int send_result;
71 int recv_result;
72 int sd;
73 struct timeval tv;
74 fd_set readfds;
75 int recv_length = 0;
76 72
77 result = np_net_connect(server_address, server_port, &sd, IPPROTO_TCP); 73 mp_state_enum connect_result =
78 if (result != STATE_OK) { 74 np_net_connect(server_address, server_port, &socket, IPPROTO_TCP);
75 if (connect_result != STATE_OK) {
79 return STATE_CRITICAL; 76 return STATE_CRITICAL;
80 } 77 }
81 78
82 send_result = send(sd, send_buffer, strlen(send_buffer), 0); 79 mp_state_enum result;
80 ssize_t send_result = send(socket, send_buffer, strlen(send_buffer), 0);
83 if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { 81 if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) {
84 // printf("%s\n", _("Send failed")); 82 // printf("%s\n", _("Send failed"));
85 result = STATE_WARNING; 83 result = STATE_WARNING;
86 } 84 }
87 85
88 while (1) { 86 fd_set readfds;
87 ssize_t recv_length = 0;
88 while (true) {
89 /* wait up to the number of seconds for socket timeout 89 /* wait up to the number of seconds for socket timeout
90 minus one for data from the host */ 90 minus one for data from the host */
91 tv.tv_sec = socket_timeout - 1; 91 struct timeval timeout = {
92 tv.tv_usec = 0; 92 .tv_sec = socket_timeout - 1,
93 .tv_usec = 0,
94 };
93 FD_ZERO(&readfds); 95 FD_ZERO(&readfds);
94 FD_SET(sd, &readfds); 96 FD_SET(socket, &readfds);
95 select(sd + 1, &readfds, NULL, NULL, &tv); 97 select(socket + 1, &readfds, NULL, NULL, &timeout);
96 98
97 /* make sure some data has arrived */ 99 /* make sure some data has arrived */
98 if (!FD_ISSET(sd, &readfds)) { /* it hasn't */ 100 if (!FD_ISSET(socket, &readfds)) { /* it hasn't */
99 if (!recv_length) { 101 if (!recv_length) {
100 strcpy(recv_buffer, ""); 102 strcpy(recv_buffer, "");
101 // printf("%s\n", _("No data was received from host!")); 103 // printf("%s\n", _("No data was received from host!"));
@@ -104,72 +106,69 @@ int process_tcp_request2(const char *server_address, int server_port, const char
104 recv_buffer[recv_length] = 0; 106 recv_buffer[recv_length] = 0;
105 } 107 }
106 break; 108 break;
107 } else { /* it has */ 109 } /* it has */
108 recv_result = 110
109 recv(sd, recv_buffer + recv_length, (size_t)recv_size - recv_length - 1, 0); 111 ssize_t recv_result =
110 if (recv_result == -1) { 112 recv(socket, recv_buffer + recv_length, (size_t)(recv_size - recv_length - 1), 0);
111 /* recv failed, bail out */ 113 if (recv_result == -1) {
112 strcpy(recv_buffer + recv_length, ""); 114 /* recv failed, bail out */
113 result = STATE_WARNING; 115 strcpy(recv_buffer + recv_length, "");
114 break; 116 result = STATE_WARNING;
115 } else if (recv_result == 0) { 117 break;
116 /* end of file ? */ 118 }
117 recv_buffer[recv_length] = 0; 119
118 break; 120 if (recv_result == 0) {
119 } else { /* we got data! */ 121 /* end of file ? */
120 recv_length += recv_result; 122 recv_buffer[recv_length] = 0;
121 if (recv_length >= recv_size - 1) { 123 break;
122 /* buffer full, we're done */ 124 }
123 recv_buffer[recv_size - 1] = 0; 125
124 break; 126 /* we got data! */
125 } 127 recv_length += recv_result;
126 } 128 if (recv_length >= recv_size - 1) {
129 /* buffer full, we're done */
130 recv_buffer[recv_size - 1] = 0;
131 break;
127 } 132 }
128 /* end if(!FD_ISSET(sd,&readfds)) */ 133 /* end if(!FD_ISSET(sd,&readfds)) */
129 } 134 }
130 /* end while(1) */
131 135
132 close(sd); 136 close(socket);
133 return result; 137 return result;
134} 138}
135 139
136/* connects to a host on a specified port, sends a string, and gets a 140/* connects to a host on a specified port, sends a string, and gets a
137 response */ 141 response */
138int process_request(const char *server_address, int server_port, int proto, const char *send_buffer, 142mp_state_enum process_request(const char *server_address, const int server_port, const int proto,
139 char *recv_buffer, int recv_size) { 143 const char *send_buffer, char *recv_buffer, const int recv_size) {
140 int result;
141 int sd;
142 144
143 result = STATE_OK; 145 mp_state_enum result = STATE_OK;
144 146 int socket;
145 result = np_net_connect(server_address, server_port, &sd, proto); 147 result = np_net_connect(server_address, server_port, &socket, proto);
146 if (result != STATE_OK) { 148 if (result != STATE_OK) {
147 return STATE_CRITICAL; 149 return STATE_CRITICAL;
148 } 150 }
149 151
150 result = send_request(sd, proto, send_buffer, recv_buffer, recv_size); 152 result = send_request(socket, proto, send_buffer, recv_buffer, recv_size);
151 153
152 close(sd); 154 close(socket);
153 155
154 return result; 156 return result;
155} 157}
156 158
157/* opens a tcp or udp connection to a remote host or local socket */ 159/* opens a tcp or udp connection to a remote host or local socket */
158int np_net_connect(const char *host_name, int port, int *sd, int proto) { 160mp_state_enum np_net_connect(const char *host_name, int port, int *socketDescriptor,
161 const int proto) {
159 /* send back STATE_UNKOWN if there's an error 162 /* send back STATE_UNKOWN if there's an error
160 send back STATE_OK if we connect 163 send back STATE_OK if we connect
161 send back STATE_CRITICAL if we can't connect. 164 send back STATE_CRITICAL if we can't connect.
162 Let upstream figure out what to send to the user. */ 165 Let upstream figure out what to send to the user. */
163 struct addrinfo hints; 166 bool is_socket = (host_name[0] == '/');
164 struct addrinfo *r, *res; 167 int socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
165 struct sockaddr_un su;
166 char port_str[6], host[MAX_HOST_ADDRESS_LENGTH];
167 size_t len;
168 int socktype, result;
169 short is_socket = (host_name[0] == '/');
170
171 socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
172 168
169 struct addrinfo hints = {};
170 struct addrinfo *res = NULL;
171 int result;
173 /* as long as it doesn't start with a '/', it's assumed a host or ip */ 172 /* as long as it doesn't start with a '/', it's assumed a host or ip */
174 if (!is_socket) { 173 if (!is_socket) {
175 memset(&hints, 0, sizeof(hints)); 174 memset(&hints, 0, sizeof(hints));
@@ -177,38 +176,46 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) {
177 hints.ai_protocol = proto; 176 hints.ai_protocol = proto;
178 hints.ai_socktype = socktype; 177 hints.ai_socktype = socktype;
179 178
180 len = strlen(host_name); 179 size_t len = strlen(host_name);
181 /* check for an [IPv6] address (and strip the brackets) */ 180 /* check for an [IPv6] address (and strip the brackets) */
182 if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') { 181 if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') {
183 host_name++; 182 host_name++;
184 len -= 2; 183 len -= 2;
185 } 184 }
185
186 char host[MAX_HOST_ADDRESS_LENGTH];
187
186 if (len >= sizeof(host)) { 188 if (len >= sizeof(host)) {
187 return STATE_UNKNOWN; 189 return STATE_UNKNOWN;
188 } 190 }
191
189 memcpy(host, host_name, len); 192 memcpy(host, host_name, len);
190 host[len] = '\0'; 193 host[len] = '\0';
194
195 char port_str[6];
191 snprintf(port_str, sizeof(port_str), "%d", port); 196 snprintf(port_str, sizeof(port_str), "%d", port);
192 result = getaddrinfo(host, port_str, &hints, &res); 197 int getaddrinfo_err = getaddrinfo(host, port_str, &hints, &res);
193 198
194 if (result != 0) { 199 if (getaddrinfo_err != 0) {
195 // printf("%s\n", gai_strerror(result)); 200 // printf("%s\n", gai_strerror(result));
196 return STATE_UNKNOWN; 201 return STATE_UNKNOWN;
197 } 202 }
198 203
199 r = res; 204 struct addrinfo *addressPointer = res;
200 while (r) { 205 while (addressPointer) {
201 /* attempt to create a socket */ 206 /* attempt to create a socket */
202 *sd = socket(r->ai_family, socktype, r->ai_protocol); 207 *socketDescriptor =
208 socket(addressPointer->ai_family, socktype, addressPointer->ai_protocol);
203 209
204 if (*sd < 0) { 210 if (*socketDescriptor < 0) {
205 // printf("%s\n", _("Socket creation failed")); 211 // printf("%s\n", _("Socket creation failed"));
206 freeaddrinfo(r); 212 freeaddrinfo(addressPointer);
207 return STATE_UNKNOWN; 213 return STATE_UNKNOWN;
208 } 214 }
209 215
210 /* attempt to open a connection */ 216 /* attempt to open a connection */
211 result = connect(*sd, r->ai_addr, r->ai_addrlen); 217 result =
218 connect(*socketDescriptor, addressPointer->ai_addr, addressPointer->ai_addrlen);
212 219
213 if (result == 0) { 220 if (result == 0) {
214 was_refused = false; 221 was_refused = false;
@@ -223,24 +230,28 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) {
223 } 230 }
224 } 231 }
225 232
226 close(*sd); 233 close(*socketDescriptor);
227 r = r->ai_next; 234 addressPointer = addressPointer->ai_next;
228 } 235 }
236
229 freeaddrinfo(res); 237 freeaddrinfo(res);
230 } 238
231 /* else the hostname is interpreted as a path to a unix socket */ 239 } else {
232 else { 240 /* else the hostname is interpreted as a path to a unix socket */
233 if (strlen(host_name) >= UNIX_PATH_MAX) { 241 if (strlen(host_name) >= UNIX_PATH_MAX) {
234 die(STATE_UNKNOWN, _("Supplied path too long unix domain socket")); 242 die(STATE_UNKNOWN, _("Supplied path too long unix domain socket"));
235 } 243 }
236 memset(&su, 0, sizeof(su)); 244
245 struct sockaddr_un su = {};
237 su.sun_family = AF_UNIX; 246 su.sun_family = AF_UNIX;
238 strncpy(su.sun_path, host_name, UNIX_PATH_MAX); 247 strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
239 *sd = socket(PF_UNIX, SOCK_STREAM, 0); 248 *socketDescriptor = socket(PF_UNIX, SOCK_STREAM, 0);
240 if (*sd < 0) { 249
250 if (*socketDescriptor < 0) {
241 die(STATE_UNKNOWN, _("Socket creation failed")); 251 die(STATE_UNKNOWN, _("Socket creation failed"));
242 } 252 }
243 result = connect(*sd, (struct sockaddr *)&su, sizeof(su)); 253
254 result = connect(*socketDescriptor, (struct sockaddr *)&su, sizeof(su));
244 if (result < 0 && errno == ECONNREFUSED) { 255 if (result < 0 && errno == ECONNREFUSED) {
245 was_refused = true; 256 was_refused = true;
246 } 257 }
@@ -248,7 +259,9 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) {
248 259
249 if (result == 0) { 260 if (result == 0) {
250 return STATE_OK; 261 return STATE_OK;
251 } else if (was_refused) { 262 }
263
264 if (was_refused) {
252 switch (econn_refuse_state) { /* a user-defined expected outcome */ 265 switch (econn_refuse_state) { /* a user-defined expected outcome */
253 case STATE_OK: 266 case STATE_OK:
254 case STATE_WARNING: /* user wants WARN or OK on refusal, or... */ 267 case STATE_WARNING: /* user wants WARN or OK on refusal, or... */
@@ -275,14 +288,11 @@ int np_net_connect(const char *host_name, int port, int *sd, int proto) {
275 } 288 }
276} 289}
277 290
278int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size) { 291mp_state_enum send_request(const int socket, const int proto, const char *send_buffer,
279 int result = STATE_OK; 292 char *recv_buffer, const int recv_size) {
280 int send_result; 293 mp_state_enum result = STATE_OK;
281 int recv_result;
282 struct timeval tv;
283 fd_set readfds;
284 294
285 send_result = send(sd, send_buffer, strlen(send_buffer), 0); 295 ssize_t send_result = send(socket, send_buffer, strlen(send_buffer), 0);
286 if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) { 296 if (send_result < 0 || (size_t)send_result != strlen(send_buffer)) {
287 // printf("%s\n", _("Send failed")); 297 // printf("%s\n", _("Send failed"));
288 result = STATE_WARNING; 298 result = STATE_WARNING;
@@ -290,21 +300,22 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer,
290 300
291 /* wait up to the number of seconds for socket timeout minus one 301 /* wait up to the number of seconds for socket timeout minus one
292 for data from the host */ 302 for data from the host */
293 tv.tv_sec = socket_timeout - 1; 303 struct timeval timestamp = {
294 tv.tv_usec = 0; 304 .tv_sec = socket_timeout - 1,
305 .tv_usec = 0,
306 };
307 fd_set readfds;
295 FD_ZERO(&readfds); 308 FD_ZERO(&readfds);
296 FD_SET(sd, &readfds); 309 FD_SET(socket, &readfds);
297 select(sd + 1, &readfds, NULL, NULL, &tv); 310 select(socket + 1, &readfds, NULL, NULL, &timestamp);
298 311
299 /* make sure some data has arrived */ 312 /* make sure some data has arrived */
300 if (!FD_ISSET(sd, &readfds)) { 313 if (!FD_ISSET(socket, &readfds)) {
301 strcpy(recv_buffer, ""); 314 strcpy(recv_buffer, "");
302 // printf("%s\n", _("No data was received from host!")); 315 // printf("%s\n", _("No data was received from host!"));
303 result = STATE_WARNING; 316 result = STATE_WARNING;
304 } 317 } else {
305 318 ssize_t recv_result = recv(socket, recv_buffer, (size_t)(recv_size - 1), 0);
306 else {
307 recv_result = recv(sd, recv_buffer, (size_t)recv_size - 1, 0);
308 if (recv_result == -1) { 319 if (recv_result == -1) {
309 strcpy(recv_buffer, ""); 320 strcpy(recv_buffer, "");
310 if (proto != IPPROTO_TCP) { 321 if (proto != IPPROTO_TCP) {
@@ -318,6 +329,7 @@ int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer,
318 /* die returned string */ 329 /* die returned string */
319 recv_buffer[recv_size - 1] = 0; 330 recv_buffer[recv_size - 1] = 0;
320 } 331 }
332
321 return result; 333 return result;
322} 334}
323 335
@@ -339,27 +351,27 @@ bool is_addr(const char *address) {
339#ifdef USE_IPV6 351#ifdef USE_IPV6
340 if (address_family == AF_INET && is_inet_addr(address)) { 352 if (address_family == AF_INET && is_inet_addr(address)) {
341 return true; 353 return true;
342 } else if (address_family == AF_INET6 && is_inet6_addr(address)) { 354 }
355
356 if (address_family == AF_INET6 && is_inet6_addr(address)) {
343 return true; 357 return true;
344 } 358 }
345#else 359#else
346 if (is_inet_addr(address)) { 360 if (is_inet_addr(address)) {
347 return (true); 361 return true;
348 } 362 }
349#endif 363#endif
350 364
351 return (false); 365 return false;
352} 366}
353 367
354int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) { 368bool dns_lookup(const char *node_string, struct sockaddr_storage *ss, const int family) {
355 struct addrinfo hints; 369 struct addrinfo hints;
356 struct addrinfo *res;
357 int retval;
358
359 memset(&hints, 0, sizeof(struct addrinfo)); 370 memset(&hints, 0, sizeof(struct addrinfo));
360 hints.ai_family = family; 371 hints.ai_family = family;
361 372
362 retval = getaddrinfo(in, NULL, &hints, &res); 373 struct addrinfo *res;
374 int retval = getaddrinfo(node_string, NULL, &hints, &res);
363 if (retval != 0) { 375 if (retval != 0) {
364 return false; 376 return false;
365 } 377 }
@@ -367,6 +379,8 @@ int dns_lookup(const char *in, struct sockaddr_storage *ss, int family) {
367 if (ss != NULL) { 379 if (ss != NULL) {
368 memcpy(ss, res->ai_addr, res->ai_addrlen); 380 memcpy(ss, res->ai_addr, res->ai_addrlen);
369 } 381 }
382
370 freeaddrinfo(res); 383 freeaddrinfo(res);
384
371 return true; 385 return true;
372} 386}
diff --git a/plugins/netutils.h b/plugins/netutils.h
index 6adb8e01..c4461113 100644
--- a/plugins/netutils.h
+++ b/plugins/netutils.h
@@ -31,7 +31,6 @@
31#ifndef _NETUTILS_H_ 31#ifndef _NETUTILS_H_
32#define _NETUTILS_H_ 32#define _NETUTILS_H_
33 33
34#include "common.h"
35#include "output.h" 34#include "output.h"
36#include "states.h" 35#include "states.h"
37#include "utils.h" 36#include "utils.h"
@@ -56,25 +55,26 @@
56 process_request(addr, port, IPPROTO_TCP, sbuf, rbuf, rsize) 55 process_request(addr, port, IPPROTO_TCP, sbuf, rbuf, rsize)
57#define process_udp_request(addr, port, sbuf, rbuf, rsize) \ 56#define process_udp_request(addr, port, sbuf, rbuf, rsize) \
58 process_request(addr, port, IPPROTO_UDP, sbuf, rbuf, rsize) 57 process_request(addr, port, IPPROTO_UDP, sbuf, rbuf, rsize)
59int process_tcp_request2(const char *address, int port, const char *sbuffer, char *rbuffer, 58mp_state_enum process_tcp_request2(const char *server_address, int server_port,
60 int rsize); 59 const char *send_buffer, char *recv_buffer, int recv_size);
61int process_request(const char *address, int port, int proto, const char *sbuffer, char *rbuffer, 60mp_state_enum process_request(const char *server_address, int server_port, int proto,
62 int rsize); 61 const char *send_buffer, char *recv_buffer, int recv_size);
63 62
64/* my_connect and wrapper macros */ 63/* my_connect and wrapper macros */
65#define my_tcp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_TCP) 64#define my_tcp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_TCP)
66#define my_udp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_UDP) 65#define my_udp_connect(addr, port, s) np_net_connect(addr, port, s, IPPROTO_UDP)
67int np_net_connect(const char *address, int port, int *sd, int proto); 66mp_state_enum np_net_connect(const char *host_name, int port, int *socketDescriptor, int proto);
68 67
69/* send_request and wrapper macros */ 68/* send_request and wrapper macros */
70#define send_tcp_request(s, sbuf, rbuf, rsize) send_request(s, IPPROTO_TCP, sbuf, rbuf, rsize) 69#define send_tcp_request(s, sbuf, rbuf, rsize) send_request(s, IPPROTO_TCP, sbuf, rbuf, rsize)
71#define send_udp_request(s, sbuf, rbuf, rsize) send_request(s, IPPROTO_UDP, sbuf, rbuf, rsize) 70#define send_udp_request(s, sbuf, rbuf, rsize) send_request(s, IPPROTO_UDP, sbuf, rbuf, rsize)
72int send_request(int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size); 71mp_state_enum send_request(int socket, int proto, const char *send_buffer, char *recv_buffer,
72 int recv_size);
73 73
74/* "is_*" wrapper macros and functions */ 74/* "is_*" wrapper macros and functions */
75bool is_host(const char *); 75bool is_host(const char *);
76bool is_addr(const char *); 76bool is_addr(const char *);
77int dns_lookup(const char *, struct sockaddr_storage *, int); 77bool dns_lookup(const char *, struct sockaddr_storage *, int);
78void host_or_die(const char *str); 78void host_or_die(const char *str);
79#define resolve_host_or_addr(addr, family) dns_lookup(addr, NULL, family) 79#define resolve_host_or_addr(addr, family) dns_lookup(addr, NULL, family)
80#define is_inet_addr(addr) resolve_host_or_addr(addr, AF_INET) 80#define is_inet_addr(addr) resolve_host_or_addr(addr, AF_INET)
@@ -86,8 +86,8 @@ void host_or_die(const char *str);
86#endif 86#endif
87 87
88extern unsigned int socket_timeout; 88extern unsigned int socket_timeout;
89extern unsigned int socket_timeout_state; 89extern mp_state_enum socket_timeout_state;
90extern int econn_refuse_state; 90extern mp_state_enum econn_refuse_state;
91extern bool was_refused; 91extern bool was_refused;
92extern int address_family; 92extern int address_family;
93 93
@@ -106,12 +106,12 @@ void socket_timeout_alarm_handler(int) __attribute__((noreturn));
106# define MP_TLSv1_1_OR_NEWER 9 106# define MP_TLSv1_1_OR_NEWER 9
107# define MP_TLSv1_2_OR_NEWER 10 107# define MP_TLSv1_2_OR_NEWER 10
108/* maybe this could be merged with the above np_net_connect, via some flags */ 108/* maybe this could be merged with the above np_net_connect, via some flags */
109int np_net_ssl_init(int sd); 109int np_net_ssl_init(int socket);
110int np_net_ssl_init_with_hostname(int sd, char *host_name); 110int np_net_ssl_init_with_hostname(int socket, char *host_name);
111int np_net_ssl_init_with_hostname_and_version(int sd, char *host_name, int version); 111int np_net_ssl_init_with_hostname_and_version(int socket, char *host_name, int version);
112int np_net_ssl_init_with_hostname_version_and_cert(int sd, char *host_name, int version, char *cert, 112int np_net_ssl_init_with_hostname_version_and_cert(int socket, char *host_name, int version,
113 char *privkey); 113 char *cert, char *privkey);
114void np_net_ssl_cleanup(); 114void np_net_ssl_cleanup(void);
115int np_net_ssl_write(const void *buf, int num); 115int np_net_ssl_write(const void *buf, int num);
116int np_net_ssl_read(void *buf, int num); 116int np_net_ssl_read(void *buf, int num);
117mp_state_enum np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit); 117mp_state_enum np_net_ssl_check_cert(int days_till_exp_warn, int days_till_exp_crit);