[Openswan dev] [PATCH] Kill duplicate pack_str code in whack.c

Herbert Xu herbert at gondor.apana.org.au
Mon Sep 19 13:02:55 CEST 2005


Hi:

I noticed that the pack_str code is duplicated between whacklib.c and
whack.c.  Here is a patch to get whack.c to use the packing code from
whacklib.c so it's all in one place.

Cheers,
-- 
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <herbert at gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
-------------- next part --------------
Index: programs/pluto/whack.c
===================================================================
RCS file: /public/cvs/openswan-2/programs/pluto/whack.c,v
retrieving revision 1.149
diff -u -r1.149 whack.c
--- programs/pluto/whack.c	18 Sep 2005 05:26:26 -0000	1.149
+++ programs/pluto/whack.c	19 Sep 2005 01:56:32 -0000
@@ -679,31 +679,6 @@
 #endif
 
 
-/* helper variables and function to encode strings from whack message */
-
-static unsigned char
-    *next_str,
-    *str_roof;
-
-static bool
-pack_str(char **p)
-{
-    const char *s = *p == NULL? "" : *p;	/* note: NULL becomes ""! */
-    size_t len = strlen(s) + 1;
-
-    if (str_roof - next_str < (ptrdiff_t)len)
-    {
-	return FALSE;	/* fishy: no end found */
-    }
-    else
-    {
-	strcpy((char *)next_str, s);
-	next_str += len;
-	*p = NULL;	/* don't send pointers on the wire! */
-	return TRUE;
-    }
-}
-
 static void
 check_life_time(time_t life, time_t limit, const char *which
 , const struct whack_message *msg)
@@ -867,6 +842,7 @@
 main(int argc, char **argv)
 {
     struct whack_message msg;
+    struct whackpacker wp;
     char esp_buf[256];	/* uses snprintf */
     lset_t
         opts_seen = LEMPTY,
@@ -882,6 +858,7 @@
     char xauthpass[128];
     int xauthnamelen = 0, xauthpasslen = 0;
     bool gotxauthname = FALSE, gotxauthpass = FALSE;
+    const char *ugh;
 
     /* check division of numbering space */
 #ifdef DEBUG
@@ -1078,7 +1055,7 @@
 	    {
 		static char keyspace[RSA_MAX_ENCODING_BYTES];
 		char diag_space[TTODATAV_BUF];
-		const char *ugh = ttodatav(optarg, 0, 0
+		ugh = ttodatav(optarg, 0, 0
 		    , keyspace, sizeof(keyspace)
 		    , &msg.keyval.len, diag_space, sizeof(diag_space)
 		    , TTODATAV_SPACECOUNTS);
@@ -1752,8 +1729,7 @@
 
 
     /* pack strings for inclusion in message */
-    next_str = msg.string;
-    str_roof = &msg.string[sizeof(msg.string)];
+    wp.msg = &msg;
 
     /* build esp message as esp="<esp>;<pfsgroup>" */
     if (msg.pfsgroup) {
@@ -1762,33 +1738,9 @@
 		    msg.pfsgroup ? msg.pfsgroup : "");
 	    msg.esp=esp_buf;
     }
-    if (!pack_str(&msg.name)		/* string  1 */
-    || !pack_str(&msg.left.id)		/* string  2 */
-    || !pack_str(&msg.left.cert)	/* string  3 */
-    || !pack_str(&msg.left.ca)		/* string  4 */
-    || !pack_str(&msg.left.groups)	/* string  5 */
-    || !pack_str(&msg.left.updown)	/* string  6 */
-#ifdef VIRTUAL_IP
-    || !pack_str(&msg.left.virt)
-#endif
-    || !pack_str(&msg.right.id)		/* string  7 */
-    || !pack_str(&msg.right.cert)	/* string  8 */
-    || !pack_str(&msg.right.ca)		/* string  9 */
-    || !pack_str(&msg.right.groups)	/* string  10 */
-    || !pack_str(&msg.right.updown)	/* string  11 */
-#ifdef VIRTUAL_IP
-    || !pack_str(&msg.right.virt)
-#endif
-    || !pack_str(&msg.keyid)		/* string 12 */
-    || !pack_str(&msg.myid)		/* string 13 */
-    || !pack_str(&msg.ike)		/* string 14 */
-    || !pack_str(&msg.esp)		/* string 15 */
-    || str_roof - next_str < (ptrdiff_t)msg.keyval.len)    /* chunk (sort of string 5) */
-	diag("too many bytes of strings to fit in message to pluto");
-
-    memcpy(next_str, msg.keyval.ptr, msg.keyval.len);
-    msg.keyval.ptr = NULL;
-    next_str += msg.keyval.len;
+    ugh = pack_whack_msg(&wp);
+    if (ugh)
+	diag(ugh);
 
     msg.magic = ((opts_seen & ~(LELEM(OPT_SHUTDOWN) | LELEM(OPT_STATUS)))
 		| lst_seen | cd_seen) != LEMPTY
@@ -1821,7 +1773,7 @@
     {
 	int sock = socket(AF_UNIX, SOCK_STREAM, 0);
 	int exit_status = 0;
-	ssize_t len = next_str - (unsigned char *)&msg;
+	ssize_t len = wp.str_next - (unsigned char *)&msg;
 
 	if (sock == -1)
 	{


More information about the Dev mailing list