Browse Source

[uri] Allow tftp_uri() to construct a URI with a custom port

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 9 years ago
parent
commit
e2a26f76de
4 changed files with 30 additions and 7 deletions
  1. 8
    1
      src/core/uri.c
  2. 1
    1
      src/include/ipxe/uri.h
  3. 20
    4
      src/tests/uri_test.c
  4. 1
    1
      src/usr/autoboot.c

+ 8
- 1
src/core/uri.c View File

661
  * Construct TFTP URI from next-server and filename
661
  * Construct TFTP URI from next-server and filename
662
  *
662
  *
663
  * @v next_server	Next-server address
663
  * @v next_server	Next-server address
664
+ * @v port		Port number, or zero to use the default port
664
  * @v filename		Filename
665
  * @v filename		Filename
665
  * @ret uri		URI, or NULL on failure
666
  * @ret uri		URI, or NULL on failure
666
  *
667
  *
669
  * generic URI parser.  We provide a mechanism for directly
670
  * generic URI parser.  We provide a mechanism for directly
670
  * constructing a TFTP URI from the next-server and filename.
671
  * constructing a TFTP URI from the next-server and filename.
671
  */
672
  */
672
-struct uri * tftp_uri ( struct in_addr next_server, const char *filename ) {
673
+struct uri * tftp_uri ( struct in_addr next_server, unsigned int port,
674
+			const char *filename ) {
675
+	char buf[ 6 /* "65535" + NUL */ ];
673
 	struct uri uri;
676
 	struct uri uri;
674
 
677
 
675
 	memset ( &uri, 0, sizeof ( uri ) );
678
 	memset ( &uri, 0, sizeof ( uri ) );
676
 	uri.scheme = "tftp";
679
 	uri.scheme = "tftp";
677
 	uri.host = inet_ntoa ( next_server );
680
 	uri.host = inet_ntoa ( next_server );
681
+	if ( port ) {
682
+		snprintf ( buf, sizeof ( buf ), "%d", port );
683
+		uri.port = buf;
684
+	}
678
 	uri.path = filename;
685
 	uri.path = filename;
679
 	return uri_dup ( &uri );
686
 	return uri_dup ( &uri );
680
 }
687
 }

+ 1
- 1
src/include/ipxe/uri.h View File

203
 			     const char *relative_path );
203
 			     const char *relative_path );
204
 extern struct uri * resolve_uri ( const struct uri *base_uri,
204
 extern struct uri * resolve_uri ( const struct uri *base_uri,
205
 				  struct uri *relative_uri );
205
 				  struct uri *relative_uri );
206
-extern struct uri * tftp_uri ( struct in_addr next_server,
206
+extern struct uri * tftp_uri ( struct in_addr next_server, unsigned int port,
207
 			       const char *filename );
207
 			       const char *filename );
208
 extern void churi ( struct uri *uri );
208
 extern void churi ( struct uri *uri );
209
 
209
 

+ 20
- 4
src/tests/uri_test.c View File

