Kaynağa Gözat

[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 yıl önce
ebeveyn
işleme
e2a26f76de
4 değiştirilmiş dosya ile 30 ekleme ve 7 silme
  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 Dosyayı Görüntüle

@@ -661,6 +661,7 @@ struct uri * resolve_uri ( const struct uri *base_uri,
661 661
  * Construct TFTP URI from next-server and filename
662 662
  *
663 663
  * @v next_server	Next-server address
664
+ * @v port		Port number, or zero to use the default port
664 665
  * @v filename		Filename
665 666
  * @ret uri		URI, or NULL on failure
666 667
  *
@@ -669,12 +670,18 @@ struct uri * resolve_uri ( const struct uri *base_uri,
669 670
  * generic URI parser.  We provide a mechanism for directly
670 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 676
 	struct uri uri;
674 677
 
675 678
 	memset ( &uri, 0, sizeof ( uri ) );
676 679
 	uri.scheme = "tftp";
677 680
 	uri.host = inet_ntoa ( next_server );
681
+	if ( port ) {
682
+		snprintf ( buf, sizeof ( buf ), "%d", port );
683
+		uri.port = buf;
684
+	}
678 685
 	uri.path = filename;
679 686
 	return uri_dup ( &uri );
680 687
 }

+ 1
- 1
src/include/ipxe/uri.h Dosyayı Görüntüle

@@ -203,7 +203,7 @@ extern char * resolve_path ( const char *base_path,
203 203
 			     const char *relative_path );
204 204
 extern struct uri * resolve_uri ( const struct uri *base_uri,
205 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 207
 			       const char *filename );
208 208
 extern void churi ( struct uri *uri );
209 209
 

+ 20
- 4
src/tests/uri_test.c Dosyayı Görüntüle

@@ -66,6 +66,8 @@ struct uri_resolve_test {
66 66
 struct uri_tftp_test {
67 67
 	/** Next-server address */
68 68
 	struct in_addr next_server;
69
+	/** Port number */
70
+	unsigned int port;
69 71
 	/** Filename */
70 72
 	const char *filename;
71 73
 	/** URI */
@@ -330,7 +332,7 @@ static void uri_tftp_okx ( struct uri_tftp_test *test, const char *file,
330 332
 	size_t len;
331 333
 
332 334
 	/* Construct URI */
333
-	uri = tftp_uri ( test->next_server, test->filename );
335
+	uri = tftp_uri ( test->next_server, test->port, test->filename );
334 336
 	okx ( uri != NULL, file, line );
335 337
 	if ( uri ) {
336 338
 		uri_okx ( uri, &test->uri, file, line );
@@ -674,7 +676,7 @@ static struct uri_resolve_test uri_fragment = {
674 676
 
675 677
 /** TFTP URI with absolute path */
676 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 680
 	"/absolute/path",
679 681
 	{
680 682
 		.scheme = "tftp",
@@ -686,7 +688,7 @@ static struct uri_tftp_test uri_tftp_absolute = {
686 688
 
687 689
 /** TFTP URI with relative path */
688 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 692
 	"relative/path",
691 693
 	{
692 694
 		.scheme = "tftp",
@@ -698,7 +700,7 @@ static struct uri_tftp_test uri_tftp_relative = {
698 700
 
699 701
 /** TFTP URI with path containing special characters */
700 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 704
 	"C:\\tftpboot\\icky#path",
703 705
 	{
704 706
 		.scheme = "tftp",
@@ -708,6 +710,19 @@ static struct uri_tftp_test uri_tftp_icky = {
708 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 726
 /** Current working URI test */
712 727
 static struct uri_churi_test uri_churi[] = {
713 728
 	{
@@ -842,6 +857,7 @@ static void uri_test_exec ( void ) {
842 857
 	uri_tftp_ok ( &uri_tftp_absolute );
843 858
 	uri_tftp_ok ( &uri_tftp_relative );
844 859
 	uri_tftp_ok ( &uri_tftp_icky );
860
+	uri_tftp_ok ( &uri_tftp_port );
845 861
 
846 862
 	/* Current working URI tests */
847 863
 	uri_churi_ok ( uri_churi );

+ 1
- 1
src/usr/autoboot.c Dosyayı Görüntüle

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

Loading…
İptal
Kaydet