Free port logic
[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 <libtest/common.h>
23
24 #include <cassert>
25 #include <cstdlib>
26 #include <cstring>
27 #include <ctime>
28 #include <fnmatch.h>
29 #include <iostream>
30 #include <sys/socket.h>
31 #include <sys/stat.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <unistd.h>
36
37 #include <signal.h>
38
39 #include <libtest/stats.h>
40 #include <libtest/signal.h>
41
42 #ifndef __INTEL_COMPILER
43 #pragma GCC diagnostic ignored "-Wold-style-cast"
44 #endif
45
46 using namespace libtest;
47
48 struct socket_st {
49 std::vector<int> fd;
50
51 ~socket_st()
52 {
53 for(std::vector<int>::iterator iter= fd.begin(); iter != fd.end(); iter++)
54 {
55 close(*iter);
56 }
57 }
58 };
59
60 static socket_st all_socket_fd;
61
62 static in_port_t global_port= 0;
63
64 namespace libtest {
65
66 in_port_t default_port()
67 {
68 if (global_port == 0)
69 {
70 global_port= get_free_port();
71 }
72
73 return global_port;
74 }
75
76 in_port_t get_free_port()
77 {
78 in_port_t ret_port= in_port_t(0);
79
80 int retries= 1024;
81
82 while (retries--)
83 {
84 int sd;
85 if ((sd= socket(AF_INET, SOCK_STREAM, 0)) != -1)
86 {
87 int optval= 1;
88 if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) != -1)
89 {
90 struct sockaddr_in sin;
91 sin.sin_port= 0;
92 sin.sin_addr.s_addr= 0;
93 sin.sin_addr.s_addr= INADDR_ANY;
94 sin.sin_family= AF_INET;
95
96 if (bind(sd, (struct sockaddr *)&sin,sizeof(struct sockaddr_in) ) != -1)
97 {
98 socklen_t addrlen= sizeof(sin);
99
100 if (getsockname(sd, (struct sockaddr *)&sin, &addrlen) != -1)
101 {
102 ret_port= sin.sin_port;
103 }
104 }
105 }
106
107 all_socket_fd.fd.push_back(sd);
108 }
109
110 if (ret_port > 1024)
111 {
112 break;
113 }
114 }
115
116 // We handle the case where if we max out retries, we still abort.
117 if (ret_port <= 1024)
118 {
119 throw fatal_message("No port could be found");
120 }
121
122 return ret_port;
123 }
124
125 } // namespace libtest