6edf20b
diff -up dhcp-4.0.0/client/dhclient.c.port dhcp-4.0.0/client/dhclient.c
e2381ff
--- dhcp-4.0.0/client/dhclient.c.port	2008-10-29 13:27:33.000000000 -1000
e2381ff
+++ dhcp-4.0.0/client/dhclient.c	2008-10-29 13:27:33.000000000 -1000
6edf20b
@@ -218,7 +218,7 @@ int main(int argc, char **argv, char **e
6edf20b
 		} else if (!strcmp (argv [i], "-p")) {
6edf20b
 			if (++i == argc)
6edf20b
 				usage ();
6edf20b
-			local_port = htons (atoi (argv [i]));
6edf20b
+			local_port = validate_port(argv[i]);
6edf20b
 			log_debug ("binding to user-specified port %d",
6edf20b
 			       ntohs (local_port));
6edf20b
 		} else if (!strcmp (argv [i], "-d")) {
6edf20b
diff -up dhcp-4.0.0/common/inet.c.port dhcp-4.0.0/common/inet.c
db59186
--- dhcp-4.0.0/common/inet.c.port	2007-07-12 20:43:41.000000000 -1000
e2381ff
+++ dhcp-4.0.0/common/inet.c	2008-10-29 13:27:33.000000000 -1000
db59186
@@ -604,3 +604,20 @@ piaddrcidr(const struct iaddr *addr, uns
6edf20b
 	return ret;
6edf20b
 }
db59186
 
6edf20b
+/* Check the port number specified */
6edf20b
+u_int16_t
6edf20b
+validate_port(char *port) {
6edf20b
+	u_int16_t local_port = 0;
6edf20b
+	int lower = 1;
6edf20b
+	int upper = 65535;
6edf20b
+
6edf20b
+	errno = 0;
6edf20b
+	local_port = strtol(port, NULL, 10);
6edf20b
+	if ((errno == ERANGE) || (errno == EINVAL))
6edf20b
+		log_fatal ("Invalid port number specification: %s", port);
6edf20b
+
6edf20b
+	if (local_port < lower || local_port > upper)
6edf20b
+		log_fatal("Port number specified is out of range (%d-%d).", lower, upper);
6edf20b
+
6edf20b
+	return htons(local_port);
6edf20b
+}
6edf20b
diff -up dhcp-4.0.0/includes/dhcpd.h.port dhcp-4.0.0/includes/dhcpd.h
e2381ff
--- dhcp-4.0.0/includes/dhcpd.h.port	2008-10-29 13:27:33.000000000 -1000
e2381ff
+++ dhcp-4.0.0/includes/dhcpd.h	2008-10-29 13:28:57.000000000 -1000
b0d88d6
@@ -64,6 +64,7 @@
b0d88d6
 #endif
6edf20b
 
b0d88d6
 #include <setjmp.h>
b0d88d6
+#include <errno.h>
b0d88d6
 
b0d88d6
 #include "cdefs.h"
b0d88d6
 #include "osdep.h"
e2381ff
@@ -2432,6 +2433,7 @@ isc_result_t free_iaddrcidrnetlist(struc
e2381ff
 const char *piaddr PROTO ((struct iaddr));
e2381ff
 char *piaddrmask(struct iaddr *, struct iaddr *);
e2381ff
 char *piaddrcidr(const struct iaddr *, unsigned int);
e2381ff
+u_int16_t validate_port(char *port);
e2381ff
 
e2381ff
 /* dhclient.c */
e2381ff
 extern int nowait;
6edf20b
diff -up dhcp-4.0.0/relay/dhcrelay.c.port dhcp-4.0.0/relay/dhcrelay.c
e2381ff
--- dhcp-4.0.0/relay/dhcrelay.c.port	2008-10-29 13:27:33.000000000 -1000
e2381ff
+++ dhcp-4.0.0/relay/dhcrelay.c	2008-10-29 13:27:33.000000000 -1000
6edf20b
@@ -141,7 +141,7 @@ main(int argc, char **argv) {
6edf20b
 		if (!strcmp (argv [i], "-p")) {
6edf20b
 			if (++i == argc)
6edf20b
 				usage ();
6edf20b
-			local_port = htons (atoi (argv [i]));
6edf20b
+			local_port = validate_port(argv[i]);
6edf20b
 			log_debug ("binding to user-specified port %d",
6edf20b
 			       ntohs (local_port));
6edf20b
 		} else if (!strcmp (argv [i], "-d")) {
6edf20b
diff -up dhcp-4.0.0/server/dhcpd.c.port dhcp-4.0.0/server/dhcpd.c
e2381ff
--- dhcp-4.0.0/server/dhcpd.c.port	2008-10-29 13:27:33.000000000 -1000
e2381ff
+++ dhcp-4.0.0/server/dhcpd.c	2008-10-29 13:27:33.000000000 -1000
6edf20b
@@ -257,15 +257,7 @@ main(int argc, char **argv) {
6edf20b
 		if (!strcmp (argv [i], "-p")) {
6edf20b
 			if (++i == argc)
6edf20b
 				usage ();
6edf20b
-			for (s = argv [i]; *s; s++)
6edf20b
-				if (!isdigit ((unsigned char)*s))
6edf20b
-					log_fatal ("%s: not a valid UDP port",
6edf20b
-					       argv [i]);
6edf20b
-			status = atoi (argv [i]);
6edf20b
-			if (status < 1 || status > 65535)
6edf20b
-				log_fatal ("%s: not a valid UDP port",
6edf20b
-				       argv [i]);
6edf20b
-			local_port = htons (status);
6edf20b
+			local_port = validate_port(argv[i]);
6edf20b
 			log_debug ("binding to user-specified port %d",
6edf20b
 			       ntohs (local_port));
6edf20b
 		} else if (!strcmp (argv [i], "-f")) {
6edf20b
@@ -438,7 +430,7 @@ main(int argc, char **argv) {
6edf20b
 	if (!local_port)
6edf20b
 	{
6edf20b
 		if ((s = getenv ("DHCPD_PORT"))) {
6edf20b
-			local_port = htons (atoi (s));
6edf20b
+			local_port = validate_port(s);
6edf20b
 			log_debug ("binding to environment-specified port %d",
6edf20b
 				   ntohs (local_port));
6edf20b
 		} else {