You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uri.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. /** @file
  21. *
  22. * Uniform Resource Identifiers
  23. *
  24. */
  25. #include <stdint.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <libgen.h>
  29. #include <ctype.h>
  30. #include <ipxe/vsprintf.h>
  31. #include <ipxe/params.h>
  32. #include <ipxe/uri.h>
  33. /**
  34. * Decode URI field (in place)
  35. *
  36. * @v string String
  37. *
  38. * URI decoding can never increase the length of a string; we can
  39. * therefore safely decode in place.
  40. */
  41. static void uri_decode ( char *string ) {
  42. char *dest = string;
  43. char hexbuf[3];
  44. char *hexbuf_end;
  45. char c;
  46. char decoded;
  47. unsigned int skip;
  48. /* Copy string, decoding escaped characters as necessary */
  49. do {
  50. c = *(string++);
  51. if ( c == '%' ) {
  52. snprintf ( hexbuf, sizeof ( hexbuf ), "%s", string );
  53. decoded = strtoul ( hexbuf, &hexbuf_end, 16 );
  54. skip = ( hexbuf_end - hexbuf );
  55. string += skip;
  56. if ( skip )
  57. c = decoded;
  58. }
  59. *(dest++) = c;
  60. } while ( c );
  61. }
  62. /**
  63. * Check if character should be escaped within a URI field
  64. *
  65. * @v c Character
  66. * @v field URI field index
  67. * @ret escaped Character should be escaped
  68. */
  69. static int uri_character_escaped ( char c, unsigned int field ) {
  70. /* Non-printing characters and whitespace should always be
  71. * escaped, since they cannot sensibly be displayed as part of
  72. * a coherent URL string. (This test also catches control
  73. * characters such as CR and LF, which could affect the
  74. * operation of line-based protocols such as HTTP.)
  75. *
  76. * We should also escape characters which would alter the
  77. * interpretation of the URL if not escaped, i.e. characters
  78. * which have significance to the URL parser. We should not
  79. * blindly escape all such characters, because this would lead
  80. * to some very strange-looking URLs (e.g. if we were to
  81. * always escape '/' as "%2F" even within the URI path).
  82. *
  83. * We do not need to be perfect. Our primary role is as a
  84. * consumer of URIs rather than a producer; the main situation
  85. * in which we produce a URI string is for display to a human
  86. * user, who can probably tolerate some variance from the
  87. * formal specification. The only situation in which we
  88. * currently produce a URI string to be consumed by a computer
  89. * is when constructing an HTTP request URI, which contains
  90. * only the path and query fields.
  91. *
  92. * We can therefore sacrifice some correctness for the sake of
  93. * code size. For example, colons within the URI host should
  94. * be escaped unless they form part of an IPv6 literal
  95. * address; doing this correctly would require the URI
  96. * formatter to be aware of whether or not the URI host
  97. * contained an IPv4 address, an IPv6 address, or a host name.
  98. * We choose to simplify and never escape colons within the
  99. * URI host field: in the event of a pathological hostname
  100. * containing colons, this could potentially produce a URI
  101. * string which could not be reparsed.
  102. *
  103. * After excluding non-printing characters, whitespace, and
  104. * '%', the full set of characters with significance to the
  105. * URL parser is "/#:@?". We choose for each URI field which
  106. * of these require escaping in our use cases.
  107. */
  108. static const char *escaped[URI_FIELDS] = {
  109. /* Scheme: escape everything */
  110. [URI_SCHEME] = "/#:@?",
  111. /* Opaque part: escape characters which would affect
  112. * the reparsing of the URI, allowing everything else
  113. * (e.g. ':', which will appear in iSCSI URIs).
  114. */
  115. [URI_OPAQUE] = "/#",
  116. /* User name: escape everything */
  117. [URI_USER] = "/#:@?",
  118. /* Password: escape everything */
  119. [URI_PASSWORD] = "/#:@?",
  120. /* Host name: escape everything except ':', which may
  121. * appear as part of an IPv6 literal address.
  122. */
  123. [URI_HOST] = "/#@?",
  124. /* Port number: escape everything */
  125. [URI_PORT] = "/#:@?",
  126. /* Path: escape everything except '/', which usually
  127. * appears within paths.
  128. */
  129. [URI_PATH] = "#:@?",
  130. /* Query: escape everything except '/', which
  131. * sometimes appears within queries.
  132. */
  133. [URI_QUERY] = "#:@?",
  134. /* Fragment: escape everything */
  135. [URI_FRAGMENT] = "/#:@?",
  136. };
  137. return ( /* Always escape non-printing characters and whitespace */
  138. ( ! isprint ( c ) ) || ( c == ' ' ) ||
  139. /* Always escape '%' */
  140. ( c == '%' ) ||
  141. /* Escape field-specific characters */
  142. strchr ( escaped[field], c ) );
  143. }
  144. /**
  145. * Encode URI field
  146. *
  147. * @v uri URI
  148. * @v field URI field index
  149. * @v buf Buffer to contain encoded string
  150. * @v len Length of buffer
  151. * @ret len Length of encoded string (excluding NUL)
  152. */
  153. size_t uri_encode ( const char *string, unsigned int field,
  154. char *buf, ssize_t len ) {
  155. ssize_t remaining = len;
  156. size_t used;
  157. char c;
  158. /* Ensure encoded string is NUL-terminated even if empty */
  159. if ( len > 0 )
  160. buf[0] = '\0';
  161. /* Copy string, escaping as necessary */
  162. while ( ( c = *(string++) ) ) {
  163. if ( uri_character_escaped ( c, field ) ) {
  164. used = ssnprintf ( buf, remaining, "%%%02X", c );
  165. } else {
  166. used = ssnprintf ( buf, remaining, "%c", c );
  167. }
  168. buf += used;
  169. remaining -= used;
  170. }
  171. return ( len - remaining );
  172. }
  173. /**
  174. * Dump URI for debugging
  175. *
  176. * @v uri URI
  177. */
  178. static void uri_dump ( const struct uri *uri ) {
  179. if ( ! uri )
  180. return;
  181. if ( uri->scheme )
  182. DBGC ( uri, " scheme \"%s\"", uri->scheme );
  183. if ( uri->opaque )
  184. DBGC ( uri, " opaque \"%s\"", uri->opaque );
  185. if ( uri->user )
  186. DBGC ( uri, " user \"%s\"", uri->user );
  187. if ( uri->password )
  188. DBGC ( uri, " password \"%s\"", uri->password );
  189. if ( uri->host )
  190. DBGC ( uri, " host \"%s\"", uri->host );
  191. if ( uri->port )
  192. DBGC ( uri, " port \"%s\"", uri->port );
  193. if ( uri->path )
  194. DBGC ( uri, " path \"%s\"", uri->path );
  195. if ( uri->query )
  196. DBGC ( uri, " query \"%s\"", uri->query );
  197. if ( uri->fragment )
  198. DBGC ( uri, " fragment \"%s\"", uri->fragment );
  199. if ( uri->params )
  200. DBGC ( uri, " params \"%s\"", uri->params->name );
  201. }
  202. /**
  203. * Free URI
  204. *
  205. * @v refcnt Reference count
  206. */
  207. static void uri_free ( struct refcnt *refcnt ) {
  208. struct uri *uri = container_of ( refcnt, struct uri, refcnt );
  209. params_put ( uri->params );
  210. free ( uri );
  211. }
  212. /**
  213. * Parse URI
  214. *
  215. * @v uri_string URI as a string
  216. * @ret uri URI
  217. *
  218. * Splits a URI into its component parts. The return URI structure is
  219. * dynamically allocated and must eventually be freed by calling
  220. * uri_put().
  221. */
  222. struct uri * parse_uri ( const char *uri_string ) {
  223. struct uri *uri;
  224. struct parameters *params;
  225. char *raw;
  226. char *tmp;
  227. char *path;
  228. char *authority;
  229. size_t raw_len;
  230. unsigned int field;
  231. /* Allocate space for URI struct and a copy of the string */
  232. raw_len = ( strlen ( uri_string ) + 1 /* NUL */ );
  233. uri = zalloc ( sizeof ( *uri ) + raw_len );
  234. if ( ! uri )
  235. return NULL;
  236. ref_init ( &uri->refcnt, uri_free );
  237. raw = ( ( ( void * ) uri ) + sizeof ( *uri ) );
  238. /* Copy in the raw string */
  239. memcpy ( raw, uri_string, raw_len );
  240. /* Identify the parameter list, if present */
  241. if ( ( tmp = strstr ( raw, "##params" ) ) ) {
  242. *tmp = '\0';
  243. tmp += 8 /* "##params" */;
  244. params = find_parameters ( *tmp ? ( tmp + 1 ) : NULL );
  245. if ( params ) {
  246. uri->params = claim_parameters ( params );
  247. } else {
  248. /* Ignore non-existent submission blocks */
  249. }
  250. }
  251. /* Chop off the fragment, if it exists */
  252. if ( ( tmp = strchr ( raw, '#' ) ) ) {
  253. *(tmp++) = '\0';
  254. uri->fragment = tmp;
  255. }
  256. /* Identify absolute/relative URI */
  257. if ( ( tmp = strchr ( raw, ':' ) ) ) {
  258. /* Absolute URI: identify hierarchical/opaque */
  259. uri->scheme = raw;
  260. *(tmp++) = '\0';
  261. if ( *tmp == '/' ) {
  262. /* Absolute URI with hierarchical part */
  263. path = tmp;
  264. } else {
  265. /* Absolute URI with opaque part */
  266. uri->opaque = tmp;
  267. path = NULL;
  268. }
  269. } else {
  270. /* Relative URI */
  271. path = raw;
  272. }
  273. /* If we don't have a path (i.e. we have an absolute URI with
  274. * an opaque portion, we're already finished processing
  275. */
  276. if ( ! path )
  277. goto done;
  278. /* Chop off the query, if it exists */
  279. if ( ( tmp = strchr ( path, '?' ) ) ) {
  280. *(tmp++) = '\0';
  281. uri->query = tmp;
  282. }
  283. /* If we have no path remaining, then we're already finished
  284. * processing.
  285. */
  286. if ( ! path[0] )
  287. goto done;
  288. /* Identify net/absolute/relative path */
  289. if ( strncmp ( path, "//", 2 ) == 0 ) {
  290. /* Net path. If this is terminated by the first '/'
  291. * of an absolute path, then we have no space for a
  292. * terminator after the authority field, so shuffle
  293. * the authority down by one byte, overwriting one of
  294. * the two slashes.
  295. */
  296. authority = ( path + 2 );
  297. if ( ( tmp = strchr ( authority, '/' ) ) ) {
  298. /* Shuffle down */
  299. uri->path = tmp;
  300. memmove ( ( authority - 1 ), authority,
  301. ( tmp - authority ) );
  302. authority--;
  303. *(--tmp) = '\0';
  304. }
  305. } else {
  306. /* Absolute/relative path */
  307. uri->path = path;
  308. authority = NULL;
  309. }
  310. /* If we don't have an authority (i.e. we have a non-net
  311. * path), we're already finished processing
  312. */
  313. if ( ! authority )
  314. goto done;
  315. /* Split authority into user[:password] and host[:port] portions */
  316. if ( ( tmp = strchr ( authority, '@' ) ) ) {
  317. /* Has user[:password] */
  318. *(tmp++) = '\0';
  319. uri->host = tmp;
  320. uri->user = authority;
  321. if ( ( tmp = strchr ( authority, ':' ) ) ) {
  322. /* Has password */
  323. *(tmp++) = '\0';
  324. uri->password = tmp;
  325. }
  326. } else {
  327. /* No user:password */
  328. uri->host = authority;
  329. }
  330. /* Split host into host[:port] */
  331. if ( ( uri->host[ strlen ( uri->host ) - 1 ] != ']' ) &&
  332. ( tmp = strrchr ( uri->host, ':' ) ) ) {
  333. *(tmp++) = '\0';
  334. uri->port = tmp;
  335. }
  336. /* Decode fields in-place */
  337. for ( field = 0 ; field < URI_FIELDS ; field++ ) {
  338. if ( uri_field ( uri, field ) )
  339. uri_decode ( ( char * ) uri_field ( uri, field ) );
  340. }
  341. done:
  342. DBGC ( uri, "URI parsed \"%s\" to", uri_string );
  343. uri_dump ( uri );
  344. DBGC ( uri, "\n" );
  345. return uri;
  346. }
  347. /**
  348. * Get port from URI
  349. *
  350. * @v uri URI, or NULL
  351. * @v default_port Default port to use if none specified in URI
  352. * @ret port Port
  353. */
  354. unsigned int uri_port ( const struct uri *uri, unsigned int default_port ) {
  355. if ( ( ! uri ) || ( ! uri->port ) )
  356. return default_port;
  357. return ( strtoul ( uri->port, NULL, 0 ) );
  358. }
  359. /**
  360. * Format URI
  361. *
  362. * @v uri URI
  363. * @v buf Buffer to fill with URI string
  364. * @v size Size of buffer
  365. * @ret len Length of URI string
  366. */
  367. size_t format_uri ( const struct uri *uri, char *buf, size_t len ) {
  368. static const char prefixes[URI_FIELDS] = {
  369. [URI_OPAQUE] = ':',
  370. [URI_PASSWORD] = ':',
  371. [URI_PORT] = ':',
  372. [URI_PATH] = '/',
  373. [URI_QUERY] = '?',
  374. [URI_FRAGMENT] = '#',
  375. };
  376. char prefix;
  377. size_t used = 0;
  378. unsigned int field;
  379. /* Ensure buffer is NUL-terminated */
  380. if ( len )
  381. buf[0] = '\0';
  382. /* Special-case NULL URI */
  383. if ( ! uri )
  384. return 0;
  385. /* Generate fields */
  386. for ( field = 0 ; field < URI_FIELDS ; field++ ) {
  387. /* Skip non-existent fields */
  388. if ( ! uri_field ( uri, field ) )
  389. continue;
  390. /* Prefix this field, if applicable */
  391. prefix = prefixes[field];
  392. if ( ( field == URI_HOST ) && ( uri->user != NULL ) )
  393. prefix = '@';
  394. if ( ( field == URI_PATH ) && ( uri->path[0] == '/' ) )
  395. prefix = '\0';
  396. if ( prefix ) {
  397. used += ssnprintf ( ( buf + used ), ( len - used ),
  398. "%c", prefix );
  399. }
  400. /* Encode this field */
  401. used += uri_encode ( uri_field ( uri, field ), field,
  402. ( buf + used ), ( len - used ) );
  403. /* Suffix this field, if applicable */
  404. if ( ( field == URI_SCHEME ) && ( ! uri->opaque ) ) {
  405. used += ssnprintf ( ( buf + used ), ( len - used ),
  406. "://" );
  407. }
  408. }
  409. if ( len ) {
  410. DBGC ( uri, "URI formatted" );
  411. uri_dump ( uri );
  412. DBGC ( uri, " to \"%s%s\"\n", buf,
  413. ( ( used > len ) ? "<TRUNCATED>" : "" ) );
  414. }
  415. return used;
  416. }
  417. /**
  418. * Format URI
  419. *
  420. * @v uri URI
  421. * @ret string URI string, or NULL on failure
  422. *
  423. * The caller is responsible for eventually freeing the allocated
  424. * memory.
  425. */
  426. char * format_uri_alloc ( const struct uri *uri ) {
  427. size_t len;
  428. char *string;
  429. len = ( format_uri ( uri, NULL, 0 ) + 1 /* NUL */ );
  430. string = malloc ( len );
  431. if ( string )
  432. format_uri ( uri, string, len );
  433. return string;
  434. }
  435. /**
  436. * Copy URI fields
  437. *
  438. * @v src Source URI
  439. * @v dest Destination URI, or NULL to calculate length
  440. * @ret len Length of raw URI
  441. */
  442. static size_t uri_copy_fields ( const struct uri *src, struct uri *dest ) {
  443. size_t len = sizeof ( *dest );
  444. char *out = ( ( void * ) dest + len );
  445. unsigned int field;
  446. size_t field_len;
  447. /* Copy existent fields */
  448. for ( field = 0 ; field < URI_FIELDS ; field++ ) {
  449. /* Skip non-existent fields */
  450. if ( ! uri_field ( src, field ) )
  451. continue;
  452. /* Calculate field length */
  453. field_len = ( strlen ( uri_field ( src, field ) )
  454. + 1 /* NUL */ );
  455. len += field_len;
  456. /* Copy field, if applicable */
  457. if ( dest ) {
  458. memcpy ( out, uri_field ( src, field ), field_len );
  459. uri_field ( dest, field ) = out;
  460. out += field_len;
  461. }
  462. }
  463. return len;
  464. }
  465. /**
  466. * Duplicate URI
  467. *
  468. * @v uri URI
  469. * @ret uri Duplicate URI
  470. *
  471. * Creates a modifiable copy of a URI.
  472. */
  473. struct uri * uri_dup ( const struct uri *uri ) {
  474. struct uri *dup;
  475. size_t len;
  476. /* Allocate new URI */
  477. len = uri_copy_fields ( uri, NULL );
  478. dup = zalloc ( len );
  479. if ( ! dup )
  480. return NULL;
  481. ref_init ( &dup->refcnt, uri_free );
  482. /* Copy fields */
  483. uri_copy_fields ( uri, dup );
  484. /* Copy parameters */
  485. dup->params = params_get ( uri->params );
  486. DBGC ( uri, "URI duplicated" );
  487. uri_dump ( uri );
  488. DBGC ( uri, "\n" );
  489. return dup;
  490. }
  491. /**
  492. * Resolve base+relative path
  493. *
  494. * @v base_uri Base path
  495. * @v relative_uri Relative path
  496. * @ret resolved_uri Resolved path
  497. *
  498. * Takes a base path (e.g. "/var/lib/tftpboot/vmlinuz" and a relative
  499. * path (e.g. "initrd.gz") and produces a new path
  500. * (e.g. "/var/lib/tftpboot/initrd.gz"). Note that any non-directory
  501. * portion of the base path will automatically be stripped; this
  502. * matches the semantics used when resolving the path component of
  503. * URIs.
  504. */
  505. char * resolve_path ( const char *base_path,
  506. const char *relative_path ) {
  507. size_t base_len = ( strlen ( base_path ) + 1 );
  508. char base_path_copy[base_len];
  509. char *base_tmp = base_path_copy;
  510. char *resolved;
  511. /* If relative path is absolute, just re-use it */
  512. if ( relative_path[0] == '/' )
  513. return strdup ( relative_path );
  514. /* Create modifiable copy of path for dirname() */
  515. memcpy ( base_tmp, base_path, base_len );
  516. base_tmp = dirname ( base_tmp );
  517. /* Process "./" and "../" elements */
  518. while ( *relative_path == '.' ) {
  519. relative_path++;
  520. if ( *relative_path == 0 ) {
  521. /* Do nothing */
  522. } else if ( *relative_path == '/' ) {
  523. relative_path++;
  524. } else if ( *relative_path == '.' ) {
  525. relative_path++;
  526. if ( *relative_path == 0 ) {
  527. base_tmp = dirname ( base_tmp );
  528. } else if ( *relative_path == '/' ) {
  529. base_tmp = dirname ( base_tmp );
  530. relative_path++;
  531. } else {
  532. relative_path -= 2;
  533. break;
  534. }
  535. } else {
  536. relative_path--;
  537. break;
  538. }
  539. }
  540. /* Create and return new path */
  541. if ( asprintf ( &resolved, "%s%s%s", base_tmp,
  542. ( ( base_tmp[ strlen ( base_tmp ) - 1 ] == '/' ) ?
  543. "" : "/" ), relative_path ) < 0 )
  544. return NULL;
  545. return resolved;
  546. }
  547. /**
  548. * Resolve base+relative URI
  549. *
  550. * @v base_uri Base URI, or NULL
  551. * @v relative_uri Relative URI
  552. * @ret resolved_uri Resolved URI
  553. *
  554. * Takes a base URI (e.g. "http://ipxe.org/kernels/vmlinuz" and a
  555. * relative URI (e.g. "../initrds/initrd.gz") and produces a new URI
  556. * (e.g. "http://ipxe.org/initrds/initrd.gz").
  557. */
  558. struct uri * resolve_uri ( const struct uri *base_uri,
  559. struct uri *relative_uri ) {
  560. struct uri tmp_uri;
  561. char *tmp_path = NULL;
  562. struct uri *new_uri;
  563. /* If relative URI is absolute, just re-use it */
  564. if ( uri_is_absolute ( relative_uri ) || ( ! base_uri ) )
  565. return uri_get ( relative_uri );
  566. /* Mangle URI */
  567. memcpy ( &tmp_uri, base_uri, sizeof ( tmp_uri ) );
  568. if ( relative_uri->path ) {
  569. tmp_path = resolve_path ( ( base_uri->path ?
  570. base_uri->path : "/" ),
  571. relative_uri->path );
  572. tmp_uri.path = tmp_path;
  573. tmp_uri.query = relative_uri->query;
  574. tmp_uri.fragment = relative_uri->fragment;
  575. tmp_uri.params = relative_uri->params;
  576. } else if ( relative_uri->query ) {
  577. tmp_uri.query = relative_uri->query;
  578. tmp_uri.fragment = relative_uri->fragment;
  579. tmp_uri.params = relative_uri->params;
  580. } else if ( relative_uri->fragment ) {
  581. tmp_uri.fragment = relative_uri->fragment;
  582. tmp_uri.params = relative_uri->params;
  583. } else if ( relative_uri->params ) {
  584. tmp_uri.params = relative_uri->params;
  585. }
  586. /* Create demangled URI */
  587. new_uri = uri_dup ( &tmp_uri );
  588. free ( tmp_path );
  589. return new_uri;
  590. }
  591. /**
  592. * Construct TFTP URI from next-server and filename
  593. *
  594. * @v next_server Next-server address
  595. * @v port Port number, or zero to use the default port
  596. * @v filename Filename
  597. * @ret uri URI, or NULL on failure
  598. *
  599. * TFTP filenames specified via the DHCP next-server field often
  600. * contain characters such as ':' or '#' which would confuse the
  601. * generic URI parser. We provide a mechanism for directly
  602. * constructing a TFTP URI from the next-server and filename.
  603. */
  604. struct uri * tftp_uri ( struct in_addr next_server, unsigned int port,
  605. const char *filename ) {
  606. char buf[ 6 /* "65535" + NUL */ ];
  607. struct uri uri;
  608. memset ( &uri, 0, sizeof ( uri ) );
  609. uri.scheme = "tftp";
  610. uri.host = inet_ntoa ( next_server );
  611. if ( port ) {
  612. snprintf ( buf, sizeof ( buf ), "%d", port );
  613. uri.port = buf;
  614. }
  615. uri.path = filename;
  616. return uri_dup ( &uri );
  617. }