2 * Copyright 1994, 1995, 2000 Neil Russell.
4 * Copyright 2000, 2001 DENX Software Engineering, Wolfgang Denk, wd@denx.de
5 * Copyright 2011 Comelit Group SpA,
6 * Luca Ceresoli <luca.ceresoli@comelit.it>
15 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
19 /* Well known TFTP port # */
20 #define WELL_KNOWN_PORT 69
21 /* Millisecs to timeout for lost pkt */
22 #define TIMEOUT 5000UL
23 #ifndef CONFIG_NET_RETRY_COUNT
24 /* # of timeouts before giving up */
25 # define TIMEOUT_COUNT 10
27 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
29 /* Number of "loading" hashes per line (for checking the image size) */
30 #define HASHES_PER_LINE 65
42 static ulong TftpTimeoutMSecs
= TIMEOUT
;
43 static int TftpTimeoutCountMax
= TIMEOUT_COUNT
;
44 static ulong time_start
; /* Record time we started tftp */
47 * These globals govern the timeout behavior when attempting a connection to a
48 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
49 * wait for the server to respond to initial connection. Second global,
50 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
51 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
52 * positive. The globals are meant to be set (and restored) by code needing
53 * non-standard timeout behavior when initiating a TFTP transfer.
55 ulong TftpRRQTimeoutMSecs
= TIMEOUT
;
56 int TftpRRQTimeoutCountMax
= TIMEOUT_COUNT
;
59 TFTP_ERR_UNDEFINED
= 0,
60 TFTP_ERR_FILE_NOT_FOUND
= 1,
61 TFTP_ERR_ACCESS_DENIED
= 2,
62 TFTP_ERR_DISK_FULL
= 3,
63 TFTP_ERR_UNEXPECTED_OPCODE
= 4,
64 TFTP_ERR_UNKNOWN_TRANSFER_ID
= 5,
65 TFTP_ERR_FILE_ALREADY_EXISTS
= 6,
68 static IPaddr_t TftpRemoteIP
;
69 /* The UDP port at their end */
70 static int TftpRemotePort
;
71 /* The UDP port at our end */
72 static int TftpOurPort
;
73 static int TftpTimeoutCount
;
74 /* packet sequence number */
75 static ulong TftpBlock
;
76 /* last packet sequence number received */
77 static ulong TftpLastBlock
;
78 /* count of sequence number wraparounds */
79 static ulong TftpBlockWrap
;
80 /* memory offset due to wrapping */
81 static ulong TftpBlockWrapOffset
;
83 #ifdef CONFIG_TFTP_TSIZE
84 /* The file size reported by the server */
86 /* The number of hashes we printed */
87 static short TftpNumchars
;
89 #ifdef CONFIG_CMD_TFTPPUT
90 static int TftpWriting
; /* 1 if writing, else 0 */
91 static int TftpFinalBlock
; /* 1 if we have sent the last block */
96 #define STATE_SEND_RRQ 1
98 #define STATE_TOO_LARGE 3
99 #define STATE_BAD_MAGIC 4
101 #define STATE_RECV_WRQ 6
102 #define STATE_SEND_WRQ 7
104 /* default TFTP block size */
105 #define TFTP_BLOCK_SIZE 512
106 /* sequence number is 16 bit */
107 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
109 #define DEFAULT_NAME_LEN (8 + 4 + 1)
110 static char default_filename
[DEFAULT_NAME_LEN
];
112 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
115 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
118 static char tftp_filename
[MAX_LEN
];
120 /* 512 is poor choice for ethernet, MTU is typically 1500.
121 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
122 * almost-MTU block sizes. At least try... fall back to 512 if need be.
123 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
125 #ifdef CONFIG_TFTP_BLOCKSIZE
126 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
128 #define TFTP_MTU_BLOCKSIZE 1468
131 static unsigned short TftpBlkSize
= TFTP_BLOCK_SIZE
;
132 static unsigned short TftpBlkSizeOption
= TFTP_MTU_BLOCKSIZE
;
134 #ifdef CONFIG_MCAST_TFTP
136 #define MTFTP_BITMAPSIZE 0x1000
137 static unsigned *Bitmap
;
138 static int PrevBitmapHole
, Mapsize
= MTFTP_BITMAPSIZE
;
139 static uchar ProhibitMcast
, MasterClient
;
140 static uchar Multicast
;
141 static int Mcast_port
;
142 static ulong TftpEndingBlock
; /* can get 'last' block before done..*/
144 static void parse_multicast_oack(char *pkt
, int len
);
150 eth_mcast_join(Mcast_addr
, 0);
154 Mcast_addr
= Multicast
= Mcast_port
= 0;
155 TftpEndingBlock
= -1;
158 #endif /* CONFIG_MCAST_TFTP */
161 store_block(int block
, uchar
*src
, unsigned len
)
163 ulong offset
= block
* TftpBlkSize
+ TftpBlockWrapOffset
;
164 ulong newsize
= offset
+ len
;
165 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
168 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
169 /* start address in flash? */
170 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
)
172 if (load_addr
+ offset
>= flash_info
[i
].start
[0]) {
178 if (rc
) { /* Flash is destination for this packet */
179 rc
= flash_write((char *)src
, (ulong
)(load_addr
+offset
), len
);
182 net_set_state(NETLOOP_FAIL
);
186 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
188 void *ptr
= map_sysmem(load_addr
+ offset
, len
);
190 memcpy(ptr
, src
, len
);
193 #ifdef CONFIG_MCAST_TFTP
195 ext2_set_bit(block
, Bitmap
);
198 if (NetBootFileXferSize
< newsize
)
199 NetBootFileXferSize
= newsize
;
202 /* Clear our state ready for a new transfer */
203 static void new_transfer(void)
207 TftpBlockWrapOffset
= 0;
208 #ifdef CONFIG_CMD_TFTPPUT
213 #ifdef CONFIG_CMD_TFTPPUT
215 * Load the next block from memory to be sent over tftp.
217 * @param block Block number to send
218 * @param dst Destination buffer for data
219 * @param len Number of bytes in block (this one and every other)
220 * @return number of bytes loaded
222 static int load_block(unsigned block
, uchar
*dst
, unsigned len
)
224 /* We may want to get the final block from the previous set */
225 ulong offset
= ((int)block
- 1) * len
+ TftpBlockWrapOffset
;
228 tosend
= min(NetBootFileXferSize
- offset
, tosend
);
229 (void)memcpy(dst
, (void *)(save_addr
+ offset
), tosend
);
230 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__
,
231 block
, offset
, len
, tosend
);
236 static void TftpSend(void);
237 static void TftpTimeout(void);
239 /**********************************************************************/
241 static void show_block_marker(void)
243 #ifdef CONFIG_TFTP_TSIZE
245 ulong pos
= TftpBlock
* TftpBlkSize
+ TftpBlockWrapOffset
;
247 while (TftpNumchars
< pos
* 50 / TftpTsize
) {
254 if (((TftpBlock
- 1) % 10) == 0)
256 else if ((TftpBlock
% (10 * HASHES_PER_LINE
)) == 0)
262 * restart the current transfer due to an error
264 * @param msg Message to print for user
266 static void restart(const char *msg
)
268 printf("\n%s; starting again\n", msg
);
269 #ifdef CONFIG_MCAST_TFTP
276 * Check if the block number has wrapped, and update progress
278 * TODO: The egregious use of global variables in this file should be tidied.
280 static void update_block_number(void)
283 * RFC1350 specifies that the first data packet will
284 * have sequence number 1. If we receive a sequence
285 * number of 0 this means that there was a wrap
286 * around of the (16 bit) counter.
288 if (TftpBlock
== 0 && TftpLastBlock
!= 0) {
290 TftpBlockWrapOffset
+= TftpBlkSize
* TFTP_SEQUENCE_SIZE
;
291 TftpTimeoutCount
= 0; /* we've done well, reset thhe timeout */
297 /* The TFTP get or put is complete */
298 static void tftp_complete(void)
300 #ifdef CONFIG_TFTP_TSIZE
301 /* Print hash marks for the last packet received */
302 while (TftpTsize
&& TftpNumchars
< 49) {
307 print_size(TftpTsize
, "");
309 time_start
= get_timer(time_start
);
310 if (time_start
> 0) {
311 puts("\n\t "); /* Line up with "Loading: " */
312 print_size(NetBootFileXferSize
/
313 time_start
* 1000, "/s");
316 net_set_state(NETLOOP_SUCCESS
);
327 #ifdef CONFIG_MCAST_TFTP
328 /* Multicast TFTP.. non-MasterClients do not ACK data. */
330 && (TftpState
== STATE_DATA
)
331 && (MasterClient
== 0))
335 * We will always be sending some sort of packet, so
336 * cobble together the packet headers now.
338 pkt
= NetTxPacket
+ NetEthHdrSize() + IP_UDP_HDR_SIZE
;
345 #ifdef CONFIG_CMD_TFTPPUT
346 *s
++ = htons(TftpState
== STATE_SEND_RRQ
? TFTP_RRQ
:
349 *s
++ = htons(TFTP_RRQ
);
352 strcpy((char *)pkt
, tftp_filename
);
353 pkt
+= strlen(tftp_filename
) + 1;
354 strcpy((char *)pkt
, "octet");
355 pkt
+= 5 /*strlen("octet")*/ + 1;
356 strcpy((char *)pkt
, "timeout");
357 pkt
+= 7 /*strlen("timeout")*/ + 1;
358 sprintf((char *)pkt
, "%lu", TftpTimeoutMSecs
/ 1000);
359 debug("send option \"timeout %s\"\n", (char *)pkt
);
360 pkt
+= strlen((char *)pkt
) + 1;
361 #ifdef CONFIG_TFTP_TSIZE
362 pkt
+= sprintf((char *)pkt
, "tsize%c%lu%c",
363 0, NetBootFileXferSize
, 0);
365 /* try for more effic. blk size */
366 pkt
+= sprintf((char *)pkt
, "blksize%c%d%c",
367 0, TftpBlkSizeOption
, 0);
368 #ifdef CONFIG_MCAST_TFTP
369 /* Check all preconditions before even trying the option */
370 if (!ProhibitMcast
) {
371 Bitmap
= malloc(Mapsize
);
372 if (Bitmap
&& eth_get_dev()->mcast
) {
375 pkt
+= sprintf((char *)pkt
, "multicast%c%c",
379 #endif /* CONFIG_MCAST_TFTP */
384 #ifdef CONFIG_MCAST_TFTP
385 /* My turn! Start at where I need blocks I missed.*/
387 TftpBlock
= ext2_find_next_zero_bit(Bitmap
,
396 s
[0] = htons(TFTP_ACK
);
397 s
[1] = htons(TftpBlock
);
398 pkt
= (uchar
*)(s
+ 2);
399 #ifdef CONFIG_CMD_TFTPPUT
401 int toload
= TftpBlkSize
;
402 int loaded
= load_block(TftpBlock
, pkt
, toload
);
404 s
[0] = htons(TFTP_DATA
);
406 TftpFinalBlock
= (loaded
< toload
);
412 case STATE_TOO_LARGE
:
415 *s
++ = htons(TFTP_ERROR
);
419 strcpy((char *)pkt
, "File too large");
420 pkt
+= 14 /*strlen("File too large")*/ + 1;
424 case STATE_BAD_MAGIC
:
427 *s
++ = htons(TFTP_ERROR
);
430 strcpy((char *)pkt
, "File has bad magic");
431 pkt
+= 18 /*strlen("File has bad magic")*/ + 1;
436 NetSendUDPPacket(NetServerEther
, TftpRemoteIP
, TftpRemotePort
,
440 #ifdef CONFIG_CMD_TFTPPUT
441 static void icmp_handler(unsigned type
, unsigned code
, unsigned dest
,
442 IPaddr_t sip
, unsigned src
, uchar
*pkt
, unsigned len
)
444 if (type
== ICMP_NOT_REACH
&& code
== ICMP_NOT_REACH_PORT
) {
445 /* Oh dear the other end has gone away */
446 restart("TFTP server died");
452 TftpHandler(uchar
*pkt
, unsigned dest
, IPaddr_t sip
, unsigned src
,
459 if (dest
!= TftpOurPort
) {
460 #ifdef CONFIG_MCAST_TFTP
462 && (!Mcast_port
|| (dest
!= Mcast_port
)))
466 if (TftpState
!= STATE_SEND_RRQ
&& src
!= TftpRemotePort
&&
467 TftpState
!= STATE_RECV_WRQ
&& TftpState
!= STATE_SEND_WRQ
)
473 /* warning: don't use increment (++) in ntohs() macros!! */
477 switch (ntohs(proto
)) {
483 #ifdef CONFIG_CMD_TFTPPUT
485 if (TftpFinalBlock
) {
489 * Move to the next block. We want our block
490 * count to wrap just like the other end!
492 int block
= ntohs(*s
);
493 int ack_ok
= (TftpBlock
== block
);
495 TftpBlock
= (unsigned short)(block
+ 1);
496 update_block_number();
498 TftpSend(); /* Send next data block */
507 #ifdef CONFIG_CMD_TFTPSRV
511 TftpRemotePort
= src
;
512 TftpOurPort
= 1024 + (get_timer(0) % 3072);
514 TftpSend(); /* Send ACK(0) */
519 debug("Got OACK: %s %s\n",
521 pkt
+ strlen((char *)pkt
) + 1);
522 TftpState
= STATE_OACK
;
523 TftpRemotePort
= src
;
525 * Check for 'blksize' option.
526 * Careful: "i" is signed, "len" is unsigned, thus
527 * something like "len-8" may give a *huge* number
529 for (i
= 0; i
+8 < len
; i
++) {
530 if (strcmp((char *)pkt
+i
, "blksize") == 0) {
531 TftpBlkSize
= (unsigned short)
532 simple_strtoul((char *)pkt
+i
+8, NULL
,
534 debug("Blocksize ack: %s, %d\n",
535 (char *)pkt
+i
+8, TftpBlkSize
);
537 #ifdef CONFIG_TFTP_TSIZE
538 if (strcmp((char *)pkt
+i
, "tsize") == 0) {
539 TftpTsize
= simple_strtoul((char *)pkt
+i
+6,
541 debug("size = %s, %d\n",
542 (char *)pkt
+i
+6, TftpTsize
);
546 #ifdef CONFIG_MCAST_TFTP
547 parse_multicast_oack((char *)pkt
, len
-1);
548 if ((Multicast
) && (!MasterClient
))
549 TftpState
= STATE_DATA
; /* passive.. */
552 #ifdef CONFIG_CMD_TFTPPUT
554 /* Get ready to send the first block */
555 TftpState
= STATE_DATA
;
559 TftpSend(); /* Send ACK or first data block */
565 TftpBlock
= ntohs(*(__be16
*)pkt
);
567 update_block_number();
569 if (TftpState
== STATE_SEND_RRQ
)
570 debug("Server did not acknowledge timeout option!\n");
572 if (TftpState
== STATE_SEND_RRQ
|| TftpState
== STATE_OACK
||
573 TftpState
== STATE_RECV_WRQ
) {
574 /* first block received */
575 TftpState
= STATE_DATA
;
576 TftpRemotePort
= src
;
579 #ifdef CONFIG_MCAST_TFTP
580 if (Multicast
) { /* start!=1 common if mcast */
581 TftpLastBlock
= TftpBlock
- 1;
584 if (TftpBlock
!= 1) { /* Assertion */
585 printf("\nTFTP error: "
586 "First block is not block 1 (%ld)\n"
587 "Starting again\n\n",
594 if (TftpBlock
== TftpLastBlock
) {
596 * Same block again; ignore it.
601 TftpLastBlock
= TftpBlock
;
602 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
603 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
605 store_block(TftpBlock
- 1, pkt
+ 2, len
);
608 * Acknowledge the block just received, which will prompt
609 * the remote for the next one.
611 #ifdef CONFIG_MCAST_TFTP
612 /* if I am the MasterClient, actively calculate what my next
613 * needed block is; else I'm passive; not ACKING
616 if (len
< TftpBlkSize
) {
617 TftpEndingBlock
= TftpBlock
;
618 } else if (MasterClient
) {
619 TftpBlock
= PrevBitmapHole
=
620 ext2_find_next_zero_bit(
624 if (TftpBlock
> ((Mapsize
*8) - 1)) {
625 printf("tftpfile too big\n");
626 /* try to double it and retry */
632 TftpLastBlock
= TftpBlock
;
638 #ifdef CONFIG_MCAST_TFTP
640 if (MasterClient
&& (TftpBlock
>= TftpEndingBlock
)) {
641 puts("\nMulticast tftp done\n");
643 net_set_state(NETLOOP_SUCCESS
);
647 if (len
< TftpBlkSize
)
652 printf("\nTFTP error: '%s' (%d)\n",
653 pkt
+ 2, ntohs(*(__be16
*)pkt
));
655 switch (ntohs(*(__be16
*)pkt
)) {
656 case TFTP_ERR_FILE_NOT_FOUND
:
657 case TFTP_ERR_ACCESS_DENIED
:
658 puts("Not retrying...\n");
660 net_set_state(NETLOOP_FAIL
);
662 case TFTP_ERR_UNDEFINED
:
663 case TFTP_ERR_DISK_FULL
:
664 case TFTP_ERR_UNEXPECTED_OPCODE
:
665 case TFTP_ERR_UNKNOWN_TRANSFER_ID
:
666 case TFTP_ERR_FILE_ALREADY_EXISTS
:
668 puts("Starting again\n\n");
669 #ifdef CONFIG_MCAST_TFTP
683 if (++TftpTimeoutCount
> TftpTimeoutCountMax
) {
684 restart("Retry count exceeded");
687 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
688 if (TftpState
!= STATE_RECV_WRQ
)
694 void TftpStart(enum proto_t protocol
)
696 char *ep
; /* Environment pointer */
699 * Allow the user to choose TFTP blocksize and timeout.
700 * TFTP protocol has a minimal timeout of 1 second.
702 ep
= getenv("tftpblocksize");
704 TftpBlkSizeOption
= simple_strtol(ep
, NULL
, 10);
706 ep
= getenv("tftptimeout");
708 TftpTimeoutMSecs
= simple_strtol(ep
, NULL
, 10);
710 if (TftpTimeoutMSecs
< 1000) {
711 printf("TFTP timeout (%ld ms) too low, "
712 "set minimum = 1000 ms\n",
714 TftpTimeoutMSecs
= 1000;
717 debug("TFTP blocksize = %i, timeout = %ld ms\n",
718 TftpBlkSizeOption
, TftpTimeoutMSecs
);
720 TftpRemoteIP
= NetServerIP
;
721 if (BootFile
[0] == '\0') {
722 sprintf(default_filename
, "%02X%02X%02X%02X.img",
724 (NetOurIP
>> 8) & 0xFF,
725 (NetOurIP
>> 16) & 0xFF,
726 (NetOurIP
>> 24) & 0xFF);
728 strncpy(tftp_filename
, default_filename
, MAX_LEN
);
729 tftp_filename
[MAX_LEN
-1] = 0;
731 printf("*** Warning: no boot file name; using '%s'\n",
734 char *p
= strchr(BootFile
, ':');
737 strncpy(tftp_filename
, BootFile
, MAX_LEN
);
738 tftp_filename
[MAX_LEN
-1] = 0;
740 TftpRemoteIP
= string_to_ip(BootFile
);
741 strncpy(tftp_filename
, p
+ 1, MAX_LEN
);
742 tftp_filename
[MAX_LEN
-1] = 0;
746 printf("Using %s device\n", eth_get_name());
747 printf("TFTP %s server %pI4; our IP address is %pI4",
748 #ifdef CONFIG_CMD_TFTPPUT
749 protocol
== TFTPPUT
? "to" : "from",
753 &TftpRemoteIP
, &NetOurIP
);
755 /* Check if we need to send across this subnet */
756 if (NetOurGatewayIP
&& NetOurSubnetMask
) {
757 IPaddr_t OurNet
= NetOurIP
& NetOurSubnetMask
;
758 IPaddr_t RemoteNet
= TftpRemoteIP
& NetOurSubnetMask
;
760 if (OurNet
!= RemoteNet
)
761 printf("; sending through gateway %pI4",
766 printf("Filename '%s'.", tftp_filename
);
768 if (NetBootFileSize
) {
769 printf(" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
770 print_size(NetBootFileSize
<<9, "");
774 #ifdef CONFIG_CMD_TFTPPUT
775 TftpWriting
= (protocol
== TFTPPUT
);
777 printf("Save address: 0x%lx\n", save_addr
);
778 printf("Save size: 0x%lx\n", save_size
);
779 NetBootFileXferSize
= save_size
;
781 TftpState
= STATE_SEND_WRQ
;
786 printf("Load address: 0x%lx\n", load_addr
);
787 puts("Loading: *\b");
788 TftpState
= STATE_SEND_RRQ
;
791 time_start
= get_timer(0);
792 TftpTimeoutCountMax
= TftpRRQTimeoutCountMax
;
794 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
795 net_set_udp_handler(TftpHandler
);
796 #ifdef CONFIG_CMD_TFTPPUT
797 net_set_icmp_handler(icmp_handler
);
799 TftpRemotePort
= WELL_KNOWN_PORT
;
800 TftpTimeoutCount
= 0;
801 /* Use a pseudo-random port unless a specific port is set */
802 TftpOurPort
= 1024 + (get_timer(0) % 3072);
804 #ifdef CONFIG_TFTP_PORT
805 ep
= getenv("tftpdstp");
807 TftpRemotePort
= simple_strtol(ep
, NULL
, 10);
808 ep
= getenv("tftpsrcp");
810 TftpOurPort
= simple_strtol(ep
, NULL
, 10);
814 /* zero out server ether in case the server ip has changed */
815 memset(NetServerEther
, 0, 6);
816 /* Revert TftpBlkSize to dflt */
817 TftpBlkSize
= TFTP_BLOCK_SIZE
;
818 #ifdef CONFIG_MCAST_TFTP
821 #ifdef CONFIG_TFTP_TSIZE
829 #ifdef CONFIG_CMD_TFTPSRV
831 TftpStartServer(void)
833 tftp_filename
[0] = 0;
835 printf("Using %s device\n", eth_get_name());
836 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP
);
837 printf("Load address: 0x%lx\n", load_addr
);
839 puts("Loading: *\b");
841 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
842 TftpTimeoutCount
= 0;
843 TftpTimeoutMSecs
= TIMEOUT
;
844 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
846 /* Revert TftpBlkSize to dflt */
847 TftpBlkSize
= TFTP_BLOCK_SIZE
;
849 TftpOurPort
= WELL_KNOWN_PORT
;
851 #ifdef CONFIG_TFTP_TSIZE
856 TftpState
= STATE_RECV_WRQ
;
857 net_set_udp_handler(TftpHandler
);
859 /* zero out server ether in case the server ip has changed */
860 memset(NetServerEther
, 0, 6);
862 #endif /* CONFIG_CMD_TFTPSRV */
864 #ifdef CONFIG_MCAST_TFTP
865 /* Credits: atftp project.
868 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
870 * +-------+-----------+---+-------~~-------+---+
871 * | opc | multicast | 0 | addr, port, mc | 0 |
872 * +-------+-----------+---+-------~~-------+---+
873 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
874 * I am the new master-client so must send ACKs to DataBlocks. If I am not
875 * master-client, I'm a passive client, gathering what DataBlocks I may and
876 * making note of which ones I got in my bitmask.
877 * In theory, I never go from master->passive..
878 * .. this comes in with pkt already pointing just past opc
880 static void parse_multicast_oack(char *pkt
, int len
)
884 char *mc_adr
, *port
, *mc
;
886 mc_adr
= port
= mc
= NULL
;
887 /* march along looking for 'multicast\0', which has to start at least
888 * 14 bytes back from the end.
890 for (i
= 0; i
< len
-14; i
++)
891 if (strcmp(pkt
+i
, "multicast") == 0)
893 if (i
>= (len
-14)) /* non-Multicast OACK, ign. */
896 i
+= 10; /* strlen multicast */
898 for (; i
< len
; i
++) {
899 if (*(pkt
+i
) == ',') {
909 if (!port
|| !mc_adr
|| !mc
)
911 if (Multicast
&& MasterClient
) {
912 printf("I got a OACK as master Client, WRONG!\n");
915 /* ..I now accept packets destined for this MCAST addr, port */
918 printf("Internal failure! no mcast.\n");
924 /* I malloc instead of pre-declare; so that if the file ends
925 * up being too big for this bitmap I can retry
927 Bitmap
= malloc(Mapsize
);
929 printf("No Bitmap, no multicast. Sorry.\n");
933 memset(Bitmap
, 0, Mapsize
);
937 addr
= string_to_ip(mc_adr
);
938 if (Mcast_addr
!= addr
) {
940 eth_mcast_join(Mcast_addr
, 0);
942 if (eth_mcast_join(Mcast_addr
, 1)) {
943 printf("Fail to set mcast, revert to TFTP\n");
949 MasterClient
= (unsigned char)simple_strtoul((char *)mc
, NULL
, 10);
950 Mcast_port
= (unsigned short)simple_strtoul(port
, NULL
, 10);
951 printf("Multicast: %s:%d [%d]\n", mc_adr
, Mcast_port
, MasterClient
);
955 #endif /* Multicast TFTP */