summaryrefslogtreecommitdiff
path: root/lib/gibber/gibber-tcp-transport.c
blob: efe2fc8a80a661807191e1843887d1d263c54fe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * gibber-tcp-transport.c - Source for GibberTCPTransport
 * Copyright (C) 2006 Collabora Ltd.
 * @author Sjoerd Simons <sjoerd@luon.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>

#include <errno.h>
#include <string.h>

#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

#include "gibber-sockets.h"
#include "gibber-tcp-transport.h"

#define DEBUG_FLAG DEBUG_NET
#include "gibber-debug.h"

#include "errno.h"

G_DEFINE_TYPE(GibberTCPTransport, gibber_tcp_transport,
              GIBBER_TYPE_FD_TRANSPORT)

/* private structure */
typedef struct _GibberTCPTransportPrivate GibberTCPTransportPrivate;

struct _GibberTCPTransportPrivate
{
  GIOChannel *channel;
  struct addrinfo *ans;
  struct addrinfo *tmpaddr;
  guint watch_in;

  gboolean dispose_has_run;
};

#define GIBBER_TCP_TRANSPORT_GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), GIBBER_TYPE_TCP_TRANSPORT, \
   GibberTCPTransportPrivate))

static void
gibber_tcp_transport_init (GibberTCPTransport *obj)
{
  /* GibberTCPTransportPrivate *priv = GIBBER_TCP_TRANSPORT_GET_PRIVATE (obj);
   */

  /* allocate any data required by the object here */
}

static void gibber_tcp_transport_dispose (GObject *object);
static void gibber_tcp_transport_finalize (GObject *object);

static void
gibber_tcp_transport_class_init (
  GibberTCPTransportClass *gibber_tcp_transport_class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (gibber_tcp_transport_class);

  g_type_class_add_private (gibber_tcp_transport_class,
    sizeof (GibberTCPTransportPrivate));

  object_class->dispose = gibber_tcp_transport_dispose;
  object_class->finalize = gibber_tcp_transport_finalize;
}

static void
clean_connect_attempt (GibberTCPTransport *self)
{
  GibberTCPTransportPrivate *priv = GIBBER_TCP_TRANSPORT_GET_PRIVATE (
      self);

  if (priv->watch_in != 0)
    {
      g_source_remove (priv->watch_in);
      priv->watch_in = 0;
    }

  if (priv->channel != NULL)
    {
      g_io_channel_unref (priv->channel);
      priv->channel = NULL;
    }
}

static void
clean_all_connect_attempts (GibberTCPTransport *self)
{
  GibberTCPTransportPrivate *priv = GIBBER_TCP_TRANSPORT_GET_PRIVATE (
      self);

  clean_connect_attempt (self);

  if (priv->ans != NULL)
    {
      freeaddrinfo (priv->ans);
      priv->ans = NULL;
    }

  priv->tmpaddr = NULL;
}

void
gibber_tcp_transport_dispose (GObject *object)
{
  GibberTCPTransport *self = GIBBER_TCP_TRANSPORT (object);
  GibberTCPTransportPrivate *priv = GIBBER_TCP_TRANSPORT_GET_PRIVATE (self);

  if (priv->dispose_has_run)
    return;

  priv->dispose_has_run = TRUE;

  clean_all_connect_attempts (self);
  /* release any references held by the object here */

  if (G_OBJECT_CLASS (gibber_tcp_transport_parent_class)->dispose)
    G_OBJECT_CLASS (gibber_tcp_transport_parent_class)->dispose (object);
}

void
gibber_tcp_transport_finalize (GObject *object)
{
  /*
  GibberTCPTransport *self = GIBBER_TCP_TRANSPORT (object);
  GibberTCPTransportPrivate *priv = GIBBER_TCP_TRANSPORT_GET_PRIVATE (self);
  */

  /* free any data held directly by the object here */

  G_OBJECT_CLASS (gibber_tcp_transport_parent_class)->finalize (object);
}

GibberTCPTransport *
gibber_tcp_transport_new ()
{
  return g_object_new (GIBBER_TYPE_TCP_TRANSPORT, NULL);
}

static void new_connect_attempt (GibberTCPTransport *self);

