add capability to read UDP datagrams from the socket and pass them to the parser
[deb_libnfs.git] / lib / socket.c
CommitLineData
84004dbf
RS
1/*
2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <stdio.h>
98f5fee8 19#include <stdlib.h>
84004dbf
RS
20#include <unistd.h>
21#include <fcntl.h>
22#include <poll.h>
23#include <string.h>
24#include <errno.h>
98f5fee8 25#include <rpc/rpc.h>
84004dbf
RS
26#include <rpc/xdr.h>
27#include <arpa/inet.h>
28#include <sys/ioctl.h>
29#include "libnfs.h"
30#include "libnfs-raw.h"
31#include "libnfs-private.h"
32#include "slist.h"
33
34static void set_nonblocking(int fd)
35{
36 unsigned v;
37 v = fcntl(fd, F_GETFL, 0);
38 fcntl(fd, F_SETFL, v | O_NONBLOCK);
39}
40
41int rpc_get_fd(struct rpc_context *rpc)
42{
43 return rpc->fd;
44}
45
46int rpc_which_events(struct rpc_context *rpc)
47{
912f7ad5 48 int events = rpc->is_connected ? POLLIN : POLLOUT;
84004dbf 49
5911f3e8
RS
50 if (rpc->is_udp != 0) {
51 /* for udp sockets we only wait for pollin */
52 return POLLIN;
53 }
54
84004dbf
RS
55 if (rpc->outqueue) {
56 events |= POLLOUT;
57 }
58 return events;
59}
60
61static int rpc_write_to_socket(struct rpc_context *rpc)
62{
63 ssize_t count;
64
65 if (rpc == NULL) {
84004dbf
RS
66 return -1;
67 }
68 if (rpc->fd == -1) {
1896d37b
RS
69 rpc_set_error(rpc, "trying to write but not connected");
70 return -1;
84004dbf
RS
71 }
72
73 while (rpc->outqueue != NULL) {
74 ssize_t total;
75
76 total = rpc->outqueue->outdata.size;
77
78 count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written);
79 if (count == -1) {
80 if (errno == EAGAIN || errno == EWOULDBLOCK) {
84004dbf
RS
81 return 0;
82 }
1896d37b
RS
83 rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno);
84 return -1;
84004dbf
RS
85 }
86
87 rpc->outqueue->written += count;
88 if (rpc->outqueue->written == total) {
89 struct rpc_pdu *pdu = rpc->outqueue;
90
91 SLIST_REMOVE(&rpc->outqueue, pdu);
92 SLIST_ADD_END(&rpc->waitpdu, pdu);
93 }
94 }
95 return 0;
96}
97
98static int rpc_read_from_socket(struct rpc_context *rpc)
99{
100 int available;
101 int size;
cdb19ec1 102 int pdu_size;
84004dbf
RS
103 ssize_t count;
104
105 if (ioctl(rpc->fd, FIONREAD, &available) != 0) {
106 rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno);
107 return -1;
108 }
109 if (available == 0) {
110 rpc_set_error(rpc, "Socket has been closed");
1896d37b 111 return -1;
84004dbf 112 }
cdb19ec1 113
0268794f
RS
114 if (rpc->is_udp) {
115 char *buf;
116 socklen_t socklen = sizeof(rpc->udp_src);
117
118 buf = malloc(available);
119 if (buf == NULL) {
120 rpc_set_error(rpc, "Failed to malloc buffer for recvfrom");
121 return -1;
122 }
123 count = recvfrom(rpc->fd, buf, available, MSG_DONTWAIT, (struct sockaddr *)&rpc->udp_src, &socklen);
124 if (count < 0) {
125 rpc_set_error(rpc, "Failed recvfrom: %s", strerror(errno));
126 free(buf);
127 }
128 if (rpc_process_pdu(rpc, buf, count) != 0) {
129 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Ignoring PDU");
130 free(buf);
131 return -1;
132 }
133 free(buf);
134 return 0;
135 }
136
cdb19ec1
RS
137 /* read record marker, 4 bytes at the beginning of every pdu */
138 if (rpc->inbuf == NULL) {
139 rpc->insize = 4;
140 rpc->inbuf = malloc(rpc->insize);
141 if (rpc->inbuf == NULL) {
142 rpc_set_error(rpc, "Failed to allocate buffer for record marker, errno:%d. Closing socket.", errno);
143 return -1;
144 }
145 }
146 if (rpc->inpos < 4) {
147 size = 4 - rpc->inpos;
148
149 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
150 if (count == -1) {
151 if (errno == EINTR) {
152 return 0;
153 }
154 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
155 return -1;
156 }
157 available -= count;
158 rpc->inpos += count;
159 }
160
161 if (available == 0) {
162 return 0;
163 }
164
165 pdu_size = rpc_get_pdu_size(rpc->inbuf);
166 if (rpc->insize < pdu_size) {
167 unsigned char *buf;
168
169 buf = malloc(pdu_size);
170 if (buf == NULL) {
171 rpc_set_error(rpc, "Failed to allocate buffer of %d bytes for pdu, errno:%d. Closing socket.", pdu_size, errno);
172 return -1;
173 }
174 memcpy(buf, rpc->inbuf, rpc->insize);
175 free(rpc->inbuf);
176 rpc->inbuf = buf;
177 rpc->insize = rpc_get_pdu_size(rpc->inbuf);
84004dbf 178 }
cdb19ec1
RS
179
180 size = available;
181 if (size > rpc->insize - rpc->inpos) {
182 size = rpc->insize - rpc->inpos;
84004dbf
RS
183 }
184
cdb19ec1 185 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
84004dbf
RS
186 if (count == -1) {
187 if (errno == EINTR) {
84004dbf
RS
188 return 0;
189 }
190 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
1896d37b 191 return -1;
84004dbf 192 }
cdb19ec1
RS
193 available -= count;
194 rpc->inpos += count;
84004dbf 195
cdb19ec1
RS
196 if (rpc->inpos == rpc->insize) {
197 if (rpc_process_pdu(rpc, rpc->inbuf, pdu_size) != 0) {
84004dbf 198 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket");
1896d37b 199 return -1;
84004dbf 200 }
cdb19ec1
RS
201 free(rpc->inbuf);
202 rpc->inbuf = NULL;
203 rpc->insize = 0;
204 rpc->inpos = 0;
84004dbf 205 }
cdb19ec1 206
84004dbf
RS
207 return 0;
208}
209
210
211
212int rpc_service(struct rpc_context *rpc, int revents)
213{
214 if (revents & POLLERR) {
912f7ad5
RS
215 int err = 0;
216 socklen_t err_size = sizeof(err);
217
218 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
219 &err, &err_size) != 0 || err != 0) {
220 if (err == 0) {
221 err = errno;
222 }
223 rpc_set_error(rpc, "rpc_service: socket error "
224 "%s(%d).",
225 strerror(err), err);
84004dbf 226 } else {
912f7ad5
RS
227 rpc_set_error(rpc, "rpc_service: POLLERR, "
228 "Unknown socket error.");
84004dbf
RS
229 }
230 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
231 return -1;
232 }
233 if (revents & POLLHUP) {
84004dbf
RS
234 rpc_set_error(rpc, "Socket failed with POLLHUP");
235 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
1896d37b 236 return -1;
84004dbf
RS
237 }
238
239 if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) {
912f7ad5
RS
240 int err = 0;
241 socklen_t err_size = sizeof(err);
242
243 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
244 &err, &err_size) != 0 || err != 0) {
245 if (err == 0) {
246 err = errno;
247 }
248 rpc_set_error(rpc, "rpc_service: socket error "
249 "%s(%d) while connecting.",
250 strerror(err), err);
251 rpc->connect_cb(rpc, RPC_STATUS_ERROR,
252 NULL, rpc->connect_data);
253 return -1;
254 }
255
84004dbf
RS
256 rpc->is_connected = 1;
257 rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data);
258 return 0;
259 }
260
261 if (revents & POLLOUT && rpc->outqueue != NULL) {
262 if (rpc_write_to_socket(rpc) != 0) {
1896d37b
RS
263 rpc_set_error(rpc, "write to socket failed");
264 return -1;
84004dbf
RS
265 }
266 }
267
268 if (revents & POLLIN) {
269 if (rpc_read_from_socket(rpc) != 0) {
270 rpc_disconnect(rpc, rpc_get_error(rpc));
1896d37b 271 return -1;
84004dbf
RS
272 }
273 }
274
275 return 0;
276}
277
278
279int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data)
280{
281 struct sockaddr_storage s;
282 struct sockaddr_in *sin = (struct sockaddr_in *)&s;
283 int socksize;
284
285 if (rpc->fd != -1) {
286 rpc_set_error(rpc, "Trying to connect while already connected");
84004dbf
RS
287 return -1;
288 }
289
290 sin->sin_family = AF_INET;
291 sin->sin_port = htons(port);
292 if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) {
293 rpc_set_error(rpc, "Not a valid server ip address");
1896d37b 294 return -1;
84004dbf
RS
295 }
296
297 switch (s.ss_family) {
298 case AF_INET:
84004dbf 299 socksize = sizeof(struct sockaddr_in);
ea214e45
RS
300#ifdef HAVE_SOCK_SIN_LEN
301 sin->sin_len = socksize;
302#endif
303 rpc->fd = socket(AF_INET, SOCK_STREAM, 0);
84004dbf
RS
304 break;
305 }
306
307 if (rpc->fd == -1) {
308 rpc_set_error(rpc, "Failed to open socket");
1896d37b 309 return -1;
84004dbf
RS
310 }
311
312 rpc->connect_cb = cb;
313 rpc->connect_data = private_data;
314
315 set_nonblocking(rpc->fd);
316
317 if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) {
318 rpc_set_error(rpc, "connect() to server failed");
1896d37b 319 return -1;
84004dbf
RS
320 }
321
322 return 0;
323}
324
325int rpc_disconnect(struct rpc_context *rpc, char *error)
326{
327 if (rpc->fd != -1) {
328 close(rpc->fd);
329 }
330 rpc->fd = -1;
331
332 rpc->is_connected = 0;
333
334 rpc_error_all_pdus(rpc, error);
335
336 return 0;
337}