9f9a5bfeede83d9053a1504b2f44cc70bc5e1ab5
[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 <signal.h>
54
55 #include <libtest/signal.h>
56
57 #ifndef __INTEL_COMPILER
58 #pragma GCC diagnostic ignored "-Wold-style-cast"
59 #endif
60
61 using namespace libtest;
62
63 struct socket_st {
64 std::vector<int> fd;
65
66 ~socket_st()
67 {
68 for(std::vector<int>::iterator iter= fd.begin(); iter != fd.end(); iter++)
69 {
70 close(*iter);
71 }
72 }
73 };
74
75 static socket_st all_socket_fd;
76
77 static in_port_t global_port= 0;
78
79 namespace libtest {
80
81 in_port_t default_port()
82 {
83 if (global_port == 0)
84 {
85 global_port= get_free_port();
86 }
87
88 return global_port;
89 }
90
91 in_port_t get_free_port()
92 {
93 in_port_t ret_port= in_port_t(0);
94
95 int retries= 1024;
96
97 while (retries--)
98 {
99 int sd;
100 if ((sd= socket(AF_INET, SOCK_STREAM, 0)) != -1)
101 {
102 int optval= 1;
103 if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) != -1)
104 {
105 struct sockaddr_in sin;
106 sin.sin_port= 0;
107 sin.sin_addr.s_addr= 0;
108 sin.sin_addr.s_addr= INADDR_ANY;
109 sin.sin_family= AF_INET;
110
111 if (bind(sd, (struct sockaddr *)&sin,sizeof(struct sockaddr_in) ) != -1)
112 {
113 socklen_t addrlen= sizeof(sin);
114
115 if (getsockname(sd, (struct sockaddr *)&sin, &addrlen) != -1)
116 {
117 ret_port= sin.sin_port;
118 }
119 }
120 }
121
122 all_socket_fd.fd.push_back(sd);
123 }
124
125 if (ret_port > 1024)
126 {
127 break;
128 }
129 }
130
131 // We handle the case where if we max out retries, we still abort.
132 if (ret_port <= 1024)
133 {
134 fatal_message("No port could be found");
135 }
136
137 return ret_port;
138 }
139
140 } // namespace libtest