Merge branch 'master' of ssh+git://git.claws-mail.org/home/git/claws
[claws.git] / src / common / socket.h
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifndef __SOCKET_H__
21 #define __SOCKET_H__
22
23 #ifdef HAVE_CONFIG_H
24 #include "claws-features.h"
25 #endif
26
27 #if (defined (_XOPEN_SOURCE) && !defined (_BSD_SOURCE))
28 #define _BSD_SOURCE
29 #endif
30
31 #include <glib.h>
32 #if HAVE_NETDB_H
33 #  include <netdb.h>
34 #endif
35
36 typedef struct _SockInfo        SockInfo;
37
38 #ifdef USE_GNUTLS
39 #  include "ssl.h"
40 #endif
41
42 typedef enum
43 {
44         CONN_READY,
45         CONN_LOOKUPSUCCESS,
46         CONN_ESTABLISHED,
47         CONN_LOOKUPFAILED,
48         CONN_FAILED,
49         CONN_DISCONNECTED
50 } ConnectionState;
51
52 typedef gint (*SockConnectFunc)         (SockInfo       *sock,
53                                          gpointer        data);
54 typedef gboolean (*SockFunc)            (SockInfo       *sock,
55                                          GIOCondition    condition,
56                                          gpointer        data);
57
58 struct _SockInfo
59 {
60         gint sock;
61 #if USE_GNUTLS
62         gnutls_session_t ssl;
63         gnutls_certificate_credentials_t xcred;
64         gnutls_x509_crt_t client_crt;
65         gnutls_x509_privkey_t client_key;
66         gchar *gnutls_priority;
67 #endif
68         guint g_source;
69         GIOChannel *sock_ch;
70
71         gchar *hostname;
72         gushort port;
73         ConnectionState state;
74         gpointer data;
75
76         SockFunc callback;
77         GIOCondition condition;
78         gchar *canonical_name;
79
80         const void *account;
81         gboolean is_smtp;
82         gboolean ssl_cert_auto_accept;
83 };
84
85 void refresh_resolvers                  (void);
86 gint sock_init                          (void);
87 gint sock_cleanup                       (void);
88
89 gint sock_set_io_timeout                (guint sec);
90
91 gint sock_set_nonblocking_mode          (SockInfo *sock, gboolean nonblock);
92 gboolean sock_is_nonblocking_mode       (SockInfo *sock);
93
94 guint sock_add_watch                    (SockInfo *sock, GIOCondition condition,
95                                          SockFunc func, gpointer data);
96
97 struct hostent *my_gethostbyname        (const gchar *hostname);
98
99 SockInfo *sock_connect                  (const gchar *hostname, gushort port);
100 gint sock_connect_async                 (const gchar *hostname, gushort port,
101                                          SockConnectFunc func, gpointer data);
102 gint sock_connect_async_cancel          (gint id);
103
104 /* Basic I/O functions */
105 gint sock_read          (SockInfo *sock, gchar *buf, gint len);
106 gint sock_write         (SockInfo *sock, const gchar *buf, gint len);
107 gint sock_write_all     (SockInfo *sock, const gchar *buf, gint len);
108 gint sock_close         (SockInfo *sock);
109
110 /* Functions to directly work on FD.  They are needed for pipes */
111 gint fd_connect_unix    (const gchar *path);
112 gint fd_open_unix       (const gchar *path);
113 gint fd_accept          (gint sock);
114
115 gint fd_connect_inet(gushort port);
116 gint fd_open_inet(gushort port);
117
118 gint fd_write           (gint sock, const gchar *buf, gint len);
119 gint fd_write_all       (gint sock, const gchar *buf, gint len);
120 gint fd_gets            (gint sock, gchar *buf, gint len);
121 gint fd_close           (gint sock);
122
123 #endif /* __SOCKET_H__ */