Revision 44290311 src/rm/RequestManager.cc

View differences:

src/rm/RequestManager.cc
54 54

  
55 55
#include <sys/signal.h>
56 56
#include <sys/socket.h>
57
#include <netinet/in.h>
58
#include <netinet/tcp.h>
59
#include <arpa/inet.h>
57
#include <sys/types.h>
58
#include <netdb.h>
60 59
#include <unistd.h>
61 60
#include <fcntl.h>
62 61
#include <string.h>
......
64 63

  
65 64

  
66 65
RequestManager::RequestManager(
67
        int _port,
66
        const string& _port,
68 67
        int _max_conn,
69 68
        int _max_conn_backlog,
70 69
        int _keepalive_timeout,
71 70
        int _keepalive_max_conn,
72 71
        int _timeout,
73
        const string _xml_log_file,
74
        const string call_log_format,
75
        const string _listen_address,
72
        const string& _xml_log_file,
73
        const string& call_log_format,
74
        const string& _listen_address,
76 75
        int message_size):
77 76
            port(_port),
78 77
            socket_fd(-1),
......
167 166

  
168 167
int RequestManager::setup_socket()
169 168
{
170
    int                 rc;
171
    int                 yes = 1;
172
    struct sockaddr_in  rm_addr;
169
    int rc;
170
    int yes = 1;
173 171

  
174
    socket_fd = socket(AF_INET, SOCK_STREAM, 0);
172
    struct addrinfo hints = {0};
173
    struct addrinfo * result;
175 174

  
176
    if ( socket_fd == -1 )
175
    hints.ai_family   = AF_UNSPEC;
176
    hints.ai_socktype = SOCK_STREAM;
177
    hints.ai_flags    = AI_PASSIVE;
178

  
179
    rc = getaddrinfo(listen_address.c_str(), port.c_str(), &hints, &result);
180

  
181
    if ( rc != 0 )
177 182
    {
178 183
        ostringstream oss;
179 184

  
180
        oss << "Cannot open server socket: " << strerror(errno);
185
        oss << "Cannot open server socket: " << gai_strerror(rc);
181 186
        NebulaLog::log("ReM",Log::ERROR,oss);
182 187

  
183 188
        return -1;
184 189
    }
185 190

  
186
    rc = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
191
    socket_fd = socket(result->ai_family, result->ai_socktype, 0);
187 192

  
188
    if ( rc == -1 )
193
    if ( socket_fd == -1 )
189 194
    {
190 195
        ostringstream oss;
191 196

  
192
        oss << "Cannot set socket options: " << strerror(errno);
197
        oss << "Cannot open server socket: " << strerror(errno);
193 198
        NebulaLog::log("ReM",Log::ERROR,oss);
194 199

  
195
        close(socket_fd);
200
        freeaddrinfo(result);
196 201

  
197 202
        return -1;
198 203
    }
199 204

  
200
    fcntl(socket_fd,F_SETFD,FD_CLOEXEC); // Close socket in MADs
201

  
202
    rm_addr.sin_family      = AF_INET;
203
    rm_addr.sin_port        = htons(port);
204

  
205
    rc = inet_aton(listen_address.c_str(), &rm_addr.sin_addr);
205
    rc = setsockopt(socket_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
206 206

  
207
    if ( rc == 0 )
207
    if ( rc == -1 )
208 208
    {
209 209
        ostringstream oss;
210 210

  
211
        oss << "Invalid listen address: " << listen_address;
211
        oss << "Cannot set socket options: " << strerror(errno);
212 212
        NebulaLog::log("ReM",Log::ERROR,oss);
213 213

  
214 214
        close(socket_fd);
215 215

  
216
        freeaddrinfo(result);
217

  
216 218
        return -1;
217 219
    }
218 220

  
219
    rc = bind(socket_fd,(struct sockaddr *) &(rm_addr),sizeof(struct sockaddr));
221
    fcntl(socket_fd,F_SETFD,FD_CLOEXEC); // Close socket in MADs
222

  
223
    rc = bind(socket_fd, result->ai_addr, result->ai_addrlen);
224

  
225
    freeaddrinfo(result);
220 226

  
221 227
    if ( rc == -1)
222 228
    {
223 229
        ostringstream oss;
224 230

  
225
        oss << "Cannot bind to " << listen_address << ":" << port << " : " << strerror(errno);
231
        oss << "Cannot bind to " << listen_address << ":" << port << " : "
232
            << strerror(errno);
233

  
226 234
        NebulaLog::log("ReM",Log::ERROR,oss);
227 235

  
228 236
        close(socket_fd);

Also available in: Unified diff