summaryrefslogtreecommitdiff
blob: edafd4f9f1cbbad378830e121a9e7ef8ab4f6edd (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
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

/* "POSIX.1  does not require the inclusion of <sys/types.h>" */
/* - man page for setsockopt */
/* #include <sys/types.h> */
#include <sys/socket.h>
#include <sys/time.h>

#include "error.h"

#include "network.h"

static int re_create_socket (struct ZoO_network net [const restrict static 1])
{
   struct timeval timeout;
   const int old_errno = errno;

   errno = 0;
   timeout.tv_sec = ZoO_NETWORK_TIMEOUT;
   timeout.tv_usec = 0;

   if (net->connection != -1)
   {
      close(net->connection);
   }

   net->connection =
      socket
      (
         net->addrinfo->ai_family,
         net->addrinfo->ai_socktype,
         net->addrinfo->ai_protocol
      );

   if (net->connection == -1)
   {
      ZoO_ERROR("Could not create socket: %s.", strerror(errno));

      goto RETURN_FAILED;
   }

   if
   (
      (
         setsockopt
         (
            net->connection,
            SOL_SOCKET,
            SO_RCVTIMEO,
            (const void *) &timeout,
            (socklen_t) sizeof(struct timeval)
         ) < 0
      )
      ||
      (
         setsockopt
         (
            net->connection,
            SOL_SOCKET,
            SO_SNDTIMEO,
            (const void *) &timeout,
            (socklen_t) sizeof(struct timeval)
         ) < 0
      )
   )
   {
      ZoO_ERROR("Could not set timeout on network socket: %s", strerror(errno));

      goto RETURN_FAILED;
   }

   ZoO_S_DEBUG(ZoO_DEBUG_NETWORK, "(Re)connecting to network...");

   if
   (
      connect
      (
         net->connection,
         net->addrinfo->ai_addr,
         net->addrinfo->ai_addrlen
      ) != 0
   )
   {
      ZoO_ERROR("Could not establish connection: %s", strerror(errno));

      goto RETURN_FAILED;
   }

   errno = old_errno;

   return 0;

RETURN_FAILED:
   errno = old_errno;

   return -1;
}

static int reconnect (struct ZoO_network net [const restrict static 1])
{
   const int old_errno = errno;

   memset(net->in, 0, (sizeof(ZoO_char) * 513));
   memset(net->out, 0, (sizeof(ZoO_char) * 513));
   memset(net->buffer, 0, (sizeof(ZoO_char) * 513));

   net->buffer_index = 0;
   net->buffer_remaining = 0;

   if (re_create_socket(net) < 0)
   {
      return -1;
   }

   snprintf(net->out, 512, "USER %s 8 * :%s\r\n", net->user, net->name);

   if (write(net->connection, net->out, strlen(net->out)) < 1)
   {
      goto RETURN_WRITE_FAILED;
   }

   snprintf(net->out, 512, "NICK %s\r\n", net->nick);

   if (write(net->connection, net->out, strlen(net->out)) < 1)
   {
      goto RETURN_WRITE_FAILED;
   }

   net->buffer_remaining = 0;
   net->buffer_index = 0;

   ZoO_S_DEBUG(ZoO_DEBUG_NETWORK, "(Re)connected.");

   errno = old_errno;

   return 0;

RETURN_WRITE_FAILED:
   ZoO_ERROR
   (
      "Unable to write to the network: %s",
      strerror(errno)
   );

   errno = old_errno;

   return -1;
}

int ZoO_network_connect
(
   struct ZoO_network net [const static 1],
   const char host [const restrict static 1],
   const char port [const restrict static 1],
   const char channel [const restrict static 1],
   const char user [const restrict static 1],
   const char name [const restrict static 1],
   const char nick [const restrict static 1]
)
{
   int error;
   struct addrinfo hints;
   const int old_errno = errno;

   net->connection = -1;
   net->channel = channel;
   net->user = user;
   net->name = name;
   net->nick = nick;
   net->buffer_index = 0;
   net->buffer_remaining = 0;

   memset(&hints, 0, sizeof(struct addrinfo));
   memset(net->in, 0, (sizeof(ZoO_char) * 513));
   memset(net->out, 0, (sizeof(ZoO_char) * 513));
   memset(net->buffer, 0, (sizeof(ZoO_char) * 513));

   hints.ai_family = AF_INET;
   hints.ai_socktype = SOCK_STREAM;

   errno = 0;

   error = getaddrinfo(host, port, &hints, &(net->addrinfo));

   if (error != 0)
   {
      if (error == EAI_SYSTEM)
      {
         ZoO_ERROR
         (
            "Could not retrieve server information: %s.",
            strerror(errno)
         );
      }
      else
      {
         ZoO_FATAL
         (
            "Could not retrieve server information: %s.",
            gai_strerror(error)
         );
      }

      errno = old_errno;

      return -1;
   }

   errno = old_errno;

   reconnect(net);

   return 0;
}

static void buffer_msg
(
   struct ZoO_network net [const static 1]
)
{
   ssize_t in_count, i;

   if (net->buffer_remaining > 0)
   {
      in_count = net->buffer_remaining;
      net->buffer_remaining = 0;

      goto PARSE_READ;
   }

READ_MORE:
   in_count = read(net->connection, net->buffer, 512);

   if (in_count <= 0)
   {
      ZoO_ERROR("Could not read from network: %s", strerror(errno));

      while (reconnect(net) < 0)
      {
         ZoO_S_DEBUG
         (
            ZoO_DEBUG_NETWORK,
            "Attempting new connection in 5s."
         );
         sleep(5);
      }

      goto READ_MORE;
   }

PARSE_READ:
   for (i = 0; i < in_count; ++i)
   {
      net->in[net->buffer_index] = net->buffer[i];

      if
      (
         (net->buffer_index > 0)
         && (net->in[net->buffer_index - 1] == '\r')
         && (net->in[net->buffer_index] == '\n')
      )
      {
         net->buffer_remaining = (in_count - (i + 1));
         net->in_length = (net->buffer_index - 1);
         net->buffer_index = 0;

         if (net->buffer_remaining > 0)
         {
            memmove
            (
               (void *) net->buffer,
               (const void *) (net->buffer + (i + 1)),
               net->buffer_remaining
            );
         }

         return;
      }

      net->buffer_index += 1;

      if (net->buffer_index > 512)
      {
         ZoO_S_WARNING("Incoming message is too long. Discarded.");

         net->buffer_index = 0;
         net->buffer_remaining = 0;

         break;
      }
   }

   goto READ_MORE;
}

void handle_ping (struct ZoO_network net [const restrict static 1])
{
   const int old_errno = errno;

   #if ZoO_RANDOMLY_IGNORE_PING == 1
   if ((rand() % 10) < 3)
   {
      ZoO_S_DEBUG(ZoO_DEBUG_NETWORK, "Ping request ignored.");

      return;
   }

   #endif

   #if ZoO_DEBUG_NETWORK_PING == 1
      net->in[net->in_length] = '\0';

      ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->in] %s", net->in);

      net->in[net->in_length] = '\r';
   #endif

   net->in[1] = 'O';

   errno = 0;

   if (write(net->connection, net->in, (net->in_length + 2)) < 1)
   {
      ZoO_ERROR("Could not reply to PING request: %s", strerror(errno));

      errno = old_errno;

      while (reconnect(net) < 0)
      {
         ZoO_S_DEBUG
         (
            ZoO_DEBUG_NETWORK,
            "Attempting new connection in 5s."
         );
         sleep(5);
      }

      return;
   }

   errno = old_errno;

#if ZoO_DEBUG_NETWORK_PING == 1
   net->in[net->in_length] = '\0';

   ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->in);
