diff options
Diffstat (limited to 'libsylph/socket.h')
| -rw-r--r-- | libsylph/socket.h | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/libsylph/socket.h b/libsylph/socket.h new file mode 100644 index 0000000..15bcef7 --- /dev/null +++ b/libsylph/socket.h | |||
| @@ -0,0 +1,163 @@ | |||
| 1 | /* | ||
| 2 | * LibSylph -- E-Mail client library | ||
| 3 | * Copyright (C) 1999-2012 Hiroyuki Yamamoto | ||
| 4 | * | ||
| 5 | * This library is free software; you can redistribute it and/or | ||
| 6 | * modify it under the terms of the GNU Lesser General Public | ||
| 7 | * License as published by the Free Software Foundation; either | ||
| 8 | * version 2.1 of the License, or (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This library is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | * Lesser General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU Lesser General Public | ||
| 16 | * License along with this library; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifndef __SOCKET_H__ | ||
| 21 | #define __SOCKET_H__ | ||
| 22 | |||
| 23 | #ifdef HAVE_CONFIG_H | ||
| 24 | # include "config.h" | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #include <glib.h> | ||
| 28 | #if HAVE_NETDB_H | ||
| 29 | # include <netdb.h> | ||
| 30 | #endif | ||
| 31 | #ifdef G_OS_WIN32 | ||
| 32 | # include <winsock2.h> | ||
| 33 | #endif | ||
| 34 | |||
| 35 | typedef struct _SockInfo SockInfo; | ||
| 36 | |||
| 37 | #if USE_SSL | ||
| 38 | # include "ssl.h" | ||
| 39 | #endif | ||
| 40 | |||
| 41 | typedef enum | ||
| 42 | { | ||
| 43 | CONN_READY, | ||
| 44 | CONN_LOOKUPSUCCESS, | ||
| 45 | CONN_ESTABLISHED, | ||
| 46 | CONN_LOOKUPFAILED, | ||
| 47 | CONN_FAILED | ||
| 48 | } ConnectionState; | ||
| 49 | |||
| 50 | typedef enum | ||
| 51 | { | ||
| 52 | SYL_SOCK_NONBLOCK = 1 << 0, | ||
| 53 | SYL_SOCK_CHECK_IO = 1 << 1 | ||
| 54 | } SockFlags; | ||
| 55 | |||
| 56 | #define SOCK_SET_FLAGS(flags, set) { (flags) |= (set); } | ||
| 57 | #define SOCK_UNSET_FLAGS(flags, set) { (flags) &= ~(set); } | ||
| 58 | #define SOCK_IS_NONBLOCK(flags) ((flags & SYL_SOCK_NONBLOCK) != 0) | ||
| 59 | #define SOCK_IS_CHECK_IO(flags) ((flags & SYL_SOCK_CHECK_IO) != 0) | ||
| 60 | |||
| 61 | typedef gint (*SockConnectFunc) (SockInfo *sock, | ||
| 62 | gpointer data); | ||
| 63 | typedef gboolean (*SockFunc) (SockInfo *sock, | ||
| 64 | GIOCondition condition, | ||
| 65 | gpointer data); | ||
| 66 | |||
| 67 | struct _SockInfo | ||
| 68 | { | ||
| 69 | gint sock; | ||
| 70 | #if USE_SSL | ||
| 71 | SSL *ssl; | ||
| 72 | #else | ||
| 73 | gpointer ssl; | ||
| 74 | #endif | ||
| 75 | GIOChannel *sock_ch; | ||
| 76 | |||
| 77 | gchar *hostname; | ||
| 78 | gushort port; | ||
| 79 | ConnectionState state; | ||
| 80 | SockFlags flags; | ||
| 81 | gpointer data; | ||
| 82 | |||
| 83 | SockFunc callback; | ||
| 84 | GIOCondition condition; | ||
| 85 | }; | ||
| 86 | |||
| 87 | gint sock_init (void); | ||
| 88 | gint sock_cleanup (void); | ||
| 89 | |||
| 90 | gint sock_set_io_timeout (guint sec); | ||
| 91 | |||
| 92 | SockInfo *sock_new (const gchar *hostname, gushort port); | ||
| 93 | |||
| 94 | gint sock_set_nonblocking_mode (SockInfo *sock, gboolean nonblock); | ||
| 95 | gboolean sock_is_nonblocking_mode (SockInfo *sock); | ||
| 96 | |||
| 97 | gboolean sock_has_read_data (SockInfo *sock); | ||
| 98 | |||
| 99 | guint sock_add_watch (SockInfo *sock, GIOCondition condition, | ||
| 100 | SockFunc func, gpointer data); | ||
| 101 | guint sock_add_watch_poll (SockInfo *sock, GIOCondition condition, | ||
| 102 | SockFunc func, gpointer data); | ||
| 103 | |||
| 104 | struct hostent *my_gethostbyname (const gchar *hostname); | ||
| 105 | |||
| 106 | SockInfo *sock_connect (const gchar *hostname, gushort port); | ||
| 107 | #ifdef G_OS_UNIX | ||
| 108 | gint sock_connect_async (const gchar *hostname, gushort port, | ||
| 109 | SockConnectFunc func, gpointer data); | ||
| 110 | gint sock_connect_async_cancel (gint id); | ||
| 111 | #endif | ||
| 112 | #if USE_THREADS | ||
| 113 | gint sock_connect_async_thread (const gchar *hostname, gushort port); | ||
| 114 | gint sock_connect_async_thread_wait (gint id, SockInfo **sock); | ||
| 115 | #endif | ||
| 116 | |||
| 117 | gint sock_info_connect (SockInfo *sock); | ||
| 118 | #ifdef G_OS_UNIX | ||
| 119 | gint sock_info_connect_async (SockInfo *sock, | ||
| 120 | SockConnectFunc func, gpointer data); | ||
| 121 | #endif | ||
| 122 | #if USE_THREADS | ||
| 123 | gint sock_info_connect_async_thread (SockInfo *sock); | ||
| 124 | gint sock_info_connect_async_thread_wait(gint id, SockInfo **sock); | ||
| 125 | #endif | ||
| 126 | |||
| 127 | /* Basic I/O functions */ | ||
| 128 | gint sock_printf (SockInfo *sock, const gchar *format, ...) | ||
| 129 | G_GNUC_PRINTF(2, 3); | ||
| 130 | gint sock_read (SockInfo *sock, gchar *buf, gint len); | ||
| 131 | gint sock_write (SockInfo *sock, const gchar *buf, gint len); | ||
| 132 | gint sock_write_all (SockInfo *sock, const gchar *buf, gint len); | ||
| 133 | gint sock_gets (SockInfo *sock, gchar *buf, gint len); | ||
| 134 | gint sock_getline (SockInfo *sock, gchar **line); | ||
| 135 | gint sock_puts (SockInfo *sock, const gchar *buf); | ||
| 136 | gint sock_peek (SockInfo *sock, gchar *buf, gint len); | ||
| 137 | gint sock_close (SockInfo *sock); | ||
| 138 | |||
| 139 | /* Functions to directly work on FD. They are needed for pipes */ | ||
| 140 | gint fd_connect_inet (gushort port); | ||
| 141 | gint fd_open_inet (gushort port); | ||
| 142 | gint fd_connect_unix (const gchar *path); | ||
| 143 | gint fd_open_unix (const gchar *path); | ||
| 144 | gint fd_accept (gint sock); | ||
| 145 | |||
| 146 | gint fd_read (gint sock, gchar *buf, gint len); | ||
| 147 | gint fd_write (gint sock, const gchar *buf, gint len); | ||
| 148 | gint fd_write_all (gint sock, const gchar *buf, gint len); | ||
| 149 | gint fd_gets (gint sock, gchar *buf, gint len); | ||
| 150 | gint fd_getline (gint sock, gchar **line); | ||
| 151 | gint fd_close (gint sock); | ||
| 152 | |||
| 153 | /* Functions for SSL */ | ||
| 154 | #if USE_SSL | ||
| 155 | gint ssl_read (SSL *ssl, gchar *buf, gint len); | ||
| 156 | gint ssl_write (SSL *ssl, const gchar *buf, gint len); | ||
| 157 | gint ssl_write_all (SSL *ssl, const gchar *buf, gint len); | ||
| 158 | gint ssl_gets (SSL *ssl, gchar *buf, gint len); | ||
| 159 | gint ssl_getline (SSL *ssl, gchar **line); | ||
| 160 | gint ssl_peek (SSL *ssl, gchar *buf, gint len); | ||
| 161 | #endif | ||
| 162 | |||
| 163 | #endif /* __SOCKET_H__ */ | ||
