Commit 3002f580 authored by Andrew Price's avatar Andrew Price
Browse files

Parse the -server arg early

Split the -server string in main() to keep the host and port separate
from the get go. Removes some alloca's in the ipc_connect code.
parent ec5ceec1
Loading
Loading
Loading
Loading
Loading
+12 −6
Original line number Diff line number Diff line
@@ -65,7 +65,6 @@ int internet=0;
int autochat=1;
int autowho=0;
const char *autoexec_arg;
const char *targethost = NULL;

struct alarm *timeout_event_1, *timeout_event_2;
extern int script_terminate;
@@ -346,6 +345,10 @@ int main(int argc, char **argv)
	int folderuser_num = -1;
	int folderuser_replyto = -1;
	int msguser_num = -1;
	char default_host[] = IPCHOST_DEFAULT;
	char default_port[] = IPCPORT_DEFAULT;
	char *svrhost = default_host;
	char *svrport = default_port;

	init_locale();
#if 0
@@ -424,7 +427,7 @@ int main(int argc, char **argv)
		{
			if (al < (argc - 1))
			{
				targethost = argv[al+1];
				svrhost = argv[al+1];
				al++;
				continue;
			}
@@ -609,10 +612,13 @@ int main(int argc, char **argv)
	}

	/* we need to be logged in before we connect to the server */
	if (targethost == NULL)
		targethost = "localhost";

	if (ipc_connect(targethost, user) < 0)
	if ((svrport = strrchr(svrhost, ':')) != NULL) {
		*svrport = 0;
		svrport++;
	} else {
		svrport = default_port;
	}
	if (ipc_connect(svrhost, svrport, user) < 0)
		exit(1);

	/* Now that we have a connection, announce the new user (if new) */
+17 −12
Original line number Diff line number Diff line
@@ -12,7 +12,8 @@

/* client mode uses this as its connection */
ipc_connection_t * ipcsock = NULL;
char *ipc_parent = NULL;
char *ipc_host = NULL;
char *ipc_port = NULL;
struct user *ipc_user;

const char *get_nonce(void);
@@ -20,7 +21,7 @@ const char *get_nonce(void);
int ipc_connected(void)
{
	/* we have never connected */
	if (ipc_parent == NULL) return -1;
	if (ipc_host == NULL) return -1;
	/* we are not connected now */
	if (ipcsock == NULL) return 0;
	if (ipcsock->fd == -1) return 0;
@@ -29,12 +30,14 @@ int ipc_connected(void)
	return 1;
}

int ipc_connect(const char *target, struct user *user)
int ipc_connect(const char *host, const char *port, struct user *user)
{
	const char * host = target;

	if (target == NULL) {
		fprintf(stderr, "Need a target to connect to.\n");
	if (host == NULL) {
		fprintf(stderr, "Need a host to connect to.\n");
		return -1;
	}
	if (port == NULL) {
		fprintf(stderr, "Need a port to connect to.\n");
		return -1;
	}
	if (user == NULL) {
@@ -53,15 +56,17 @@ int ipc_connect(const char *target, struct user *user)
		return -1;
	}

	int fd = ipcconn_connect(host);
	int fd = ipcconn_connect(host, port);
	if (fd < 0) {
		fprintf(stderr, "Connection to server failed.\n");
		return -1;
	}

	if (ipc_parent != target) {
		if (ipc_parent != NULL) free(ipc_parent);
		ipc_parent = strdup(target);
	if (ipc_host != host) {
		if (ipc_host != NULL) free(ipc_host);
		if (ipc_port != NULL) free(ipc_port);
		ipc_host = strdup(host);
		ipc_port = strdup(port);
	}
	ipc_user = user;

@@ -94,7 +99,7 @@ void ipc_check(void)
{
	/* not connected, try again */
	if (ipc_connected() <= 0)
		ipc_connect(ipc_parent, ipc_user);
		ipc_connect(ipc_host, ipc_port, ipc_user);
}

void ipc_close()
+1 −1
Original line number Diff line number Diff line
@@ -65,7 +65,7 @@ enum ipc_errors {
#endif


int ipc_connect(const char *target, struct user *user);
int ipc_connect(const char *host, const char *port, struct user *user);
int ipc_connected(void);
void ipc_check(void);
void ipc_close(void);
+1 −13
Original line number Diff line number Diff line
@@ -128,24 +128,12 @@ ipc_connection_t * ipcconn_create(void)
}

/* open a tcp socket, returning the fd */
int ipcconn_connect(const char * target)
int ipcconn_connect(const char *host, const char *port)
{
	struct addrinfo hint;
	struct addrinfo *list;
	int fd;

	char *host = alloca(strlen(target) + 1);
	char *port = NULL;
	strcpy(host, target);

	if ((port = strrchr(host, ':'))!=NULL) {
		*port = 0;
		port++;
	} else {
		port = alloca(6);
		snprintf(port, 6, "%d", IPCPORT_DEFAULT);
	}

	memset(&hint, 0, sizeof(hint));
	hint.ai_family = AF_INET;
	hint.ai_socktype = SOCK_STREAM;
+3 −2
Original line number Diff line number Diff line
@@ -5,7 +5,8 @@
#include <jansson.h>
#include "list.h"

#define IPCPORT_DEFAULT	9999
#define IPCHOST_DEFAULT	"localhost"
#define IPCPORT_DEFAULT	"9999"

/* which userposn is the System user,
 * 0 is possible but 1 is an impossible location
@@ -74,7 +75,7 @@ void ipcmsg_destroy(ipc_message_t *msg);
void ipcmsg_send(ipc_message_t *msg, ipc_connection_t *conn);
void ipcconn_bad(ipc_connection_t *conn);
ipc_connection_t *ipcconn_create(void);
int ipcconn_connect(const char *target);
int ipcconn_connect(const char *host, const char *port);
ipc_message_t *read_socket(ipc_connection_t *conn, int doread);
json_t *ipcmsg_json_decode(ipc_message_t *msg);
int ipcmsg_json_encode(ipc_message_t *msg, json_t *js);
Loading