summaryrefslogtreecommitdiff
path: root/libmailutils/server/server.c
blob: 2fe791c7e3952137aa4ac704be73d88ed06b863f (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2007-2008, 2010-2012, 2014-2015 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 3 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, see
   <http://www.gnu.org/licenses/>.  */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <mailutils/server.h>
#include <mailutils/errno.h>
#include <mailutils/acl.h>


struct _mu_connection
{
  struct _mu_connection *next, *prev;
  int fd;
  mu_conn_loop_fp f_loop;
  mu_conn_free_fp f_free;
  void *data;
};

#define MU_SERVER_TIMEOUT 0x1

unsigned long mu_session_id;

struct _mu_server
{
  int nfd;
  fd_set fdset;
  int flags;
  struct timeval timeout;
  struct _mu_connection *head, *tail;
  mu_server_idle_fp f_idle;
  mu_server_free_fp f_free;
  void *server_data;
};

void
recompute_nfd (mu_server_t srv)
{
  struct _mu_connection *p;
  int nfd = 0;
  for (p = srv->head; p; p = p->next)
    if (p->fd > nfd)
      nfd = p->fd;
  srv->nfd = nfd + 1;
}

void
destroy_connection (mu_server_t srv, struct _mu_connection *conn)
{
  if (conn->f_free)
    conn->f_free (conn->data, srv->server_data);
  free (conn);
}

void
remove_connection (mu_server_t srv, struct _mu_connection *conn)
{
  struct _mu_connection *p;
  
  close (conn->fd);
  FD_CLR (conn->fd, &srv->fdset);

  p = conn->prev;
  if (p)
    p->next = conn->next;
  else /* we're at head */
    srv->head = conn->next;

  p = conn->next;
  if (p)
    p->prev = conn->prev;
  else /* we're at tail */
    srv->tail = conn->prev;

  if (conn->fd == srv->nfd - 1)
    recompute_nfd (srv);
  
  destroy_connection (srv, conn);
}

int
connection_loop (mu_server_t srv, fd_set *fdset)
{
  struct _mu_connection *conn;
  for (conn = srv->head; conn;)
    {
      struct _mu_connection *next = conn->next;
      if (FD_ISSET (conn->fd, fdset))
	{
	  int rc;

	  ++mu_session_id;
	  rc = conn->f_loop (conn->fd, conn->data, srv->server_data);
	  switch (rc)
	    {
	    case 0:
	      break;
	      
	    case MU_SERVER_CLOSE_CONN:
	    default:
	      remove_connection (srv, conn);
	      break;
	      
	    case MU_SERVER_SHUTDOWN:
	      return 1;
	    }
	}
      conn = next;
    }
  return 0;
}

void
make_fdset (mu_server_t srv)
{
  struct _mu_connection *p;
  int nfd = 0;
  
  FD_ZERO (&srv->fdset);
  for (p = srv->head; p; p = p->next)
    {
      FD_SET (p->fd, &srv->fdset);
      if (p->fd > nfd)
	nfd = p->fd;
    }
  srv->nfd = nfd + 1;
}

int
mu_server_run (mu_server_t srv)
{
  int status = 0;
  
  if (!srv)
    return EINVAL;
  if (!srv->head)
    return MU_ERR_NOENT;
  
  make_fdset (srv);  
  
  while (1)
    {
      int rc;
      fd_set rdset;
      struct timeval *to;
      
      rdset = srv->fdset;
      to = (srv->flags & MU_SERVER_TIMEOUT) ? &srv->timeout : NULL;
      rc = select (srv->nfd, &rdset, NULL, NULL, to);
      if (rc == -1 && errno == EINTR)
	{
	  if (srv->f_idle && srv->f_idle (srv->server_data))
	    break;
	  continue;
	}
      if (rc < 0)
	return errno;

      if (connection_loop (srv, &rdset))
	{
	  status = MU_ERR_FAILURE;
	  break;
	}
    }
  return status;
}

int
mu_server_create (mu_server_t *psrv)
{
  mu_server_t srv = calloc (1, sizeof (*srv));
  if (!srv)
    return ENOMEM;
  *psrv = srv;
  return 0;
}

int
mu_server_destroy (mu_server_t *psrv)
{
  mu_server_t srv;
  struct _mu_connection *p;
  
  if (!psrv)
    return EINVAL;
  srv = *psrv;
  if (!srv)
    return 0;

  for (p = srv->head; p; )
    {
      struct _mu_connection *next = p->next;
      destroy_connection (srv, p);
      p = next;
    }

  if (srv->f_free)
    srv->f_free (srv->server_data);
  
  free (srv);
  *psrv = NULL;
  return 0;
}

int
mu_server_count (mu_server_t srv, size_t *pcount)
{
  size_t n = 0;
  struct _mu_connection *p;

  if (!srv)
    return EINVAL;
  for (p = srv->head; p; p = p->next)
    n++;
  *pcount = n;
  return 0;
}

int
mu_server_set_idle (mu_server_t srv, mu_server_idle_fp fp)
{
  if (!srv)
    return EINVAL;
  srv->f_idle = fp;
  return 0;
}

int
mu_server_set_data (mu_server_t srv, void *data, mu_server_free_fp fp)
{
  if (!srv)
    return EINVAL;
  srv->server_data = data;
  srv->f_free = fp;
  return 0;
}

int
mu_server_set_timeout (mu_server_t srv, struct timeval *to)
{
  if (!srv)
    return EINVAL;
  if (!to)
    srv->flags &= ~MU_SERVER_TIMEOUT;
  else
    {
      srv->timeout = *to;
      srv->flags |= MU_SERVER_TIMEOUT;
    }
  return 0;
}

int
mu_server_add_connection (mu_server_t srv,
			  int fd, void *data,
			  mu_conn_loop_fp loop, mu_conn_free_fp free)
{
  struct _mu_connection *p;

  if (!srv || !loop)
    return EINVAL;

  p = malloc (sizeof (*p));
  if (!p)
    return ENOMEM;
  p->fd = fd;
  p->f_loop = loop;
  p->f_free = free;
  p->data = data;

  p->next = NULL;
  p->prev = srv->tail;
  if (srv->tail)
    srv->tail->next = p;
  else
    srv->head = p;
  srv->tail = p;
  return 0;
}

int
mu_acl_set_session_id (mu_acl_t acl)
{
  char sessidstr[9];
  snprintf (sessidstr, sizeof sessidstr, "%08lx", mu_session_id);
  return mu_acl_setenv (acl, "sessid", sessidstr);
}

Return to:

Send suggestions and report system problems to the System administrator.