Line data Source code
1 : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
2 : * Copyright (C) 2018 Ziglio Frediano
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Library General Public
6 : * License as published by the Free Software Foundation; either
7 : * version 2 of the License, or (at your option) any later version.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Library General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Library General Public
15 : * License along with this library; if not, write to the
16 : * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 : * Boston, MA 02111-1307, USA.
18 : */
19 :
20 : #include <config.h>
21 :
22 : #include <stdio.h>
23 :
24 : #if HAVE_SYS_TYPES_H
25 : #include <sys/types.h>
26 : #endif /* HAVE_SYS_TYPES_H */
27 :
28 : #if HAVE_ERRNO_H
29 : #include <errno.h>
30 : #endif /* HAVE_ERRNO_H */
31 :
32 : #if HAVE_UNISTD_H
33 : #include <unistd.h>
34 : #endif /* HAVE_UNISTD_H */
35 :
36 : #if HAVE_STDLIB_H
37 : #include <stdlib.h>
38 : #endif /* HAVE_STDLIB_H */
39 :
40 : #if HAVE_SYS_SOCKET_H
41 : #include <sys/socket.h>
42 : #endif /* HAVE_SYS_SOCKET_H */
43 :
44 : #if HAVE_NETINET_IN_H
45 : #include <netinet/in.h>
46 : #endif /* HAVE_NETINET_IN_H */
47 :
48 : #if HAVE_NETINET_TCP_H
49 : #include <netinet/tcp.h>
50 : #endif /* HAVE_NETINET_TCP_H */
51 :
52 : #ifdef _WIN32
53 : #include <winsock2.h>
54 : #include <ws2tcpip.h>
55 : #include <mstcpip.h>
56 : #endif
57 :
58 : #include <freetds/utils.h>
59 : #include <freetds/utils/nosigpipe.h>
60 : #include <freetds/macros.h>
61 :
62 : #if !defined(SOL_TCP) && (defined(IPPROTO_TCP) || defined(_WIN32))
63 : /* fix incompatibility between MS headers */
64 : # ifndef IPPROTO_TCP
65 : # define IPPROTO_TCP IPPROTO_TCP
66 : # endif
67 : # define SOL_TCP IPPROTO_TCP
68 : #endif
69 :
70 : /* under VMS we have to define TCP_NODELAY */
71 : #if defined(__VMS) && !defined(TCP_NODELAY)
72 : #define TCP_NODELAY 1
73 : #endif
74 :
75 : /**
76 : * \addtogroup network
77 : * @{
78 : */
79 :
80 : /**
81 : * Set socket to not throw SIGPIPE.
82 : * Not many systems support this feature (in this case ENOTSUP can be
83 : * returned).
84 : * @param sock socket to set
85 : * @param on flag if enable or disable
86 : * @return 0 on success or error code
87 : */
88 : int
89 140 : tds_socket_set_nosigpipe(TDS_SYS_SOCKET sock TDS_UNUSED, int on TDS_UNUSED)
90 : {
91 : #if defined(SO_NOSIGPIPE)
92 : if (setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (const void *) &on, sizeof(on)))
93 : return sock_errno;
94 : return 0;
95 : #elif defined(_WIN32)
96 : return 0;
97 : #else
98 140 : return on ? ENOTSUP : 0;
99 : #endif
100 : }
101 :
102 : int
103 752 : tds_socket_set_nodelay(TDS_SYS_SOCKET sock)
104 : {
105 752 : int on = 1;
106 :
107 752 : if (setsockopt(sock, SOL_TCP, TCP_NODELAY, (const void *) &on, sizeof(on)))
108 0 : return sock_errno;
109 : return 0;
110 : }
111 :
112 : /** @} */
113 :
|