clnt_gen.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (c) 2010, Oracle America, Inc.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are
  6. * met:
  7. *
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above
  11. * copyright notice, this list of conditions and the following
  12. * disclaimer in the documentation and/or other materials
  13. * provided with the distribution.
  14. * * Neither the name of the "Oracle America, Inc." nor the names of its
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  23. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  27. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <alloca.h>
  32. #include <errno.h>
  33. #include <string.h>
  34. #include <rpc/rpc.h>
  35. #include <sys/socket.h>
  36. #include <netdb.h>
  37. #include <shlib-compat.h>
  38. /*
  39. * Generic client creation: takes (hostname, program-number, protocol) and
  40. * returns client handle. Default options are set, which the user can
  41. * change using the rpc equivalent of ioctl()'s.
  42. */
  43. CLIENT *
  44. clnt_create (const char *hostname, u_long prog, u_long vers,
  45. const char *proto)
  46. {
  47. struct protoent protobuf, *p;
  48. size_t prtbuflen;
  49. char *prttmpbuf;
  50. struct sockaddr_in sin;
  51. struct sockaddr_un sun;
  52. int sock;
  53. struct timeval tv;
  54. CLIENT *client;
  55. if (strcmp (proto, "unix") == 0)
  56. {
  57. memset ((char *)&sun, 0, sizeof (sun));
  58. sun.sun_family = AF_UNIX;
  59. strcpy (sun.sun_path, hostname);
  60. sock = RPC_ANYSOCK;
  61. client = clntunix_create (&sun, prog, vers, &sock, 0, 0);
  62. if (client == NULL)
  63. return NULL;
  64. #if 0
  65. /* This is not wanted. This would disable the user from having
  66. a timeout in the clnt_call() call. Only a call to cnlt_control()
  67. by the user should set the timeout value. */
  68. tv.tv_sec = 25;
  69. tv.tv_usec = 0;
  70. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  71. #endif
  72. return client;
  73. }
  74. if (__libc_rpc_gethostbyname (hostname, &sin) != 0)
  75. return NULL;
  76. prtbuflen = 1024;
  77. prttmpbuf = __alloca (prtbuflen);
  78. while (__getprotobyname_r (proto, &protobuf, prttmpbuf, prtbuflen, &p) != 0
  79. || p == NULL)
  80. if (errno != ERANGE)
  81. {
  82. struct rpc_createerr *ce = &get_rpc_createerr ();
  83. ce->cf_stat = RPC_UNKNOWNPROTO;
  84. ce->cf_error.re_errno = EPFNOSUPPORT;
  85. return NULL;
  86. }
  87. else
  88. {
  89. /* Enlarge the buffer. */
  90. prtbuflen *= 2;
  91. prttmpbuf = __alloca (prtbuflen);
  92. }
  93. sock = RPC_ANYSOCK;
  94. switch (p->p_proto)
  95. {
  96. case IPPROTO_UDP:
  97. tv.tv_sec = 5;
  98. tv.tv_usec = 0;
  99. client = clntudp_create (&sin, prog, vers, tv, &sock);
  100. if (client == NULL)
  101. {
  102. return NULL;
  103. }
  104. #if 0
  105. /* This is not wanted. This would disable the user from having
  106. a timeout in the clnt_call() call. Only a call to cnlt_control()
  107. by the user should set the timeout value. */
  108. tv.tv_sec = 25;
  109. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  110. #endif
  111. break;
  112. case IPPROTO_TCP:
  113. client = clnttcp_create (&sin, prog, vers, &sock, 0, 0);
  114. if (client == NULL)
  115. {
  116. return NULL;
  117. }
  118. #if 0
  119. /* This is not wanted. This would disable the user from having
  120. a timeout in the clnt_call() call. Only a call to cnlt_control()
  121. by the user should set the timeout value. */
  122. tv.tv_sec = 25;
  123. tv.tv_usec = 0;
  124. clnt_control (client, CLSET_TIMEOUT, (char *)&tv);
  125. #endif
  126. break;
  127. default:
  128. {
  129. struct rpc_createerr *ce = &get_rpc_createerr ();
  130. ce->cf_stat = RPC_SYSTEMERROR;
  131. ce->cf_error.re_errno = EPFNOSUPPORT;
  132. }
  133. return (NULL);
  134. }
  135. return client;
  136. }
  137. #ifdef EXPORT_RPC_SYMBOLS
  138. libc_hidden_def (clnt_create)
  139. #else
  140. libc_hidden_nolink_sunrpc (clnt_create, GLIBC_2_0)
  141. #endif