#include "mr_private.h"
#include <sys/types.h>
-#include <sys/socket.h>
-
-#include <netinet/in.h>
-#include <netdb.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
+
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
+
+#ifndef _WIN32
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#ifndef closesocket
+#define closesocket close
+#endif
+#ifndef SOCKET_ERROR
+#define SOCKET_ERROR -1
+#endif
+#endif
#ifdef HAVE_HESIOD
#include <hesiod.h>
+#ifdef _WIN32
+/* This is declared in wshelper's resolv.h, but the definition of
+ * the putlong macro conflicts with Moira's
+ */
+struct hostent * WINAPI rgethostbyname(char *name);
+#endif
#endif
RCSID("$Header$");
+#define DEFAULT_SERV "moira_db"
+#define DEFAULT_PORT 775
+
int _mr_conn = 0;
static char *mr_server_host = NULL;
int mr_connect_internal(char *server, char *port)
{
- int fd, size, more;
+ int size, more;
struct sockaddr_in target;
struct hostent *shost;
char actualresponse[53];
-
+ char *host = NULL;
+ int fd = SOCKET_ERROR;
+ int ok = 0;
+ int on = 1; /* Value variable for setsockopt() */
+
+#if defined(_WIN32) && defined(HAVE_HESIOD)
+ shost = rgethostbyname(server);
+#else
shost = gethostbyname(server);
+#endif
if (!shost)
- return 0;
+ goto cleanup;
+
+ /* Get the host info in case some library decides to clobber shost. */
+ memcpy(&target.sin_addr, shost->h_addr, shost->h_length);
+ target.sin_family = shost->h_addrtype;
+ host = strdup(shost->h_name);
if (port[0] == '#')
- target.sin_port = htons(atoi(port + 1));
+ target.sin_port = htons((unsigned short)atoi(port + 1));
else
{
struct servent *s;
+ target.sin_port = 0;
s = getservbyname(port, "tcp");
if (s)
target.sin_port = s->s_port;
- else
- return 0;
+#ifdef HAVE_HESIOD
+ if (!target.sin_port)
+ {
+ s = hes_getservbyname(port, "tcp");
+ if (s)
+ target.sin_port = s->s_port;
+ }
+#endif
+ if (!target.sin_port && !strcasecmp(port, DEFAULT_SERV))
+ target.sin_port = htons(DEFAULT_PORT);
+ if (!target.sin_port)
+ goto cleanup;
}
- memcpy(&target.sin_addr, shost->h_addr, shost->h_length);
- target.sin_family = shost->h_addrtype;
-
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
- return 0;
+ goto cleanup;
+
+ if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof(int)) < 0)
+ goto cleanup;
if (connect(fd, (struct sockaddr *)&target, sizeof(target)) < 0)
- {
- close(fd);
- return 0;
- }
+ goto cleanup;
/* Do magic mrgdb initialization */
- size = write(fd, challenge, sizeof(challenge));
+ size = send(fd, challenge, sizeof(challenge), 0);
if (size != sizeof(challenge))
- {
- close(fd);
- return 0;
- }
+ goto cleanup;
for (size = 0; size < sizeof(actualresponse); size += more)
{
- more = read(fd, actualresponse + size, sizeof(actualresponse) - size);
+ more = recv(fd, actualresponse + size, sizeof(actualresponse) - size, 0);
if (more <= 0)
break;
}
if (size != sizeof(actualresponse))
- {
- close(fd);
- return 0;
- }
+ goto cleanup;
if (memcmp(actualresponse, response, sizeof(actualresponse)))
+ goto cleanup;
+
+ ok = 1;
+ mr_server_host = host;
+
+ cleanup:
+ if (!ok)
{
- close(fd);
+ if (host)
+ free(host);
+ if (fd != SOCKET_ERROR)
+ closesocket(fd);
return 0;
}
-
- mr_server_host = strdup(shost->h_name);
-
/* You win */
return fd;
}
int mr_disconnect(void)
{
CHECK_CONNECTED;
- close(_mr_conn);
+ closesocket(_mr_conn);
_mr_conn = 0;
free(mr_server_host);
mr_server_host = NULL;
int s, on = 1;
memset(&sin, 0, sizeof(sin));
+ sin.sin_family = AF_INET;
if (port[0] == '#')
sin.sin_port = atoi(port + 1);
else
if (s)
sin.sin_port = s->s_port;
else
+#ifndef HAVE_HESIOD
return -1;
+#else
+ {
+ s = hes_getservbyname(port, "tcp");
+ if (s)
+ sin.sin_port = s->s_port;
+ else
+ return -1;
+ }
+#endif /* HAVE_HESIOD */
}
-
+
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
return -1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(int)) < 0)
{
- close(s);
+ closesocket(s);
return -1;
}
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
{
- close(s);
+ closesocket(s);
return -1;
}
if (listen(s, 5) < 0)
{
- close(s);
+ closesocket(s);
return -1;
}
int mr_accept(int s, struct sockaddr_in *sin)
{
- int conn, addrlen = sizeof(struct sockaddr_in), nread, status;
+ int conn = -1, addrlen = sizeof(struct sockaddr_in), nread, status;
char *buf = NULL;
- conn = accept(s, (struct sockaddr *)sin, &addrlen);
- if (conn < 0)
- return -1;
+ while (conn < 0)
+ {
+ conn = accept(s, (struct sockaddr *)sin, &addrlen);
+ if (conn < 0 && errno != EINTR
+#ifdef ERESTART
+ && errno != ERESTART
+#endif
+#ifdef ECONNABORTED
+ && errno != ECONNABORTED
+#endif
+ )
+ return -1;
+ }
do
status = mr_cont_accept(conn, &buf, &nread);
if (!*buf)
{
char lbuf[4];
- if (read(conn, lbuf, 4) != 4)
+ if (recv(conn, lbuf, 4, 0) != 4)
{
- close(conn);
+ closesocket(conn);
return 0;
}
getlong(lbuf, len);
len += 4;
+ if (len < 58 || len > 1000)
+ {
+ closesocket(conn);
+ return 0;
+ }
+
*buf = malloc(len);
- if (!*buf || len < 58)
+ if (!*buf)
{
- close(conn);
- free(*buf);
+ closesocket(conn);
return 0;
}
putlong(*buf, len);
else
getlong(*buf, len);
- more = read(conn, *buf + *nread, len - *nread);
+ more = recv(conn, *buf + *nread, len - *nread, 0);
- if (more == -1 && errno != EINTR)
+ switch (more)
{
- close(conn);
- free(*buf);
- return 0;
+ case 0:
+ /* If we read 0 bytes, the remote end has gone away. */
+ break;
+ case -1:
+ /* If errno is EINTR, return -1 and try again, otherwise we failed. */
+ if (errno == EINTR)
+ return -1;
+ else
+ {
+ closesocket(conn);
+ free(*buf);
+ return 0;
+ }
+ break;
+ default:
+ *nread += more;
+ if (*nread != len)
+ return -1;
+ break;
}
- *nread += more;
-
- if (*nread != len)
- return -1;
-
if (memcmp(*buf + 4, challenge + 4, 34))
{
- close(conn);
+ closesocket(conn);
free(*buf);
return 0;
}
/* good enough */
free(*buf);
- if (write(conn, response, sizeof(response)) != sizeof(response))
+ if (send(conn, response, sizeof(response), 0) != sizeof(response))
{
- close(conn);
+ closesocket(conn);
return 0;
}
return conn;