Statistics
| Revision:

root / libsylph / socket.h @ 3118

History | View | Annotate | Download (4.6 kB)

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
32
typedef struct _SockInfo        SockInfo;
33
34
#if USE_SSL
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
} ConnectionState;
46
47
typedef enum
48
{
49
        SYL_SOCK_NONBLOCK        = 1 << 0,
50
        SYL_SOCK_CHECK_IO        = 1 << 1
51
} SockFlags;
52
53
#define SOCK_SET_FLAGS(flags, set)        { (flags) |= (set); }
54
#define SOCK_UNSET_FLAGS(flags, set)        { (flags) &= ~(set); }
55
#define SOCK_IS_NONBLOCK(flags)                ((flags & SYL_SOCK_NONBLOCK) != 0)
56
#define SOCK_IS_CHECK_IO(flags)                ((flags & SYL_SOCK_CHECK_IO) != 0)
57
58
typedef gint (*SockConnectFunc)                (SockInfo        *sock,
59
                                         gpointer         data);
60
typedef gboolean (*SockFunc)                (SockInfo        *sock,
61
                                         GIOCondition         condition,
62
                                         gpointer         data);
63
64
struct _SockInfo
65
{
66
        gint sock;
67
#if USE_SSL
68
        SSL *ssl;
69
#else
70
        gpointer ssl;
71
#endif
72
        GIOChannel *sock_ch;
73
74
        gchar *hostname;
75
        gushort port;
76
        ConnectionState state;
77
        SockFlags flags;
78
        gpointer data;
79
80
        SockFunc callback;
81
        GIOCondition condition;
82
};
83
84
gint sock_init                                (void);
85
gint sock_cleanup                        (void);
86
87
gint sock_set_io_timeout                (guint sec);
88
89
SockInfo *sock_new                        (const gchar *hostname, gushort port);
90
91
gint sock_set_nonblocking_mode                (SockInfo *sock, gboolean nonblock);
92
gboolean sock_is_nonblocking_mode        (SockInfo *sock);
93
94
gboolean sock_has_read_data                (SockInfo *sock);
95
96
guint sock_add_watch                        (SockInfo *sock, GIOCondition condition,
97
                                         SockFunc func, gpointer data);
98
guint sock_add_watch_poll                (SockInfo *sock, GIOCondition condition,
99
                                         SockFunc func, gpointer data);
100
101
struct hostent *my_gethostbyname        (const gchar *hostname);
102
103
SockInfo *sock_connect                        (const gchar *hostname, gushort port);
104
#ifdef G_OS_UNIX
105
gint sock_connect_async                        (const gchar *hostname, gushort port,
106
                                         SockConnectFunc func, gpointer data);
107
gint sock_connect_async_cancel                (gint id);
108
#endif
109
#if USE_THREADS
110
gint sock_connect_async_thread                (const gchar *hostname, gushort port);
111
gint sock_connect_async_thread_wait        (gint id, SockInfo **sock);
112
#endif
113
114
gint sock_info_connect                        (SockInfo *sock);
115
#ifdef G_OS_UNIX
116
gint sock_info_connect_async                (SockInfo *sock,
117
                                         SockConnectFunc func, gpointer data);
118
#endif
119
#if USE_THREADS
120
gint sock_info_connect_async_thread        (SockInfo *sock);
121
gint sock_info_connect_async_thread_wait(gint id, SockInfo **sock);
122
#endif
123
124
/* Basic I/O functions */
125
gint sock_printf        (SockInfo *sock, const gchar *format, ...)
126
                         G_GNUC_PRINTF(2, 3);
127
gint sock_read                (SockInfo *sock, gchar *buf, gint len);
128
gint sock_write                (SockInfo *sock, const gchar *buf, gint len);
129
gint sock_write_all        (SockInfo *sock, const gchar *buf, gint len);
130
gint sock_gets                (SockInfo *sock, gchar *buf, gint len);
131
gint sock_getline        (SockInfo *sock, gchar **line);
132
gint sock_puts                (SockInfo *sock, const gchar *buf);
133
gint sock_peek                (SockInfo *sock, gchar *buf, gint len);
134
gint sock_close                (SockInfo *sock);
135
136
/* Functions to directly work on FD.  They are needed for pipes */
137
gint fd_connect_inet        (gushort port);
138
gint fd_open_inet        (gushort port);
139
gint fd_connect_unix        (const gchar *path);
140
gint fd_open_unix        (const gchar *path);
141
gint fd_accept                (gint sock);
142
143
gint fd_read                (gint sock, gchar *buf, gint len);
144
gint fd_write                (gint sock, const gchar *buf, gint len);
145
gint fd_write_all        (gint sock, const gchar *buf, gint len);
146
gint fd_gets                (gint sock, gchar *buf, gint len);
147
gint fd_getline                (gint sock, gchar **line);
148
gint fd_close                (gint sock);
149
150
/* Functions for SSL */
151
#if USE_SSL
152
gint ssl_read                (SSL *ssl, gchar *buf, gint len);
153
gint ssl_write                (SSL *ssl, const gchar *buf, gint len);
154
gint ssl_write_all        (SSL *ssl, const gchar *buf, gint len);
155
gint ssl_gets                (SSL *ssl, gchar *buf, gint len);
156
gint ssl_getline        (SSL *ssl, gchar **line);
157
gint ssl_peek                (SSL *ssl, gchar *buf, gint len);
158
#endif
159
160
#endif /* __SOCKET_H__ */