Start rhizome HTTP server on first available port

This commit is contained in:
Andrew Bettison 2012-07-02 16:24:07 +09:30
parent 1193763abd
commit 08a8ec13e8
2 changed files with 10 additions and 2 deletions

View File

@ -34,6 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#define RHIZOME_CRYPT_PAGE_SIZE 4096
#define RHIZOME_HTTP_PORT 4110
#define RHIZOME_HTTP_PORT_MAX 4150
extern long long rhizome_voice_timeout;

View File

@ -96,12 +96,18 @@ static int rhizome_http_server_start()
return WHY("Failed to start rhizome HTTP server");
}
/* Starting at the default port, look for a free port to bind to. */
struct sockaddr_in address;
bzero((char *) &address, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(RHIZOME_HTTP_PORT);
if (bind(rhizome_server_socket, (struct sockaddr *) &address, sizeof(address)) == -1) {
int port = RHIZOME_HTTP_PORT;
int result = -1;
do {
address.sin_port = htons(port);
result = bind(rhizome_server_socket, (struct sockaddr *) &address, sizeof(address));
} while (result == -1 && errno == EADDRINUSE && ++port <= RHIZOME_HTTP_PORT_MAX);
if (result == -1) {
WHY_perror("bind");
close(rhizome_server_socket);
rhizome_server_socket = -1;
@ -122,6 +128,7 @@ static int rhizome_http_server_start()
return WHY("Failed to start rhizome HTTP server");
}
INFOF("Started Rhizome HTTP server on port %d", port);
return 0;
}