Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

uri.c 18KB

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