147
|
1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
|
2 |
/* |
|
3 |
* Copyright (C) 2006 Imendio AB |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or |
|
6 |
* modify it under the terms of the GNU Lesser General Public License as |
|
7 |
* published by the Free Software Foundation; either version 2 of the |
|
8 |
* License, or (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 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 program; if not, write to the |
|
17 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 |
* Boston, MA 02111-1307, USA. |
|
19 |
*/ |
|
20 |
|
|
21 |
#include "lm-socket.h" |
|
22 |
|
149
|
23 |
#ifndef G_OS_WIN32 |
|
24 |
typedef int LmSock; |
|
25 |
#else /* G_OS_WIN32 */ |
|
26 |
typedef SOCKET LmSock; |
|
27 |
#endif /* G_OS_WIN32 */ |
|
28 |
|
|
29 |
/* FIXME: Integrate with the SSL stuff */ |
|
30 |
|
|
31 |
struct _LmSocket { |
|
32 |
LmSock sock; |
147
|
33 |
/* FIXME: Add the rest */ |
|
34 |
|
149
|
35 |
LmSocketFuncs funcs; |
147
|
36 |
|
149
|
37 |
LmSocketState state; |
|
38 |
|
|
39 |
gboolean is_blocking; |
147
|
40 |
|
|
41 |
gint ref; |
|
42 |
}; |
|
43 |
|
149
|
44 |
static LmSocket * socket_create (void); |
|
45 |
static void socket_free (LmSocket *socket); |
|
46 |
|
|
47 |
static LmSocket * |
|
48 |
socket_create (void) |
|
49 |
{ |
|
50 |
LmSocket *socket; |
|
51 |
|
|
52 |
socket = g_new0 (LmSocket, 1); |
|
53 |
socket->ref_count = 1; |
|
54 |
socket->is_blocking = FALSE; |
|
55 |
|
|
56 |
return socket; |
|
57 |
} |
147
|
58 |
|
|
59 |
static void |
149
|
60 |
socket_free (LmSocket *socket) |
147
|
61 |
{ |
|
62 |
/* FIXME: Free up the rest of the memory */ |
149
|
63 |
g_free (socket->host); |
|
64 |
|
|
65 |
g_free (socket); |
147
|
66 |
} |
|
67 |
|
149
|
68 |
LmSocket * |
|
69 |
lm_socket_new (LmSocketFuncs funcs, const gchar *host, guint port) |
|
70 |
{ |
|
71 |
LmSocket *socket; |
147
|
72 |
|
149
|
73 |
socket = socket_create (); |
|
74 |
|
|
75 |
socket->funcs = funcs; |
|
76 |
socket->host = g_strdup (host); |
|
77 |
socket->port = port; |
|
78 |
|
|
79 |
return socket; |
|
80 |
} |
147
|
81 |
|
149
|
82 |
void |
|
83 |
lm_socket_open (LmSocket *socket) |
|
84 |
{ |
|
85 |
} |
|
86 |
|
|
87 |
int |
|
88 |
lm_socket_get_fd (LmSocket *socket) |
147
|
89 |
{ |
149
|
90 |
g_return_val_if_fail (socket != NULL, -1); |
|
91 |
|
|
92 |
return socket->fd; |
|
93 |
} |
|
94 |
|
|
95 |
gboolean |
|
96 |
lm_socket_get_is_blocking (LmSocket *socket) |
|
97 |
{ |
|
98 |
return socket->is_blocking; |
147
|
99 |
} |
|
100 |
|
|
101 |
void |
149
|
102 |
lm_socket_set_is_blocking (LmSocket *socket, gboolean is_block) |
147
|
103 |
{ |
149
|
104 |
int res; |
|
105 |
|
|
106 |
g_return_if_fail (socket != NULL); |
|
107 |
|
|
108 |
/* FIXME: Don't unset all flags */ |
|
109 |
|
|
110 |
#ifndef G_OS_WIN32 |
|
111 |
res = fcntl (socket->sock, F_SETFL, is_block ? 0 : O_NONBLOCK); |
|
112 |
#else /* G_OS_WIN32 */ |
|
113 |
u_long mode = (is_block ? 0 : 1); |
|
114 |
res = ioctlsocket (socket->sock, FIONBIO, &mode); |
|
115 |
#endif /* G_OS_WIN32 */ |
|
116 |
|
|
117 |
if (res != 0) { |
|
118 |
g_log (LM_LOG_DOMAIN, LM_LOG_LEVEL_NET, |
|
119 |
"Could not set socket to be %s\n", |
|
120 |
block ? "blocking" : "non-blocking"); |
|
121 |
} |
|
122 |
|
|
123 |
socket->is_blocking = is_block; |
|
124 |
} |
|
125 |
|
|
126 |
int |
|
127 |
lm_socket_write (LmSocket *socket, |
|
128 |
gsize size, |
|
129 |
gchar *buf, |
|
130 |
GError **error) |
|
131 |
{ |
|
132 |
} |
|
133 |
|
|
134 |
int |
|
135 |
lm_socket_read (LmSocket *socket, |
|
136 |
gsize size, |
|
137 |
gchar *buf, |
|
138 |
GError **error) |
|
139 |
{ |
|
140 |
} |
|
141 |
|
|
142 |
gboolean |
|
143 |
lm_socket_close (LmSocket *socket, GError **error) |
|
144 |
{ |
|
145 |
} |
|
146 |
|
|
147 |
LmSocket * |
|
148 |
lm_socket_ref (LmSocket *socket) |
|
149 |
{ |
|
150 |
g_return_val_if_fail (socket != NULL, NULL); |
|
151 |
socket->ref++; |
147
|
152 |
|
149
|
153 |
return socket; |
|
154 |
} |
|
155 |
|
|
156 |
void |
|
157 |
lm_socket_unref (LmSocket *socket) |
|
158 |
{ |
|
159 |
g_return_if_fail (socket != NULL); |
147
|
160 |
|
149
|
161 |
socket->ref--; |
|
162 |
|
|
163 |
if (socket->ref <= 0) { |
|
164 |
socket_free (socket); |
147
|
165 |
} |
|
166 |
} |
149
|
167 |
|