123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- /* Low-level protocol for MCFS.
- Copyright (C) 1995, 1996 Miguel de Icaza
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU Library General Public License
- as published by the Free Software Foundation; either version 2 of
- the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Library General Public License for more details.
- You should have received a copy of the GNU Library General Public
- License along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
- /**
- * \file
- * \brief Source: Low-level protocol for MCFS
- * \author Miguel de Icaza
- * \date 1995, 1996
- */
- #include <config.h>
- #if defined(ENABLE_VFS_MCFS) || defined(ENABLE_MCSERVER)
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <signal.h>
- #include <pwd.h>
- #include <sys/types.h>
- #include <netdb.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #ifdef HAVE_ARPA_INET_H
- #include <arpa/inet.h>
- #endif
- #ifdef HAVE_PMAP_SET
- #include <rpc/rpc.h>
- #include <rpc/pmap_prot.h>
- #ifdef HAVE_RPC_PMAP_CLNT_H
- #include <rpc/pmap_clnt.h>
- #endif
- #endif
- #include <errno.h>
- #include "lib/global.h"
- #include "src/wtools.h" /* message() */
- #include "src/main.h" /* print_vfs_message */
- #include "utilvfs.h"
- #include "mcfsutil.h"
- #include "netutil.h"
- #include "mcfs.h" /* tcp_invalidate_socket() */
- #define CHECK_SIG_PIPE(sock) if (got_sigpipe) \
- { tcp_invalidate_socket (sock); return got_sigpipe = 0; }
- /* Reads a block on dest for len bytes from sock */
- /* Returns a boolean indicating the success status */
- int
- socket_read_block (int sock, char *dest, int len)
- {
- int nread, n;
- for (nread = 0; nread < len;)
- {
- n = read (sock, dest + nread, len - nread);
- if (n <= 0)
- {
- tcp_invalidate_socket (sock);
- return 0;
- }
- nread += n;
- }
- return 1;
- }
- int
- socket_write_block (int sock, const char *buffer, int len)
- {
- int left, status;
- for (left = len; left > 0;)
- {
- status = write (sock, buffer, left);
- CHECK_SIG_PIPE (sock);
- if (status < 0)
- return 0;
- left -= status;
- buffer += status;
- }
- return 1;
- }
- int
- rpc_send (int sock, ...)
- {
- long int tmp, len, cmd;
- char *text;
- va_list ap;
- va_start (ap, sock);
- for (;;)
- {
- cmd = va_arg (ap, int);
- switch (cmd)
- {
- case RPC_END:
- va_end (ap);
- return 1;
- case RPC_INT:
- tmp = htonl (va_arg (ap, int));
- if (write (sock, &tmp, sizeof (tmp)) != sizeof (tmp))
- {
- vfs_die ("RPC: write failed (RPC_INT)");
- break;
- }
- CHECK_SIG_PIPE (sock);
- break;
- case RPC_STRING:
- text = va_arg (ap, char *);
- len = strlen (text);
- tmp = htonl (len);
- if (write (sock, &tmp, sizeof (tmp)) != sizeof (tmp))
- {
- vfs_die ("RPC: write failed (RPC_STRING)");
- break;
- }
- CHECK_SIG_PIPE (sock);
- if (write (sock, text, len) != len)
- {
- vfs_die ("RPC: write failed (RPC_STRING)");
- break;
- }
- CHECK_SIG_PIPE (sock);
- break;
- case RPC_BLOCK:
- len = va_arg (ap, int);
- text = va_arg (ap, char *);
- tmp = htonl (len);
- if (write (sock, text, len) != len)
- {
- vfs_die ("RPC: write failed (RPC_BLOCK)");
- break;
- }
- CHECK_SIG_PIPE (sock);
- break;
- default:
- vfs_die ("Unknown rpc message\n");
- }
- }
- }
- int
- rpc_get (int sock, ...)
- {
- long int tmp, len;
- char *text, **str_dest;
- int *dest, cmd;
- va_list ap;
- va_start (ap, sock);
- for (;;)
- {
- cmd = va_arg (ap, int);
- switch (cmd)
- {
- case RPC_END:
- va_end (ap);
- return 1;
- case RPC_INT:
- if (socket_read_block (sock, (char *) &tmp, sizeof (tmp)) == 0)
- {
- va_end (ap);
- return 0;
- }
- dest = va_arg (ap, int *);
- *dest = ntohl (tmp);
- break;
- /* returns an allocated string */
- case RPC_LIMITED_STRING:
- case RPC_STRING:
- if (socket_read_block (sock, (char *) &tmp, sizeof (tmp)) == 0)
- {
- va_end (ap);
- return 0;
- }
- len = ntohl (tmp);
- if (cmd == RPC_LIMITED_STRING)
- if (len > 16 * 1024)
- {
- /* silently die */
- abort ();
- }
- if (len > 128 * 1024)
- abort ();
- /* Don't use glib functions here - this code is used by mcserv */
- text = malloc (len + 1);
- if (socket_read_block (sock, text, len) == 0)
- {
- free (text);
- va_end (ap);
- return 0;
- }
- text[len] = '\0';
- str_dest = va_arg (ap, char **);
- *str_dest = text;
- break;
- case RPC_BLOCK:
- len = va_arg (ap, int);
- text = va_arg (ap, char *);
- if (socket_read_block (sock, text, len) == 0)
- {
- va_end (ap);
- return 0;
- }
- break;
- default:
- vfs_die ("Unknown rpc message\n");
- }
- }
- }
- #endif /* ENABLE_VFS_MCFS || ENABLE_MCSERVER */
|