static gboolean
try_to_connect (GibberTCPTransport *self)
{
  GibberTCPTransportPrivate *priv = GIBBER_TCP_TRANSPORT_GET_PRIVATE (
      self);
  gint fd;
  int ret;

  g_assert (priv->channel != NULL);

  fd = g_io_channel_unix_get_fd (priv->channel);
  ret = connect (fd, priv->tmpaddr->ai_addr, priv->tmpaddr->ai_addrlen);

  if (ret == 0)
    {
      DEBUG ("connect succeeded");

      clean_all_connect_attempts (self);
      gibber_fd_transport_set_fd (GIBBER_FD_TRANSPORT (self), fd, TRUE);
      return FALSE;
    }

  if (gibber_connect_errno_requires_retry ())
    {
      /* We have to wait longer */
      return TRUE;
    }

  clean_connect_attempt (self);
  priv->tmpaddr = priv->tmpaddr->ai_next;
  new_connect_attempt (self);
  return FALSE;
}

static gboolean
_channel_io (GIOChannel *source,
             GIOCondition condition,
             gpointer data)
{
  GibberTCPTransport *self = GIBBER_TCP_TRANSPORT (data);

  return try_to_connect (self);
}

static void
new_connect_attempt (GibberTCPTransport *self)
{
  GibberTCPTransportPrivate *priv = GIBBER_TCP_TRANSPORT_GET_PRIVATE (
      self);
  int fd;
  char name[NI_MAXHOST], portname[NI_MAXSERV];

  if (priv->tmpaddr == NULL)
    {
      /* no more candidate to try */
      DEBUG ("connection failed");
      goto failed;
    }

  getnameinfo (priv->tmpaddr->ai_addr, priv->tmpaddr->ai_addrlen,
      name, sizeof (name), portname, sizeof (portname),
      NI_NUMERICHOST | NI_NUMERICSERV);

  DEBUG ("Trying %s port %s...", name, portname);

  fd = socket (priv->tmpaddr->ai_family, priv->tmpaddr->ai_socktype,
      priv->tmpaddr->ai_protocol);

  if (fd < 0)
    {
      DEBUG("socket failed: #%d %s", gibber_socket_errno (),
          gibber_socket_strerror ());
      goto failed;
    }

  gibber_socket_set_nonblocking (fd);
  priv->channel = gibber_io_channel_new_from_socket (fd);
  g_io_channel_set_close_on_unref (priv->channel, FALSE);
  g_io_channel_set_encoding (priv->channel, NULL, NULL);
  g_io_channel_set_buffered (priv->channel, FALSE);

  priv->watch_in = g_io_add_watch (priv->channel, G_IO_IN | G_IO_PRI | G_IO_OUT,
      _channel_io, self);

  try_to_connect (self);
  return;

failed:
  clean_all_connect_attempts (self);

  gibber_transport_set_state (GIBBER_TRANSPORT (self),
      GIBBER_TRANSPORT_DISCONNECTED);
}

void
gibber_tcp_transport_connect (GibberTCPTransport *tcp_transport,
    const gchar *host, const gchar *port)
{
  GibberTCPTransportPrivate *priv = GIBBER_TCP_TRANSPORT_GET_PRIVATE (
      tcp_transport);
  int ret = -1;
  struct addrinfo req;

  gibber_transport_set_state (GIBBER_TRANSPORT (tcp_transport),
                             GIBBER_TRANSPORT_CONNECTING);

  memset (&req, 0, sizeof (req));
  req.ai_flags = 0;
  req.ai_family = AF_UNSPEC;
  req.ai_socktype = SOCK_STREAM;
  req.ai_protocol = IPPROTO_TCP;

  g_assert (priv->ans == NULL);
  g_assert (priv->tmpaddr == NULL);
  g_assert (priv->channel == NULL);

  ret = getaddrinfo (host, port, &req, &priv->ans);
  if (ret != 0)
    {
      DEBUG("getaddrinfo failed: %s", gai_strerror (ret));

      gibber_transport_set_state (GIBBER_TRANSPORT (tcp_transport),
          GIBBER_TRANSPORT_DISCONNECTED);
      return;
    }

  priv->tmpaddr = priv->ans;

  new_connect_attempt (tcp_transport);
}