~ubuntu-branches/ubuntu/lucid/monit/lucid

« back to all changes in this revision

Viewing changes to socket.c

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Alfredsson
  • Date: 2009-08-18 22:49:13 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20090818224913-6z5wghd0qfiuy7fh
Tags: 1:5.0.3-3
* Daemon poll interval moved to rc-file instead of defaults
  (Closes: #541425)
* Package upgraded (Closes: 453248)
* Configuration snippets are included from /etc/monit/conf.d/*
  (Closes: #296479)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (C), 2000-2007 by the monit project group.
3
 
 * All Rights Reserved.
 
2
 * Copyright (C) 2009 Tildeslash Ltd. All rights reserved.
4
3
 *
5
4
 * This program is free software: you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation, either version 3 of the License, or
8
 
 * (at your option) any later version.
 
5
 * it under the terms of the GNU General Public License version 3.
9
6
 *
10
7
 * This program is distributed in the hope that it will be useful,
11
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
11
 *
15
12
 * You should have received a copy of the GNU General Public License
16
13
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
14
 *
 
15
 * In addition, as a special exception, the copyright holders give
 
16
 * permission to link the code of portions of this program with the
 
17
 * OpenSSL library under certain conditions as described in each
 
18
 * individual source file, and distribute linked combinations
 
19
 * including the two.
 
20
 *
 
21
 * You must obey the GNU General Public License in all respects
 
22
 * for all of the code used other than OpenSSL.  If you modify
 
23
 * file(s) with this exception, you may extend this exception to your
 
24
 * version of the file(s), but you are not obligated to do so.  If you
 
25
 * do not wish to do so, delete this exception statement from your
 
26
 * version.  If you delete this exception statement from all source
 
27
 * files in the program, then also delete it here.
17
28
 */
18
29
 
19
30
#include "config.h"
38
49
#include <unistd.h>
39
50
#endif
40
51
 
 
52
#ifdef HAVE_ARPA_INET_H
 
53
#include <arpa/inet.h>
 
54
#endif
 
55
 
 
56
#ifdef HAVE_NETINET_IN_H
 
57
#include <netinet/in.h>
 
58
#endif
 
59
 
 
60
 
41
61
#include "net.h"
42
62
#include "ssl.h"
43
63
#include "monitor.h"
49
69
 * Implementation of the socket interface.
50
70
 * 
51
71
 * @author Jan-Henrik Haukeland, <hauk@tildeslash.com>
52
 
 * @version \$Id: socket.c,v 1.63 2007/10/02 13:58:49 hauk Exp $
 
72
 * @version \$Id: socket.c,v 1.72 2009/02/13 09:18:10 hauk Exp $
53
73
 * @file
54
74
 */
55
75
 
85
105
/* ------------------------------------------------------------------ Public */
86
106
 
87
107
 
88
 
/**
89
 
 * Create a new Socket opened against host:port. The returned Socket
90
 
 * is a connected socket. This method can be used to create either TCP
91
 
 * or UDP sockets and the type parameter is used to select the socket
92
 
 * type. If the use_ssl parameter is TRUE the socket is created using
93
 
 * SSL. Only TCP sockets may use SSL.
94
 
 * @param host The remote host to open the Socket against. The host
95
 
 * may be a hostname found in the DNS or an IP address string.
96
 
 * @param port The port number to connect to
97
 
 * @param type The socket type to use (SOCKET_TCP or SOCKET_UPD)
98
 
 * @param use_ssl If TRUE the socket is created supporting SSL
99
 
 * @param timeout The timeout value in seconds
100
 
 * @return The connected Socket or NULL if an error occurred
101
 
 */
102
108
Socket_T socket_new(const char *host, int port, int type, int use_ssl,
103
109
                    int timeout) {
104
110
  
113
119
}
114
120
 
115
121
 
116
 
/**
117
 
 * Factory method for creating a new Socket from a monit Port object
118
 
 * @param port The port object to create a socket from
119
 
 * @return The connected Socket or NULL if an error occurred
120
 
 */
121
122
Socket_T socket_create(void *port) {
122
123
  
123
124
  int s;
157
158
}
158
159
 
159
160
 
160
 
/**
161
 
 * Create a new Socket opened against host:port with an explicit
162
 
 * ssl value for connect and read. Otherwise, same as socket_new()
163
 
 * @param host The remote host to open the Socket against. The host
164
 
 * may be a hostname found in the DNS or an IP address string.
165
 
 * @param port The port number to connect to
166
 
 * @param type The socket type to use (SOCKET_TCP or SOCKET_UPD)
167
 
 * @param ssl Options for SSL
168
 
 * @param timeout The timeout value in seconds
169
 
 * @return The connected Socket or NULL if an error occurred
170
 
 */
171
161
Socket_T socket_create_t(const char *host, int port, int type, Ssl_T ssl,
172
162
                         int timeout) {
173
163
  
207
197
}
208
198
 
209
199
 
210
 
/**
211
 
 * Factory method for creating a Socket object from an accepted
212
 
 * socket. The given socket must be a socket created from accept(2).
213
 
 * If the sslserver context is non-null the socket will support
214
 
 * ssl. This method does only support TCP sockets.
215
 
 * @param socket The accepted socket
216
 
 * @param remote_host The remote host from where the socket connection
217
 
 * originated
218
 
 * @param port The localhost port number from where the connection
219
 
 * arrived.
220
 
 * @param sslserver A ssl server connection context, may be NULL
221
 
 * @return A Socket or NULL if an error occurred
222
 
 */
223
200
Socket_T socket_create_a(int socket, const char *remote_host,
224
201
                         int port, void *sslserver) {
225
202
  
257
234
}
258
235
 
259
236
 
260
 
/**
261
 
 * Destroy a Socket object. Close the socket and release allocated
262
 
 * resources. 
263
 
 * @param S A Socket object reference
264
 
 */
265
237
void socket_free(Socket_T *S) {
266
238
  
267
239
  ASSERT(S && *S);
287
259
/* ------------------------------------------------------------ Properties */
288
260
 
289
261
 
290
 
/**
291
 
 * Returns TRUE if the socket is ready for i|o
292
 
 * @param S A Socket object
293
 
 * @return TRUE if the socket is ready otherwise FALSE 
294
 
 */
295
262
int socket_is_ready(Socket_T S) {
296
263
  
297
264
  ASSERT(S);
313
280
}
314
281
 
315
282
 
316
 
/**
317
 
 * Get the underlying socket descriptor
318
 
 * @param S A Socket object
319
 
 * @return The socket descriptor
320
 
 */
 
283
int socket_is_secure(Socket_T S) {
 
284
  
 
285
  ASSERT(S);
 
286
  
 
287
  return (S->ssl != NULL);
 
288
  
 
289
}
 
290
 
 
291
 
321
292
int socket_get_socket(Socket_T S) {
322
293
  
323
294
  ASSERT(S);
327
298
}
328
299
 
329
300
 
330
 
/**
331
 
 * Get the type of this socket.
332
 
 * @param S A Socket object
333
 
 * @return The socket type
334
 
 */
335
301
int socket_get_type(Socket_T S) {
336
302
  
337
303
  ASSERT(S);
341
307
}
342
308
 
343
309
 
344
 
/**
345
 
 * Get the Port object used to create this socket. If no Port object
346
 
 * was used this method returns NULL.
347
 
 * @param S A Socket object
348
 
 * @return The Port object or NULL
349
 
 */
350
310
void *socket_get_Port(Socket_T S) {
351
311
  
352
312
  ASSERT(S);
356
316
}
357
317
 
358
318
 
359
 
/**
360
 
 * Get the remote port number the socket is connected to
361
 
 * @param S A Socket object
362
 
 * @return The remote host's port number
363
 
 */
364
319
int socket_get_remote_port(Socket_T S) {
365
320
  
366
321
  ASSERT(S);
370
325
}
371
326
 
372
327
 
373
 
/**
374
 
 * Get the remote host this socket is connected to. The host is either
375
 
 * a host name in DNS or an IP address string.
376
 
 * @param S A Socket object
377
 
 * @return The remote host
378
 
 */
379
328
const char *socket_get_remote_host(Socket_T S) {
380
329
  
381
330
  ASSERT(S);
385
334
}
386
335
 
387
336
 
 
337
int socket_get_local_port(Socket_T S) {
 
338
  struct sockaddr sock;
 
339
  socklen_t len = sizeof(sock);
 
340
 
 
341
  ASSERT(S);
 
342
 
 
343
  if(getsockname (S->socket, &sock, &len ) == 0)
 
344
    return ntohs (((struct sockaddr_in *)&sock)->sin_port);
 
345
  return -1;
 
346
  
 
347
}
 
348
 
 
349
 
 
350
const char *socket_get_local_host(Socket_T S) {
 
351
  struct sockaddr sock;
 
352
  socklen_t len = sizeof(sock);
 
353
 
 
354
  ASSERT(S);
 
355
 
 
356
  if(getsockname(S->socket, &sock, &len) == 0) 
 
357
    return inet_ntoa(((struct sockaddr_in *)&sock)->sin_addr);
 
358
  return NULL;
 
359
  
 
360
}
 
361
 
 
362
 
388
363
/* ---------------------------------------------------------------- Public */
389
364
 
390
365
 
391
 
/**
392
 
 * Switches a connected socket to ssl.
393
 
 * @param S The already connected socket
394
 
 * @param ssl Options for ssl
395
 
 * @return TRUE if ssl is ready otherwise FALSE
396
 
 */
397
366
int socket_switch2ssl(Socket_T S, Ssl_T ssl)  {
398
367
 
399
368
  if(! (S->ssl= new_ssl_connection(NULL, ssl.version)))
406
375
    LogError("md5sum of certificate does not match!");
407
376
    return FALSE;
408
377
  }
409
 
 
 
378
  
410
379
  return TRUE;
411
380
}
412
381
 
413
382
 
414
 
/**
415
 
 * Writes a character string. Use this function to send text based
416
 
 * messages to a client.
417
 
 * @param S A Socket_T object
418
 
 * @param m A String to send to the client
419
 
 * @return The bytes sent or -1 if an error occured
420
 
 */
421
383
int socket_print(Socket_T S, const char *m, ...) {
422
384
  
423
385
  int n;
440
402
}
441
403
 
442
404
 
443
 
/**
444
 
 * Write size bytes from the buffer b.
445
 
 * @param S A Socket_T object
446
 
 * @param b The data to be written
447
 
 * @param size The size of the data in b
448
 
 * @return The bytes sent or -1 if an error occured
449
 
 */
450
405
int socket_write(Socket_T S, void *b, int size) {
451
406
  
452
407
  int n= 0;
484
439
}
485
440
 
486
441
 
487
 
/**
488
 
 * Read a single byte. The byte is returned as an int in the range 0
489
 
 * to 255.
490
 
 * @param S A Socket_T object
491
 
 * @return The byte read, or -1 if the end of the stream has been reached
492
 
 */
493
442
int socket_read_byte(Socket_T S) {
494
443
  
495
444
  ASSERT(S);
504
453
}
505
454
 
506
455
 
507
 
/**
508
 
 * Reads size bytes and stores them into the byte buffer pointed to by b.
509
 
 * @param S A Socket_T object
510
 
 * @param b A Byte buffer
511
 
 * @param size The size of the buffer b
512
 
 * @return The bytes read or -1 if an error occured
513
 
 */
514
456
int socket_read(Socket_T S, void *b, int size) {
515
457
  
516
458
  int c;
527
469
}
528
470
 
529
471
 
530
 
/**
531
 
 * Reads in at most one less than size <code>characters</code> and
532
 
 * stores them into the buffer pointed to by s. Reading stops after
533
 
 * an EOF or a newline.  If a newline is read, it is stored into the
534
 
 * buffer.   A '\0' is stored after the last character in the buffer.
535
 
 * @param S A Socket_T object
536
 
 * @param s A character buffer to store the string in
537
 
 * @param size The size of the string buffer, s
538
 
 * @return s on success, and NULL on error or when end of file occurs
539
 
 * while no characters have been read.
540
 
 */
541
472
char *socket_readln(Socket_T S, char *s, int size) {
542
473
  
543
474
  int c;
561
492
}
562
493
 
563
494
 
564
 
/**
565
 
 * Clears any data that exists in the input buffer
566
 
 * @param S A Socket_T object
567
 
 */
568
495
void socket_reset(Socket_T S) {
569
496
 
570
497
  ASSERT(S);
577
504
}
578
505
 
579
506
 
 
507
int socket_shutdown_write(Socket_T S) {
 
508
        ASSERT(S);
 
509
        return (shutdown(S->socket, 1) == 0);
 
510
}
 
511
 
 
512
 
580
513
/* --------------------------------------------------------------- Private */
581
514
 
582
515