summaryrefslogtreecommitdiff
blob: bc18e95dfea567d7ec5842843711f338de3919a3 (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
/**** POSIX *******************************************************************/
/*
 * To get the POSIX 'getline' function.
 * We don't know what POSIX version is set by default.
 */
#define _POSIX_C_SOURCE 200809L

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

/**** RELABSD *****************************************************************/
#include <relabsd/debug.h>
#include <relabsd/server.h>

/******************************************************************************/
/**** LOCAL FUNCTIONS *********************************************************/
/******************************************************************************/
static void handle_input
(
   const ssize_t input_size __attribute__((unused)),
   const char input [const static 1] __attribute__((unused)),
   struct relabsd_server server [const static 1] __attribute__((unused))
)
{
}

/******************************************************************************/
/**** EXPORTED FUNCTIONS ******************************************************/
/******************************************************************************/
int relabsd_server_handle_client
(
   const int socket,
   struct relabsd_server server [const static 1]
)
{
   FILE * socket_as_file;
   /* FIXME: reallocating at every new connection is kind of wasteful. */
   char * input;
   ssize_t input_size;
   size_t input_buffer_size;

   errno = 0;
   socket_as_file = fdopen(socket, "r");

   if (socket_as_file == ((FILE *) NULL))
   {
      RELABSD_ERROR
      (
         "Unable to open client socket as a FILE: %s.",
         strerror(errno)
      );

      (void) close(socket);

      return -1;
   }

   errno = 0;

   input_size = getline(&input, &input_buffer_size, socket_as_file);

   if (input_size < 1)
   {
      RELABSD_ERROR
      (
         "Unable to read line from client socket: %s.",
         strerror(errno)
      );

      (void) free((void *) input);

      /* This also closes 'socket' */
      (void) fclose(socket_as_file);

      return -1;
   }

   handle_input(input_size, input, server);

   (void) free((void *) input);

   /* This also closes 'socket' */
   (void) fclose(socket_as_file);

   return 0;
}