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