geom_gate userland utility improvements
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

219 lines
5.3 KiB

  1. /*
  2. * Copyright (C) 2015 Patrick Monnerat, D+H <patrick.monnerat@dh.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms,
  6. * with or without modification, are permitted provided
  7. * that the following conditions are met:
  8. *
  9. * Redistributions of source code must retain the above
  10. * copyright notice, this list of conditions and the
  11. * following disclaimer.
  12. *
  13. * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials
  16. * provided with the distribution.
  17. *
  18. * Neither the name of the copyright holder nor the names
  19. * of any other contributors may be used to endorse or
  20. * promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  24. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  25. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  26. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  28. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  33. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  35. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  36. * OF SUCH DAMAGE.
  37. */
  38. /* OS/400 additional support. */
  39. #define LIBSSH2_DISABLE_QADRT_EXT
  40. #include "libssh2_priv.h"
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/un.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <stddef.h>
  47. #include <stdarg.h>
  48. #include <string.h>
  49. #include <alloca.h>
  50. #include <netdb.h>
  51. #include <qadrt.h>
  52. #include <errno.h>
  53. #include <netinet/in.h>
  54. #include <arpa/inet.h>
  55. #ifdef LIBSSH2_HAVE_ZLIB
  56. # include <zlib.h>
  57. #endif
  58. /**
  59. *** QADRT OS/400 ASCII runtime defines only the most used procedures, but
  60. *** a lot of them are not supported. This module implements
  61. *** ASCII wrappers for those that are used by libssh2, but not
  62. *** defined by QADRT.
  63. **/
  64. #pragma convert(37) /* Restore EBCDIC. */
  65. static int
  66. convert_sockaddr(struct sockaddr_storage * dstaddr,
  67. const struct sockaddr * srcaddr, int srclen)
  68. {
  69. const struct sockaddr_un * srcu;
  70. struct sockaddr_un * dstu;
  71. unsigned int i;
  72. unsigned int dstsize;
  73. /* Convert a socket address into job CCSID, if needed. */
  74. if(!srcaddr || srclen < offsetof(struct sockaddr, sa_family) +
  75. sizeof srcaddr->sa_family || srclen > sizeof *dstaddr) {
  76. errno = EINVAL;
  77. return -1;
  78. }
  79. memcpy((char *) dstaddr, (char *) srcaddr, srclen);
  80. switch (srcaddr->sa_family) {
  81. case AF_UNIX:
  82. srcu = (const struct sockaddr_un *) srcaddr;
  83. dstu = (struct sockaddr_un *) dstaddr;
  84. dstsize = sizeof *dstaddr - offsetof(struct sockaddr_un, sun_path);
  85. srclen -= offsetof(struct sockaddr_un, sun_path);
  86. i = QadrtConvertA2E(dstu->sun_path, srcu->sun_path, dstsize - 1, srclen);
  87. dstu->sun_path[i] = '\0';
  88. i += offsetof(struct sockaddr_un, sun_path);
  89. srclen = i;
  90. }
  91. return srclen;
  92. }
  93. int
  94. _libssh2_os400_connect(int sd, struct sockaddr * destaddr, int addrlen)
  95. {
  96. int i;
  97. struct sockaddr_storage laddr;
  98. i = convert_sockaddr(&laddr, destaddr, addrlen);
  99. if(i < 0)
  100. return -1;
  101. return connect(sd, (struct sockaddr *) &laddr, i);
  102. }
  103. int
  104. _libssh2_os400_vsnprintf(char *dst, size_t len, const char *fmt, va_list args)
  105. {
  106. size_t l = 4096;
  107. int i;
  108. char *buf;
  109. if (!dst || !len) {
  110. errno = EINVAL;
  111. return -1;
  112. }
  113. if (l < len)
  114. l = len;
  115. buf = alloca(l);
  116. if (!buf) {
  117. errno = ENOMEM;
  118. return -1;
  119. }
  120. i = vsprintf(buf, fmt, args);
  121. if (i < 0)
  122. return i;
  123. if (--len > i)
  124. len = i;
  125. if (len)
  126. memcpy(dst, buf, len);
  127. dst[len] = '\0';
  128. return len;
  129. }
  130. /* VARARGS3 */
  131. int
  132. _libssh2_os400_snprintf(char *dst, size_t len, const char *fmt, ...)
  133. {
  134. va_list args;
  135. int ret;
  136. va_start(args, fmt);
  137. ret = _libssh2_os400_vsnprintf(dst, len, fmt, args);
  138. va_end(args);
  139. return ret;
  140. }
  141. #ifdef LIBSSH2_HAVE_ZLIB
  142. int
  143. _libssh2_os400_inflateInit_(z_streamp strm,
  144. const char *version, int stream_size)
  145. {
  146. char *ebcversion;
  147. int i;
  148. if (!version)
  149. return Z_VERSION_ERROR;
  150. i = strlen(version);
  151. ebcversion = alloca(i + 1);
  152. if (!ebcversion)
  153. return Z_VERSION_ERROR;
  154. i = QadrtConvertA2E(ebcversion, version, i, i - 1);
  155. ebcversion[i] = '\0';
  156. return inflateInit_(strm, ebcversion, stream_size);
  157. }
  158. int
  159. _libssh2_os400_deflateInit_(z_streamp strm, int level,
  160. const char *version, int stream_size)
  161. {
  162. char *ebcversion;
  163. int i;
  164. if (!version)
  165. return Z_VERSION_ERROR;
  166. i = strlen(version);
  167. ebcversion = alloca(i + 1);
  168. if (!ebcversion)
  169. return Z_VERSION_ERROR;
  170. i = QadrtConvertA2E(ebcversion, version, i, i - 1);
  171. ebcversion[i] = '\0';
  172. return deflateInit_(strm, level, ebcversion, stream_size);
  173. }
  174. #endif