Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/crypto/dist/ipsec-tools/src/setkey - in script mode always o...



details:   https://anonhg.NetBSD.org/src/rev/45f755897a21
branches:  trunk
changeset: 1010037:45f755897a21
user:      christos <christos%NetBSD.org@localhost>
date:      Tue May 12 16:17:58 2020 +0000

description:
- in script mode always output errors to stderr prefixed by the program name.
- in command mode always output errors to stdout not prefixed " " "
- perror(3) -> warn(3)

diffstat:

 crypto/dist/ipsec-tools/src/setkey/extern.h |  10 +++++-
 crypto/dist/ipsec-tools/src/setkey/setkey.c |  52 +++++++++++++---------------
 crypto/dist/ipsec-tools/src/setkey/token.l  |   7 ++-
 3 files changed, 39 insertions(+), 30 deletions(-)

diffs (201 lines):

diff -r eff24c0db59b -r 45f755897a21 crypto/dist/ipsec-tools/src/setkey/extern.h
--- a/crypto/dist/ipsec-tools/src/setkey/extern.h       Tue May 12 14:29:06 2020 +0000
+++ b/crypto/dist/ipsec-tools/src/setkey/extern.h       Tue May 12 16:17:58 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: extern.h,v 1.8 2020/05/12 14:29:06 christos Exp $      */
+/*     $NetBSD: extern.h,v 1.9 2020/05/12 16:17:58 christos Exp $      */
 
 /*
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -46,6 +46,7 @@
 #ifdef HAVE_POLICY_FWD
 extern int f_rfcmode;
 #endif
+extern int f_mode;
 extern const char *filename;
 extern int lineno;
 extern int exit_now;
@@ -53,3 +54,10 @@
 extern int last_msg_type;
 extern uint32_t last_priority;
 #endif
+
+#define MODE_SCRIPT    1
+#define MODE_CMDDUMP   2
+#define MODE_CMDFLUSH  3
+#define MODE_PROMISC   4
+#define MODE_STDIN     5
+
diff -r eff24c0db59b -r 45f755897a21 crypto/dist/ipsec-tools/src/setkey/setkey.c
--- a/crypto/dist/ipsec-tools/src/setkey/setkey.c       Tue May 12 14:29:06 2020 +0000
+++ b/crypto/dist/ipsec-tools/src/setkey/setkey.c       Tue May 12 16:17:58 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: setkey.c,v 1.21 2020/05/12 14:29:06 christos Exp $     */
+/*     $NetBSD: setkey.c,v 1.22 2020/05/12 16:17:58 christos Exp $     */
 /*     $KAME: setkey.c,v 1.36 2003/09/24 23:52:51 itojun Exp $ */
 
 /*
@@ -110,12 +110,6 @@
 static int32_t gmt2local(time_t);
 static void stdin_loop(void);
 
-#define MODE_SCRIPT    1
-#define MODE_CMDDUMP   2
-#define MODE_CMDFLUSH  3
-#define MODE_PROMISC   4
-#define MODE_STDIN     5
-
 int so;
 
 int f_forever = 0;
@@ -275,8 +269,7 @@
 
        so = pfkey_open();
        if (so < 0) {
-               perror("pfkey_open");
-               exit(1);
+               err(1, "pfkey_open");
        }
 
        switch (f_mode) {
@@ -497,7 +490,7 @@
        tv.tv_sec = 1;
        tv.tv_usec = 0;
        if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
-               perror("setsockopt");
+               warn("setsockopt");
                return NULL;
        }
     }
@@ -531,14 +524,14 @@
        }
 
        if ((l = send(so, buf, len, 0)) < 0) {
-               perror("send");
+               warn("send");
                return NULL;
        }
 
        m = (struct sadb_msg *)rbuf;
        do {
                if ((l = recv(so, rbuf, sizeof(rbuf), 0)) < 0) {
-                       perror("recv");
+                       warn("recv");
                        fail = 1;
                        break;
                }
@@ -638,7 +631,7 @@
        tv.tv_sec = 1;
        tv.tv_usec = 0;
        if (setsockopt(so, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
-               perror("setsockopt");
+               warn("setsockopt");
                goto end;
        }
     }
@@ -664,14 +657,14 @@
        }
 
        if ((l = send(so, buf, len, 0)) < 0) {
-               perror("send");
+               warn("send");
                goto end;
        }
 
        msg = (struct sadb_msg *)rbuf;
        do {
                if ((l = recv(so, rbuf, sizeof(rbuf), 0)) < 0) {
-                       perror("recv");
+                       warn("recv");
                        goto end;
                }
 
@@ -706,15 +699,8 @@
 #endif
 
        if (msg->sadb_msg_errno != 0) {
-               char inf[80];
                const char *errmsg = NULL;
 
-               if (f_mode == MODE_SCRIPT)
-                       snprintf(inf, sizeof(inf), "The result of line %d: ",
-                           lineno);
-               else
-                       inf[0] = '\0';
-
                switch (msg->sadb_msg_errno) {
                case ENOENT:
                        switch (msg->sadb_msg_type) {
@@ -734,7 +720,10 @@
                default:
                        errmsg = strerror(msg->sadb_msg_errno);
                }
-               printf("%s%s.\n", inf, errmsg);
+               if (f_mode == MODE_SCRIPT)
+                       warnx("%s,%d: %s", filename, lineno, errmsg);
+               else
+                       printf("%s.\n", errmsg);
                return -1;
        }
 
@@ -806,18 +795,27 @@
 
        /* check pfkey message. */
        if (pfkey_align(m, mhp)) {
-               printf("(%s\n", ipsec_strerror());
+               if (f_mode == MODE_SCRIPT)
+                       warnx("%s", ipsec_strerror());
+               else
+                       printf("%s\n", ipsec_strerror());
                return 0;
        }
        if (pfkey_check(mhp)) {
-               printf("%s\n", ipsec_strerror());
+               if (f_mode == MODE_SCRIPT)
+                       warnx("%s", ipsec_strerror());
+               else
+                       printf("%s\n", ipsec_strerror());
                return 0;
        }
 
        xpl = (struct sadb_x_policy *) mhp[SADB_X_EXT_POLICY];
 
        if (xpl == NULL) {
-               printf("no X_POLICY extension.\n");
+               if (f_mode == MODE_SCRIPT)
+                       warnx("no X_POLICY extension.");
+               else
+                       printf("no X_POLICY extension.\n");
                return 0;
        }
 
