tftp.c 17 KB

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