From 516600858cb54906fb728d04e5edf1131ee7b3b2 Mon Sep 17 00:00:00 2001
From: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
Date: Tue, 10 Apr 2018 20:48:35 +0200
Subject: Fix parsing service names for ports

Parsing is attempted both for numbers and service names and
the temporary stored error message triggered to reset the state
parameters about the set. Reported by Yuri D'Elia.
---
 Make_global.am             |  2 +-
 include/libipset/session.h |  1 +
 lib/libipset.map           |  5 +++++
 lib/parse.c                | 22 ++++++++++++----------
 lib/session.c              | 15 +++++++++++++++
 tests/hash:ip,port.t       |  4 ++++
 6 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/Make_global.am b/Make_global.am
index 4b0ac11..10334cc 100644
--- a/Make_global.am
+++ b/Make_global.am
@@ -69,7 +69,7 @@
 # interface. 
 
 #            curr:rev:age
-LIBVERSION = 11:0:0
+LIBVERSION = 12:0:1
 
 AM_CPPFLAGS = $(kinclude_CFLAGS) $(all_includes) -I$(top_srcdir)/include
 
diff --git a/include/libipset/session.h b/include/libipset/session.h
index 833f763..bac4d19 100644
--- a/include/libipset/session.h
+++ b/include/libipset/session.h
@@ -42,6 +42,7 @@ enum ipset_err_type {
 extern int ipset_session_report(struct ipset_session *session,
 				enum ipset_err_type type,
 				const char *fmt, ...);
+extern int ipset_session_warning_as_error(struct ipset_session *session);
 
 #define ipset_err(session, fmt, args...) \
 	ipset_session_report(session, IPSET_ERROR, fmt , ## args)
diff --git a/lib/libipset.map b/lib/libipset.map
index a2e9bd3..475fae5 100644
--- a/lib/libipset.map
+++ b/lib/libipset.map
@@ -168,3 +168,8 @@ LIBIPSET_4.6 {
 global:
   ipset_keyword;
 } LIBIPSET_4.5;
+
+LIBIPSET_4.7 {
+global:
+  ipset_session_warning_as_error;
+} LIBIPSET_4.6;
diff --git a/lib/parse.c b/lib/parse.c
index bc8e777..7cd6436 100644
--- a/lib/parse.c
+++ b/lib/parse.c
@@ -292,7 +292,8 @@ parse_portname(struct ipset_session *session, const char *str,
 
 error:
 	free(saved);
-	return syntax_err("cannot parse '%s' as a %s port", str, proto);
+	return ipset_warn(session, "cannot parse '%s' as a %s port",
+			  str, proto);
 }
 
 /**
@@ -313,21 +314,22 @@ ipset_parse_port(struct ipset_session *session,
 		 const char *proto)
 {
 	uint16_t port;
-	int err;
 
 	assert(session);
 	assert(opt == IPSET_OPT_PORT || opt == IPSET_OPT_PORT_TO);
 	assert(str);
 
-	if ((err = string_to_u16(session, str, &port)) == 0 ||
-	    (err = parse_portname(session, str, &port, proto)) == 0)
-		err = ipset_session_data_set(session, opt, &port);
-
-	if (!err)
-		/* No error, so reset false error messages! */
+	if (parse_portname(session, str, &port, proto) == 0) {
+		return ipset_session_data_set(session, opt, &port);
+	}
+	/* Error is stored as warning in session report */
+	if (string_to_u16(session, str, &port) == 0) {
+		/* No error, so reset false error messages */
 		ipset_session_report_reset(session);
-
-	return err;
+		return ipset_session_data_set(session, opt, &port);
+	}
+	/* Restore warning as error */
+	return ipset_session_warning_as_error(session);
 }
 
 /**
diff --git a/lib/session.c b/lib/session.c
index b1c5f5e..ca96aaa 100644
--- a/lib/session.c
+++ b/lib/session.c
@@ -239,6 +239,21 @@ ipset_session_report(struct ipset_session *session,
 	return -1;
 }
 
+/**
+ * ipset_session_warning_as_error - set warning as error
+ * @session: session structrure
+ *
+ * Returns -1.
+ */
+int
+ipset_session_warning_as_error(struct ipset_session *session)
+{
+	session->errmsg = session->report;
+	session->warnmsg = NULL;
+	ipset_data_reset(ipset_session_data(session));
+	return -1;
+}
+
 /**
  * ipset_session_reset - reset the report buffer
  * @session: session structure
diff --git a/tests/hash:ip,port.t b/tests/hash:ip,port.t
index be323e0..aba8fdc 100644
--- a/tests/hash:ip,port.t
+++ b/tests/hash:ip,port.t
@@ -26,6 +26,10 @@
 0 ipset add test 2.0.0.0,5
 # Try to add value after second random value
 0 ipset add test 2.1.0.1,128
+# Add port by name
+0 ipset add test 2.1.0.3,smtp
+# Delete port by number
+0 ipset del test 2.1.0.3,25
 # List set
 0 ipset list test | grep -v Revision: | sed 's/timeout ./timeout x/' > .foo0 && ./sort.sh .foo0
 # Check listing
-- 
cgit v1.2.1

