Files @ 974e60d6a71b
Branch filter:

Location: DistRen/src/server/listen.c

binki
Server-side sockets partial implementation
  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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
  Copyright 2009 Nathan Phillip Brink

  This file is a part of DistRen.

  DistRen is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  DistRen 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "listen.h"

#include <errno.h>
#include <fcntl.h>
#include <list.h>
#include <malloc.h>
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <unistd.h>

/* local */

struct distrend_clientset
{
  LIST *clients;

  int nfds;
};

struct distrend_packet
{
  size_t len;
  char *data;
};

int distrend_client_new(struct distrend_client **client, int sock, enum distrend_client_state state);
int distrend_client_free(struct distrend_client *client);
void distrend_packet_free(struct distrend_packet *packet);
int distrend_makesocknonblock(int sock);

int distrend_listen(struct distrend_config *config, struct distrend_clientset **clients)
{
  int tmp;

  struct sockaddr_in6 sockaddr =
    {
      .sin6_family = AF_INET6,
      .sin6_port = 0,
      .sin6_flowinfo = 0,
      .sin6_addr = IN6ADDR_ANY_INIT,
      .sin6_scope_id = 0
    };

  *clients = malloc(sizeof(struct distrend_clientset));

  (*clients)->clients = list_init();
  (*clients)->nfds = 1;

  sockaddr.sin6_port = htons(4050);

  config->listens->sock = socket(AF_INET6, SOCK_STREAM, 0);
  tmp = bind(config->listens->sock, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
  if(tmp == -1)
    {
      perror("bind");
      free(*clients);

      return 1;
    }

  tmp = listen(config->listens->sock, 1);
  if(tmp == -1)
    {
      perror("listen");
      free(*clients);
      
      return 1;
    }

  tmp = distrend_makesocknonblock(config->listens->sock);
  if(tmp)
    {
      free(*clients);
      return 1;
    }

  return 0;
}

int distrend_handleread(struct distrend_config *config,
		    struct distrend_client *client)
{
  struct distrend_packet *packet;
  ssize_t readlen;
  char buf[8192];

  packet = malloc(sizeof(struct distrend_packet));
  if(!packet)
    {
      fprintf(stderr, "OOM\n");
      return 1;
    }
  packet->len = 0;
  packet->data = NULL;

  readlen = read(client->sock, buf, sizeof(buf));
  if(readlen == -1)
    {
      perror("read");
      switch(errno)
	{
	case EINTR:
	case EAGAIN:
	  break;

	default:
	  client->state = DISTREND_CLIENT_DEAD;

	  free(packet);
	  return 1;
	}
    }
  if(readlen == 0)
    /* handle EOF */
    {
      client->state = DISTREND_CLIENT_DEAD;

      free(packet);
      return 1;
    }

  packet->len = readlen;
  packet->data = malloc(readlen);
  if(!packet->data)
    {
      fprintf(stderr, "OOM!\n");

      free(packet);
      return 1;
    }
  memcpy(packet->data, buf, readlen);

  return 0;
}

struct distrend_accept_fdset_prepare_data
{
  fd_set readfds;
  fd_set writefds;
  /**
     stores the highest FD number which is one less than the first
   arfgument to select(). So do select(maxfd + 1, ...)
  */
  int maxfd;
};
int distrend_accept_fdset_prepare(struct distrend_accept_fdset_prepare_data *data,
				  struct distrend_client *client)
{
  if(client->state == DISTREND_CLIENT_DEAD)
    return TRUE;

  if(client->sock > data->maxfd)
    data->maxfd = client->sock;
  FD_SET(client->sock, &data->readfds);
  /**
     Only select on an outgoing socket if we have something to
     say.
   */
  if(!q_empty(client->outmsgs))
    FD_SET(client->sock, &data->writefds);
  
  return TRUE;
}

struct distrend_accept_client_proc_data
{
  fd_set *fdset;
  struct distrend_config *config;
  enum {DISTREND_ACCEPT_CLIENT_READ, DISTREND_ACCEPT_CLIENT_WRITE} mode;
};
int distrend_accept_client_proc(struct distrend_accept_client_proc_data *data,
				struct distrend_client *client)
{
  struct distrend_packet *packet;
  ssize_t written_amount;

  if(client->state == DISTREND_CLIENT_DEAD)
    return TRUE;

  if(!FD_ISSET(client->sock, data->fdset))
    /** continue iteration through the list */
    return TRUE;

  fprintf(stderr, "%s:%d: My traversal says that sock %d is available for %sing\n",
	  __FILE__, __LINE__, client->sock,
	  (data->mode == DISTREND_ACCEPT_CLIENT_READ) ? "read" : "write");

  switch(data->mode)
    {
    case DISTREND_ACCEPT_CLIENT_WRITE:
      if(q_empty(client->outmsgs))
	return TRUE;

      packet = q_front(client->outmsgs);
      written_amount = write(client->sock, packet->data, packet->len);
      /**
	 Disconnect in case of write error.
       */
      if(written_amount == -1)
	{
	  perror("write");
	  /*
	    distrend_client_free();

	    Until liblist has the ability to delete nodes
	    during traversal, we'll have to free the client
	    during a client-list cleanup/pruning cycle.
	  */
	  client->state = DISTREND_CLIENT_DEAD;
	}
      if(packet->len == written_amount)
	{
	  q_dequeue(client->outmsgs);
	  distrend_packet_free(packet);
	}
      else
	{
	  /**
	     shifting seems the simplest solution.
	   */
	  packet->len -= written_amount;
	}
      break;

    case DISTREND_ACCEPT_CLIENT_READ:
      distrend_handleread(data->config, client);
      break;
    }

  /** continue iteration through the list */
  return TRUE;
}

int distrend_accept(struct distrend_config *config, struct distrend_clientset *clients)
{
  int tmp;
  int newclientsock;

  struct distrend_accept_fdset_prepare_data fdsets;
  struct distrend_accept_client_proc_data travinfo;
  struct distrend_client *newclient;

  FD_ZERO(&fdsets.readfds);
  FD_ZERO(&fdsets.writefds);
  FD_SET(config->listens->sock, &fdsets.readfds);
  fdsets.maxfd = config->listens->sock;

  list_traverse(clients->clients, &fdsets, (list_traverse_func_t)&distrend_accept_fdset_prepare, LIST_FRNT | LIST_ALTR);

  fprintf(stderr, "select()...");
  tmp = select(fdsets.maxfd + 1, &fdsets.readfds, &fdsets.writefds, NULL, (struct timeval *)NULL);
  if(tmp == -1)
    {
      perror("select");

      return 1;
    }

  /**
     Deal first with data waiting to be sent and then
     with input data.
   */
  travinfo.config = config;

  travinfo.fdset = &fdsets.writefds;
  travinfo.mode = DISTREND_ACCEPT_CLIENT_WRITE;
  list_traverse(clients->clients, &travinfo, (list_traverse_func_t)&distrend_accept_client_proc, LIST_FRNT | LIST_ALTR);

  travinfo.fdset = &fdsets.readfds;
  travinfo.mode = DISTREND_ACCEPT_CLIENT_READ;
  list_traverse(clients->clients, &travinfo, (list_traverse_func_t)&distrend_accept_client_proc, LIST_FRNT | LIST_ALTR);

  /**
     Handle new connections.
   */
  if(FD_ISSET(config->listens->sock, &fdsets.readfds))
    {
      newclientsock = accept(config->listens->sock, (struct sockaddr *)NULL, (socklen_t *)NULL);
      if(distrend_client_new(&newclient, newclientsock, DISTREND_CLIENT_PREAUTH))
	{
	  fprintf(stderr, "error allocating client struct\n");
	  return 1;
	}
      clients->nfds ++;
      list_insert_after(clients->clients, newclient, 0);
      fprintf(stderr, "accepted new connection\n");
      distrend_client_write(newclient, "hello\n", 6);
    }

  /**
     Until liblist supports deleting nodes during a traversal,
     we clean dead clients here:
   */
  list_mvfront(clients->clients);
  newclient = list_curr(clients->clients);
  while(newclient)
    {
      if(newclient->state == DISTREND_CLIENT_DEAD)
	{
	  distrend_client_free(newclient);
	  list_remove_curr(clients->clients);
	  fprintf(stderr, "removed dead connection\n");
	}
      list_mvnext(clients->clients);
      /* provide for termination of this loop */
      if(newclient == list_curr(clients->clients))
	newclient = NULL;
      else
	newclient = list_curr(clients->clients);
    }

  return 0;
}

int distrend_unlisten(struct distrend_listen *listens, struct distrend_clientset *clients)
{
  fprintf(stderr, "%s:%d: I am a stub that needn't be implemented 'til later\n", __FILE__, __LINE__);

  return 1;
}
/**
   This is probably just NOT a placeholder for remotio
*/
void remotio_send_to_client(struct distrend_client *client, const char *msg, size_t len)
{
    fprintf(stderr, "%s:%d: STUB I should queue data for writing to a client\n", __FILE__, __LINE__);
}

int distrend_client_new(struct distrend_client **client, int sock, enum distrend_client_state state)
{
  int tmp;

  tmp = distrend_makesocknonblock(sock);
  if(tmp)
    {
      fprintf(stderr, "Not accepting this connection because I cannot make the sock non-blocking\n");
      return 1;
    }

  *client = malloc(sizeof(struct distrend_client));
  if(!*client)
    {
      fprintf(stderr, "OOM\n");
      return 1;
    }
  (*client)->sock = sock;
  (*client)->state = state;
  (*client)->inmsgs = q_init();
  (*client)->outmsgs = q_init();

  return 0;
}

int distrend_client_free(struct distrend_client *client)
{
  q_free(client->inmsgs, (list_dealloc_func_t)distrend_packet_free);
  q_free(client->outmsgs, (list_dealloc_func_t)distrend_packet_free);

  close(client->sock);
  
  return 0;
}

void distrend_packet_free(struct distrend_packet *packet)
{
  free(packet->data);
  free(packet); 
}

int distrend_makesocknonblock(int sock)
{
  int fdstatus;
  int tmp;

  fdstatus = fcntl(sock, F_GETFL);
  fdstatus |= O_NONBLOCK;
  tmp = fcntl(sock, F_SETFL, fdstatus);
  if(tmp == -1)
    {
      perror("fcntl");
      return 1;
    }
  
  return 0;
}


int distrend_client_write(struct distrend_client *client, char *towrite, size_t msglen)
{
  struct distrend_packet *packet;

  packet = malloc(sizeof(struct distrend_packet));
  if(!packet)
    {
      fprintf(stderr, "OOM\n");
      return 1;
    }

  packet->len = msglen;
  packet->data = malloc(msglen);
  if(!packet->data)
    {
      free(packet);
      fprintf(stderr, "OOM\n");
      return 1;
    }

  memcpy(packet->data, towrite, msglen);

  q_enqueue(client->outmsgs, packet, 0);

  return 0;
}

int distrend_client_read(struct distrend_client *client, char **toread, size_t *lenread)
{
  struct distrend_packet *packet;

  *lenread = 0;
  *toread = NULL;

  if(q_empty(client->inmsgs))
    return 1;

  packet = q_dequeue(client->inmsgs);
  *lenread = packet->len;
  *toread = packet->data;
  free(packet);

  return 0;
}