#endif

}

int ZoO_network_receive
(
   struct ZoO_network net [const restrict static 1],
   size_t msg_offset [const restrict static 1],
   size_t msg_size [const restrict static 1],
   enum ZoO_msg_type type [const restrict static 1]
)
{
   const int old_errno = errno;
   ssize_t cmd, i;

READ_NEW_MSG:
   buffer_msg(net);

   net->in[net->in_length + 2] = '\0';

   /* XXX: doesn't that prevent net [restrict]? */
   if (ZoO_IS_PREFIX("PING", net->in))
   {

      handle_ping(net);

      goto READ_NEW_MSG;
   }

   if (net->in_length == 0)
   {
      goto READ_NEW_MSG;
   }

   net->in[net->in_length] = '\0';

   ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->in] %s", net->in);

   if (net->in[0] == ':')
   {
      cmd = 0;

      for (i = 1; i < 512; i++)
      {
         if (net->in[i] == ' ')
         {
            cmd = (i + 1);

            break;
         }
      }

      if (ZoO_IS_PREFIX("001", (net->in + cmd)))
      {
         snprintf
         (
            net->out,
            512,
            "JOIN :%s\r\n",
            net->channel
         );

         errno = 0;

         if (write(net->connection, net->out, strlen(net->out)) < 1)
         {
            ZoO_ERROR
            (
               "Could not send JOIN request: %s",
               strerror(errno)
            );

            errno = old_errno;

            if (reconnect(net) < 0)
            {
               return -1;
            }
         }

         errno = old_errno;

         ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->out);

         goto READ_NEW_MSG;
      }

      if (ZoO_IS_PREFIX("JOIN", (net->in + cmd)))
      {
         for (i = 1; (i < 512) && (net->in[i] != '!'); ++i)
         {
         }

         if ((i == 512) || (i == 1))
         {
            ZoO_ERROR("Could not find JOIN username: %s", net->in);

            goto READ_NEW_MSG;
         }

         *msg_offset = 1;
         *msg_size = (i - 1);
         net->in[i] = '\0';

         *type = ZoO_JOIN;

         return 0;
      }

      if (ZoO_IS_PREFIX("PRIVMSG", (net->in + cmd)))
      {

         for (; i < 512; i++)
         {
            if (net->in[i] == ':')
            {
               cmd = (i + 1);

               break;
            }
         }

         *msg_offset = cmd;
         *msg_size = (net->in_length - cmd);

         /*net->in[*msg_size - 1] = '\0'; */

         *type = ZoO_PRIVMSG;

         return 0;
      }
   }

   if (ZoO_IS_PREFIX("ERROR", (net->in + cmd)))
   {
      while (reconnect(net) < 0)
      {
         ZoO_S_DEBUG
         (
            ZoO_DEBUG_NETWORK,
            "Attempting new connection in 5s."
         );
         sleep(5);
      }
   }

   goto READ_NEW_MSG;
}

int ZoO_network_send (struct ZoO_network net [const restrict static 1])
{
   int const old_errno = errno;

   if (ZoO_IS_PREFIX("\001action", net->out))
   {

      net->out[1] = 'A';
      net->out[2] = 'C';
      net->out[3] = 'T';
      net->out[4] = 'I';
      net->out[5] = 'O';
      net->out[6] = 'N';

      snprintf
      (
         net->in,
         512,
         "PRIVMSG %s :%s\001\r\n",
         net->channel,
         net->out
      );
   }
   else
   {
      snprintf
      (
         net->in,
         512,
         "PRIVMSG %s :%s\r\n",
         net->channel,
         net->out
      );
   }

   errno = 0;

   if (write(net->connection, net->in, strlen(net->in)) < 1)
   {
      ZoO_ERROR
      (
         "Could not send PRIVMSG: %s.",
         strerror(errno)
      );

      errno = old_errno;

      if (reconnect(net) < 0)
      {
         return -2;
      }
      else
      {
         return -1;
      }
   }

   errno = old_errno;

   ZoO_DEBUG(ZoO_DEBUG_NETWORK, "[NET->out] %s", net->in);

   return 0;
}

void ZoO_network_disconnect (struct ZoO_network net [const restrict static 1])
{
   freeaddrinfo(net->addrinfo);
   close(net->connection);
}