summaryrefslogtreecommitdiff
path: root/mailbox/tcp.c
blob: 893070e5b8c8300c115fe02daaa9fd271e49e227 (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
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 1999, 2000, 2004 Free Software Foundation, Inc.

   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 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA  */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <sys/socket.h>
#include <sys/types.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <mailutils/errno.h>
#include <mailutils/stream.h>
#include <mailutils/mutil.h>

#define TCP_STATE_INIT 		1
#define TCP_STATE_RESOLVE	2
#define TCP_STATE_RESOLVING	3
#define TCP_STATE_CONNECTING 	4
#define TCP_STATE_CONNECTED	5

struct _tcp_instance {
	int 		fd;
	char 		*host;
	int 		port;
	int		state;
	unsigned long	address;
};

/* On solaris inet_addr() return -1.  */
#ifndef INADDR_NONE
# define INADDR_NONE (unsigned long)-1
#endif

static int
_tcp_close (stream_t stream)
{
  struct _tcp_instance *tcp = stream_get_owner (stream);
  int err = 0;

  if (tcp->fd != -1)
    {
      if (close (tcp->fd) != 0)
	{
	  err = errno;
	}
    }
  tcp->fd = -1;
  tcp->state = TCP_STATE_INIT;
  return err;
}

static int
_tcp_open (stream_t stream)
{
  struct _tcp_instance *tcp = stream_get_owner (stream);
  int flgs, ret;
  socklen_t namelen;
  struct sockaddr_in peer_addr;
  struct hostent *phe;
  struct sockaddr_in soc_addr;
  int flags;

  stream_get_flags(stream, &flags);

  switch (tcp->state)
    {
    case TCP_STATE_INIT:
      if (tcp->fd == -1)
	{
	  if ((tcp->fd = socket (PF_INET, SOCK_STREAM, 0)) == -1)
	    return errno;
	}
      if (flags & MU_STREAM_NONBLOCK)
	{
	  flgs = fcntl (tcp->fd, F_GETFL);
	  flgs |= O_NONBLOCK;
	  fcntl (tcp->fd, F_SETFL, flgs);
	  stream_set_flags (stream, MU_STREAM_NONBLOCK);
	}
      tcp->state = TCP_STATE_RESOLVING;
    case TCP_STATE_RESOLVING:
      assert (tcp->host != NULL && tcp->port > 0);
      tcp->address = inet_addr (tcp->host);
      if (tcp->address == INADDR_NONE)
	{
	  phe = gethostbyname (tcp->host);
	  if (!phe)
	    {
	      _tcp_close (stream);
	      return MU_ERR_GETHOSTBYNAME;
	    }
	  tcp->address = *(((unsigned long **) phe->h_addr_list)[0]);
	}
      tcp->state = TCP_STATE_RESOLVE;
    case TCP_STATE_RESOLVE:
      memset (&soc_addr, 0, sizeof (soc_addr));
      soc_addr.sin_family = AF_INET;
      soc_addr.sin_port = htons (tcp->port);
      soc_addr.sin_addr.s_addr = tcp->address;

      if ((connect (tcp->fd,
	      (struct sockaddr *) &soc_addr, sizeof (soc_addr))) == -1)
	{
	  ret = errno;
	  if (ret == EINPROGRESS || ret == EAGAIN)
	    {
	      tcp->state = TCP_STATE_CONNECTING;
	      ret = EAGAIN;
	    }
	  else
	    _tcp_close (stream);
	  return ret;
	}
      tcp->state = TCP_STATE_CONNECTING;
    case TCP_STATE_CONNECTING:
      namelen = sizeof (peer_addr);
      if (getpeername (tcp->fd,
	    (struct sockaddr *) &peer_addr, &namelen) == 0)
	tcp->state = TCP_STATE_CONNECTED;
      else
	{
	  ret = errno;
	  _tcp_close (stream);
	  return ret;
	}
      break;
    }
  return 0;
}


static int
_tcp_get_transport2 (stream_t stream, mu_transport_t *tr, mu_transport_t *tr2)
{
  struct _tcp_instance *tcp = stream_get_owner (stream);

  if (tcp->fd == -1)
    return EINVAL;

  if (tr)
    *tr = (mu_transport_t) tcp->fd;
  if (tr2)
    *tr2 = NULL;
  return 0;
}

static int
_tcp_read (stream_t stream, char *buf, size_t buf_size, off_t offset, size_t * br)
{
  struct _tcp_instance *tcp = stream_get_owner (stream);
  int bytes;

  offset = offset;
  if (br == NULL)
    return MU_ERR_OUT_NULL;
  *br = 0;
  if ((bytes = recv (tcp->fd, buf, buf_size, 0)) == -1)
    {
      *br = 0;
      return errno;
    }
  *br = bytes;
  return 0;
}

static int
_tcp_write (stream_t stream, const char *buf, size_t buf_size, off_t offset,
	    size_t * bw)
{
  struct _tcp_instance *tcp = stream_get_owner (stream);
  int bytes;

  offset = offset;
  if (bw == NULL)
    return MU_ERR_OUT_NULL;
  *bw = 0;
  if ((bytes = send (tcp->fd, buf, buf_size, 0)) == -1)
    {
      *bw = 0;
      return errno;
    }
  *bw = bytes;
  return 0;
}

static void
_tcp_destroy (stream_t stream)
{
  struct _tcp_instance *tcp = stream_get_owner (stream);

  if (tcp->host)
    free (tcp->host);
  if (tcp->fd != -1)
    close (tcp->fd);

  free (tcp);
}

int
_tcp_wait (stream_t stream, int *pflags, struct timeval *tvp)
{
  struct _tcp_instance *tcp = stream_get_owner (stream);
  if (tcp->fd == -1)
    return EINVAL;
  return mu_fd_wait (tcp->fd, pflags, tvp);
}

int
tcp_stream_create (stream_t * stream, const char* host, int port, int flags)
{
  struct _tcp_instance *tcp;
  int ret;

  if (host == NULL)
    return MU_ERR_TCP_NO_HOST;

  if (port < 1)
    return MU_ERR_TCP_NO_PORT;

  if ((tcp = malloc (sizeof (*tcp))) == NULL)
    return ENOMEM;
  tcp->fd = -1;
  tcp->host = strdup (host);
  if(!tcp->host)
  {
    free (tcp);
    return ENOMEM;
  }
  tcp->port = port;
  tcp->state = TCP_STATE_INIT;

  if ((ret = stream_create (stream,
	  flags | MU_STREAM_NO_CHECK | MU_STREAM_RDWR, tcp)) != 0)
  {
    free (tcp->host);
    free (tcp);

    return ret;
  }

  stream_set_open (*stream, _tcp_open, tcp);
  stream_set_close (*stream, _tcp_close, tcp);
  stream_set_read (*stream, _tcp_read, tcp);
  stream_set_write (*stream, _tcp_write, tcp);
  stream_set_get_transport2 (*stream, _tcp_get_transport2, tcp);
  stream_set_destroy (*stream, _tcp_destroy, tcp);
  stream_set_wait (*stream, _tcp_wait, tcp);

  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.