MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ndb_socket_posix.h
1 /*
2  Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include <netdb.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <fcntl.h>
22 #include <sys/uio.h>
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 
27 #define MY_SOCKET_FORMAT "%d"
28 #define MY_SOCKET_FORMAT_VALUE(x) (x.fd)
29 
30 typedef int ndb_native_socket_t;
31 typedef struct { int fd; } ndb_socket_t;
32 
33 static inline ndb_native_socket_t
34 ndb_socket_get_native(ndb_socket_t s)
35 {
36  return s.fd;
37 }
38 
39 static inline int my_socket_valid(ndb_socket_t s)
40 {
41  return (s.fd != -1);
42 }
43 
44 static inline ndb_socket_t* my_socket_invalidate(ndb_socket_t *s)
45 {
46  s->fd= -1;
47  return s;
48 }
49 
50 static inline ndb_socket_t my_socket_create_invalid()
51 {
52  ndb_socket_t s;
53  my_socket_invalidate(&s);
54  return s;
55 }
56 
57 static inline int my_socket_get_fd(ndb_socket_t s)
58 {
59  return s.fd;
60 }
61 
62 static inline int my_socket_close(ndb_socket_t s)
63 {
64  return close(s.fd);
65 }
66 
67 static inline int my_socket_errno()
68 {
69  return errno;
70 }
71 
72 static inline void my_socket_set_errno(int error)
73 {
74  errno= error;
75 }
76 
77 static inline ndb_socket_t my_socket_create(int domain, int type, int protocol)
78 {
79  ndb_socket_t s;
80  s.fd= socket(domain, type, protocol);
81 
82  return s;
83 }
84 
85 static inline ssize_t my_recv(ndb_socket_t s, char* buf, size_t len, int flags)
86 {
87  return recv(s.fd, buf, len, flags);
88 }
89 
90 static inline
91 ssize_t my_send(ndb_socket_t s, const char* buf, size_t len, int flags)
92 {
93  return send(s.fd, buf, len, flags);
94 }
95 
96 static inline int my_socket_reuseaddr(ndb_socket_t s, int enable)
97 {
98  const int on = enable;
99  return setsockopt(s.fd, SOL_SOCKET, SO_REUSEADDR,
100  (const void*)&on, sizeof(on));
101 }
102 
103 static inline int my_socket_nonblock(ndb_socket_t s, int enable)
104 {
105  int flags;
106  flags = fcntl(s.fd, F_GETFL, 0);
107  if (flags < 0)
108  return flags;
109 
110 #if defined(O_NONBLOCK)
111 #define NONBLOCKFLAG O_NONBLOCK
112 #elif defined(O_NDELAY)
113 #define NONBLOCKFLAG O_NDELAY
114 #endif
115 
116  if(enable)
117  flags |= NONBLOCKFLAG;
118  else
119  flags &= ~NONBLOCKFLAG;
120 
121  if (fcntl(s.fd, F_SETFL, flags) == -1)
122  return my_socket_errno();
123 
124  return 0;
125 #undef NONBLOCKFLAG
126 }
127 
128 static inline int my_bind(ndb_socket_t s, const struct sockaddr *my_addr,
129  SOCKET_SIZE_TYPE len)
130 {
131  return bind(s.fd, my_addr, len);
132 }
133 
134 static inline int my_bind_inet(ndb_socket_t s, const struct sockaddr_in *my_addr)
135 {
136  return bind(s.fd, (struct sockaddr*)my_addr, sizeof(struct sockaddr_in));
137 }
138 
139 static inline int my_socket_get_port(ndb_socket_t s, unsigned short *port)
140 {
141  struct sockaddr_in servaddr;
142  SOCKET_SIZE_TYPE sock_len = sizeof(servaddr);
143  if(getsockname(s.fd, (struct sockaddr*)&servaddr, &sock_len) < 0) {
144  return 1;
145  }
146 
147  *port= ntohs(servaddr.sin_port);
148  return 0;
149 }
150 
151 static inline int my_listen(ndb_socket_t s, int backlog)
152 {
153  return listen(s.fd, backlog);
154 }
155 
156 static inline
157 ndb_socket_t my_accept(ndb_socket_t s, struct sockaddr *addr,
158  SOCKET_SIZE_TYPE *addrlen)
159 {
160  ndb_socket_t r;
161  r.fd= accept(s.fd, addr, addrlen);
162  return r;
163 }
164 
165 static inline int my_connect_inet(ndb_socket_t s, const struct sockaddr_in *addr)
166 {
167  return connect(s.fd, (const struct sockaddr*)addr,
168  sizeof(struct sockaddr_in));
169 }
170 
171 static inline
172 int my_getsockopt(ndb_socket_t s, int level, int optname,
173  void *optval, SOCKET_SIZE_TYPE *optlen)
174 {
175  return getsockopt(s.fd, level, optname, optval, optlen);
176 }
177 
178 static inline
179 int my_setsockopt(ndb_socket_t s, int level, int optname,
180  void *optval, SOCKET_SIZE_TYPE optlen)
181 {
182  return setsockopt(s.fd, level, optname, optval, optlen);
183 }
184 
185 static inline int my_socket_connect_address(ndb_socket_t s, struct in_addr *a)
186 {
187  struct sockaddr_in addr;
188  SOCKET_SIZE_TYPE addrlen= sizeof(addr);
189  if(getpeername(s.fd, (struct sockaddr*)&addr, &addrlen))
190  return my_socket_errno();
191 
192  *a= addr.sin_addr;
193  return 0;
194 }
195 
196 static inline int my_getpeername(ndb_socket_t s, struct sockaddr *a, SOCKET_SIZE_TYPE *addrlen)
197 {
198  if(getpeername(s.fd, a, addrlen))
199  return my_socket_errno();
200 
201  return 0;
202 }
203 
204 static inline int my_shutdown(ndb_socket_t s, int how)
205 {
206  return shutdown(s.fd, how);
207 }
208 
209 static inline int my_socket_equal(ndb_socket_t s1, ndb_socket_t s2)
210 {
211  return s1.fd==s2.fd;
212 }
213 
214 static inline ssize_t my_socket_readv(ndb_socket_t s, const struct iovec *iov,
215  int iovcnt)
216 {
217  return readv(s.fd, iov, iovcnt);
218 }
219 static inline ssize_t my_socket_writev(ndb_socket_t s, const struct iovec *iov,
220  int iovcnt)
221 {
222  return writev(s.fd, iov, iovcnt);
223 }