aboutsummaryrefslogtreecommitdiff
blob: 5e0c00b36c797cb9481bd8348e3d40ba299f708a (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
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

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

#include "../error/error.h"

#include "server.h"

static int create_socket (int result [const restrict static 1])
{
   const int old_errno = errno;

   errno = 0;
   *result = socket(AF_UNIX, SOCK_STREAM, 0);

   if (*result == -1)
   {
      JH_FATAL
      (
         stderr,
         "Unable to create server socket: %s.",
         strerror(errno)
      );

      errno = old_errno;

      return -1;
   }

   errno = old_errno;

   return 0;
}

static int bind_socket
(
   const int socket,
   const char socket_name [const restrict static 1]
)
{
   struct sockaddr_un addr;
   const int old_errno = errno;

   errno = 0;
   memset(&addr, 0, sizeof(struct sockaddr_un));

   addr.sun_family = AF_UNIX;

   strncpy
   (
      (void *) addr.sun_path,
      (const void *) socket_name,
      (sizeof(addr.sun_path) - 1)
   );

   errno = old_errno;

   if
   (
      bind
      (
         socket,
         (const struct sockaddr *) &addr,
         (socklen_t) sizeof(struct sockaddr_un)
      ) != 0
   )
   {
      JH_FATAL
      (
         stderr,
         "Unable to bind server socket to %s: %s.",
         socket_name,
         strerror(errno)
      );

      errno = old_errno;

      return -1;
   }

   errno = old_errno;

   return 0;
}

static int set_socket_to_unblocking (const int socket)
{
   int current_flags;
   const int old_errno = errno;

   current_flags = fcntl(socket, F_GETFD);

   if (current_flags == -1)
   {
      JH_FATAL
      (
         stderr,
         "Unable to get server socket properties: %s.",
         strerror(errno)
      );

      errno = old_errno;

      return -1;
   }

   /* current_flags = current_flags & (~O_NONBLOCK); */

   current_flags = fcntl(socket, F_SETFD, (current_flags | O_NONBLOCK));

   if (current_flags == -1)
   {
      JH_FATAL
      (
         stderr,
         "Unable to set server socket properties: %s.",
         strerror(errno)
      );

      errno = old_errno;

      return -2;
   }

   errno = old_errno;

   return 0;
}

static int set_socket_as_listener (const int socket)
{
   const int old_errno = errno;

   if (listen(socket, JH_SERVER_SOCKET_LISTEN_BACKLOG) != 0)
   {
      JH_FATAL
      (
         stderr,
         "Unable to set server socket properties: %s.",
         strerror(errno)
      );

      errno = old_errno;

      return -1;
   }

   errno = old_errno;

   return 0;
}

int JH_server_socket_open
(
   struct JH_server_socket server_socket [const restrict static 1],
   const char socket_name [const restrict static 1]
)
{
   printf("\"%s\"\n", socket_name);
   if (create_socket(&(server_socket->file_descriptor)) < 0)
   {
      return -1;
   }

   if (bind_socket(server_socket->file_descriptor, socket_name) < 0)
   {
      close(server_socket->file_descriptor);

      return -1;
   }

   if (set_socket_to_unblocking(server_socket->file_descriptor) < 0)
   {
      close(server_socket->file_descriptor);

      return -1;
   }

   if (set_socket_as_listener(server_socket->file_descriptor) < 0)
   {
      close(server_socket->file_descriptor);

      return -1;
   }

   FD_ZERO(&(server_socket->as_a_set));
   FD_SET(server_socket->file_descriptor, &(server_socket->as_a_set));

   return 0;
}