mcfsutil.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Low-level protocol for MCFS.
  2. Copyright (C) 1995, 1996 Miguel de Icaza
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License
  5. as published by the Free Software Foundation; either version 2 of
  6. the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. /**
  15. * \file
  16. * \brief Source: Low-level protocol for MCFS
  17. * \author Miguel de Icaza
  18. * \date 1995, 1996
  19. */
  20. #include <config.h>
  21. #if defined(ENABLE_VFS_MCFS) || defined(ENABLE_MCSERVER)
  22. #include <unistd.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <signal.h>
  27. #include <pwd.h>
  28. #include <sys/types.h>
  29. #include <netdb.h>
  30. #include <sys/socket.h>
  31. #include <netinet/in.h>
  32. #ifdef HAVE_ARPA_INET_H
  33. #include <arpa/inet.h>
  34. #endif
  35. #ifdef HAVE_PMAP_SET
  36. #include <rpc/rpc.h>
  37. #include <rpc/pmap_prot.h>
  38. #ifdef HAVE_RPC_PMAP_CLNT_H
  39. #include <rpc/pmap_clnt.h>
  40. #endif
  41. #endif
  42. #include <errno.h>
  43. #include "lib/global.h"
  44. #include "src/wtools.h" /* message() */
  45. #include "src/main.h" /* print_vfs_message */
  46. #include "utilvfs.h"
  47. #include "mcfsutil.h"
  48. #include "netutil.h"
  49. #include "mcfs.h" /* tcp_invalidate_socket() */
  50. #define CHECK_SIG_PIPE(sock) if (got_sigpipe) \
  51. { tcp_invalidate_socket (sock); return got_sigpipe = 0; }
  52. /* Reads a block on dest for len bytes from sock */
  53. /* Returns a boolean indicating the success status */
  54. int
  55. socket_read_block (int sock, char *dest, int len)
  56. {
  57. int nread, n;
  58. for (nread = 0; nread < len;)
  59. {
  60. n = read (sock, dest + nread, len - nread);
  61. if (n <= 0)
  62. {
  63. tcp_invalidate_socket (sock);
  64. return 0;
  65. }
  66. nread += n;
  67. }
  68. return 1;
  69. }
  70. int
  71. socket_write_block (int sock, const char *buffer, int len)
  72. {
  73. int left, status;
  74. for (left = len; left > 0;)
  75. {
  76. status = write (sock, buffer, left);
  77. CHECK_SIG_PIPE (sock);
  78. if (status < 0)
  79. return 0;
  80. left -= status;
  81. buffer += status;
  82. }
  83. return 1;
  84. }
  85. int
  86. rpc_send (int sock, ...)
  87. {
  88. long int tmp, len, cmd;
  89. char *text;
  90. va_list ap;
  91. va_start (ap, sock);
  92. for (;;)
  93. {
  94. cmd = va_arg (ap, int);
  95. switch (cmd)
  96. {
  97. case RPC_END:
  98. va_end (ap);
  99. return 1;
  100. case RPC_INT:
  101. tmp = htonl (va_arg (ap, int));
  102. if (write (sock, &tmp, sizeof (tmp)) != sizeof (tmp))
  103. {
  104. vfs_die ("RPC: write failed (RPC_INT)");
  105. break;
  106. }
  107. CHECK_SIG_PIPE (sock);
  108. break;
  109. case RPC_STRING:
  110. text = va_arg (ap, char *);
  111. len = strlen (text);
  112. tmp = htonl (len);
  113. if (write (sock, &tmp, sizeof (tmp)) != sizeof (tmp))
  114. {
  115. vfs_die ("RPC: write failed (RPC_STRING)");
  116. break;
  117. }
  118. CHECK_SIG_PIPE (sock);
  119. if (write (sock, text, len) != len)
  120. {
  121. vfs_die ("RPC: write failed (RPC_STRING)");
  122. break;
  123. }
  124. CHECK_SIG_PIPE (sock);
  125. break;
  126. case RPC_BLOCK:
  127. len = va_arg (ap, int);
  128. text = va_arg (ap, char *);
  129. tmp = htonl (len);
  130. if (write (sock, text, len) != len)
  131. {
  132. vfs_die ("RPC: write failed (RPC_BLOCK)");
  133. break;
  134. }
  135. CHECK_SIG_PIPE (sock);
  136. break;
  137. default:
  138. vfs_die ("Unknown rpc message\n");
  139. }
  140. }
  141. }
  142. int
  143. rpc_get (int sock, ...)
  144. {
  145. long int tmp, len;
  146. char *text, **str_dest;
  147. int *dest, cmd;
  148. va_list ap;
  149. va_start (ap, sock);
  150. for (;;)
  151. {
  152. cmd = va_arg (ap, int);
  153. switch (cmd)
  154. {
  155. case RPC_END:
  156. va_end (ap);
  157. return 1;
  158. case RPC_INT:
  159. if (socket_read_block (sock, (char *) &tmp, sizeof (tmp)) == 0)
  160. {
  161. va_end (ap);
  162. return 0;
  163. }
  164. dest = va_arg (ap, int *);
  165. *dest = ntohl (tmp);
  166. break;
  167. /* returns an allocated string */
  168. case RPC_LIMITED_STRING:
  169. case RPC_STRING:
  170. if (socket_read_block (sock, (char *) &tmp, sizeof (tmp)) == 0)
  171. {
  172. va_end (ap);
  173. return 0;
  174. }
  175. len = ntohl (tmp);
  176. if (cmd == RPC_LIMITED_STRING)
  177. if (len > 16 * 1024)
  178. {
  179. /* silently die */
  180. abort ();
  181. }
  182. if (len > 128 * 1024)
  183. abort ();
  184. /* Don't use glib functions here - this code is used by mcserv */
  185. text = malloc (len + 1);
  186. if (socket_read_block (sock, text, len) == 0)
  187. {
  188. free (text);
  189. va_end (ap);
  190. return 0;
  191. }
  192. text[len] = '\0';
  193. str_dest = va_arg (ap, char **);
  194. *str_dest = text;
  195. break;
  196. case RPC_BLOCK:
  197. len = va_arg (ap, int);
  198. text = va_arg (ap, char *);
  199. if (socket_read_block (sock, text, len) == 0)
  200. {
  201. va_end (ap);
  202. return 0;
  203. }
  204. break;
  205. default:
  206. vfs_die ("Unknown rpc message\n");
  207. }
  208. }
  209. }
  210. #endif /* ENABLE_VFS_MCFS || ENABLE_MCSERVER */