1 /* CVS socket client stuff.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details. */
14 *** THIS FILE SHOULD NEVER BE COMPILED UNLESS NO_SOCKET_TO_FD IS DEFINED.
26 #include "socket-client.h"
29 /* Under certain circumstances, we must communicate with the server
30 via a socket using send() and recv(). This is because under some
31 operating systems (OS/2 and Windows 95 come to mind), a socket
32 cannot be converted to a file descriptor -- it must be treated as a
33 socket and nothing else.
35 We may also need to deal with socket routine error codes differently
36 in these cases. This is handled through the SOCK_ERRNO and
37 SOCK_STRERROR macros. */
39 /* These routines implement a buffer structure which uses send and
40 recv. The buffer is always in blocking mode so we don't implement
43 /* Note that it is important that these routines always handle errors
44 internally and never return a positive errno code, since it would in
45 general be impossible for the caller to know in general whether any
46 error code came from a socket routine (to decide whether to use
47 SOCK_STRERROR or simply strerror to print an error message). */
49 /* We use an instance of this structure as the closure field. */
53 /* The socket number. */
59 /* The buffer input function for a buffer built on a socket. */
62 socket_buffer_input (void *closure
, char *data
, size_t need
, size_t size
,
65 struct socket_buffer
*sb
= closure
;
68 /* I believe that the recv function gives us exactly the semantics
69 we want. If there is a message, it returns immediately with
70 whatever it could get. If there is no message, it waits until
71 one comes in. In other words, it is not like read, which in
72 blocking mode normally waits until all the requested data is
75 assert (size
>= need
);
82 /* Note that for certain (broken?) networking stacks, like
83 VMS's UCX (not sure what version, problem reported with
84 recv() in 1997), and (according to windows-NT/config.h)
85 Windows NT 3.51, we must call recv or send with a
86 moderately sized buffer (say, less than 200K or something),
87 or else there may be network errors (somewhat hard to
88 produce, e.g. WAN not LAN or some such). buf_read_data
89 makes sure that we only recv() BUFFER_DATA_SIZE bytes at
92 nbytes
= recv (sb
->socket
, data
+ *got
, size
- *got
, 0);
94 error (1, 0, "reading from server: %s",
95 SOCK_STRERROR (SOCK_ERRNO
));
98 /* End of file (for example, the server has closed
99 the connection). If we've already read something, we
100 just tell the caller about the data, not about the end of
101 file. If we've read nothing, we return end of file. */
116 /* The buffer output function for a buffer built on a socket. */
119 socket_buffer_output (void *closure
, const char *data
, size_t have
,
122 struct socket_buffer
*sb
= closure
;
126 /* See comment in socket_buffer_input regarding buffer size we pass
129 # ifdef SEND_NEVER_PARTIAL
130 /* If send() never will produce a partial write, then just do it. This
131 is needed for systems where its return value is something other than
132 the number of bytes written. */
133 if (send (sb
->socket
, data
, have
, 0) < 0)
134 error (1, 0, "writing to server socket: %s",
135 SOCK_STRERROR (SOCK_ERRNO
));
141 nbytes
= send (sb
->socket
, data
, have
, 0);
143 error (1, 0, "writing to server socket: %s",
144 SOCK_STRERROR (SOCK_ERRNO
));
156 /* The buffer flush function for a buffer built on a socket. */
160 socket_buffer_flush (void *closure
)
162 /* Nothing to do. Sockets are always flushed. */
169 socket_buffer_shutdown (struct buffer
*buf
)
171 struct socket_buffer
*n
= buf
->closure
;
174 /* no need to flush children of an endpoint buffer here */
179 if (! buf_empty_p (buf
)
180 || (err
= recv (n
->socket
, &tmp
, 1, 0)) > 0)
181 error (0, 0, "dying gasps from %s unexpected",
182 current_parsed_root
->hostname
);
184 error (0, 0, "reading from %s: %s", current_parsed_root
->hostname
,
185 SOCK_STRERROR (SOCK_ERRNO
));
187 /* shutdown() socket */
188 # ifdef SHUTDOWN_SERVER
189 if (current_parsed_root
->method
!= server_method
)
191 if (shutdown (n
->socket
, 0) < 0)
193 error (1, 0, "shutting down server socket: %s",
194 SOCK_STRERROR (SOCK_ERRNO
));
199 else if (buf
->output
)
201 /* shutdown() socket */
202 # ifdef SHUTDOWN_SERVER
203 /* FIXME: Should have a SHUTDOWN_SERVER_INPUT &
204 * SHUTDOWN_SERVER_OUTPUT
206 if (current_parsed_root
->method
== server_method
)
207 SHUTDOWN_SERVER (n
->socket
);
210 if (shutdown (n
->socket
, 1) < 0)
212 error (1, 0, "shutting down server socket: %s",
213 SOCK_STRERROR (SOCK_ERRNO
));
224 /* Create a buffer based on a socket. */
227 socket_buffer_initialize (int socket
, int input
,
228 void (*memory
) (struct buffer
*))
230 struct socket_buffer
*sbuf
= xmalloc (sizeof *sbuf
);
231 sbuf
->socket
= socket
;
232 return buf_initialize (input
? socket_buffer_input
: NULL
,
233 input
? NULL
: socket_buffer_output
,
234 input
? NULL
: socket_buffer_flush
,
236 socket_buffer_shutdown
,
241 #endif /* CLIENT_SUPPORT */