home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / libncftp / libncftp-3.2.5-src.zip / libncftp-3.2.5 / sio / SocketUtil.c < prev    next >
C/C++ Source or Header  |  2005-09-27  |  7KB  |  364 lines

  1. #include "syshdrs.h"
  2. #ifdef PRAGMA_HDRSTOP
  3. #    pragma hdrstop
  4. #endif
  5.  
  6. #ifndef SO_RCVBUF
  7. int
  8. GetSocketBufSize(const int UNUSED(sockfd), size_t *const rsize, size_t *const ssize)
  9. {
  10.     LIBSIO_USE_VAR(sockfd);
  11.     if (ssize != NULL)
  12.         *ssize = 0;
  13.     if (rsize != NULL)
  14.         *rsize = 0;
  15.     return (-1);
  16. }    /* GetSocketBufSize */
  17. #else
  18. int
  19. GetSocketBufSize(const int sockfd, size_t *const rsize, size_t *const ssize)
  20. {
  21.     int rc = -1;
  22.     int opt;
  23.     sockopt_size_t optsize;
  24.  
  25.     if (ssize != NULL) {
  26.         opt = 0;
  27.         optsize = (sockopt_size_t) sizeof(opt);
  28.         rc = getsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, GETSOCKOPT_ARG4 &opt, &optsize);
  29.         if (rc == 0)
  30.             *ssize = (size_t) opt;
  31.         else
  32.             *ssize = 0;
  33.     }
  34.     if (rsize != NULL) {
  35.         opt = 0;
  36.         optsize = (sockopt_size_t) sizeof(opt);
  37.         rc = getsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, GETSOCKOPT_ARG4 &opt, &optsize);
  38.         if (rc == 0)
  39.             *rsize = (size_t) opt;
  40.         else
  41.             *rsize = 0;
  42.     }
  43.     return (rc);
  44. }    /* GetSocketBufSize */
  45. #endif
  46.  
  47.  
  48.  
  49.  
  50. #ifndef SO_RCVBUF
  51. int
  52. SetSocketBufSize(const int UNUSED(sockfd), const size_t UNUSED(rsize), const size_t UNUSED(ssize))
  53. {
  54.     LIBSIO_USE_VAR(sockfd);
  55.     LIBSIO_USE_VAR(rsize);
  56.     LIBSIO_USE_VAR(ssize);
  57.     return (-1);
  58. }    /* SetSocketBufSize */
  59. #else
  60. int
  61. SetSocketBufSize(const int sockfd, const size_t rsize, const size_t ssize)
  62. {
  63.     int rc = -1;
  64.     int opt;
  65.     sockopt_size_t optsize;
  66.  
  67. #ifdef TCP_RFC1323
  68.     /* This is an AIX-specific socket option to do RFC1323 large windows */
  69.     if (ssize != 0 || rsize != 0) {
  70.         opt = 1;
  71.         optsize = (sockopt_size_t) sizeof(opt);
  72.         rc = setsockopt(sockfd, IPPROTO_TCP, TCP_RFC1323, &opt, optsize);
  73.     }
  74. #endif
  75.  
  76.     if (ssize != 0) {
  77.         opt = (int) ssize;
  78.         optsize = (sockopt_size_t) sizeof(opt);
  79.         rc = setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF, GETSOCKOPT_ARG4 &opt, optsize);
  80.         if (rc < 0)
  81.             return (rc);
  82.     }
  83.     if (rsize != 0) {
  84.         opt = (int) rsize;
  85.         optsize = (sockopt_size_t) sizeof(opt);
  86.         rc = setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, GETSOCKOPT_ARG4 &opt, optsize);
  87.         if (rc < 0)
  88.             return (rc);
  89.     }
  90.     return (0);
  91. }    /* SetSocketBufSize */
  92. #endif
  93.  
  94.  
  95.  
  96.  
  97. #ifndef SO_KEEPALIVE
  98. int
  99. GetSocketKeepAlive(const int UNUSED(fd))
  100. {
  101.     LIBSIO_USE_VAR(fd);
  102.     return (-1);
  103. }    /* GetSocketKeepAlive */
  104. #else
  105. int
  106. GetSocketKeepAlive(const int fd)
  107. {
  108.     sockopt_size_t optsize;
  109.     int opt;
  110.  
  111.     opt = -2;
  112.     optsize = (sockopt_size_t) sizeof(opt);
  113.     if (getsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, &optsize) < 0)
  114.         return (-1);
  115.     return (opt);
  116. }    /* GetSocketKeepAlive */
  117. #endif    /* SO_KEEPALIVE */
  118.  
  119.  
  120.  
  121.  
  122.  
  123. #ifndef SO_KEEPALIVE
  124. int
  125. SetSocketKeepAlive(const int UNUSED(fd), const int UNUSED(onoff))
  126. {
  127.     LIBSIO_USE_VAR(fd);
  128.     LIBSIO_USE_VAR(onoff);
  129.     return (-1);
  130. }    /* SetSocketKeepAlive */
  131. #else
  132. int
  133. SetSocketKeepAlive(const int fd, const int onoff)
  134. {
  135.     int opt;
  136.  
  137.     opt = onoff;
  138.     return (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *) &opt, (sockopt_size_t) sizeof(opt)));
  139. }    /* SetSocketKeepAlive */
  140. #endif    /* SO_KEEPALIVE */
  141.  
  142.  
  143.  
  144.  
  145. #ifndef TCP_NODELAY
  146. int
  147. GetSocketNagleAlgorithm(const int UNUSED(fd))
  148. {
  149.     LIBSIO_USE_VAR(fd);
  150.     return (-1);
  151. }    /* GetSocketNagleAlgorithm */
  152. #else
  153. int
  154. GetSocketNagleAlgorithm(const int fd)
  155. {
  156.     sockopt_size_t optsize;
  157.     int opt;
  158.  
  159.     opt = -2;
  160.     optsize = (sockopt_size_t) sizeof(opt);
  161.     if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, &optsize) < 0)
  162.         return (-1);
  163.     return (opt);
  164. }    /* GetSocketNagleAlgorithm */
  165. #endif    /* TCP_NODELAY */
  166.  
  167.  
  168.  
  169.  
  170.  
  171. #ifndef TCP_NODELAY
  172. int
  173. SetSocketNagleAlgorithm(const int UNUSED(fd), const int UNUSED(onoff))
  174. {
  175.     LIBSIO_USE_VAR(fd);
  176.     LIBSIO_USE_VAR(onoff);
  177.     return (-1);
  178. }    /* SetSocketNagleAlgorithm */
  179. #else
  180. int
  181. SetSocketNagleAlgorithm(const int fd, const int onoff)
  182. {
  183.     int opt;
  184.  
  185.     opt = onoff;
  186.     return (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, (sockopt_size_t) sizeof(opt)));
  187. }    /* SetSocketNagleAlgorithm */
  188. #endif    /* TCP_NODELAY */
  189.  
  190.  
  191.  
  192.  
  193. #ifndef SO_OOBINLINE
  194. int
  195. GetSocketInlineOutOfBandData(const int UNUSED(fd))
  196. {
  197.     LIBSIO_USE_VAR(fd);
  198.     return (-1);
  199. }    /* GetSocketInlineOutOfBandData */
  200. #else
  201. int
  202. GetSocketInlineOutOfBandData(const int fd)
  203. {
  204.     sockopt_size_t optsize;
  205.     int opt;
  206.  
  207.     opt = -2;
  208.     optsize = (sockopt_size_t) sizeof(opt);
  209.     if (getsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char *) &opt, &optsize) < 0)
  210.         return (-1);
  211.     return (opt);
  212. }    /* GetSocketInlineOutOfBandData */
  213. #endif    /* SO_OOBINLINE */
  214.  
  215.  
  216.  
  217.  
  218.  
  219. #ifndef SO_OOBINLINE
  220. int
  221. SetSocketInlineOutOfBandData(const int UNUSED(fd), const int UNUSED(onoff))
  222. {
  223.     LIBSIO_USE_VAR(fd);
  224.     LIBSIO_USE_VAR(onoff);
  225.     return (-1);
  226. }    /* SetSocketInlineOutOfBandData */
  227. #else
  228. int
  229. SetSocketInlineOutOfBandData(const int fd, const int onoff)
  230. {
  231.     int opt;
  232.  
  233.     opt = onoff;
  234.     return (setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, (char *) &opt, (sockopt_size_t) sizeof(opt)));
  235. }    /* SetSocketInlineOutOfBandData */
  236. #endif    /* SO_OOBINLINE */
  237.  
  238.  
  239.  
  240.  
  241. #ifndef IP_TOS
  242. int
  243. GetSocketTypeOfService(const int UNUSED(fd))
  244. {
  245.     LIBSIO_USE_VAR(fd);
  246.     return (-1);
  247. }    /* GetSocketTypeOfService */
  248. #else
  249. int
  250. GetSocketTypeOfService(const int fd)
  251. {
  252.     sockopt_size_t optsize;
  253.     int opt;
  254.  
  255.     opt = -2;
  256.     optsize = (sockopt_size_t) sizeof(opt);
  257.     if (getsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, &optsize) < 0)
  258.         return (-1);
  259.     return (opt);
  260. }    /* GetSocketTypeOfService */
  261. #endif    /* IP_TOS */
  262.  
  263.  
  264.  
  265.  
  266.  
  267. #ifndef IP_TOS
  268. int
  269. SetSocketTypeOfService(const int UNUSED(fd), const int UNUSED(tosType))
  270. {
  271.     LIBSIO_USE_VAR(fd);
  272.     LIBSIO_USE_VAR(tosType);
  273.     return (-1);
  274. }    /* SetSocketTypeOfService */
  275. #else
  276. int
  277. SetSocketTypeOfService(const int fd, const int tosType)
  278. {
  279.     int opt;
  280.  
  281.     opt = tosType;
  282.     return (setsockopt(fd, IPPROTO_IP, IP_TOS, (char *) &opt, (sockopt_size_t) sizeof(opt)));
  283. }    /* SetSocketTypeOfService */
  284. #endif    /* IP_TOS */
  285.  
  286.  
  287.  
  288.  
  289. #ifndef SO_LINGER
  290. int
  291. GetSocketLinger(const int UNUSED(fd), int *const UNUSED(lingertime))
  292. {
  293.     LIBSIO_USE_VAR(fd);
  294.     LIBSIO_USE_VAR(lingertime);
  295.     return (-1);
  296. }    /* GetSocketLinger */
  297. #else
  298. int
  299. GetSocketLinger(const int fd, int *const lingertime)
  300. {
  301.     sockopt_size_t optsize;
  302.     struct linger opt;
  303.  
  304.     optsize = (sockopt_size_t) sizeof(opt);
  305.     opt.l_onoff = 0;
  306.     opt.l_linger = 0;
  307.     if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &opt, &optsize) < 0)
  308.         return (-1);
  309.     if (lingertime != NULL)
  310.         *lingertime = opt.l_linger;
  311.     return (opt.l_onoff);
  312. }    /* GetSocketLinger */
  313. #endif    /* SO_LINGER */
  314.  
  315.  
  316.  
  317. #ifndef SO_LINGER
  318. int
  319. SetSocketLinger(const int UNUSED(fd), const int UNUSED(l_onoff), const int UNUSED(l_linger))
  320. {
  321.     LIBSIO_USE_VAR(fd);
  322.     LIBSIO_USE_VAR(l_onoff);
  323.     LIBSIO_USE_VAR(l_linger);
  324.     return (-1);
  325. }    /* SetSocketLinger */
  326. #else
  327. int
  328. SetSocketLinger(const int fd, const int l_onoff, const int l_linger)
  329. {
  330.     struct linger opt;
  331.     sockopt_size_t optsize;
  332. /*
  333.  * From hpux:
  334.  *
  335.  * Structure used for manipulating linger option.
  336.  *
  337.  * if l_onoff == 0:
  338.  *    close(2) returns immediately; any buffered data is sent later
  339.  *    (default)
  340.  * 
  341.  * if l_onoff != 0:
  342.  *    if l_linger == 0, close(2) returns after discarding any unsent data
  343.  *    if l_linger != 0, close(2) does not return until buffered data is sent
  344.  */
  345. #if 0
  346. struct    linger {
  347.     int    l_onoff;        /* 0 = do not wait to send data */
  348.                     /* non-0 = see l_linger         */
  349.     int    l_linger;        /* 0 = discard unsent data      */
  350.                     /* non-0 = wait to send data    */
  351. };
  352. #endif
  353. #if (defined(WIN32) || defined(_WINDOWS)) && !defined(__CYGWIN__)
  354.     opt.l_onoff = (unsigned short) l_onoff;
  355.     opt.l_linger = (unsigned short)  l_linger;
  356. #else
  357.     opt.l_onoff = l_onoff;
  358.     opt.l_linger = l_linger;
  359. #endif
  360.     optsize = (sockopt_size_t) sizeof(opt);
  361.     return (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *) &opt, optsize));
  362. }    /* SetSocketLinger */
  363. #endif    /* SO_LINGER */
  364.