tftp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. /*
  2. * Copyright 1994, 1995, 2000 Neil Russell.
  3. * (See License)
  4. * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
  5. */
  6. #include <common.h>
  7. #include <command.h>
  8. #include <net.h>
  9. #include "tftp.h"
  10. #include "bootp.h"
  11. #if defined(CONFIG_CMD_NET)
  12. #define WELL_KNOWN_PORT 69 /* Well known TFTP port # */
  13. #define TIMEOUT 5000UL /* Millisecs to timeout for lost pkt */
  14. #ifndef CONFIG_NET_RETRY_COUNT
  15. # define TIMEOUT_COUNT 10 /* # of timeouts before giving up */
  16. #else
  17. # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
  18. #endif
  19. /* (for checking the image size) */
  20. #define HASHES_PER_LINE 65 /* Number of "loading" hashes per line */
  21. /*
  22. * TFTP operations.
  23. */
  24. #define TFTP_RRQ 1
  25. #define TFTP_WRQ 2
  26. #define TFTP_DATA 3
  27. #define TFTP_ACK 4
  28. #define TFTP_ERROR 5
  29. #define TFTP_OACK 6
  30. static ulong TftpTimeoutMSecs = TIMEOUT;
  31. static int TftpTimeoutCountMax = TIMEOUT_COUNT;
  32. /*
  33. * These globals govern the timeout behavior when attempting a connection to a
  34. * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
  35. * wait for the server to respond to initial connection. Second global,
  36. * TftpRRQTimeoutCountMax, gives the number of such connection retries.
  37. * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
  38. * positive. The globals are meant to be set (and restored) by code needing
  39. * non-standard timeout behavior when initiating a TFTP transfer.
  40. */
  41. ulong TftpRRQTimeoutMSecs = TIMEOUT;
  42. int TftpRRQTimeoutCountMax = TIMEOUT_COUNT;
  43. static IPaddr_t TftpServerIP;
  44. static int TftpServerPort; /* The UDP port at their end */
  45. static int TftpOurPort; /* The UDP port at our end */
  46. static int TftpTimeoutCount;
  47. static ulong TftpBlock; /* packet sequence number */
  48. static ulong TftpLastBlock; /* last packet sequence number received */
  49. static ulong TftpBlockWrap; /* count of sequence number wraparounds */
  50. static ulong TftpBlockWrapOffset; /* memory offset due to wrapping */
  51. static int TftpState;
  52. #ifdef CONFIG_TFTP_TSIZE
  53. static int TftpTsize; /* The file size reported by the server */
  54. static short TftpNumchars; /* The number of hashes we printed */
  55. #endif
  56. #define STATE_RRQ 1
  57. #define STATE_DATA 2
  58. #define STATE_TOO_LARGE 3
  59. #define STATE_BAD_MAGIC 4
  60. #define STATE_OACK 5
  61. #define TFTP_BLOCK_SIZE 512 /* default TFTP block size */
  62. #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16)) /* sequence number is 16 bit */
  63. #define DEFAULT_NAME_LEN (8 + 4 + 1)
  64. static char default_filename[DEFAULT_NAME_LEN];
  65. #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
  66. #define MAX_LEN 128
  67. #else
  68. #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
  69. #endif
  70. static char tftp_filename[MAX_LEN];
  71. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  72. extern flash_info_t flash_info[];
  73. #endif
  74. /* 512 is poor choice for ethernet, MTU is typically 1500.
  75. * Minus eth.hdrs thats 1468. Can get 2x better throughput with
  76. * almost-MTU block sizes. At least try... fall back to 512 if need be.
  77. * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
  78. */
  79. #ifdef CONFIG_TFTP_BLOCKSIZE
  80. #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
  81. #else
  82. #define TFTP_MTU_BLOCKSIZE 1468
  83. #endif
  84. static unsigned short TftpBlkSize=TFTP_BLOCK_SIZE;
  85. static unsigned short TftpBlkSizeOption=TFTP_MTU_BLOCKSIZE;
  86. #ifdef CONFIG_MCAST_TFTP
  87. #include <malloc.h>
  88. #define MTFTP_BITMAPSIZE 0x1000
  89. static unsigned *Bitmap;
  90. static int PrevBitmapHole,Mapsize=MTFTP_BITMAPSIZE;
  91. static uchar ProhibitMcast=0, MasterClient=0;
  92. static uchar Multicast=0;
  93. extern IPaddr_t Mcast_addr;
  94. static int Mcast_port;
  95. static ulong TftpEndingBlock; /* can get 'last' block before done..*/
  96. static void parse_multicast_oack(char *pkt,int len);
  97. static void
  98. mcast_cleanup(void)
  99. {
  100. if (Mcast_addr) eth_mcast_join(Mcast_addr, 0);
  101. if (Bitmap) free(Bitmap);
  102. Bitmap=NULL;
  103. Mcast_addr = Multicast = Mcast_port = 0;
  104. TftpEndingBlock = -1;
  105. }
  106. #endif /* CONFIG_MCAST_TFTP */
  107. static __inline__ void
  108. store_block (unsigned block, uchar * src, unsigned len)
  109. {
  110. ulong offset = block * TftpBlkSize + TftpBlockWrapOffset;
  111. ulong newsize = offset + len;
  112. #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
  113. int i, rc = 0;
  114. for (i=0; i<CONFIG_SYS_MAX_FLASH_BANKS; i++) {
  115. /* start address in flash? */
  116. if (flash_info[i].flash_id == FLASH_UNKNOWN)
  117. continue;
  118. if (load_addr + offset >= flash_info[i].start[0]) {
  119. rc = 1;
  120. break;
  121. }
  122. }
  123. if (rc) { /* Flash is destination for this packet */
  124. rc = flash_write ((char *)src, (ulong)(load_addr+offset), len);
  125. if (rc) {
  126. flash_perror (rc);
  127. NetState = NETLOOP_FAIL;
  128. return;
  129. }
  130. }
  131. else
  132. #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
  133. {
  134. (void)memcpy((void *)(load_addr + offset), src, len);
  135. }
  136. #ifdef CONFIG_MCAST_TFTP
  137. if (Multicast)
  138. ext2_set_bit(block, Bitmap);
  139. #endif
  140. if (NetBootFileXferSize < newsize)
  141. NetBootFileXferSize = newsize;
  142. }
  143. static void TftpSend (void);
  144. static void TftpTimeout (void);
  145. /**********************************************************************/
  146. static void
  147. TftpSend (void)
  148. {
  149. volatile uchar * pkt;
  150. volatile uchar * xp;
  151. int len = 0;
  152. volatile ushort *s;
  153. #ifdef CONFIG_MCAST_TFTP
  154. /* Multicast TFTP.. non-MasterClients do not ACK data. */
  155. if (Multicast
  156. && (TftpState == STATE_DATA)
  157. && (MasterClient == 0))
  158. return;
  159. #endif
  160. /*
  161. * We will always be sending some sort of packet, so
  162. * cobble together the packet headers now.
  163. */
  164. pkt = NetTxPacket + NetEthHdrSize() + IP_HDR_SIZE;
  165. switch (TftpState) {
  166. case STATE_RRQ:
  167. xp = pkt;
  168. s = (ushort *)pkt;
  169. *s++ = htons(TFTP_RRQ);
  170. pkt = (uchar *)s;
  171. strcpy ((char *)pkt, tftp_filename);
  172. pkt += strlen(tftp_filename) + 1;
  173. strcpy ((char *)pkt, "octet");
  174. pkt += 5 /*strlen("octet")*/ + 1;
  175. strcpy ((char *)pkt, "timeout");
  176. pkt += 7 /*strlen("timeout")*/ + 1;
  177. sprintf((char *)pkt, "%lu", TIMEOUT / 1000);
  178. debug("send option \"timeout %s\"\n", (char *)pkt);
  179. pkt += strlen((char *)pkt) + 1;
  180. #ifdef CONFIG_TFTP_TSIZE
  181. memcpy((char *)pkt, "tsize\0000\0", 8);
  182. pkt += 8;
  183. #endif
  184. /* try for more effic. blk size */
  185. pkt += sprintf((char *)pkt,"blksize%c%d%c",
  186. 0,TftpBlkSizeOption,0);
  187. #ifdef CONFIG_MCAST_TFTP
  188. /* Check all preconditions before even trying the option */
  189. if (!ProhibitMcast
  190. && (Bitmap=malloc(Mapsize))
  191. && eth_get_dev()->mcast) {
  192. free(Bitmap);
  193. Bitmap=NULL;
  194. pkt += sprintf((char *)pkt,"multicast%c%c",0,0);
  195. }
  196. #endif /* CONFIG_MCAST_TFTP */
  197. len = pkt - xp;
  198. break;
  199. case STATE_OACK:
  200. #ifdef CONFIG_MCAST_TFTP
  201. /* My turn! Start at where I need blocks I missed.*/
  202. if (Multicast)
  203. TftpBlock=ext2_find_next_zero_bit(Bitmap,(Mapsize*8),0);
  204. /*..falling..*/
  205. #endif
  206. case STATE_DATA:
  207. xp = pkt;
  208. s = (ushort *)pkt;
  209. *s++ = htons(TFTP_ACK);
  210. *s++ = htons(TftpBlock);
  211. pkt = (uchar *)s;
  212. len = pkt - xp;
  213. break;
  214. case STATE_TOO_LARGE:
  215. xp = pkt;
  216. s = (ushort *)pkt;
  217. *s++ = htons(TFTP_ERROR);
  218. *s++ = htons(3);
  219. pkt = (uchar *)s;
  220. strcpy ((char *)pkt, "File too large");
  221. pkt += 14 /*strlen("File too large")*/ + 1;
  222. len = pkt - xp;
  223. break;
  224. case STATE_BAD_MAGIC:
  225. xp = pkt;
  226. s = (ushort *)pkt;
  227. *s++ = htons(TFTP_ERROR);
  228. *s++ = htons(2);
  229. pkt = (uchar *)s;
  230. strcpy ((char *)pkt, "File has bad magic");
  231. pkt += 18 /*strlen("File has bad magic")*/ + 1;
  232. len = pkt - xp;
  233. break;
  234. }
  235. NetSendUDPPacket(NetServerEther, TftpServerIP, TftpServerPort, TftpOurPort, len);
  236. }
  237. static void
  238. TftpHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
  239. {
  240. ushort proto;
  241. ushort *s;
  242. int i;
  243. if (dest != TftpOurPort) {
  244. #ifdef CONFIG_MCAST_TFTP
  245. if (Multicast
  246. && (!Mcast_port || (dest != Mcast_port)))
  247. #endif
  248. return;
  249. }
  250. if (TftpState != STATE_RRQ && src != TftpServerPort) {
  251. return;
  252. }
  253. if (len < 2) {
  254. return;
  255. }
  256. len -= 2;
  257. /* warning: don't use increment (++) in ntohs() macros!! */
  258. s = (ushort *)pkt;
  259. proto = *s++;
  260. pkt = (uchar *)s;
  261. switch (ntohs(proto)) {
  262. case TFTP_RRQ:
  263. case TFTP_WRQ:
  264. case TFTP_ACK:
  265. break;
  266. default:
  267. break;
  268. case TFTP_OACK:
  269. debug("Got OACK: %s %s\n",
  270. pkt,
  271. pkt + strlen((char *)pkt) + 1);
  272. TftpState = STATE_OACK;
  273. TftpServerPort = src;
  274. /*
  275. * Check for 'blksize' option.
  276. * Careful: "i" is signed, "len" is unsigned, thus
  277. * something like "len-8" may give a *huge* number
  278. */
  279. for (i=0; i+8<len; i++) {
  280. if (strcmp ((char*)pkt+i,"blksize") == 0) {
  281. TftpBlkSize = (unsigned short)
  282. simple_strtoul((char*)pkt+i+8,NULL,10);
  283. debug("Blocksize ack: %s, %d\n",
  284. (char*)pkt+i+8,TftpBlkSize);
  285. }
  286. #ifdef CONFIG_TFTP_TSIZE
  287. if (strcmp ((char*)pkt+i,"tsize") == 0) {
  288. TftpTsize = simple_strtoul((char*)pkt+i+6,NULL,10);
  289. debug("size = %s, %d\n",
  290. (char*)pkt+i+6, TftpTsize);
  291. }
  292. #endif
  293. }
  294. #ifdef CONFIG_MCAST_TFTP
  295. parse_multicast_oack((char *)pkt,len-1);
  296. if ((Multicast) && (!MasterClient))
  297. TftpState = STATE_DATA; /* passive.. */
  298. else
  299. #endif
  300. TftpSend (); /* Send ACK */
  301. break;
  302. case TFTP_DATA:
  303. if (len < 2)
  304. return;
  305. len -= 2;
  306. TftpBlock = ntohs(*(ushort *)pkt);
  307. /*
  308. * RFC1350 specifies that the first data packet will
  309. * have sequence number 1. If we receive a sequence
  310. * number of 0 this means that there was a wrap
  311. * around of the (16 bit) counter.
  312. */
  313. if (TftpBlock == 0) {
  314. TftpBlockWrap++;
  315. TftpBlockWrapOffset += TftpBlkSize * TFTP_SEQUENCE_SIZE;
  316. printf ("\n\t %lu MB received\n\t ", TftpBlockWrapOffset>>20);
  317. }
  318. #ifdef CONFIG_TFTP_TSIZE
  319. else if (TftpTsize) {
  320. while (TftpNumchars < NetBootFileXferSize * 50 / TftpTsize) {
  321. putc('#');
  322. TftpNumchars++;
  323. }
  324. }
  325. #endif
  326. else {
  327. if (((TftpBlock - 1) % 10) == 0) {
  328. putc ('#');
  329. } else if ((TftpBlock % (10 * HASHES_PER_LINE)) == 0) {
  330. puts ("\n\t ");
  331. }
  332. }
  333. if (TftpState == STATE_RRQ)
  334. debug("Server did not acknowledge timeout option!\n");
  335. if (TftpState == STATE_RRQ || TftpState == STATE_OACK) {
  336. /* first block received */
  337. TftpState = STATE_DATA;
  338. TftpServerPort = src;
  339. TftpLastBlock = 0;
  340. TftpBlockWrap = 0;
  341. TftpBlockWrapOffset = 0;
  342. #ifdef CONFIG_MCAST_TFTP
  343. if (Multicast) { /* start!=1 common if mcast */
  344. TftpLastBlock = TftpBlock - 1;
  345. } else
  346. #endif
  347. if (TftpBlock != 1) { /* Assertion */
  348. printf ("\nTFTP error: "
  349. "First block is not block 1 (%ld)\n"
  350. "Starting again\n\n",
  351. TftpBlock);
  352. NetStartAgain ();
  353. break;
  354. }
  355. }
  356. if (TftpBlock == TftpLastBlock) {
  357. /*
  358. * Same block again; ignore it.
  359. */
  360. break;
  361. }
  362. TftpLastBlock = TftpBlock;
  363. TftpTimeoutMSecs = TIMEOUT;
  364. TftpTimeoutCountMax = TIMEOUT_COUNT;
  365. NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
  366. store_block (TftpBlock - 1, pkt + 2, len);
  367. /*
  368. * Acknoledge the block just received, which will prompt
  369. * the server for the next one.
  370. */
  371. #ifdef CONFIG_MCAST_TFTP
  372. /* if I am the MasterClient, actively calculate what my next
  373. * needed block is; else I'm passive; not ACKING
  374. */
  375. if (Multicast) {
  376. if (len < TftpBlkSize) {
  377. TftpEndingBlock = TftpBlock;
  378. } else if (MasterClient) {
  379. TftpBlock = PrevBitmapHole =
  380. ext2_find_next_zero_bit(
  381. Bitmap,
  382. (Mapsize*8),
  383. PrevBitmapHole);
  384. if (TftpBlock > ((Mapsize*8) - 1)) {
  385. printf ("tftpfile too big\n");
  386. /* try to double it and retry */
  387. Mapsize<<=1;
  388. mcast_cleanup();
  389. NetStartAgain ();
  390. return;
  391. }
  392. TftpLastBlock = TftpBlock;
  393. }
  394. }
  395. #endif
  396. TftpSend ();
  397. #ifdef CONFIG_MCAST_TFTP
  398. if (Multicast) {
  399. if (MasterClient && (TftpBlock >= TftpEndingBlock)) {
  400. puts ("\nMulticast tftp done\n");
  401. mcast_cleanup();
  402. NetState = NETLOOP_SUCCESS;
  403. }
  404. }
  405. else
  406. #endif
  407. if (len < TftpBlkSize) {
  408. /*
  409. * We received the whole thing. Try to
  410. * run it.
  411. */
  412. #ifdef CONFIG_TFTP_TSIZE
  413. /* Print out the hash marks for the last packet received */
  414. while (TftpTsize && TftpNumchars < 49) {
  415. putc('#');
  416. TftpNumchars++;
  417. }
  418. #endif
  419. puts ("\ndone\n");
  420. NetState = NETLOOP_SUCCESS;
  421. }
  422. break;
  423. case TFTP_ERROR:
  424. printf ("\nTFTP error: '%s' (%d)\n",
  425. pkt + 2, ntohs(*(ushort *)pkt));
  426. puts ("Starting again\n\n");
  427. #ifdef CONFIG_MCAST_TFTP
  428. mcast_cleanup();
  429. #endif
  430. NetStartAgain ();
  431. break;
  432. }
  433. }
  434. static void
  435. TftpTimeout (void)
  436. {
  437. if (++TftpTimeoutCount > TftpTimeoutCountMax) {
  438. puts ("\nRetry count exceeded; starting again\n");
  439. #ifdef CONFIG_MCAST_TFTP
  440. mcast_cleanup();
  441. #endif
  442. NetStartAgain ();
  443. } else {
  444. puts ("T ");
  445. NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
  446. TftpSend ();
  447. }
  448. }
  449. void
  450. TftpStart (void)
  451. {
  452. char *ep; /* Environment pointer */
  453. /* Allow the user to choose tftpblocksize */
  454. if ((ep = getenv("tftpblocksize")) != NULL)
  455. TftpBlkSizeOption = simple_strtol(ep, NULL, 10);
  456. debug("tftp block size is %i\n", TftpBlkSizeOption);
  457. TftpServerIP = NetServerIP;
  458. if (BootFile[0] == '\0') {
  459. sprintf(default_filename, "%02lX%02lX%02lX%02lX.img",
  460. NetOurIP & 0xFF,
  461. (NetOurIP >> 8) & 0xFF,
  462. (NetOurIP >> 16) & 0xFF,
  463. (NetOurIP >> 24) & 0xFF );
  464. strncpy(tftp_filename, default_filename, MAX_LEN);
  465. tftp_filename[MAX_LEN-1] = 0;
  466. printf ("*** Warning: no boot file name; using '%s'\n",
  467. tftp_filename);
  468. } else {
  469. char *p = strchr (BootFile, ':');
  470. if (p == NULL) {
  471. strncpy(tftp_filename, BootFile, MAX_LEN);
  472. tftp_filename[MAX_LEN-1] = 0;
  473. } else {
  474. TftpServerIP = string_to_ip (BootFile);
  475. strncpy(tftp_filename, p + 1, MAX_LEN);
  476. tftp_filename[MAX_LEN-1] = 0;
  477. }
  478. }
  479. #if defined(CONFIG_NET_MULTI)
  480. printf ("Using %s device\n", eth_get_name());
  481. #endif
  482. printf("TFTP from server %pI4"
  483. "; our IP address is %pI4", &TftpServerIP, &NetOurIP);
  484. /* Check if we need to send across this subnet */
  485. if (NetOurGatewayIP && NetOurSubnetMask) {
  486. IPaddr_t OurNet = NetOurIP & NetOurSubnetMask;
  487. IPaddr_t ServerNet = TftpServerIP & NetOurSubnetMask;
  488. if (OurNet != ServerNet)
  489. printf("; sending through gateway %pI4", &NetOurGatewayIP);
  490. }
  491. putc ('\n');
  492. printf ("Filename '%s'.", tftp_filename);
  493. if (NetBootFileSize) {
  494. printf (" Size is 0x%x Bytes = ", NetBootFileSize<<9);
  495. print_size (NetBootFileSize<<9, "");
  496. }
  497. putc ('\n');
  498. printf ("Load address: 0x%lx\n", load_addr);
  499. puts ("Loading: *\b");
  500. TftpTimeoutMSecs = TftpRRQTimeoutMSecs;
  501. TftpTimeoutCountMax = TftpRRQTimeoutCountMax;
  502. NetSetTimeout (TftpTimeoutMSecs, TftpTimeout);
  503. NetSetHandler (TftpHandler);
  504. TftpServerPort = WELL_KNOWN_PORT;
  505. TftpTimeoutCount = 0;
  506. TftpState = STATE_RRQ;
  507. /* Use a pseudo-random port unless a specific port is set */
  508. TftpOurPort = 1024 + (get_timer(0) % 3072);
  509. #ifdef CONFIG_TFTP_PORT
  510. if ((ep = getenv("tftpdstp")) != NULL) {
  511. TftpServerPort = simple_strtol(ep, NULL, 10);
  512. }
  513. if ((ep = getenv("tftpsrcp")) != NULL) {
  514. TftpOurPort= simple_strtol(ep, NULL, 10);
  515. }
  516. #endif
  517. TftpBlock = 0;
  518. /* zero out server ether in case the server ip has changed */
  519. memset(NetServerEther, 0, 6);
  520. /* Revert TftpBlkSize to dflt */
  521. TftpBlkSize = TFTP_BLOCK_SIZE;
  522. #ifdef CONFIG_MCAST_TFTP
  523. mcast_cleanup();
  524. #endif
  525. #ifdef CONFIG_TFTP_TSIZE
  526. TftpTsize = 0;
  527. TftpNumchars = 0;
  528. #endif
  529. TftpSend ();
  530. }
  531. #ifdef CONFIG_MCAST_TFTP
  532. /* Credits: atftp project.
  533. */
  534. /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
  535. * Frame:
  536. * +-------+-----------+---+-------~~-------+---+
  537. * | opc | multicast | 0 | addr, port, mc | 0 |
  538. * +-------+-----------+---+-------~~-------+---+
  539. * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
  540. * I am the new master-client so must send ACKs to DataBlocks. If I am not
  541. * master-client, I'm a passive client, gathering what DataBlocks I may and
  542. * making note of which ones I got in my bitmask.
  543. * In theory, I never go from master->passive..
  544. * .. this comes in with pkt already pointing just past opc
  545. */
  546. static void parse_multicast_oack(char *pkt, int len)
  547. {
  548. int i;
  549. IPaddr_t addr;
  550. char *mc_adr, *port, *mc;
  551. mc_adr=port=mc=NULL;
  552. /* march along looking for 'multicast\0', which has to start at least
  553. * 14 bytes back from the end.
  554. */
  555. for (i=0;i<len-14;i++)
  556. if (strcmp (pkt+i,"multicast") == 0)
  557. break;
  558. if (i >= (len-14)) /* non-Multicast OACK, ign. */
  559. return;
  560. i+=10; /* strlen multicast */
  561. mc_adr = pkt+i;
  562. for (;i<len;i++) {
  563. if (*(pkt+i) == ',') {
  564. *(pkt+i) = '\0';
  565. if (port) {
  566. mc = pkt+i+1;
  567. break;
  568. } else {
  569. port = pkt+i+1;
  570. }
  571. }
  572. }
  573. if (!port || !mc_adr || !mc ) return;
  574. if (Multicast && MasterClient) {
  575. printf ("I got a OACK as master Client, WRONG!\n");
  576. return;
  577. }
  578. /* ..I now accept packets destined for this MCAST addr, port */
  579. if (!Multicast) {
  580. if (Bitmap) {
  581. printf ("Internal failure! no mcast.\n");
  582. free(Bitmap);
  583. Bitmap=NULL;
  584. ProhibitMcast=1;
  585. return ;
  586. }
  587. /* I malloc instead of pre-declare; so that if the file ends
  588. * up being too big for this bitmap I can retry
  589. */
  590. if (!(Bitmap = malloc (Mapsize))) {
  591. printf ("No Bitmap, no multicast. Sorry.\n");
  592. ProhibitMcast=1;
  593. return;
  594. }
  595. memset (Bitmap,0,Mapsize);
  596. PrevBitmapHole = 0;
  597. Multicast = 1;
  598. }
  599. addr = string_to_ip(mc_adr);
  600. if (Mcast_addr != addr) {
  601. if (Mcast_addr)
  602. eth_mcast_join(Mcast_addr, 0);
  603. if (eth_mcast_join(Mcast_addr=addr, 1)) {
  604. printf ("Fail to set mcast, revert to TFTP\n");
  605. ProhibitMcast=1;
  606. mcast_cleanup();
  607. NetStartAgain();
  608. }
  609. }
  610. MasterClient = (unsigned char)simple_strtoul((char *)mc,NULL,10);
  611. Mcast_port = (unsigned short)simple_strtoul(port,NULL,10);
  612. printf ("Multicast: %s:%d [%d]\n", mc_adr, Mcast_port, MasterClient);
  613. return;
  614. }
  615. #endif /* Multicast TFTP */
  616. #endif