]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[uri] Allow tftp_uri() to construct a URI with a custom port
authorMichael Brown <mcb30@ipxe.org>
Fri, 6 Feb 2015 12:18:18 +0000 (12:18 +0000)
committerMichael Brown <mcb30@ipxe.org>
Fri, 6 Feb 2015 12:18:18 +0000 (12:18 +0000)
Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/core/uri.c
src/include/ipxe/uri.h
src/tests/uri_test.c
src/usr/autoboot.c

index 9ec21cee43d5a8c75a474202ef190722b9adf08a..d60f5d2b746000ec9f773f5e405604dfc204fbd3 100644 (file)
@@ -661,6 +661,7 @@ struct uri * resolve_uri ( const struct uri *base_uri,
  * Construct TFTP URI from next-server and filename
  *
  * @v next_server      Next-server address
+ * @v port             Port number, or zero to use the default port
  * @v filename         Filename
  * @ret uri            URI, or NULL on failure
  *
@@ -669,12 +670,18 @@ struct uri * resolve_uri ( const struct uri *base_uri,
  * generic URI parser.  We provide a mechanism for directly
  * constructing a TFTP URI from the next-server and filename.
  */
-struct uri * tftp_uri ( struct in_addr next_server, const char *filename ) {
+struct uri * tftp_uri ( struct in_addr next_server, unsigned int port,
+                       const char *filename ) {
+       char buf[ 6 /* "65535" + NUL */ ];
        struct uri uri;
 
        memset ( &uri, 0, sizeof ( uri ) );
        uri.scheme = "tftp";
        uri.host = inet_ntoa ( next_server );
+       if ( port ) {
+               snprintf ( buf, sizeof ( buf ), "%d", port );
+               uri.port = buf;
+       }
        uri.path = filename;
        return uri_dup ( &uri );
 }
index 7613d578df1538d974ce6a106bb4f4f6f012eafe..fb7728aa0c8c7b258f9a105a4cb2f0be35aa4647 100644 (file)
@@ -203,7 +203,7 @@ extern char * resolve_path ( const char *base_path,
                             const char *relative_path );
 extern struct uri * resolve_uri ( const struct uri *base_uri,
                                  struct uri *relative_uri );
-extern struct uri * tftp_uri ( struct in_addr next_server,
+extern struct uri * tftp_uri ( struct in_addr next_server, unsigned int port,
                               const char *filename );
 extern void churi ( struct uri *uri );
 
index 14f1b4ad0bab79179d8a0aeb6a494fe892f03e34..b2578b8443a668ba1297395e9330325552af1bd8 100644 (file)
@@ -66,6 +66,8 @@ struct uri_resolve_test {
 struct uri_tftp_test {
        /** Next-server address */
        struct in_addr next_server;
+       /** Port number */
+       unsigned int port;
        /** Filename */
        const char *filename;
        /** URI */
@@ -330,7 +332,7 @@ static void uri_tftp_okx ( struct uri_tftp_test *test, const char *file,
        size_t len;
 
        /* Construct URI */
-       uri = tftp_uri ( test->next_server, test->filename );
+       uri = tftp_uri ( test->next_server, test->port, test->filename );
        okx ( uri != NULL, file, line );
        if ( uri ) {
                uri_okx ( uri, &test->uri, file, line );
@@ -674,7 +676,7 @@ static struct uri_resolve_test uri_fragment = {
 
 /** TFTP URI with absolute path */
 static struct uri_tftp_test uri_tftp_absolute = {
-       { .s_addr = htonl ( 0xc0a80002 ) /* 192.168.0.2 */ },
+       { .s_addr = htonl ( 0xc0a80002 ) /* 192.168.0.2 */ }, 0,
        "/absolute/path",
        {
                .scheme = "tftp",
@@ -686,7 +688,7 @@ static struct uri_tftp_test uri_tftp_absolute = {
 
 /** TFTP URI with relative path */
 static struct uri_tftp_test uri_tftp_relative = {
-       { .s_addr = htonl ( 0xc0a80003 ) /* 192.168.0.3 */ },
+       { .s_addr = htonl ( 0xc0a80003 ) /* 192.168.0.3 */ }, 0,
        "relative/path",
        {
                .scheme = "tftp",
@@ -698,7 +700,7 @@ static struct uri_tftp_test uri_tftp_relative = {
 
 /** TFTP URI with path containing special characters */
 static struct uri_tftp_test uri_tftp_icky = {
-       { .s_addr = htonl ( 0x0a000006 ) /* 10.0.0.6 */ },
+       { .s_addr = htonl ( 0x0a000006 ) /* 10.0.0.6 */ }, 0,
        "C:\\tftpboot\\icky#path",
        {
                .scheme = "tftp",
@@ -708,6 +710,19 @@ static struct uri_tftp_test uri_tftp_icky = {
        "tftp://10.0.0.6/C%3A\\tftpboot\\icky%23path",
 };
 
+/** TFTP URI with custom port */
+static struct uri_tftp_test uri_tftp_port = {
+       { .s_addr = htonl ( 0xc0a80001 ) /* 192.168.0.1 */ }, 4069,
+       "/another/path",
+       {
+               .scheme = "tftp",
+               .host = "192.168.0.1",
+               .port = "4069",
+               .path = "/another/path",
+       },
+       "tftp://192.168.0.1:4069/another/path",
+};
+
 /** Current working URI test */
 static struct uri_churi_test uri_churi[] = {
        {
@@ -842,6 +857,7 @@ static void uri_test_exec ( void ) {
        uri_tftp_ok ( &uri_tftp_absolute );
        uri_tftp_ok ( &uri_tftp_relative );
        uri_tftp_ok ( &uri_tftp_icky );
+       uri_tftp_ok ( &uri_tftp_port );
 
        /* Current working URI tests */
        uri_churi_ok ( uri_churi );
index 47476ae404d96a57d613fd2f170243c2dfee9c0d..4aba593e9898b8fbdd2802ad70958f62c56081ca 100644 (file)
@@ -101,7 +101,7 @@ static struct uri * parse_next_server_and_filename ( struct in_addr next_server,
        /* Construct a TFTP URI for the filename, if applicable */
        if ( next_server.s_addr && filename[0] && ! uri_is_absolute ( uri ) ) {
                uri_put ( uri );
-               uri = tftp_uri ( next_server, filename );
+               uri = tftp_uri ( next_server, 0, filename );
                if ( ! uri )
                        return NULL;
        }