clnt_generic.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  3. * unrestricted use provided that this legend is included on all tape
  4. * media and as a part of the software program in whole or part. Users
  5. * may copy or modify Sun RPC without charge, but are not authorized
  6. * to license or distribute it to anyone else except as part of a product or
  7. * program developed by the user.
  8. *
  9. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  10. * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  11. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  12. *
  13. * Sun RPC is provided with no support and without any obligation on the
  14. * part of Sun Microsystems, Inc. to assist in its use, correction,
  15. * modification or enhancement.
  16. *
  17. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  18. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  19. * OR ANY PART THEREOF.
  20. *
  21. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  22. * or profits or other special, indirect and consequential damages, even if
  23. * Sun has been advised of the possibility of such damages.
  24. *
  25. * Sun Microsystems, Inc.
  26. * 2550 Garcia Avenue
  27. * Mountain View, California 94043
  28. */
  29. /*
  30. * Copyright (C) 1987, Sun Microsystems, Inc.
  31. */
  32. #define __FORCE_GLIBC
  33. #include <features.h>
  34. #include <alloca.h>
  35. #include <errno.h>
  36. #include <string.h>
  37. #include <rpc/rpc.h>
  38. #include <sys/socket.h>
  39. #include <sys/errno.h>
  40. #include <netdb.h>
  41. /*
  42. * Generic client creation: takes (hostname, program-number, protocol) and
  43. * returns client handle. Default options are set, which the user can
  44. * change using the rpc equivalent of ioctl()'s.
  45. */
  46. CLIENT *
  47. clnt_create (const char *hostname, u_long prog, u_long vers,
  48. const char *proto)
  49. {
  50. struct hostent hostbuf, *h;
  51. size_t hstbuflen;
  52. char *hsttmpbuf;
  53. struct protoent *p;
  54. struct sockaddr_in sin;
  55. struct sockaddr_un sun;
  56. int sock;
  57. struct timeval tv;
  58. CLIENT *client;
  59. int herr;
  60. if (strcmp (proto, "unix") == 0)
  61. {
  62. bzero ((char *)&sun, sizeof (sun));
  63. sun.sun_family = AF_UNIX;
  64. strcpy (sun.sun_path, hostname);
  65. sock = RPC_ANYSOCK;
  66. client = clntunix_create (&sun, prog, vers, &sock, 0, 0);
  67. if (client == NULL)
  68. return NULL;
  69. #if 0
  70. /* This is not wanted. This would disable the user from having
  71. a timeout in the clnt_call() call. Only a call to cnlt_control()
  72. by the user should set the timeout value. */
  73. tv.tv_sec = 25;
  74. tv.tv_usec = 0;
  75. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  76. #endif
  77. return client;
  78. }
  79. hstbuflen = 1024;
  80. hsttmpbuf = alloca (hstbuflen);
  81. while (gethostbyname_r (hostname, &hostbuf, hsttmpbuf, hstbuflen,
  82. &h, &herr) != 0
  83. || h == NULL)
  84. if (herr != NETDB_INTERNAL || errno != ERANGE)
  85. {
  86. get_rpc_createerr().cf_stat = RPC_UNKNOWNHOST;
  87. return NULL;
  88. }
  89. else
  90. {
  91. /* Enlarge the buffer. */
  92. hstbuflen *= 2;
  93. hsttmpbuf = alloca (hstbuflen);
  94. }
  95. if (h->h_addrtype != AF_INET)
  96. {
  97. /*
  98. * Only support INET for now
  99. */
  100. struct rpc_createerr *ce = &get_rpc_createerr ();
  101. ce->cf_stat = RPC_SYSTEMERROR;
  102. ce->cf_error.re_errno = EAFNOSUPPORT;
  103. return NULL;
  104. }
  105. sin.sin_family = h->h_addrtype;
  106. sin.sin_port = 0;
  107. bzero (sin.sin_zero, sizeof (sin.sin_zero));
  108. memcpy ((char *) &sin.sin_addr, h->h_addr, h->h_length);
  109. #warning getprotobyname is not reentrant... Add getprotobyname_r
  110. p = getprotobyname(proto);
  111. if (p == NULL) {
  112. struct rpc_createerr *ce = &get_rpc_createerr ();
  113. ce->cf_stat = RPC_UNKNOWNPROTO;
  114. ce->cf_error.re_errno = EPFNOSUPPORT;
  115. return NULL;
  116. }
  117. sock = RPC_ANYSOCK;
  118. switch (p->p_proto)
  119. {
  120. case IPPROTO_UDP:
  121. tv.tv_sec = 5;
  122. tv.tv_usec = 0;
  123. client = clntudp_create (&sin, prog, vers, tv, &sock);
  124. if (client == NULL)
  125. {
  126. return NULL;
  127. }
  128. #if 0
  129. /* This is not wanted. This would disable the user from having
  130. a timeout in the clnt_call() call. Only a call to cnlt_control()
  131. by the user should set the timeout value. */
  132. tv.tv_sec = 25;
  133. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  134. #endif
  135. break;
  136. case IPPROTO_TCP:
  137. client = clnttcp_create (&sin, prog, vers, &sock, 0, 0);
  138. if (client == NULL)
  139. {
  140. return NULL;
  141. }
  142. #if 0
  143. /* This is not wanted. This would disable the user from having
  144. a timeout in the clnt_call() call. Only a call to cnlt_control()
  145. by the user should set the timeout value. */
  146. tv.tv_sec = 25;
  147. tv.tv_usec = 0;
  148. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  149. #endif
  150. break;
  151. default:
  152. {
  153. struct rpc_createerr *ce = &get_rpc_createerr ();
  154. ce->cf_stat = RPC_SYSTEMERROR;
  155. ce->cf_error.re_errno = EPFNOSUPPORT;
  156. }
  157. return (NULL);
  158. }
  159. return client;
  160. }