1 /* Low-level protocol for MCFS.
3 Copyright (C) 1995, 1996 Miguel de Icaza
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License
7 as published by the Free Software Foundation; either version 2 of
8 the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
28 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #ifdef HAVE_ARPA_INET_H
33 #include <arpa/inet.h>
38 #include <rpc/pmap_prot.h>
39 #ifdef HAVE_RPC_PMAP_CLNT_H
40 #include <rpc/pmap_clnt.h>
49 #include "mcfs.h" /* tcp_invalidate_socket() */
51 #define CHECK_SIG_PIPE(sock) if (got_sigpipe) \
52 { tcp_invalidate_socket (sock); return got_sigpipe = 0; }
54 /* Reads a block on dest for len bytes from sock */
55 /* Returns a boolean indicating the success status */
57 socket_read_block (int sock
, char *dest
, int len
)
61 for (nread
= 0; nread
< len
;) {
62 n
= read (sock
, dest
+ nread
, len
- nread
);
64 tcp_invalidate_socket (sock
);
73 socket_write_block (int sock
, char *buffer
, int len
)
77 for (left
= len
; left
> 0;) {
78 status
= write (sock
, buffer
, left
);
79 CHECK_SIG_PIPE (sock
);
89 rpc_send (int sock
, ...)
91 long int tmp
, len
, cmd
;
98 cmd
= va_arg (ap
, int);
105 tmp
= htonl (va_arg (ap
, int));
106 write (sock
, &tmp
, sizeof (tmp
));
107 CHECK_SIG_PIPE (sock
);
111 text
= va_arg (ap
, char *);
114 write (sock
, &tmp
, sizeof (tmp
));
115 CHECK_SIG_PIPE (sock
);
116 write (sock
, text
, len
);
117 CHECK_SIG_PIPE (sock
);
121 len
= va_arg (ap
, int);
122 text
= va_arg (ap
, char *);
124 write (sock
, text
, len
);
125 CHECK_SIG_PIPE (sock
);
129 vfs_die ("Unknown rpc message\n");
135 rpc_get (int sock
, ...)
138 char *text
, **str_dest
;
145 cmd
= va_arg (ap
, int);
152 if (socket_read_block (sock
, (char *) &tmp
, sizeof (tmp
)) == 0) {
156 dest
= va_arg (ap
, int *);
160 /* returns an allocated string */
161 case RPC_LIMITED_STRING
:
163 if (socket_read_block (sock
, (char *) &tmp
, sizeof (tmp
)) == 0) {
168 if (cmd
== RPC_LIMITED_STRING
)
169 if (len
> 16 * 1024) {
173 if (len
> 128 * 1024)
176 /* Don't use glib functions here - this code is used by mcserv */
177 text
= malloc (len
+ 1);
178 if (socket_read_block (sock
, text
, len
) == 0) {
185 str_dest
= va_arg (ap
, char **);
190 len
= va_arg (ap
, int);
191 text
= va_arg (ap
, char *);
192 if (socket_read_block (sock
, text
, len
) == 0) {
199 vfs_die ("Unknown rpc message\n");
203 #endif /* WITH_MCFS */