diff options
-rw-r--r-- | camel/ChangeLog | 12 | ||||
-rw-r--r-- | camel/camel-stream-ssl.c | 28 | ||||
-rw-r--r-- | camel/camel-tcp-stream-raw.c | 196 | ||||
-rw-r--r-- | camel/camel-tcp-stream-raw.h | 64 | ||||
-rw-r--r-- | camel/camel-tcp-stream-ssl.c | 205 | ||||
-rw-r--r-- | camel/camel-tcp-stream-ssl.h | 66 |
6 files changed, 557 insertions, 14 deletions
diff --git a/camel/ChangeLog b/camel/ChangeLog index 95e981644d..3c82004792 100644 --- a/camel/ChangeLog +++ b/camel/ChangeLog @@ -1,3 +1,15 @@ +2001-01-14 Jeffrey Stedfast <fejj@helixcode.com> + + * camel-tcp-stream-ssl.[c,h]: New CamelTcpStream class that + implements nspr sockets and eventually will use nss for + SSL/TLS. Currently doesn't do any SSL/TLS but it should still + work. It's functionally equivalent to CamelTcpStreamRaw at the + moment only it uses nspr i/o. + + * camel-tcp-stream-raw.[c,h]: New CamelTcpStream class that + implements native sockets. Should be usable but may have some bugs + yet. + 2001-01-13 Jeffrey Stedfast <fejj@ximian.com> * camel-tcp-stream.[c,h]: New abstract class for TCP streams. The diff --git a/camel/camel-stream-ssl.c b/camel/camel-stream-ssl.c index e91d6f60db..9e328e47fe 100644 --- a/camel/camel-stream-ssl.c +++ b/camel/camel-stream-ssl.c @@ -49,7 +49,7 @@ camel_stream_ssl_class_init (CamelStreamSSLClass *camel_stream_ssl_class) CamelStreamClass *camel_stream_class = CAMEL_STREAM_CLASS (camel_stream_ssl_class); - parent_class = CAMEL_STREAM_CLASS (camel_type_get_global_classfuncs (camel_seekable_stream_get_type ())); + parent_class = CAMEL_STREAM_CLASS (camel_type_get_global_classfuncs (camel_stream_get_type ())); /* virtual method overload */ camel_stream_class->read = stream_read; @@ -70,7 +70,7 @@ camel_stream_ssl_init (gpointer object, gpointer klass) static void camel_stream_ssl_finalize (CamelObject *object) { - CamelStreamSSL *stream = CAMEL_STREAM_FS (object); + CamelStreamSSL *stream = CAMEL_STREAM_SSL (object); if (stream->ssl) { SSL_shutdown (stream->ssl); @@ -89,20 +89,20 @@ camel_stream_ssl_finalize (CamelObject *object) CamelType camel_stream_ssl_get_type (void) { - static CamelType camel_stream_ssl_type = CAMEL_INVALID_TYPE; - - if (camel_stream_ssl_type == CAMEL_INVALID_TYPE) { - camel_stream_ssl_type = - camel_type_register (camel_stream_get_type (), "CamelStreamSSL", - sizeof (CamelStreamSSL), - sizeof (CamelStreamSSLClass), - (CamelObjectClassInitFunc) camel_stream_ssl_class_init, - NULL, - (CamelObjectInitFunc) camel_stream_ssl_init, - (CamelObjectFinalizeFunc) camel_stream_ssl_finalize); + static CamelType type = CAMEL_INVALID_TYPE; + + if (type == CAMEL_INVALID_TYPE) { + type = camel_type_register (camel_stream_get_type (), + "CamelStreamSSL", + sizeof (CamelStreamSSL), + sizeof (CamelStreamSSLClass), + (CamelObjectClassInitFunc) camel_stream_ssl_class_init, + NULL, + (CamelObjectInitFunc) camel_stream_ssl_init, + (CamelObjectFinalizeFunc) camel_stream_ssl_finalize); } - return camel_stream_ssl_type; + return type; } static int diff --git a/camel/camel-tcp-stream-raw.c b/camel/camel-tcp-stream-raw.c new file mode 100644 index 0000000000..4a547a2f6f --- /dev/null +++ b/camel/camel-tcp-stream-raw.c @@ -0,0 +1,196 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2001 Ximian, Inc. (www.ximian.com) + * + * 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 Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#include <config.h> +#include "camel-tcp-stream-raw.h" +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> + +static CamelTcpStreamClass *parent_class = NULL; + +/* Returns the class for a CamelTcpStreamRaw */ +#define CTSR_CLASS(so) CAMEL_TCP_STREAM_RAW_CLASS (CAMEL_OBJECT_GET_CLASS (so)) + +static ssize_t stream_read (CamelStream *stream, char *buffer, size_t n); +static ssize_t stream_write (CamelStream *stream, const char *buffer, size_t n); +static int stream_flush (CamelStream *stream); +static int stream_close (CamelStream *stream); + +static int stream_connect (CamelTcpStream *stream, struct hostent *host, int port); +static int stream_disconnect (CamelTcpStream *stream); + +static void +camel_tcp_stream_raw_class_init (CamelTcpStreamRawClass *camel_tcp_stream_raw_class) +{ + CamelTcpStreamClass *camel_tcp_stream_class = + CAMEL_TCP_STREAM_CLASS (camel_tcp_stream_raw_class); + CamelStreamClass *camel_stream_class = + CAMEL_STREAM_CLASS (camel_tcp_stream_raw_class); + + parent_class = CAMEL_STREAM_CLASS (camel_type_get_global_classfuncs (camel_tcp_stream_get_type ())); + + /* virtual method overload */ + camel_stream_class->read = stream_read; + camel_stream_class->write = stream_write; + camel_stream_class->flush = stream_flush; + camel_stream_class->close = stream_close; + + camel_tcp_stream_class->connect = stream_connect; + camel_tcp_stream_class->disconnect = stream_disconnect; +} + +static void +camel_tcp_stream_raw_init (gpointer object, gpointer klass) +{ + CamelTcpStreamRaw *stream = CAMEL_TCP_STREAM_RAW (object); + + stream->sockfd = -1; +} + +static void +camel_tcp_stream_raw_finalize (CamelObject *object) +{ + CamelTcpStreamRaw *stream = CAMEL_TCP_STREAM_RAW (object); + + if (stream->sockfd != -1) + close (stream->sockfd); +} + + +CamelType +camel_tcp_stream_raw_get_type (void) +{ + static CamelType type = CAMEL_INVALID_TYPE; + + if (type == CAMEL_INVALID_TYPE) { + type = camel_type_register (camel_stream_get_type (), + "CamelTcpStreamRaw", + sizeof (CamelTcpStreamRaw), + sizeof (CamelTcpStreamRawClass), + (CamelObjectClassInitFunc) camel_tcp_stream_raw_class_init, + NULL, + (CamelObjectInitFunc) camel_tcp_stream_raw_init, + (CamelObjectFinalizeFunc) camel_tcp_stream_raw_finalize); + } + + return type; +} + + +/** + * camel_tcp_stream_raw_new: + * + * Return value: a tcp stream + **/ +CamelStream * +camel_tcp_stream_raw_new () +{ + CamelTcpStreamRaw *stream; + + stream = CAMEL_TCP_STREAM_RAW (camel_object_new (camel_tcp_stream_raw_get_type ())); + + return CAMEL_STREAM (stream); +} + +static ssize_t +stream_read (CamelStream *stream, char *buffer, size_t n) +{ + CamelTcpStreamRaw *tcp_stream_raw = CAMEL_TCP_STREAM_RAW (stream); + ssize_t nread; + + do { + nread = read (tcp_stream_raw->sockfd, buffer, n); + } while (nread == -1 && errno == EINTR); + + return nread; +} + +static ssize_t +stream_write (CamelStream *stream, const char *buffer, size_t n) +{ + CamelTcpStreamRaw *tcp_stream_raw = CAMEL_TCP_STREAM_RAW (stream); + ssize_t v, written = 0; + + do { + v = write (tcp_stream_raw->sockfd, buffer, n); + if (v > 0) + written += v; + } while (v == -1 && errno == EINTR); + + if (v == -1) + return -1; + else + return written; +} + +static int +stream_flush (CamelStream *stream) +{ + return fsync (((CamelTcpStreamRaw *)stream)->sockfd); +} + +static int +stream_close (CamelStream *stream) +{ + g_warning ("CamelTcpStreamRaw::close: Better to call ::disconnect.\n"); + return close (((CamelTcpStreamRaw *)stream)->sockfd); +} + + +static int +stream_connect (CamelTcpStream *stream, struct hostent *host, int port) +{ + CamelTcpStreamRaw *raw = CAMEL_TCP_STREAM_RAW (stream); + struct sockaddr_in sin; + int fd; + + g_return_val_if_fail (host != NULL, -1); + + sin.sin_family = host->h_addrtype; + sin.sin_port = htons (port); + + memcpy (&sin.sin_addr, host->h_addr, sizeof (sin.sin_addr)); + + fd = socket (host->h_addrtype, SOCK_STREAM, 0); + + if (fd == -1 || connect (fd, (struct sockaddr *)&sin, sizeof (sin)) == -1) { + if (fd > -1) + close (fd); + + return -1; + } + + raw->sockfd = fd; + + return 0; +} + +static int +stream_disconnect (CamelTcpStream *stream) +{ + return close (((CamelTcpStreamRaw *)stream)->sockfd); +} diff --git a/camel/camel-tcp-stream-raw.h b/camel/camel-tcp-stream-raw.h new file mode 100644 index 0000000000..41304efde7 --- /dev/null +++ b/camel/camel-tcp-stream-raw.h @@ -0,0 +1,64 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2001 Ximian, Inc. (www.ximian.com) + * + * 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 Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#ifndef CAMEL_TCP_STREAM_RAW_H +#define CAMEL_TCP_STREAM_RAW_H + + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus */ + +#include <camel/camel-tcp-stream.h> + +#define CAMEL_TCP_STREAM_RAW_TYPE (camel_tcp_stream_raw_get_type ()) +#define CAMEL_TCP_STREAM_RAW(obj) (CAMEL_CHECK_CAST((obj), CAMEL_TCP_STREAM_RAW_TYPE, CamelTcpStreamRaw)) +#define CAMEL_TCP_STREAM_RAW_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), CAMEL_TCP_STREAM_RAW_TYPE, CamelTcpStreamRawClass)) +#define CAMEL_IS_TCP_STREAM_RAW(o) (CAMEL_CHECK_TYPE((o), CAMEL_TCP_STREAM_RAW_TYPE)) + +struct _CamelTcpStreamRaw +{ + CamelTcpStream parent_object; + + int sockfd; +}; + +typedef struct { + CamelTcpStreamClass parent_class; + + /* virtual functions */ + +} CamelTcpStreamRawClass; + +/* Standard Camel function */ +CamelType camel_tcp_stream_raw_get_type (void); + +/* public methods */ +CamelStream *camel_tcp_stream_raw_new (void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CAMEL_TCP_STREAM_RAW_H */ diff --git a/camel/camel-tcp-stream-ssl.c b/camel/camel-tcp-stream-ssl.c new file mode 100644 index 0000000000..e12d32251d --- /dev/null +++ b/camel/camel-tcp-stream-ssl.c @@ -0,0 +1,205 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2001 Ximian, Inc. (www.ximian.com) + * + * 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 Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#include <config.h> +#include "camel-tcp-stream-raw.h" +#include <unistd.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <errno.h> +#include <string.h> + +static CamelTcpStreamClass *parent_class = NULL; + +/* Returns the class for a CamelTcpStreamSSL */ +#define CTSS_CLASS(so) CAMEL_TCP_STREAM_SSL_CLASS (CAMEL_OBJECT_GET_CLASS (so)) + +static ssize_t stream_read (CamelStream *stream, char *buffer, size_t n); +static ssize_t stream_write (CamelStream *stream, const char *buffer, size_t n); +static int stream_flush (CamelStream *stream); +static int stream_close (CamelStream *stream); + +static int stream_connect (CamelTcpStream *stream, struct hostent *host, int port); +static int stream_disconnect (CamelTcpStream *stream); + +static void +camel_tcp_stream_ssl_class_init (CamelTcpStreamSSLClass *camel_tcp_stream_ssl_class) +{ + CamelTcpStreamClass *camel_tcp_stream_class = + CAMEL_TCP_STREAM_CLASS (camel_tcp_stream_ssl_class); + CamelStreamClass *camel_stream_class = + CAMEL_STREAM_CLASS (camel_tcp_stream_ssl_class); + + parent_class = CAMEL_STREAM_CLASS (camel_type_get_global_classfuncs (camel_tcp_stream_get_type ())); + + /* virtual method overload */ + camel_stream_class->read = stream_read; + camel_stream_class->write = stream_write; + camel_stream_class->flush = stream_flush; + camel_stream_class->close = stream_close; + + camel_tcp_stream_class->connect = stream_connect; + camel_tcp_stream_class->disconnect = stream_disconnect; +} + +static void +camel_tcp_stream_ssl_init (gpointer object, gpointer klass) +{ + CamelTcpStreamSSL *stream = CAMEL_TCP_STREAM_SSL (object); + + stream->sockfd = NULL; +} + +static void +camel_tcp_stream_ssl_finalize (CamelObject *object) +{ + CamelTcpStreamSSL *stream = CAMEL_TCP_STREAM_SSL (object); + + if (stream->sockfd != NULL) + PR_Close (stream->sockfd); +} + + +CamelType +camel_tcp_stream_ssl_get_type (void) +{ + static CamelType type = CAMEL_INVALID_TYPE; + + if (type == CAMEL_INVALID_TYPE) { + type = camel_type_register (camel_stream_get_type (), + "CamelTcpStreamSSL", + sizeof (CamelTcpStreamSSL), + sizeof (CamelTcpStreamSSLClass), + (CamelObjectClassInitFunc) camel_tcp_stream_ssl_class_init, + NULL, + (CamelObjectInitFunc) camel_tcp_stream_ssl_init, + (CamelObjectFinalizeFunc) camel_tcp_stream_ssl_finalize); + } + + return type; +} + + +/** + * camel_tcp_stream_ssl_new: + * + * Return value: a tcp stream + **/ +CamelStream * +camel_tcp_stream_ssl_new () +{ + CamelTcpStreamSSL *stream; + + stream = CAMEL_TCP_STREAM_SSL (camel_object_new (camel_tcp_stream_ssl_get_type ())); + + return CAMEL_STREAM (stream); +} + +static ssize_t +stream_read (CamelStream *stream, char *buffer, size_t n) +{ + CamelTcpStreamSSL *tcp_stream_ssl = CAMEL_TCP_STREAM_SSL (stream); + ssize_t nread; + + do { + nread = PR_Read (tcp_stream_ssl->sockfd, buffer, n); + } while (nread == -1 && errno == EINTR); + + return nread; +} + +static ssize_t +stream_write (CamelStream *stream, const char *buffer, size_t n) +{ + CamelTcpStreamSSL *tcp_stream_ssl = CAMEL_TCP_STREAM_SSL (stream); + ssize_t v, written = 0; + + do { + v = PR_Write (tcp_stream_ssl->sockfd, buffer, n); + if (v > 0) + written += v; + } while (v == -1 && errno == EINTR); + + if (v == -1) + return -1; + else + return written; +} + +static int +stream_flush (CamelStream *stream) +{ + return PR_Fsync (((CamelTcpStreamSSL *)stream)->sockfd); +} + +static int +stream_close (CamelStream *stream) +{ + g_warning ("CamelTcpStreamSSL::close: Better to call ::disconnect.\n"); + return PR_Close (((CamelTcpStreamSSL *)stream)->sockfd); +} + + +static int +stream_connect (CamelTcpStream *stream, struct hostent *host, int port) +{ + CamelTcpStreamSSL *ssl = CAMEL_TCP_STREAM_SSL (stream); + PRIntervalTime timeout; + PRNetAddr netaddr; + PRFileDesc *fd; + + g_return_val_if_fail (host != NULL, -1); + + memset ((void *) &netaddr, 0, sizeof (PRNetAddr)); + memcpy (&netaddr.inet.ip, host->h_addr, sizeof (netaddr.inet.ip)); + + if (PR_InitializeNetAddr (PR_IpAddrNull, port, &netaddr) == PR_FAILUE) + return -1; + + fd = PR_OpenTCPSocket (host->h_addrtype); + + if (fd == NULL || PR_Connect (fd, &netaddr, timeout) == PR_FAILURE) { + if (fd != NULL) + PR_Close (fd); + + return -1; + } + + ssl->sockfd = fd; + + return 0; +} + +static int +stream_disconnect (CamelTcpStream *stream) +{ + PRStatus status; + + status = PR_Shutdown (((CamelTcpStreamSSL *)stream)->sockfd, PR_SHUTDOWN_BOTH); + + if (status == PR_FAILURE) + return -1; + + return PR_Close (((CamelTcpStreamSSL *)stream)->sockfd); +} diff --git a/camel/camel-tcp-stream-ssl.h b/camel/camel-tcp-stream-ssl.h new file mode 100644 index 0000000000..2e7ad343a5 --- /dev/null +++ b/camel/camel-tcp-stream-ssl.h @@ -0,0 +1,66 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2001 Ximian, Inc. (www.ximian.com) + * + * 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 Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#ifndef CAMEL_TCP_STREAM_SSL_H +#define CAMEL_TCP_STREAM_SSL_H + + +#ifdef __cplusplus +extern "C" { +#pragma } +#endif /* __cplusplus */ + +#include <camel/camel-tcp-stream.h> +#include <mozilla/nsprpub/prio.h> +#include <mozilla/nsprpub/prnetdb.h> + +#define CAMEL_TCP_STREAM_SSL_TYPE (camel_tcp_stream_ssl_get_type ()) +#define CAMEL_TCP_STREAM_SSL(obj) (CAMEL_CHECK_CAST((obj), CAMEL_TCP_STREAM_SSL_TYPE, CamelTcpStreamSSL)) +#define CAMEL_TCP_STREAM_SSL_CLASS(k) (CAMEL_CHECK_CLASS_CAST ((k), CAMEL_TCP_STREAM_SSL_TYPE, CamelTcpStreamSSLClass)) +#define CAMEL_IS_TCP_STREAM_SSL(o) (CAMEL_CHECK_TYPE((o), CAMEL_TCP_STREAM_SSL_TYPE)) + +struct _CamelTcpStreamSSL +{ + CamelTcpStream parent_object; + + PRFileDesc *sockfd; +}; + +typedef struct { + CamelTcpStreamClass parent_class; + + /* virtual functions */ + +} CamelTcpStreamSSLClass; + +/* Standard Camel function */ +CamelType camel_tcp_stream_ssl_get_type (void); + +/* public methods */ +CamelStream *camel_tcp_stream_ssl_new (void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CAMEL_TCP_STREAM_SSL_H */ |