Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should the portrange data type accept a single port? #7242

Open
heiher opened this issue Aug 16, 2024 · 3 comments
Open

Should the portrange data type accept a single port? #7242

heiher opened this issue Aug 16, 2024 · 3 comments

Comments

@heiher
Copy link
Contributor

heiher commented Aug 16, 2024

I'm confused because, In natmap.js, the portrange data type accepts both a port range and a single port. However, In natmap.init, portrange does not accept a single port.

o = s.option(form.Value, 'port', _('Bind port'));
o.datatype = 'portrange';
o.rmempty = false;

https://github.com/openwrt/packages/blob/1019631a5a81a4b44035133364de2293bcc75d9d/net/natmap/files/natmap.init#L18-L33

validate_section_natmap() {
	uci_load_validate "${NAME}" natmap "$1" "$2" \
		'enable:bool:0' \
		'family:string' \
		'udp_mode:bool:0' \
		'interface:string' \
		'interval:uinteger' \
		'stun_server:host' \
		'http_server:host' \
		'port:portrange' \
		'forward_target:host' \
		'forward_port:port' \
		'notify_script:file' \
		'log_stdout:bool:1' \
		'log_stderr:bool:1'
}

Should the portrange data type accept a single port?

@systemcrash
Copy link
Contributor

systemcrash commented Aug 17, 2024

I would suggest that the ubox verify here also tries a 'port' type if the 'portrange' type check fails
https://git.openwrt.org/?p=project/ubox.git;a=blob;f=validate/validate.c;h=e72b8117ecd8b680778b0f5c7637ed6546a7736b;hb=refs/heads/master#l593

or

https://github.com/openwrt/ubox/blob/85f1053019caf4cd333795760950235ee4529ba7/validate/validate.c#L593

ping @jow- @robimarko @Ansuel

from e.g.

static bool
dt_type_port(struct dt_state *s, int nargs)
{
	int n;
	char *e;

	n = strtoul(s->value, &e, 10);

	return (e > s->value && *e == 0 && n <= 65535);
}

static bool
dt_type_portrange(struct dt_state *s, int nargs)
{
	int n, m;
	char *e;

	n = strtoul(s->value, &e, 10);

	if (e == s->value || *e != '-')
		return false;

	m = strtoul(e + 1, &e, 10);

	return (*e == 0 && n <= 65535 && m <= 65535 && n <= m);
}

to

static bool
dt_type_portrange(struct dt_state *s, int nargs)
{
	int n, m;
	char *e;

	n = strtoul(s->value, &e, 10);

-	if (e == s->value || *e != '-')
-		return false;
+	if (e == s->value || *e != '-') {
+		// If parsing as portrange fails, try parsing as a single port
+		return dt_type_port(s, nargs);
+	}


	m = strtoul(e + 1, &e, 10);

	return (*e == 0 && n <= 65535 && m <= 65535 && n <= m);
}

@systemcrash systemcrash changed the title Should the portrange data type accept a signle port? Should the portrange data type accept a single port? Aug 17, 2024
@systemcrash
Copy link
Contributor

Sent a patch to the dev mailing list...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants