Add ability to have version "just requested" when you initially connect.
[m6w6/libmemcached] / libtest / port.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <config.h>
38 #include <libtest/common.h>
39
40 #include <cassert>
41 #include <cstdlib>
42 #include <cstring>
43 #include <ctime>
44 #include <fnmatch.h>
45 #include <iostream>
46 #include <sys/socket.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/types.h>
50 #include <sys/wait.h>
51 #include <unistd.h>
52
53 #include <utility>
54 #include <vector>
55
56 #include <signal.h>
57
58 #include <libtest/signal.h>
59
60 #ifndef __INTEL_COMPILER
61 #pragma GCC diagnostic ignored "-Wold-style-cast"
62 #endif
63
64 using namespace libtest;
65
66 struct socket_st {
67 typedef std::vector< std::pair< int, in_port_t> > socket_port_t;
68 socket_port_t _pair;
69
70 void release(in_port_t _arg)
71 {
72 for (socket_port_t::iterator iter= _pair.begin();
73 iter != _pair.end();
74 ++iter)
75 {
76 if ((*iter).second == _arg)
77 {
78 shutdown((*iter).first, SHUT_RDWR);
79 close((*iter).first);
80 }
81 }
82 }
83
84 ~socket_st()
85 {
86 for (socket_port_t::iterator iter= _pair.begin();
87 iter != _pair.end();
88 ++iter)
89 {
90 shutdown((*iter).first, SHUT_RDWR);
91 close((*iter).first);
92 }
93 }
94 };
95
96 static socket_st all_socket_fd;
97
98 static in_port_t global_port= 0;
99
100 namespace libtest {
101
102 in_port_t default_port()
103 {
104 if (global_port == 0)
105 {
106 global_port= get_free_port();
107 }
108
109 return global_port;
110 }
111
112 void release_port(in_port_t arg)
113 {
114 all_socket_fd.release(arg);
115 }
116
117 in_port_t get_free_port()
118 {
119 in_port_t ret_port= in_port_t(0);
120
121 int retries= 1024;
122
123 while (--retries)
124 {
125 int sd;
126 if ((sd= socket(AF_INET, SOCK_STREAM, 0)) != -1)
127 {
128 int optval= 1;
129 if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) != -1)
130 {
131 struct sockaddr_in sin;
132 sin.sin_port= 0;
133 sin.sin_addr.s_addr= 0;
134 sin.sin_addr.s_addr= INADDR_ANY;
135 sin.sin_family= AF_INET;
136
137 if (bind(sd, (struct sockaddr *)&sin,sizeof(struct sockaddr_in) ) != -1)
138 {
139 socklen_t addrlen= sizeof(sin);
140
141 if (getsockname(sd, (struct sockaddr *)&sin, &addrlen) != -1)
142 {
143 ret_port= sin.sin_port;
144 }
145 }
146 }
147
148 all_socket_fd._pair.push_back(std::make_pair(sd, ret_port));
149 }
150
151 if (ret_port > 1024)
152 {
153 break;
154 }
155 }
156
157 // We handle the case where if we max out retries, we still abort.
158 if (retries == 0)
159 {
160 fatal_message("No port could be found, exhausted retry");
161 }
162
163 if (ret_port == 0)
164 {
165 fatal_message("No port could be found");
166 }
167
168 if (ret_port <= 1024)
169 {
170 fatal_message("No port could be found, though some where available below or at 1024");
171 }
172
173 return ret_port;
174 }
175
176 } // namespace libtest