mcfsutil.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. n = read (sock, dest + nread, len - nread);
  60. if (n <= 0) {
  61. tcp_invalidate_socket (sock);
  62. return 0;
  63. }
  64. nread += n;
  65. }
  66. return 1;
  67. }
  68. int
  69. socket_write_block (int sock, const char *buffer, int len)
  70. {
  71. int left, status;
  72. for (left = len; left > 0;) {
  73. status = write (sock, buffer, left);
  74. CHECK_SIG_PIPE (sock);
  75. if (status < 0)
  76. return 0;
  77. left -= status;
  78. buffer += status;
  79. }
  80. return 1;
  81. }
  82. int
  83. rpc_send (int sock, ...)
  84. {
  85. long int tmp, len, cmd;
  86. char *text;
  87. va_list ap;
  88. va_start (ap, sock);
  89. for (;;) {
  90. cmd = va_arg (ap, int);
  91. switch (cmd) {
  92. case RPC_END:
  93. va_end (ap);
  94. return 1;
  95. case RPC_INT:
  96. tmp = htonl (va_arg (ap, int));
  97. write (sock, &tmp, sizeof (tmp));
  98. CHECK_SIG_PIPE (sock);
  99. break;
  100. case RPC_STRING:
  101. text = va_arg (ap, char *);
  102. len = strlen (text);
  103. tmp = htonl (len);
  104. write (sock, &tmp, sizeof (tmp));
  105. CHECK_SIG_PIPE (sock);
  106. write (sock, text, len);
  107. CHECK_SIG_PIPE (sock);
  108. break;
  109. case RPC_BLOCK:
  110. len = va_arg (ap, int);
  111. text = va_arg (ap, char *);
  112. tmp = htonl (len);
  113. write (sock, text, len);
  114. CHECK_SIG_PIPE (sock);
  115. break;
  116. default:
  117. vfs_die ("Unknown rpc message\n");
  118. }
  119. }
  120. }
  121. int
  122. rpc_get (int sock, ...)
  123. {
  124. long int tmp, len;
  125. char *text, **str_dest;
  126. int *dest, cmd;
  127. va_list ap;
  128. va_start (ap, sock);
  129. for (;;) {
  130. cmd = va_arg (ap, int);
  131. switch (cmd) {
  132. case RPC_END:
  133. va_end (ap);
  134. return 1;
  135. case RPC_INT:
  136. if (socket_read_block (sock, (char *) &tmp, sizeof (tmp)) == 0) {
  137. va_end (ap);
  138. return 0;
  139. }
  140. dest = va_arg (ap, int *);
  141. *dest = ntohl (tmp);
  142. break;
  143. /* returns an allocated string */
  144. case RPC_LIMITED_STRING:
  145. case RPC_STRING:
  146. if (socket_read_block (sock, (char *) &tmp, sizeof (tmp)) == 0) {
  147. va_end (ap);
  148. return 0;
  149. }
  150. len = ntohl (tmp);
  151. if (cmd == RPC_LIMITED_STRING)
  152. if (len > 16 * 1024) {
  153. /* silently die */
  154. abort ();
  155. }
  156. if (len > 128 * 1024)
  157. abort ();
  158. /* Don't use glib functions here - this code is used by mcserv */
  159. text = malloc (len + 1);
  160. if (socket_read_block (sock, text, len) == 0) {
  161. free (text);
  162. va_end (ap);
  163. return 0;
  164. }
  165. text[len] = '\0';
  166. str_dest = va_arg (ap, char **);
  167. *str_dest = text;
  168. break;
  169. case RPC_BLOCK:
  170. len = va_arg (ap, int);
  171. text = va_arg (ap, char *);
  172. if (socket_read_block (sock, text, len) == 0) {
  173. va_end (ap);
  174. return 0;
  175. }
  176. break;
  177. default:
  178. vfs_die ("Unknown rpc message\n");
  179. }
  180. }
  181. }
  182. #endif /* ENABLE_VFS_MCFS || ENABLE_MCSERVER */