Merging bzr://gaz.tangent.org/libmemcached/build/ to Build branch
[awesomized/libmemcached] / libtest / port.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <config.h>
23 #include <libtest/common.h>
24
25 #include <cassert>
26 #include <cstdlib>
27 #include <cstring>
28 #include <ctime>
29 #include <fnmatch.h>
30 #include <iostream>
31 #include <sys/socket.h>
32 #include <sys/stat.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
37
38 #include <signal.h>
39
40 #include <libtest/stats.h>
41 #include <libtest/signal.h>
42
43 #ifndef __INTEL_COMPILER
44 #pragma GCC diagnostic ignored "-Wold-style-cast"
45 #endif
46
47 using namespace libtest;
48
49 struct socket_st {
50 std::vector<int> fd;
51
52 ~socket_st()
53 {
54 for(std::vector<int>::iterator iter= fd.begin(); iter != fd.end(); iter++)
55 {
56 close(*iter);
57 }
58 }
59 };
60
61 static socket_st all_socket_fd;
62
63 static in_port_t global_port= 0;
64
65 namespace libtest {
66
67 in_port_t default_port()
68 {
69 if (global_port == 0)
70 {
71 global_port= get_free_port();
72 }
73
74 return global_port;
75 }
76
77 in_port_t get_free_port()
78 {
79 in_port_t ret_port= in_port_t(0);
80
81 int retries= 1024;
82
83 while (retries--)
84 {
85 int sd;
86 if ((sd= socket(AF_INET, SOCK_STREAM, 0)) != -1)
87 {
88 int optval= 1;
89 if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) != -1)
90 {
91 struct sockaddr_in sin;
92 sin.sin_port= 0;
93 sin.sin_addr.s_addr= 0;
94 sin.sin_addr.s_addr= INADDR_ANY;
95 sin.sin_family= AF_INET;
96
97 if (bind(sd, (struct sockaddr *)&sin,sizeof(struct sockaddr_in) ) != -1)
98 {
99 socklen_t addrlen= sizeof(sin);
100
101 if (getsockname(sd, (struct sockaddr *)&sin, &addrlen) != -1)
102 {
103 ret_port= sin.sin_port;
104 }
105 }
106 }
107
108 all_socket_fd.fd.push_back(sd);
109 }
110
111 if (ret_port > 1024)
112 {
113 break;
114 }
115 }
116
117 // We handle the case where if we max out retries, we still abort.
118 if (ret_port <= 1024)
119 {
120 fatal_message("No port could be found");
121 }
122
123 return ret_port;
124 }
125
126 } // namespace libtest