@@ -1012,7 +1010,7 @@
        int s;
 
        if (gettimeofday(&tp, NULL) == -1) {
-               perror("gettimeofday");
+               warn("gettimeofday");
                return;
        }
 
diff -r eff24c0db59b -r 45f755897a21 crypto/dist/ipsec-tools/src/setkey/token.l
--- a/crypto/dist/ipsec-tools/src/setkey/token.l        Tue May 12 14:29:06 2020 +0000
+++ b/crypto/dist/ipsec-tools/src/setkey/token.l        Tue May 12 16:17:58 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: token.l,v 1.25 2020/05/12 14:29:06 christos Exp $      */
+/*     $NetBSD: token.l,v 1.26 2020/05/12 16:17:58 christos Exp $      */
 /*     $KAME: token.l,v 1.44 2003/10/21 07:20:58 itojun Exp $  */
 
 /*
@@ -336,7 +336,10 @@
 void
 yyerror(const char *s)
 {
-       warnx("%s,%d: %s at `%s'", filename, lineno, s, yytext);
+       if (f_mode == MODE_SCRIPT)
+               warnx("%s,%d: %s at `%s'", filename, lineno, s, yytext);
+       else
+               printf("%s at `%s'", s, yytext);
 }
 
 int



Home | Main Index | Thread Index | Old Index