aboutsummaryrefslogtreecommitdiffstats
path: root/src/communication.c
blob: 9ede4014ecba7c82819144c8adaa31623a435c3b (plain) (blame)
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
/**
 * coopgammad -- Cooperative gamma server
 * Copyright (C) 2016  Mattias Andrée (maandree@kth.se)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "communication.h"
#include "state.h"
#include "servers/coopgamma.h"

#include <sys/socket.h>
#include <errno.h>
#include <string.h>



/**
 * Send a message
 * 
 * @param   conn  The index of the connection
 * @param   buf   The data to send
 * @param   n     The size of `buf`
 * @return        Zero on success, -1 on error, 1 if disconncted
 *                EINTR, EAGAIN, EWOULDBLOCK, and ECONNRESET count
 *                as success (ECONNRESET cause 1 to be returned),
 *                and are handled appropriately.
 */
int send_message(size_t conn, char* restrict buf, size_t n)
{
  struct ring* restrict ring = outbound + conn;
  int fd = connections[conn];
  int saved_errno;
  size_t ptr = 0;
  ssize_t sent;
  size_t chunksize = n;
  size_t sendsize;
  size_t old_n;
  char* old_buf;
    
  while ((old_buf = ring_peek(ring, &old_n)))
    {
      size_t old_ptr = 0;
      while (old_ptr < n)
	{
	  sendsize = old_n - old_ptr < chunksize ? old_n - old_ptr : chunksize;
	  sent = send(fd, old_buf + old_ptr, sendsize, MSG_NOSIGNAL);
	  if (sent < 0)
	    {
	      if (errno == EPIPE)
		errno = ECONNRESET;
	      if (errno != EMSGSIZE)
		goto fail;
	      chunksize >>= 1;
	      if (chunksize == 0)
		goto fail;
	      continue;
	    }
	  old_ptr += (size_t)sent;
	  ring_pop(ring, (size_t)sent);
	}
    }
  
  while (ptr < n)
    {
      sendsize = n - ptr < chunksize ? n - ptr : chunksize;
      sent = send(fd, buf + ptr, sendsize, MSG_NOSIGNAL);
      if (sent < 0)
	{
	  if (errno == EPIPE)
	    errno = ECONNRESET;
	  if (errno != EMSGSIZE)
	    goto fail;
	  chunksize >>= 1;
	  if (chunksize == 0)
	    goto fail;
	  continue;
	}
      ptr += (size_t)sent;
    }
  
  free(buf);
  return 0;
  
 fail:
  switch (errno)
    {
    case EINTR:
    case EAGAIN:
#if EAGAIN != EWOULDBLOCK
    case EWOULDBLOCK:
#endif
      if (ring_push(ring, buf + ptr, n - ptr) < 0)
	goto proper_fail;
      free(buf);
      return 0;
    case ECONNRESET:
      free(buf);
      if (connection_closed(fd) < 0)
	return -1;
      return 1;
    default:
      break;
    }
 proper_fail:
  saved_errno = errno;
  free(buf);
  errno = saved_errno;
  return -1;
}


/**
 * Send a custom error without an error number
 * 
 * @param   conn        The index of the connection
 * @param   message_id  The ID of the message to which this message is a response
 * @param   desc        The error description to send
 * @return              1: Client disconnected
 *                      0: Success (possibily delayed)
 *                      -1: An error occurred
 */
int (send_error)(size_t conn, const char* restrict message_id, const char* restrict desc)
{
  char* restrict buf;
  size_t n;
  
  MAKE_MESSAGE(&buf, &n, 0,
	       "Command: error\n"
	       "In response to: %s\n"
	       "Error: custom\n"
	       "Length: %zu\n"
	       "\n"
	       "%s\n",
	       message_id, strlen(desc) + 1, desc);
  
  return send_message(conn, buf, n);
}


/**
 * Send a standard error
 * 
 * @param   conn        The index of the connection
 * @param   message_id  The ID of the message to which this message is a response
 * @param   number      The value of `errno`, 0 to indicate success
 * @return              1: Client disconnected
 *                      0: Success (possibily delayed)
 *                      -1: An error occurred
 */
int (send_errno)(size_t conn, const char* restrict message_id, int number)
{
  char* restrict buf;
  size_t n;
  
  MAKE_MESSAGE(&buf, &n, 0,
	       "Command: error\n"
	       "In response to: %s\n"
	       "Error: %i\n"
	       "\n",
	       message_id, number);
  
  return send_message(conn, buf, n);
}