/* * Copyright (C), 2000-2003 by Contributors to the monit codebase. * All Rights Reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include "config.h" #include "net.h" #include "ssl.h" #include "monitor.h" #include "socket.h" /** * This class implements a Socket. A socket is an endpoint for * communication between two machines. * * @author Jan-Henrik Haukeland, * @version \$Id: socket.c,v 1.1 2003/06/17 02:04:57 hauk Exp $ * @file */ /* ------------------------------------------------------------- Definitions */ struct Socket_T { int fd; int port; char *host; int protocol; #ifdef HAVE_OPENSSL ssl_connection *ssl; #endif }; /* ------------------------------------------------------------------ Public */ /** * Create a new Socket opened against host:port. The returned Socket * is a connected socket. If the use_ssl parameter is TRUE the socket * is created using SSL. * @param host The remote host to open the Socket against. The host * may be a hostname found in the DNS or an IP address string. * @param port The port number to connect to * @param protocol The socket protocol to use (SOCK_STREAM or SOCK_DGRAM) * @param use_ssl if TRUE the socket is created supporting SSL * @return The connected Socket or NULL if an error occurred */ Socket_T socket_new(char *host, int port, int protocol, int use_ssl) { int s; ASSERT(host); if((s= create_socket(host, port, protocol)) != -1) { Socket_T S= NEW(S); S->fd= s; S->port= port; S->protocol= protocol; S->host= xstrdup(host); if(use_ssl) { if(! (S->ssl= new_ssl_connection(Run.httpsslpem, SSL_VERSION_AUTO))) { goto ssl_error; } if(!embed_ssl_socket(S->ssl, S->fd)) { goto ssl_error; } ssl_error: socket_close(&S); return NULL; } return S; } return NULL; } /** * Destroy a Socket object. Close the socket and release allocated * resources. * @param S A Socket object reference */ void socket_close(Socket_T *S) { ASSERT(S && *S); if((*S)->ssl) { close_ssl_socket((*S)->ssl); delete_ssl_socket((*S)->ssl); } close((*S)->fd); free((*S)->host); free(*S); (*S)= NULL; } /** * Write the message 'msg' to the socket * @param s A Socket_T object * @param msg The message buffer to send * @param len The length of the message in bytes * @return The bytes sent or -1 if an error occured */ int socket_write(Socket_T s, void *msg, int len) { ASSERT(s); if(s->ssl) return send_ssl_socket(s->ssl, msg, len); else return sock_send(s->fd, msg, len, 0); } /** * Read a message from the socket. * @param s A Socket_T object * @param buf The buffer to write the message to * @param len The length of the buffer in bytes * @return The bytes read or -1 if an error occured */ int socket_read(Socket_T s, void *buf, int len) { ASSERT(s); if(s->ssl) return recv_ssl_socket(s->ssl, buf, len); else return sock_recv(s->fd, buf, len, 0); }