Dont allow a tcp _connect to a UDP context
[deb_libnfs.git] / lib / socket.c
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>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <poll.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <rpc/rpc.h>
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
34 static 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
41 int rpc_get_fd(struct rpc_context *rpc)
42 {
43 return rpc->fd;
44 }
45
46 int rpc_which_events(struct rpc_context *rpc)
47 {
48 int events = rpc->is_connected ? POLLIN : POLLOUT;
49
50 if (rpc->is_udp != 0) {
51 /* for udp sockets we only wait for pollin */
52 return POLLIN;
53 }
54
55 if (rpc->outqueue) {
56 events |= POLLOUT;
57 }
58 return events;
59 }
60
61 static int rpc_write_to_socket(struct rpc_context *rpc)
62 {
63 ssize_t count;
64
65 if (rpc == NULL) {
66 return -1;
67 }
68 if (rpc->fd == -1) {
69 rpc_set_error(rpc, "trying to write but not connected");
70 return -1;
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) {
81 return 0;
82 }
83 rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno);
84 return -1;
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
98 static int rpc_read_from_socket(struct rpc_context *rpc)
99 {
100 int available;
101 int size;
102 int pdu_size;
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");
111 return -1;
112 }
113
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
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);
178 }
179
180 size = available;
181 if (size > rpc->insize - rpc->inpos) {
182 size = rpc->insize - rpc->inpos;
183 }
184
185 count = read(rpc->fd, rpc->inbuf + rpc->inpos, size);
186 if (count == -1) {
187 if (errno == EINTR) {
188 return 0;
189 }
190 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
191 return -1;
192 }
193 available -= count;
194 rpc->inpos += count;
195
196 if (rpc->inpos == rpc->insize) {
197 if (rpc_process_pdu(rpc, rpc->inbuf, pdu_size) != 0) {
198 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket");
199 return -1;
200 }
201 free(rpc->inbuf);
202 rpc->inbuf = NULL;
203 rpc->insize = 0;
204 rpc->inpos = 0;
205 }
206
207 return 0;
208 }
209
210
211
212 int rpc_service(struct rpc_context *rpc, int revents)
213 {
214 if (revents & POLLERR) {
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);
226 } else {
227 rpc_set_error(rpc, "rpc_service: POLLERR, "
228 "Unknown socket error.");
229 }
230 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
231 return -1;
232 }
233 if (revents & POLLHUP) {
234 rpc_set_error(rpc, "Socket failed with POLLHUP");
235 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
236 return -1;
237 }
238
239 if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) {
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
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) {
263 rpc_set_error(rpc, "write to socket failed");
264 return -1;
265 }
266 }
267
268 if (revents & POLLIN) {
269 if (rpc_read_from_socket(rpc) != 0) {
270 rpc_disconnect(rpc, rpc_get_error(rpc));
271 return -1;
272 }
273 }
274
275 return 0;
276 }
277
278
279 int 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");
287 return -1;
288 }
289
290 if (rpc->is_udp != 0) {
291 rpc_set_error(rpc, "Trying to connect on UDP socket");
292 return -1;
293 }
294
295 sin->sin_family = AF_INET;
296 sin->sin_port = htons(port);
297 if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) {
298 rpc_set_error(rpc, "Not a valid server ip address");
299 return -1;
300 }
301
302 switch (s.ss_family) {
303 case AF_INET:
304 socksize = sizeof(struct sockaddr_in);
305 #ifdef HAVE_SOCK_SIN_LEN
306 sin->sin_len = socksize;
307 #endif
308 rpc->fd = socket(AF_INET, SOCK_STREAM, 0);
309 break;
310 }
311
312 if (rpc->fd == -1) {
313 rpc_set_error(rpc, "Failed to open socket");
314 return -1;
315 }
316
317 rpc->connect_cb = cb;
318 rpc->connect_data = private_data;
319
320 set_nonblocking(rpc->fd);
321
322 if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) {
323 rpc_set_error(rpc, "connect() to server failed");
324 return -1;
325 }
326
327 return 0;
328 }
329
330 int rpc_disconnect(struct rpc_context *rpc, char *error)
331 {
332 if (rpc->fd != -1) {
333 close(rpc->fd);
334 }
335 rpc->fd = -1;
336
337 rpc->is_connected = 0;
338
339 rpc_error_all_pdus(rpc, error);
340
341 return 0;
342 }