Use HAVE_SOCK_SIN_LEN and set ->sin_len
[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 <unistd.h>
20 #include <fcntl.h>
21 #include <poll.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <rpc/xdr.h>
25 #include <arpa/inet.h>
26 #include <sys/ioctl.h>
27 #include "libnfs.h"
28 #include "libnfs-raw.h"
29 #include "libnfs-private.h"
30 #include "slist.h"
31
32 static void set_nonblocking(int fd)
33 {
34 unsigned v;
35 v = fcntl(fd, F_GETFL, 0);
36 fcntl(fd, F_SETFL, v | O_NONBLOCK);
37 }
38
39 int rpc_get_fd(struct rpc_context *rpc)
40 {
41 return rpc->fd;
42 }
43
44 int rpc_which_events(struct rpc_context *rpc)
45 {
46 int events = rpc->is_connected ? POLLIN : POLLOUT;
47
48 if (rpc->outqueue) {
49 events |= POLLOUT;
50 }
51 return events;
52 }
53
54 static int rpc_write_to_socket(struct rpc_context *rpc)
55 {
56 ssize_t count;
57
58 if (rpc == NULL) {
59 printf("trying to write to socket for NULL context\n");
60 return -1;
61 }
62 if (rpc->fd == -1) {
63 printf("trying to write but not connected\n");
64 return -2;
65 }
66
67 while (rpc->outqueue != NULL) {
68 ssize_t total;
69
70 total = rpc->outqueue->outdata.size;
71
72 count = write(rpc->fd, rpc->outqueue->outdata.data + rpc->outqueue->written, total - rpc->outqueue->written);
73 if (count == -1) {
74 if (errno == EAGAIN || errno == EWOULDBLOCK) {
75 printf("socket would block, return from write to socket\n");
76 return 0;
77 }
78 printf("Error when writing to socket :%s(%d)\n", strerror(errno), errno);
79 return -3;
80 }
81
82 rpc->outqueue->written += count;
83 if (rpc->outqueue->written == total) {
84 struct rpc_pdu *pdu = rpc->outqueue;
85
86 SLIST_REMOVE(&rpc->outqueue, pdu);
87 SLIST_ADD_END(&rpc->waitpdu, pdu);
88 }
89 }
90 return 0;
91 }
92
93 static int rpc_read_from_socket(struct rpc_context *rpc)
94 {
95 int available;
96 int size;
97 unsigned char *buf;
98 ssize_t count;
99
100 if (ioctl(rpc->fd, FIONREAD, &available) != 0) {
101 rpc_set_error(rpc, "Ioctl FIONREAD returned error : %d. Closing socket.", errno);
102 return -1;
103 }
104 if (available == 0) {
105 rpc_set_error(rpc, "Socket has been closed");
106 return -2;
107 }
108 size = rpc->insize - rpc->inpos + available;
109 buf = malloc(size);
110 if (buf == NULL) {
111 rpc_set_error(rpc, "Out of memory: failed to allocate %d bytes for input buffer. Closing socket.", size);
112 return -3;
113 }
114 if (rpc->insize > rpc->inpos) {
115 memcpy(buf, rpc->inbuf + rpc->inpos, rpc->insize - rpc->inpos);
116 rpc->insize -= rpc->inpos;
117 rpc->inpos = 0;
118 }
119
120 count = read(rpc->fd, buf + rpc->insize, available);
121 if (count == -1) {
122 if (errno == EINTR) {
123 free(buf);
124 buf = NULL;
125 return 0;
126 }
127 rpc_set_error(rpc, "Read from socket failed, errno:%d. Closing socket.", errno);
128 free(buf);
129 buf = NULL;
130 return -4;
131 }
132
133 if (rpc->inbuf != NULL) {
134 free(rpc->inbuf);
135 }
136 rpc->inbuf = (char *)buf;
137 rpc->insize += count;
138
139 while (1) {
140 if (rpc->insize - rpc->inpos < 4) {
141 return 0;
142 }
143 count = rpc_get_pdu_size(rpc->inbuf + rpc->inpos);
144 if (rpc->insize + rpc->inpos < count) {
145 return 0;
146 }
147 if (rpc_process_pdu(rpc, rpc->inbuf + rpc->inpos, count) != 0) {
148 rpc_set_error(rpc, "Invalid/garbage pdu received from server. Closing socket");
149 return -5;
150 }
151 rpc->inpos += count;
152 if (rpc->inpos == rpc->insize) {
153 free(rpc->inbuf);
154 rpc->inbuf = NULL;
155 rpc->insize = 0;
156 rpc->inpos = 0;
157 }
158 }
159 return 0;
160 }
161
162
163
164 int rpc_service(struct rpc_context *rpc, int revents)
165 {
166 if (revents & POLLERR) {
167 int err = 0;
168 socklen_t err_size = sizeof(err);
169
170 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
171 &err, &err_size) != 0 || err != 0) {
172 if (err == 0) {
173 err = errno;
174 }
175 rpc_set_error(rpc, "rpc_service: socket error "
176 "%s(%d).",
177 strerror(err), err);
178 } else {
179 rpc_set_error(rpc, "rpc_service: POLLERR, "
180 "Unknown socket error.");
181 }
182 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
183 return -1;
184 }
185 if (revents & POLLHUP) {
186 printf("rpc_service: POLLHUP, socket error\n");
187 rpc_set_error(rpc, "Socket failed with POLLHUP");
188 rpc->connect_cb(rpc, RPC_STATUS_ERROR, rpc->error_string, rpc->connect_data);
189 return -2;
190 }
191
192 if (rpc->is_connected == 0 && rpc->fd != -1 && revents&POLLOUT) {
193 int err = 0;
194 socklen_t err_size = sizeof(err);
195
196 if (getsockopt(rpc->fd, SOL_SOCKET, SO_ERROR,
197 &err, &err_size) != 0 || err != 0) {
198 if (err == 0) {
199 err = errno;
200 }
201 rpc_set_error(rpc, "rpc_service: socket error "
202 "%s(%d) while connecting.",
203 strerror(err), err);
204 rpc->connect_cb(rpc, RPC_STATUS_ERROR,
205 NULL, rpc->connect_data);
206 return -1;
207 }
208
209 rpc->is_connected = 1;
210 rpc->connect_cb(rpc, RPC_STATUS_SUCCESS, NULL, rpc->connect_data);
211 return 0;
212 }
213
214 if (revents & POLLOUT && rpc->outqueue != NULL) {
215 if (rpc_write_to_socket(rpc) != 0) {
216 printf("write to socket failed\n");
217 return -3;
218 }
219 }
220
221 if (revents & POLLIN) {
222 if (rpc_read_from_socket(rpc) != 0) {
223 rpc_disconnect(rpc, rpc_get_error(rpc));
224 return -4;
225 }
226 }
227
228 return 0;
229 }
230
231
232 int rpc_connect_async(struct rpc_context *rpc, const char *server, int port, rpc_cb cb, void *private_data)
233 {
234 struct sockaddr_storage s;
235 struct sockaddr_in *sin = (struct sockaddr_in *)&s;
236 int socksize;
237
238 if (rpc->fd != -1) {
239 rpc_set_error(rpc, "Trying to connect while already connected");
240 printf("%s\n", rpc->error_string);
241 return -1;
242 }
243
244 sin->sin_family = AF_INET;
245 sin->sin_port = htons(port);
246 if (inet_pton(AF_INET, server, &sin->sin_addr) != 1) {
247 rpc_set_error(rpc, "Not a valid server ip address");
248 printf("%s\n", rpc->error_string);
249 return -2;
250 }
251
252 switch (s.ss_family) {
253 case AF_INET:
254 socksize = sizeof(struct sockaddr_in);
255 #ifdef HAVE_SOCK_SIN_LEN
256 sin->sin_len = socksize;
257 #endif
258 rpc->fd = socket(AF_INET, SOCK_STREAM, 0);
259 break;
260 }
261
262 if (rpc->fd == -1) {
263 rpc_set_error(rpc, "Failed to open socket");
264 printf("%s\n", rpc->error_string);
265 return -3;
266 }
267
268 rpc->connect_cb = cb;
269 rpc->connect_data = private_data;
270
271 set_nonblocking(rpc->fd);
272
273 if (connect(rpc->fd, (struct sockaddr *)&s, socksize) != 0 && errno != EINPROGRESS) {
274 rpc_set_error(rpc, "connect() to server failed");
275 printf("%s\n", rpc->error_string);
276 return -4;
277 }
278
279 return 0;
280 }
281
282 int rpc_disconnect(struct rpc_context *rpc, char *error)
283 {
284 if (rpc->fd != -1) {
285 close(rpc->fd);
286 }
287 rpc->fd = -1;
288
289 rpc->is_connected = 0;
290
291 rpc_error_all_pdus(rpc, error);
292
293 return 0;
294 }