~brianaker/libmemcached/1164440

« back to all changes in this revision

Viewing changes to libtest/port.cc

  • Committer: Continuous Integration
  • Date: 2012-10-22 05:56:09 UTC
  • mfrom: (1086.1.8 libmemcached-1.0)
  • Revision ID: ci@tangent.org-20121022055609-cbooaw9bcdal4qge
Merge lp:~tangent-org/libmemcached/1.0-build Build: jenkins-Libmemcached-1.0-87

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
 
58
58
#include <libtest/signal.h>
59
59
 
 
60
#ifndef SOCK_CLOEXEC 
 
61
#  define SOCK_CLOEXEC 0
 
62
#endif
 
63
 
 
64
#ifndef SOCK_NONBLOCK 
 
65
#  define SOCK_NONBLOCK 0
 
66
#endif
 
67
 
 
68
#ifndef FD_CLOEXEC
 
69
#  define FD_CLOEXEC 0
 
70
#endif
 
71
 
60
72
#ifndef __INTEL_COMPILER
61
73
#pragma GCC diagnostic ignored "-Wold-style-cast"
62
74
#endif
66
78
struct socket_st {
67
79
  typedef std::vector< std::pair< int, in_port_t> > socket_port_t;
68
80
  socket_port_t _pair;
 
81
  in_port_t last_port;
 
82
 
 
83
  socket_st():
 
84
    last_port(0)
 
85
  { }
69
86
 
70
87
  void release(in_port_t _arg)
71
88
  {
116
133
 
117
134
in_port_t get_free_port()
118
135
{
119
 
  in_port_t ret_port= in_port_t(0);
 
136
  const in_port_t default_port= in_port_t(-1);
120
137
 
121
138
  int retries= 1024;
122
139
 
 
140
  in_port_t ret_port;
123
141
  while (--retries)
124
142
  {
 
143
    ret_port= default_port;
125
144
    int sd;
126
145
    if ((sd= socket(AF_INET, SOCK_STREAM, 0)) != -1)
127
146
    {
128
147
      int optval= 1;
129
148
      if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) != -1)
130
 
      {
 
149
      { 
131
150
        struct sockaddr_in sin;
132
151
        sin.sin_port= 0;
133
152
        sin.sin_addr.s_addr= 0;
134
153
        sin.sin_addr.s_addr= INADDR_ANY;
135
154
        sin.sin_family= AF_INET;
136
155
 
137
 
        if (bind(sd, (struct sockaddr *)&sin,sizeof(struct sockaddr_in) ) != -1)
 
156
        int bind_ret;
 
157
        do
138
158
        {
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)
 
159
          if ((bind_ret= bind(sd, (struct sockaddr *)&sin, sizeof(struct sockaddr_in) )) != -1)
 
160
          {
 
161
            socklen_t addrlen= sizeof(sin);
 
162
 
 
163
            if (getsockname(sd, (struct sockaddr *)&sin, &addrlen) != -1)
 
164
            {
 
165
              ret_port= sin.sin_port;
 
166
            }
 
167
          }
 
168
          else
 
169
          {
 
170
            if (errno != EADDRINUSE)
 
171
            {
 
172
              Error << strerror(errno);
 
173
            }
 
174
          }
 
175
 
 
176
          if (errno == EADDRINUSE)
 
177
          {
 
178
            libtest::dream(2, 0);
 
179
          }
 
180
        } while (bind_ret == -1 and errno == EADDRINUSE);
 
181
 
 
182
        all_socket_fd._pair.push_back(std::make_pair(sd, ret_port));
 
183
      }
 
184
      else
 
185
      {
 
186
        Error << strerror(errno);
 
187
      }
 
188
    }
 
189
    else
 
190
    {
 
191
      Error << strerror(errno);
 
192
    }
 
193
 
 
194
    if (ret_port == default_port)
 
195
    {
 
196
      Error << "no ret_port set:" << strerror(errno);
 
197
    }
 
198
    else if (ret_port > 1024 and ret_port != all_socket_fd.last_port)
152
199
    {
153
200
      break;
154
201
    }
165
212
    fatal_message("No port could be found");
166
213
  }
167
214
 
 
215
  if (ret_port == default_port)
 
216
  {
 
217
    fatal_message("No port could be found");
 
218
  }
 
219
 
168
220
  if (ret_port <= 1024)
169
221
  {
170
222
    fatal_message("No port could be found, though some where available below or at 1024");
171
223
  }
172
224
 
 
225
  all_socket_fd.last_port= ret_port;
 
226
  release_port(ret_port);
 
227
 
173
228
  return ret_port;
174
229
}
175
230