summaryrefslogtreecommitdiff
path: root/libsylph/socket.h
diff options
context:
space:
mode:
authorSimeon Simeonov2018-02-26 11:23:00 +0100
committerSimeon Simeonov2018-02-26 11:23:00 +0100
commit0b3cbf57875fd692e4ba0b336fefa4bee1ed00dc (patch)
treeaf916a30553c78ce9d4f2d8658656a175c5b6921 /libsylph/socket.h
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'libsylph/socket.h')
-rw-r--r--libsylph/socket.h163
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
35typedef struct _SockInfo SockInfo;
36
37#if USE_SSL
38# include "ssl.h"
39#endif
40
41typedef enum
42{
43 CONN_READY,
44 CONN_LOOKUPSUCCESS,
45 CONN_ESTABLISHED,
46 CONN_LOOKUPFAILED,
47 CONN_FAILED
48} ConnectionState;
49
50typedef 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
61typedef gint (*SockConnectFunc) (SockInfo *sock,
62 gpointer data);
63typedef gboolean (*SockFunc) (SockInfo *sock,
64 GIOCondition condition,
65 gpointer data);
66
67struct _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
87gint sock_init (void);
88gint sock_cleanup (void);
89
90gint sock_set_io_timeout (guint sec);
91
92SockInfo *sock_new (const gchar *hostname, gushort port);
93
94gint sock_set_nonblocking_mode (SockInfo *sock, gboolean nonblock);
95gboolean sock_is_nonblocking_mode (SockInfo *sock);
96
97gboolean sock_has_read_data (SockInfo *sock);
98
99guint sock_add_watch (SockInfo *sock, GIOCondition condition,
100 SockFunc func, gpointer data);
101guint sock_add_watch_poll (SockInfo *sock, GIOCondition condition,
102 SockFunc func, gpointer data);
103
104struct hostent *my_gethostbyname (const gchar *hostname);
105
106SockInfo *sock_connect (const gchar *hostname, gushort port);
107#ifdef G_OS_UNIX
108gint sock_connect_async (const gchar *hostname, gushort port,
109 SockConnectFunc func, gpointer data);
110gint sock_connect_async_cancel (gint id);
111#endif
112#if USE_THREADS
113gint sock_connect_async_thread (const gchar *hostname, gushort port);
114gint sock_connect_async_thread_wait (gint id, SockInfo **sock);
115#endif
116
117gint sock_info_connect (SockInfo *sock);
118#ifdef G_OS_UNIX
119gint sock_info_connect_async (SockInfo *sock,
120 SockConnectFunc func, gpointer data);
121#endif
122#if USE_THREADS
123gint sock_info_connect_async_thread (SockInfo *sock);
124gint sock_info_connect_async_thread_wait(gint id, SockInfo **sock);
125#endif
126
127/* Basic I/O functions */
128gint sock_printf (SockInfo *sock, const gchar *format, ...)
129 G_GNUC_PRINTF(2, 3);
130gint sock_read (SockInfo *sock, gchar *buf, gint len);
131gint sock_write (SockInfo *sock, const gchar *buf, gint len);
132gint sock_write_all (SockInfo *sock, const gchar *buf, gint len);
133gint sock_gets (SockInfo *sock, gchar *buf, gint len);
134gint sock_getline (SockInfo *sock, gchar **line);
135gint sock_puts (SockInfo *sock, const gchar *buf);
136gint sock_peek (SockInfo *sock, gchar *buf, gint len);
137gint sock_close (SockInfo *sock);
138
139/* Functions to directly work on FD. They are needed for pipes */
140gint fd_connect_inet (gushort port);
141gint fd_open_inet (gushort port);
142gint fd_connect_unix (const gchar *path);
143gint fd_open_unix (const gchar *path);
144gint fd_accept (gint sock);
145
146gint fd_read (gint sock, gchar *buf, gint len);
147gint fd_write (gint sock, const gchar *buf, gint len);
148gint fd_write_all (gint sock, const gchar *buf, gint len);
149gint fd_gets (gint sock, gchar *buf, gint len);
150gint fd_getline (gint sock, gchar **line);
151gint fd_close (gint sock);
152
153/* Functions for SSL */
154#if USE_SSL
155gint ssl_read (SSL *ssl, gchar *buf, gint len);
156gint ssl_write (SSL *ssl, const gchar *buf, gint len);
157gint ssl_write_all (SSL *ssl, const gchar *buf, gint len);
158gint ssl_gets (SSL *ssl, gchar *buf, gint len);
159gint ssl_getline (SSL *ssl, gchar **line);
160gint ssl_peek (SSL *ssl, gchar *buf, gint len);
161#endif
162
163#endif /* __SOCKET_H__ */