pkstream/pkstream.c: Set a flag if we're listening.
authorMark Wooding <mdw@distorted.org.uk>
Wed, 27 Sep 2017 22:30:30 +0000 (23:30 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 16 Jun 2018 18:14:10 +0000 (19:14 +0100)
Rather than having to check the address.  This means that we don't need
to initialize `cw.me' if we /aren't/ listening, so don't.

pkstream/pkstream.c

index f1ddf77..b291a1c 100644 (file)
@@ -73,6 +73,8 @@ typedef struct pkstream {
 } pkstream;
 
 typedef struct connwait {
+  unsigned f;                          /* Various flags */
+#define cwf_port 1u                    /*   Port is defined => listen */
   sel_file a;                          /* Selector */
   struct sockaddr_in me, peer;        /* Who I'm meant to be; who peer is */
 } connwait;
@@ -115,7 +117,7 @@ static void doclose(pkstream *p)
     xfree(pk);
   }
   xfree(p);
-  if (cw.me.sin_port) dolisten();
+  if (cw.f&cwf_port) dolisten();
   else exit(0);
 }
 
@@ -337,6 +339,8 @@ int main(int argc, char *argv[])
 
 #define f_bogus 1u
 
+  cw.f = 0;
+
   ego(argv[0]);
   sel_init(&sel);
   for (;;) {
@@ -375,11 +379,14 @@ int main(int argc, char *argv[])
   if (bindsvc && connhost)
     die(1, "can't listen and connect");
 
-  initaddr(&cw.me);
   if (bindhost || bindsvc) {
     initaddr(&bindaddr);
     if (!bindsvc) parseaddr(bindhost, 0, 0, &bindaddr);
-    else parseaddr(bindhost, bindsvc, 0, &cw.me);
+    else {
+      initaddr(&cw.me);
+      parseaddr(bindhost, bindsvc, 0, &cw.me);
+      cw.f |= cwf_port;
+    }
   }
 
   initaddr(&cw.peer);