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