[mw-devel] [Git][arthur/mw][master] Parse the -server arg early

Andrew Price welshbyte at sucs.org
Mon Aug 14 13:26:09 BST 2017


Andrew Price pushed to branch master at Justin Mitchell / mw


Commits:
3002f580 by Andrew Price at 2017-08-14T13:20:19+01:00
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.

- - - - -


6 changed files:

- src/client/main.c
- src/ipc.c
- src/ipc.h
- src/socket.c
- src/socket.h
- src/webclient/mwpoll.c


Changes:

=====================================
src/client/main.c
=====================================
--- a/src/client/main.c
+++ b/src/client/main.c
@@ -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) */


=====================================
src/ipc.c
=====================================
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -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()


=====================================
src/ipc.h
=====================================
--- a/src/ipc.h
+++ b/src/ipc.h
@@ -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);


=====================================
src/socket.c
=====================================
--- a/src/socket.c
+++ b/src/socket.c
@@ -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;


=====================================
src/socket.h
=====================================
--- a/src/socket.h
+++ b/src/socket.h
@@ -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);


=====================================
src/webclient/mwpoll.c
=====================================
--- a/src/webclient/mwpoll.c
+++ b/src/webclient/mwpoll.c
@@ -68,9 +68,12 @@ int main(int argc, char ** argv)
 	int quietentry = 0;
 	int autocreate = 0;
 	const char *username = NULL;
-	const char *hostname = NULL;
 	char passwd[80];
 	char rawpw[80];
+	char default_host[] = IPCHOST_DEFAULT;
+	char default_port[] = IPCPORT_DEFAULT;
+	char *svrhost = default_host;
+	char *svrport = default_port;
 
 	while ((opt=getopt(argc,argv,"u:c:h:dsqa"))!=-1) {
 		switch (opt) {
@@ -78,7 +81,7 @@ int main(int argc, char ** argv)
 				username = optarg;
 				break;
 			case 'h':
-				hostname = optarg;
+				svrhost = optarg;
 				break;
 			case 'c':
 				channel = atoi(optarg);
@@ -209,10 +212,14 @@ int main(int argc, char ** argv)
 		printf("http://sucs.org/~arthur/mw/index.php?mwsess=a:2:{s:3:\"pid\";i:%d;s:4:\"auth\";s:%zd:\"%s\";}\n", getpid(), strlen(authtext), authtext);
 	}
 
-	/* load us up */
-	if (hostname == NULL)
-		hostname = "localhost";
-	ipc_connect(hostname, &me);
+	if ((svrport = strrchr(svrhost, ':')) != NULL) {
+		*svrport = 0;
+		svrport++;
+	} else {
+		svrport = default_port;
+	}
+	if (ipc_connect(svrhost, svrport, &me) < 0)
+		exit(1);
 	open_command_socket();
 
 	if (u_ban(&me)) {



View it on GitLab: https://projects.sucs.org/arthur/mw/commit/3002f580e12a164dba3bc6e507bfdc4d53bd081b

---
View it on GitLab: https://projects.sucs.org/arthur/mw/commit/3002f580e12a164dba3bc6e507bfdc4d53bd081b
You're receiving this email because of your account on projects.sucs.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20170814/507d63e2/attachment-0001.html>


More information about the mw-devel mailing list