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, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 */
24
25 #include "my_global.h"
26 #include "m_string.h"
27 #include "ndb_socket.h"
28
29
30 /*
31 Implement my_socketpair() so that it works both on UNIX and windows
32 */
33
34 #if defined _WIN32
35
my_socketpair(ndb_socket_t s[2])36 int my_socketpair(ndb_socket_t s[2])
37 {
38 struct sockaddr_in addr;
39 SOCKET_SIZE_TYPE addrlen = sizeof(addr);
40 ndb_socket_t listener;
41
42 my_socket_invalidate(&listener);
43 my_socket_invalidate(&s[0]);
44 my_socket_invalidate(&s[1]);
45
46 listener= my_socket_create(AF_INET, SOCK_STREAM, 0);
47 if (!my_socket_valid(listener))
48 return -1;
49
50 bzero(&addr, sizeof(addr));
51 addr.sin_family = AF_INET;
52 addr.sin_addr.s_addr = htonl(0x7f000001); /* localhost */
53 addr.sin_port = 0; /* Any port */
54
55 /* bind any local address */
56 if (my_bind_inet(listener, &addr) == -1)
57 goto err;
58
59 /* get sockname */
60 /*
61 TODO: if there was a my_getsockname, this wouldnt have to use
62 definition of my_socket (i.e l.s)
63 */
64 if (getsockname(listener.s, (struct sockaddr*)&addr, &addrlen) < 0)
65 goto err;
66
67 if (my_listen(listener, 1) == -1)
68 goto err;
69
70 s[0]= my_socket_create(AF_INET, SOCK_STREAM, 0);
71
72 if (!my_socket_valid(s[0]))
73 goto err;
74
75 if (my_connect_inet(s[0], &addr) == -1)
76 goto err;
77
78 s[1]= my_accept(listener, 0, 0);
79 if (!my_socket_valid(s[1]))
80 goto err;
81
82 my_socket_close(listener);
83 return 0;
84
85 err:
86 {
87 int save_errno = my_socket_errno();
88
89 if (my_socket_valid(listener))
90 my_socket_close(listener);
91
92 if (my_socket_valid(s[0]))
93 my_socket_close(s[0]);
94
95 if (my_socket_valid(s[1]))
96 my_socket_close(s[1]);
97
98 my_socket_set_errno(save_errno);
99 }
100 return -1;
101 }
102
103 #else
104
my_socketpair(ndb_socket_t s[2])105 int my_socketpair(ndb_socket_t s[2])
106 {
107 int ret;
108 int sock[2];
109 ret= socketpair(AF_UNIX, SOCK_STREAM, 0, sock);
110 if (ret == 0)
111 {
112 s[0].fd = sock[0];
113 s[1].fd = sock[1];
114 }
115 return ret;
116 }
117
118 #endif
119
120