2005-09-14 [paul] 1.9.14cvs26
[claws.git] / src / common / socket.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2003 Hiroyuki Yamamoto
4  *
5  * 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 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
32 typedef struct _SockInfo        SockInfo;
33
34 #if USE_OPENSSL
35 #  include "ssl.h"
36 #endif
37
38 typedef enum
39 {
40         CONN_READY,
41         CONN_LOOKUPSUCCESS,
42         CONN_ESTABLISHED,
43         CONN_LOOKUPFAILED,
44         CONN_FAILED,
45         CONN_DISCONNECTED
46 } ConnectionState;
47
48 typedef gint (*SockConnectFunc)         (SockInfo       *sock,
49                                          gpointer        data);
50 typedef gboolean (*SockFunc)            (SockInfo       *sock,
51                                          GIOCondition    condition,
52                                          gpointer        data);
53
54 struct _SockInfo
55 {
56         gint sock;
57 #if USE_OPENSSL
58         SSL *ssl;
59         guint g_source;
60 #endif
61         GIOChannel *sock_ch;
62
63         gchar *hostname;
64         gushort port;
65         ConnectionState state;
66         gpointer data;
67
68         SockFunc callback;
69         GIOCondition condition;
70 };
71
72 gint sock_init                          (void);
73 gint sock_cleanup                       (void);
74
75 gint sock_set_io_timeout                (guint sec);
76
77 gint sock_set_nonblocking_mode          (SockInfo *sock, gboolean nonblock);
78 gboolean sock_is_nonblocking_mode       (SockInfo *sock);
79
80 guint sock_add_watch                    (SockInfo *sock, GIOCondition condition,
81                                          SockFunc func, gpointer data);
82
83 struct hostent *my_gethostbyname        (const gchar *hostname);
84
85 SockInfo *sock_connect                  (const gchar *hostname, gushort port);
86 SockInfo *sock_connect_cmd              (const gchar *hostname, const gchar *tunnelcmd);
87 #ifdef G_OS_UNIX
88 gint sock_connect_async                 (const gchar *hostname, gushort port,
89                                          SockConnectFunc func, gpointer data);
90 gint sock_connect_async_cancel          (gint id);
91 #endif
92
93 /* Basic I/O functions */
94 gint sock_printf        (SockInfo *sock, const gchar *format, ...)
95                          G_GNUC_PRINTF(2, 3);
96 gint sock_read          (SockInfo *sock, gchar *buf, gint len);
97 gint sock_write         (SockInfo *sock, const gchar *buf, gint len);
98 gint sock_write_all     (SockInfo *sock, const gchar *buf, gint len);
99 gint sock_gets          (SockInfo *sock, gchar *buf, gint len);
100 gchar *sock_getline     (SockInfo *sock);
101 gint sock_puts          (SockInfo *sock, const gchar *buf);
102 gint sock_peek          (SockInfo *sock, gchar *buf, gint len);
103 gint sock_close         (SockInfo *sock);
104
105 /* Functions to directly work on FD.  They are needed for pipes */
106 gint fd_connect_unix    (const gchar *path);
107 gint fd_open_unix       (const gchar *path);
108 gint fd_accept          (gint sock);
109
110 gint fd_read            (gint sock, gchar *buf, gint len);
111 gint fd_write           (gint sock, const gchar *buf, gint len);
112 gint fd_write_all       (gint sock, const gchar *buf, gint len);
113 gint fd_gets            (gint sock, gchar *buf, gint len);
114 gint fd_getline         (gint sock, gchar **str);
115 gint fd_close           (gint sock);
116
117 /* Functions for SSL */
118 #if USE_OPENSSL
119 gint ssl_read           (SSL *ssl, gchar *buf, gint len);
120 gint ssl_write          (SSL *ssl, const gchar *buf, gint len);
121 gint ssl_write_all      (SSL *ssl, const gchar *buf, gint len);
122 gint ssl_gets           (SSL *ssl, gchar *buf, gint len);
123 gint ssl_getline        (SSL *ssl, gchar **str);
124 gint ssl_peek           (SSL *ssl, gchar *buf, gint len);
125 #endif
126
127 #endif /* __SOCKET_H__ */