66
 struct uri_tftp_test {
66
 struct uri_tftp_test {
67
 	/** Next-server address */
67
 	/** Next-server address */
68
 	struct in_addr next_server;
68
 	struct in_addr next_server;
69
+	/** Port number */
70
+	unsigned int port;
69
 	/** Filename */
71
 	/** Filename */
70
 	const char *filename;
72
 	const char *filename;
71
 	/** URI */
73
 	/** URI */
330
 	size_t len;
332
 	size_t len;
331
 
333
 
332
 	/* Construct URI */
334
 	/* Construct URI */
333
-	uri = tftp_uri ( test->next_server, test->filename );
335
+	uri = tftp_uri ( test->next_server, test->port, test->filename );
334
 	okx ( uri != NULL, file, line );
336
 	okx ( uri != NULL, file, line );
335
 	if ( uri ) {
337
 	if ( uri ) {
336
 		uri_okx ( uri, &test->uri, file, line );
338
 		uri_okx ( uri, &test->uri, file, line );
674
 
676
 
675
 /** TFTP URI with absolute path */
677
 /** TFTP URI with absolute path */
676
 static struct uri_tftp_test uri_tftp_absolute = {
678
 static struct uri_tftp_test uri_tftp_absolute = {
677
-	{ .s_addr = htonl ( 0xc0a80002 ) /* 192.168.0.2 */ },
679
+	{ .s_addr = htonl ( 0xc0a80002 ) /* 192.168.0.2 */ }, 0,
678
 	"/absolute/path",
680
 	"/absolute/path",
679
 	{
681
 	{
680
 		.scheme = "tftp",
682
 		.scheme = "tftp",
686
 
688
 
687
 /** TFTP URI with relative path */
689
 /** TFTP URI with relative path */
688
 static struct uri_tftp_test uri_tftp_relative = {
690
 static struct uri_tftp_test uri_tftp_relative = {
689
-	{ .s_addr = htonl ( 0xc0a80003 ) /* 192.168.0.3 */ },
691
+	{ .s_addr = htonl ( 0xc0a80003 ) /* 192.168.0.3 */ }, 0,
690
 	"relative/path",
692
 	"relative/path",
691
 	{
693
 	{
692
 		.scheme = "tftp",
694
 		.scheme = "tftp",
698
 
700
 
699
 /** TFTP URI with path containing special characters */
701
 /** TFTP URI with path containing special characters */
700
 static struct uri_tftp_test uri_tftp_icky = {
702
 static struct uri_tftp_test uri_tftp_icky = {
701
-	{ .s_addr = htonl ( 0x0a000006 ) /* 10.0.0.6 */ },
703
+	{ .s_addr = htonl ( 0x0a000006 ) /* 10.0.0.6 */ }, 0,
702
 	"C:\\tftpboot\\icky#path",
704
 	"C:\\tftpboot\\icky#path",
703
 	{
705
 	{
704
 		.scheme = "tftp",
706
 		.scheme = "tftp",
708
 	"tftp://10.0.0.6/C%3A\\tftpboot\\icky%23path",
710
 	"tftp://10.0.0.6/C%3A\\tftpboot\\icky%23path",
709
 };
711
 };
710
 
712
 
713
+/** TFTP URI with custom port */
714
+static struct uri_tftp_test uri_tftp_port = {
715
+	{ .s_addr = htonl ( 0xc0a80001 ) /* 192.168.0.1 */ }, 4069,
716
+	"/another/path",
717
+	{
718
+		.scheme = "tftp",
719
+		.host = "192.168.0.1",
720
+		.port = "4069",
721
+		.path = "/another/path",
722
+	},
723
+	"tftp://192.168.0.1:4069/another/path",
724
+};
725
+
711
 /** Current working URI test */
726
 /** Current working URI test */
712
 static struct uri_churi_test uri_churi[] = {
727
 static struct uri_churi_test uri_churi[] = {
713
 	{
728
 	{
842
 	uri_tftp_ok ( &uri_tftp_absolute );
857
 	uri_tftp_ok ( &uri_tftp_absolute );
843
 	uri_tftp_ok ( &uri_tftp_relative );
858
 	uri_tftp_ok ( &uri_tftp_relative );
844
 	uri_tftp_ok ( &uri_tftp_icky );
859
 	uri_tftp_ok ( &uri_tftp_icky );
860
+	uri_tftp_ok ( &uri_tftp_port );
845
 
861
 
846
 	/* Current working URI tests */
862
 	/* Current working URI tests */
847
 	uri_churi_ok ( uri_churi );
863
 	uri_churi_ok ( uri_churi );

+ 1
- 1
src/usr/autoboot.c View File

101
 	/* Construct a TFTP URI for the filename, if applicable */
101
 	/* Construct a TFTP URI for the filename, if applicable */
102
 	if ( next_server.s_addr && filename[0] && ! uri_is_absolute ( uri ) ) {
102
 	if ( next_server.s_addr && filename[0] && ! uri_is_absolute ( uri ) ) {
103
 		uri_put ( uri );
103
 		uri_put ( uri );
104
-		uri = tftp_uri ( next_server, filename );
104
+		uri = tftp_uri ( next_server, 0, filename );
105
 		if ( ! uri )
105
 		if ( ! uri )
106
 			return NULL;
106
 			return NULL;
107
 	}
107
 	}

Loading…
Cancel
Save