MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ndb_socket.cpp
1 /*
2  Copyright (c) 2009, 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 "my_global.h"
19 #include "m_string.h"
20 #include "ndb_socket.h"
21 
22 
23 /*
24  Implement my_socketpair() so that it works both on UNIX and windows
25 */
26 
27 #if defined _WIN32
28 
29 int my_socketpair(ndb_socket_t s[2])
30 {
31  struct sockaddr_in addr;
32  SOCKET_SIZE_TYPE addrlen = sizeof(addr);
33  ndb_socket_t listener;
34 
35  my_socket_invalidate(&listener);
36  my_socket_invalidate(&s[0]);
37  my_socket_invalidate(&s[1]);
38 
39  listener= my_socket_create(AF_INET, SOCK_STREAM, 0);
40  if (!my_socket_valid(listener))
41  return -1;
42 
43  bzero(&addr, sizeof(addr));
44  addr.sin_family = AF_INET;
45  addr.sin_addr.s_addr = htonl(0x7f000001); /* localhost */
46  addr.sin_port = 0; /* Any port */
47 
48  /* bind any local address */
49  if (my_bind_inet(listener, &addr) == -1)
50  goto err;
51 
52  /* get sockname */
53  /*
54  TODO: if there was a my_getsockname, this wouldnt have to use
55  definition of my_socket (i.e l.s)
56  */
57  if (getsockname(listener.s, (struct sockaddr*)&addr, &addrlen) < 0)
58  goto err;
59 
60  if (my_listen(listener, 1) == -1)
61  goto err;
62 
63  s[0]= my_socket_create(AF_INET, SOCK_STREAM, 0);
64 
65  if (!my_socket_valid(s[0]))
66  goto err;
67 
68  if (my_connect_inet(s[0], &addr) == -1)
69  goto err;
70 
71  s[1]= my_accept(listener, 0, 0);
72  if (!my_socket_valid(s[1]))
73  goto err;
74 
75  my_socket_close(listener);
76  return 0;
77 
78 err:
79  {
80  int save_errno = my_socket_errno();
81 
82  if (my_socket_valid(listener))
83  my_socket_close(listener);
84 
85  if (my_socket_valid(s[0]))
86  my_socket_close(s[0]);
87 
88  if (my_socket_valid(s[1]))
89  my_socket_close(s[1]);
90 
91  my_socket_set_errno(save_errno);
92  }
93  return -1;
94 }
95 
96 #else
97 
98 int my_socketpair(ndb_socket_t s[2])
99 {
100  int ret;
101  int sock[2];
102  ret= socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
103  if (ret == 0)
104  {
105  s[0].fd = sock[0];
106  s[1].fd = sock[1];
107  }
108  return ret;
109 }
110 
111 #endif
112