00001
00002
00003
00004
00005 #ifdef USE_PCNFS
00006 #undef USE_PCNFS
00007 #endif
00008
00009 #include "rcs_defs.hh"
00010 #include "sokintrf.h"
00011 #include "tcp_opts.hh"
00012 #include "rcs_prnt.hh"
00013
00014 #ifndef UNDER_CE
00015 #include <errno.h>
00016 #endif
00017
00018 #include <string.h>
00019
00020 #ifdef UNIX_LIKE_PLAT
00021 #include <netinet/tcp.h>
00022 #ifndef qnx
00023 #include <sys/fcntl.h>
00024 #else
00025 #include <fcntl.h>
00026 #endif
00027 #endif
00028
00029 #ifdef sunos5
00030 #include <sys/types.h>
00031 #include <fcntl.h>
00032 #endif
00033
00034 #ifdef VXWORKS
00035 #include <netinet/tcp.h>
00036 #endif
00037
00038
00039 int RCS_EXPORT
00040 set_tcp_socket_options (int socket_fd)
00041 {
00042 if (socket_fd <= 0)
00043 {
00044 return -1;
00045 }
00046 int optval = 1;
00047 #if defined(_Windows) || !defined(MSDOS) && !defined(gnuwin32)
00048 if (dl_setsockopt (socket_fd, IPPROTO_TCP, TCP_NODELAY,
00049 (char *) &optval, sizeof (optval)) < 0)
00050 {
00051 rcs_print_error (" Can`t set a socket option.\n");
00052 #ifndef UNDER_CE
00053 rcs_print_error ("errno = %d = %s\n", errno, strerror (errno));
00054 #endif
00055 return -1;
00056 }
00057 #endif
00058 optval = 1;
00059
00060 #if !defined(MSDOS) || defined(_Windows)
00061 if (dl_setsockopt (socket_fd, SOL_SOCKET, SO_REUSEADDR,
00062 (char *) &optval, sizeof (optval)) < 0)
00063 {
00064 rcs_print_error (" Can`t set a socket option.\n");
00065 #ifndef UNDER_CE
00066 rcs_print_error ("errno = %d = %s\n", errno, strerror (errno));
00067 #endif
00068 return -1;
00069 }
00070 struct linger linger_opt;
00071 linger_opt.l_onoff = 0;
00072 linger_opt.l_linger = 0;
00073 if (dl_setsockopt (socket_fd, SOL_SOCKET, SO_LINGER,
00074 (char *) &linger_opt, sizeof (linger_opt)) < 0)
00075 {
00076 rcs_print_error (" Can`t set a socket option.\n");
00077 #ifndef UNDER_CE
00078 rcs_print_error ("errno = %d = %s\n", errno, strerror (errno));
00079 #endif
00080 return -1;
00081 }
00082 #endif
00083
00084 return 0;
00085 }
00086
00087
00088 int RCS_EXPORT
00089 make_tcp_socket_nonblocking (int socket_fd)
00090 {
00091 #ifdef _Windows
00092 unsigned long arg = 1;
00093 if (dl_ioctlsocket (socket_fd, FIONBIO, &arg) < 0)
00094 {
00095 rcs_print_error ("Can't set socket to non-blocking.");
00096 }
00097 #else
00098 #ifndef VXWORKS
00099 #ifdef O_NONBLOCK
00100 if (-1 == fcntl (socket_fd, F_SETFL, O_NONBLOCK))
00101 {
00102 rcs_print_error ("Couldn's set flag for non-blocking on socket.\n");
00103 return -1;
00104 }
00105 #else
00106 #ifdef O_NDELAY
00107 if (-1 == fcntl (socket_fd, F_SETFL, O_NDELAY))
00108 {
00109 rcs_print_error ("Couldn's set flag for no delay on socket.\n");
00110 return -1;
00111 }
00112 #endif
00113 #endif
00114
00115 #else
00116
00117 if (-1 == ioctl (socket_fd, FIONBIO, 1))
00118 {
00119 rcs_print_error ("Couldn's set flag for non-blocking on socket.\n");
00120 return -1;
00121 }
00122 #endif
00123 #endif
00124 return (0);
00125 }
00126
00127 int RCS_EXPORT
00128 make_tcp_socket_blocking (int socket_fd)
00129 {
00130 #ifdef _Windows
00131 unsigned long arg = 0;
00132 if (dl_ioctlsocket (socket_fd, FIONBIO, &arg) < 0)
00133 {
00134 rcs_print_error ("Can't set socket to blocking.");
00135 }
00136 #else
00137 #ifdef VXWORKS
00138 if (-1 == ioctl (socket_fd, FIONBIO, 0))
00139 {
00140 rcs_print_error ("Couldn's set flag for blocking on socket.: %d,%s\n",
00141 errno, strerror (errno));
00142 return -1;
00143 }
00144 #else
00145
00146 #if defined(O_NONBLOCK) || defined(O_NDELAY)
00147 int val = fcntl (socket_fd, F_GETFL, 0);
00148 if (val < 0)
00149 {
00150 rcs_print_error ("fcntl error %d %s\n", errno, strerror (errno));
00151 return -1;
00152 }
00153 #ifdef O_NONBLOCK
00154 val &= ~O_NONBLOCK;
00155 #endif
00156 #ifdef O_NDELAY
00157 val &= ~O_NDELAY;
00158 #endif
00159 if (fcntl (socket_fd, F_SETFL, val) < 0)
00160 {
00161 rcs_print_error ("Couldn's set flag for blocking on socket.: %d,%s\n",
00162 errno, strerror (errno));
00163 return -1;
00164 }
00165 #endif
00166 #endif
00167 #endif
00168 return (0);
00169 }