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>
14 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
18 /* Well known TFTP port # */
19 #define WELL_KNOWN_PORT 69
20 /* Millisecs to timeout for lost pkt */
21 #define TIMEOUT 5000UL
22 #ifndef CONFIG_NET_RETRY_COUNT
23 /* # of timeouts before giving up */
24 # define TIMEOUT_COUNT 10
26 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
28 /* Number of "loading" hashes per line (for checking the image size) */
29 #define HASHES_PER_LINE 65
41 static ulong TftpTimeoutMSecs
= TIMEOUT
;
42 static int TftpTimeoutCountMax
= TIMEOUT_COUNT
;
43 static ulong time_start
; /* Record time we started tftp */
46 * These globals govern the timeout behavior when attempting a connection to a
47 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
48 * wait for the server to respond to initial connection. Second global,
49 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
50 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
51 * positive. The globals are meant to be set (and restored) by code needing
52 * non-standard timeout behavior when initiating a TFTP transfer.
54 ulong TftpRRQTimeoutMSecs
= TIMEOUT
;
55 int TftpRRQTimeoutCountMax
= TIMEOUT_COUNT
;
58 TFTP_ERR_UNDEFINED
= 0,
59 TFTP_ERR_FILE_NOT_FOUND
= 1,
60 TFTP_ERR_ACCESS_DENIED
= 2,
61 TFTP_ERR_DISK_FULL
= 3,
62 TFTP_ERR_UNEXPECTED_OPCODE
= 4,
63 TFTP_ERR_UNKNOWN_TRANSFER_ID
= 5,
64 TFTP_ERR_FILE_ALREADY_EXISTS
= 6,
67 static IPaddr_t TftpRemoteIP
;
68 /* The UDP port at their end */
69 static int TftpRemotePort
;
70 /* The UDP port at our end */
71 static int TftpOurPort
;
72 static int TftpTimeoutCount
;
73 /* packet sequence number */
74 static ulong TftpBlock
;
75 /* last packet sequence number received */
76 static ulong TftpLastBlock
;
77 /* count of sequence number wraparounds */
78 static ulong TftpBlockWrap
;
79 /* memory offset due to wrapping */
80 static ulong TftpBlockWrapOffset
;
82 #ifdef CONFIG_TFTP_TSIZE
83 /* The file size reported by the server */
85 /* The number of hashes we printed */
86 static short TftpNumchars
;
88 #ifdef CONFIG_CMD_TFTPPUT
89 static int TftpWriting
; /* 1 if writing, else 0 */
90 static int TftpFinalBlock
; /* 1 if we have sent the last block */
95 #define STATE_SEND_RRQ 1
97 #define STATE_TOO_LARGE 3
98 #define STATE_BAD_MAGIC 4
100 #define STATE_RECV_WRQ 6
101 #define STATE_SEND_WRQ 7
103 /* default TFTP block size */
104 #define TFTP_BLOCK_SIZE 512
105 /* sequence number is 16 bit */
106 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
108 #define DEFAULT_NAME_LEN (8 + 4 + 1)
109 static char default_filename
[DEFAULT_NAME_LEN
];
111 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
114 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
117 static char tftp_filename
[MAX_LEN
];
119 /* 512 is poor choice for ethernet, MTU is typically 1500.
120 * Minus eth.hdrs thats 1468. Can get 2x better throughput with
121 * almost-MTU block sizes. At least try... fall back to 512 if need be.
122 * (but those using CONFIG_IP_DEFRAG may want to set a larger block in cfg file)
124 #ifdef CONFIG_TFTP_BLOCKSIZE
125 #define TFTP_MTU_BLOCKSIZE CONFIG_TFTP_BLOCKSIZE
127 #define TFTP_MTU_BLOCKSIZE 1468
130 static unsigned short TftpBlkSize
= TFTP_BLOCK_SIZE
;
131 static unsigned short TftpBlkSizeOption
= TFTP_MTU_BLOCKSIZE
;
133 #ifdef CONFIG_MCAST_TFTP
135 #define MTFTP_BITMAPSIZE 0x1000
136 static unsigned *Bitmap
;
137 static int PrevBitmapHole
, Mapsize
= MTFTP_BITMAPSIZE
;
138 static uchar ProhibitMcast
, MasterClient
;
139 static uchar Multicast
;
140 static int Mcast_port
;
141 static ulong TftpEndingBlock
; /* can get 'last' block before done..*/
143 static void parse_multicast_oack(char *pkt
, int len
);
149 eth_mcast_join(Mcast_addr
, 0);
153 Mcast_addr
= Multicast
= Mcast_port
= 0;
154 TftpEndingBlock
= -1;
157 #endif /* CONFIG_MCAST_TFTP */
160 store_block(int block
, uchar
*src
, unsigned len
)
162 ulong offset
= block
* TftpBlkSize
+ TftpBlockWrapOffset
;
163 ulong newsize
= offset
+ len
;
164 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
167 for (i
= 0; i
< CONFIG_SYS_MAX_FLASH_BANKS
; i
++) {
168 /* start address in flash? */
169 if (flash_info
[i
].flash_id
== FLASH_UNKNOWN
)
171 if (load_addr
+ offset
>= flash_info
[i
].start
[0]) {
177 if (rc
) { /* Flash is destination for this packet */
178 rc
= flash_write((char *)src
, (ulong
)(load_addr
+offset
), len
);
181 net_set_state(NETLOOP_FAIL
);
185 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
187 (void)memcpy((void *)(load_addr
+ offset
), src
, len
);
189 #ifdef CONFIG_MCAST_TFTP
191 ext2_set_bit(block
, Bitmap
);
194 if (NetBootFileXferSize
< newsize
)
195 NetBootFileXferSize
= newsize
;
198 /* Clear our state ready for a new transfer */
199 static void new_transfer(void)
203 TftpBlockWrapOffset
= 0;
204 #ifdef CONFIG_CMD_TFTPPUT
209 #ifdef CONFIG_CMD_TFTPPUT
211 * Load the next block from memory to be sent over tftp.
213 * @param block Block number to send
214 * @param dst Destination buffer for data
215 * @param len Number of bytes in block (this one and every other)
216 * @return number of bytes loaded
218 static int load_block(unsigned block
, uchar
*dst
, unsigned len
)
220 /* We may want to get the final block from the previous set */
221 ulong offset
= ((int)block
- 1) * len
+ TftpBlockWrapOffset
;
224 tosend
= min(NetBootFileXferSize
- offset
, tosend
);
225 (void)memcpy(dst
, (void *)(save_addr
+ offset
), tosend
);
226 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__
,
227 block
, offset
, len
, tosend
);
232 static void TftpSend(void);
233 static void TftpTimeout(void);
235 /**********************************************************************/
237 static void show_block_marker(void)
239 #ifdef CONFIG_TFTP_TSIZE
241 ulong pos
= TftpBlock
* TftpBlkSize
+ TftpBlockWrapOffset
;
243 while (TftpNumchars
< pos
* 50 / TftpTsize
) {
250 if (((TftpBlock
- 1) % 10) == 0)
252 else if ((TftpBlock
% (10 * HASHES_PER_LINE
)) == 0)
258 * restart the current transfer due to an error
260 * @param msg Message to print for user
262 static void restart(const char *msg
)
264 printf("\n%s; starting again\n", msg
);
265 #ifdef CONFIG_MCAST_TFTP
272 * Check if the block number has wrapped, and update progress
274 * TODO: The egregious use of global variables in this file should be tidied.
276 static void update_block_number(void)
279 * RFC1350 specifies that the first data packet will
280 * have sequence number 1. If we receive a sequence
281 * number of 0 this means that there was a wrap
282 * around of the (16 bit) counter.
284 if (TftpBlock
== 0 && TftpLastBlock
!= 0) {
286 TftpBlockWrapOffset
+= TftpBlkSize
* TFTP_SEQUENCE_SIZE
;
287 TftpTimeoutCount
= 0; /* we've done well, reset thhe timeout */
293 /* The TFTP get or put is complete */
294 static void tftp_complete(void)
296 #ifdef CONFIG_TFTP_TSIZE
297 /* Print hash marks for the last packet received */
298 while (TftpTsize
&& TftpNumchars
< 49) {
303 print_size(TftpTsize
, "");
305 time_start
= get_timer(time_start
);
306 if (time_start
> 0) {
307 puts("\n\t "); /* Line up with "Loading: " */
308 print_size(NetBootFileXferSize
/
309 time_start
* 1000, "/s");
312 net_set_state(NETLOOP_SUCCESS
);
323 #ifdef CONFIG_MCAST_TFTP
324 /* Multicast TFTP.. non-MasterClients do not ACK data. */
326 && (TftpState
== STATE_DATA
)
327 && (MasterClient
== 0))
331 * We will always be sending some sort of packet, so
332 * cobble together the packet headers now.
334 pkt
= NetTxPacket
+ NetEthHdrSize() + IP_UDP_HDR_SIZE
;
341 #ifdef CONFIG_CMD_TFTPPUT
342 *s
++ = htons(TftpState
== STATE_SEND_RRQ
? TFTP_RRQ
:
345 *s
++ = htons(TFTP_RRQ
);
348 strcpy((char *)pkt
, tftp_filename
);
349 pkt
+= strlen(tftp_filename
) + 1;
350 strcpy((char *)pkt
, "octet");
351 pkt
+= 5 /*strlen("octet")*/ + 1;
352 strcpy((char *)pkt
, "timeout");
353 pkt
+= 7 /*strlen("timeout")*/ + 1;
354 sprintf((char *)pkt
, "%lu", TftpTimeoutMSecs
/ 1000);
355 debug("send option \"timeout %s\"\n", (char *)pkt
);
356 pkt
+= strlen((char *)pkt
) + 1;
357 #ifdef CONFIG_TFTP_TSIZE
358 pkt
+= sprintf((char *)pkt
, "tsize%c%lu%c",
359 0, NetBootFileXferSize
, 0);
361 /* try for more effic. blk size */
362 pkt
+= sprintf((char *)pkt
, "blksize%c%d%c",
363 0, TftpBlkSizeOption
, 0);
364 #ifdef CONFIG_MCAST_TFTP
365 /* Check all preconditions before even trying the option */
366 if (!ProhibitMcast
) {
367 Bitmap
= malloc(Mapsize
);
368 if (Bitmap
&& eth_get_dev()->mcast
) {
371 pkt
+= sprintf((char *)pkt
, "multicast%c%c",
375 #endif /* CONFIG_MCAST_TFTP */
380 #ifdef CONFIG_MCAST_TFTP
381 /* My turn! Start at where I need blocks I missed.*/
383 TftpBlock
= ext2_find_next_zero_bit(Bitmap
,
392 s
[0] = htons(TFTP_ACK
);
393 s
[1] = htons(TftpBlock
);
394 pkt
= (uchar
*)(s
+ 2);
395 #ifdef CONFIG_CMD_TFTPPUT
397 int toload
= TftpBlkSize
;
398 int loaded
= load_block(TftpBlock
, pkt
, toload
);
400 s
[0] = htons(TFTP_DATA
);
402 TftpFinalBlock
= (loaded
< toload
);
408 case STATE_TOO_LARGE
:
411 *s
++ = htons(TFTP_ERROR
);
415 strcpy((char *)pkt
, "File too large");
416 pkt
+= 14 /*strlen("File too large")*/ + 1;
420 case STATE_BAD_MAGIC
:
423 *s
++ = htons(TFTP_ERROR
);
426 strcpy((char *)pkt
, "File has bad magic");
427 pkt
+= 18 /*strlen("File has bad magic")*/ + 1;
432 NetSendUDPPacket(NetServerEther
, TftpRemoteIP
, TftpRemotePort
,
436 #ifdef CONFIG_CMD_TFTPPUT
437 static void icmp_handler(unsigned type
, unsigned code
, unsigned dest
,
438 IPaddr_t sip
, unsigned src
, uchar
*pkt
, unsigned len
)
440 if (type
== ICMP_NOT_REACH
&& code
== ICMP_NOT_REACH_PORT
) {
441 /* Oh dear the other end has gone away */
442 restart("TFTP server died");
448 TftpHandler(uchar
*pkt
, unsigned dest
, IPaddr_t sip
, unsigned src
,
455 if (dest
!= TftpOurPort
) {
456 #ifdef CONFIG_MCAST_TFTP
458 && (!Mcast_port
|| (dest
!= Mcast_port
)))
462 if (TftpState
!= STATE_SEND_RRQ
&& src
!= TftpRemotePort
&&
463 TftpState
!= STATE_RECV_WRQ
&& TftpState
!= STATE_SEND_WRQ
)
469 /* warning: don't use increment (++) in ntohs() macros!! */
473 switch (ntohs(proto
)) {
479 #ifdef CONFIG_CMD_TFTPPUT
481 if (TftpFinalBlock
) {
485 * Move to the next block. We want our block
486 * count to wrap just like the other end!
488 int block
= ntohs(*s
);
489 int ack_ok
= (TftpBlock
== block
);
491 TftpBlock
= (unsigned short)(block
+ 1);
492 update_block_number();
494 TftpSend(); /* Send next data block */
503 #ifdef CONFIG_CMD_TFTPSRV
507 TftpRemotePort
= src
;
508 TftpOurPort
= 1024 + (get_timer(0) % 3072);
510 TftpSend(); /* Send ACK(0) */
515 debug("Got OACK: %s %s\n",
517 pkt
+ strlen((char *)pkt
) + 1);
518 TftpState
= STATE_OACK
;
519 TftpRemotePort
= src
;
521 * Check for 'blksize' option.
522 * Careful: "i" is signed, "len" is unsigned, thus
523 * something like "len-8" may give a *huge* number
525 for (i
= 0; i
+8 < len
; i
++) {
526 if (strcmp((char *)pkt
+i
, "blksize") == 0) {
527 TftpBlkSize
= (unsigned short)
528 simple_strtoul((char *)pkt
+i
+8, NULL
,
530 debug("Blocksize ack: %s, %d\n",
531 (char *)pkt
+i
+8, TftpBlkSize
);
533 #ifdef CONFIG_TFTP_TSIZE
534 if (strcmp((char *)pkt
+i
, "tsize") == 0) {
535 TftpTsize
= simple_strtoul((char *)pkt
+i
+6,
537 debug("size = %s, %d\n",
538 (char *)pkt
+i
+6, TftpTsize
);
542 #ifdef CONFIG_MCAST_TFTP
543 parse_multicast_oack((char *)pkt
, len
-1);
544 if ((Multicast
) && (!MasterClient
))
545 TftpState
= STATE_DATA
; /* passive.. */
548 #ifdef CONFIG_CMD_TFTPPUT
550 /* Get ready to send the first block */
551 TftpState
= STATE_DATA
;
555 TftpSend(); /* Send ACK or first data block */
561 TftpBlock
= ntohs(*(__be16
*)pkt
);
563 update_block_number();
565 if (TftpState
== STATE_SEND_RRQ
)
566 debug("Server did not acknowledge timeout option!\n");
568 if (TftpState
== STATE_SEND_RRQ
|| TftpState
== STATE_OACK
||
569 TftpState
== STATE_RECV_WRQ
) {
570 /* first block received */
571 TftpState
= STATE_DATA
;
572 TftpRemotePort
= src
;
575 #ifdef CONFIG_MCAST_TFTP
576 if (Multicast
) { /* start!=1 common if mcast */
577 TftpLastBlock
= TftpBlock
- 1;
580 if (TftpBlock
!= 1) { /* Assertion */
581 printf("\nTFTP error: "
582 "First block is not block 1 (%ld)\n"
583 "Starting again\n\n",
590 if (TftpBlock
== TftpLastBlock
) {
592 * Same block again; ignore it.
597 TftpLastBlock
= TftpBlock
;
598 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
599 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
601 store_block(TftpBlock
- 1, pkt
+ 2, len
);
604 * Acknowledge the block just received, which will prompt
605 * the remote for the next one.
607 #ifdef CONFIG_MCAST_TFTP
608 /* if I am the MasterClient, actively calculate what my next
609 * needed block is; else I'm passive; not ACKING
612 if (len
< TftpBlkSize
) {
613 TftpEndingBlock
= TftpBlock
;
614 } else if (MasterClient
) {
615 TftpBlock
= PrevBitmapHole
=
616 ext2_find_next_zero_bit(
620 if (TftpBlock
> ((Mapsize
*8) - 1)) {
621 printf("tftpfile too big\n");
622 /* try to double it and retry */
628 TftpLastBlock
= TftpBlock
;
634 #ifdef CONFIG_MCAST_TFTP
636 if (MasterClient
&& (TftpBlock
>= TftpEndingBlock
)) {
637 puts("\nMulticast tftp done\n");
639 net_set_state(NETLOOP_SUCCESS
);
643 if (len
< TftpBlkSize
)
648 printf("\nTFTP error: '%s' (%d)\n",
649 pkt
+ 2, ntohs(*(__be16
*)pkt
));
651 switch (ntohs(*(__be16
*)pkt
)) {
652 case TFTP_ERR_FILE_NOT_FOUND
:
653 case TFTP_ERR_ACCESS_DENIED
:
654 puts("Not retrying...\n");
656 net_set_state(NETLOOP_FAIL
);
658 case TFTP_ERR_UNDEFINED
:
659 case TFTP_ERR_DISK_FULL
:
660 case TFTP_ERR_UNEXPECTED_OPCODE
:
661 case TFTP_ERR_UNKNOWN_TRANSFER_ID
:
662 case TFTP_ERR_FILE_ALREADY_EXISTS
:
664 puts("Starting again\n\n");
665 #ifdef CONFIG_MCAST_TFTP
679 if (++TftpTimeoutCount
> TftpTimeoutCountMax
) {
680 restart("Retry count exceeded");
683 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
684 if (TftpState
!= STATE_RECV_WRQ
)
690 void TftpStart(enum proto_t protocol
)
692 char *ep
; /* Environment pointer */
695 * Allow the user to choose TFTP blocksize and timeout.
696 * TFTP protocol has a minimal timeout of 1 second.
698 ep
= getenv("tftpblocksize");
700 TftpBlkSizeOption
= simple_strtol(ep
, NULL
, 10);
702 ep
= getenv("tftptimeout");
704 TftpTimeoutMSecs
= simple_strtol(ep
, NULL
, 10);
706 if (TftpTimeoutMSecs
< 1000) {
707 printf("TFTP timeout (%ld ms) too low, "
708 "set minimum = 1000 ms\n",
710 TftpTimeoutMSecs
= 1000;
713 debug("TFTP blocksize = %i, timeout = %ld ms\n",
714 TftpBlkSizeOption
, TftpTimeoutMSecs
);
716 TftpRemoteIP
= NetServerIP
;
717 if (BootFile
[0] == '\0') {
718 sprintf(default_filename
, "%02X%02X%02X%02X.img",
720 (NetOurIP
>> 8) & 0xFF,
721 (NetOurIP
>> 16) & 0xFF,
722 (NetOurIP
>> 24) & 0xFF);
724 strncpy(tftp_filename
, default_filename
, MAX_LEN
);
725 tftp_filename
[MAX_LEN
-1] = 0;
727 printf("*** Warning: no boot file name; using '%s'\n",
730 char *p
= strchr(BootFile
, ':');
733 strncpy(tftp_filename
, BootFile
, MAX_LEN
);
734 tftp_filename
[MAX_LEN
-1] = 0;
736 TftpRemoteIP
= string_to_ip(BootFile
);
737 strncpy(tftp_filename
, p
+ 1, MAX_LEN
);
738 tftp_filename
[MAX_LEN
-1] = 0;
742 printf("Using %s device\n", eth_get_name());
743 printf("TFTP %s server %pI4; our IP address is %pI4",
744 #ifdef CONFIG_CMD_TFTPPUT
745 protocol
== TFTPPUT
? "to" : "from",
749 &TftpRemoteIP
, &NetOurIP
);
751 /* Check if we need to send across this subnet */
752 if (NetOurGatewayIP
&& NetOurSubnetMask
) {
753 IPaddr_t OurNet
= NetOurIP
& NetOurSubnetMask
;
754 IPaddr_t RemoteNet
= TftpRemoteIP
& NetOurSubnetMask
;
756 if (OurNet
!= RemoteNet
)
757 printf("; sending through gateway %pI4",
762 printf("Filename '%s'.", tftp_filename
);
764 if (NetBootFileSize
) {
765 printf(" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
766 print_size(NetBootFileSize
<<9, "");
770 #ifdef CONFIG_CMD_TFTPPUT
771 TftpWriting
= (protocol
== TFTPPUT
);
773 printf("Save address: 0x%lx\n", save_addr
);
774 printf("Save size: 0x%lx\n", save_size
);
775 NetBootFileXferSize
= save_size
;
777 TftpState
= STATE_SEND_WRQ
;
782 printf("Load address: 0x%lx\n", load_addr
);
783 puts("Loading: *\b");
784 TftpState
= STATE_SEND_RRQ
;
787 time_start
= get_timer(0);
788 TftpTimeoutCountMax
= TftpRRQTimeoutCountMax
;
790 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
791 net_set_udp_handler(TftpHandler
);
792 #ifdef CONFIG_CMD_TFTPPUT
793 net_set_icmp_handler(icmp_handler
);
795 TftpRemotePort
= WELL_KNOWN_PORT
;
796 TftpTimeoutCount
= 0;
797 /* Use a pseudo-random port unless a specific port is set */
798 TftpOurPort
= 1024 + (get_timer(0) % 3072);
800 #ifdef CONFIG_TFTP_PORT
801 ep
= getenv("tftpdstp");
803 TftpRemotePort
= simple_strtol(ep
, NULL
, 10);
804 ep
= getenv("tftpsrcp");
806 TftpOurPort
= simple_strtol(ep
, NULL
, 10);
810 /* zero out server ether in case the server ip has changed */
811 memset(NetServerEther
, 0, 6);
812 /* Revert TftpBlkSize to dflt */
813 TftpBlkSize
= TFTP_BLOCK_SIZE
;
814 #ifdef CONFIG_MCAST_TFTP
817 #ifdef CONFIG_TFTP_TSIZE
825 #ifdef CONFIG_CMD_TFTPSRV
827 TftpStartServer(void)
829 tftp_filename
[0] = 0;
831 printf("Using %s device\n", eth_get_name());
832 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP
);
833 printf("Load address: 0x%lx\n", load_addr
);
835 puts("Loading: *\b");
837 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
838 TftpTimeoutCount
= 0;
839 TftpTimeoutMSecs
= TIMEOUT
;
840 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
842 /* Revert TftpBlkSize to dflt */
843 TftpBlkSize
= TFTP_BLOCK_SIZE
;
845 TftpOurPort
= WELL_KNOWN_PORT
;
847 #ifdef CONFIG_TFTP_TSIZE
852 TftpState
= STATE_RECV_WRQ
;
853 net_set_udp_handler(TftpHandler
);
855 /* zero out server ether in case the server ip has changed */
856 memset(NetServerEther
, 0, 6);
858 #endif /* CONFIG_CMD_TFTPSRV */
860 #ifdef CONFIG_MCAST_TFTP
861 /* Credits: atftp project.
864 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
866 * +-------+-----------+---+-------~~-------+---+
867 * | opc | multicast | 0 | addr, port, mc | 0 |
868 * +-------+-----------+---+-------~~-------+---+
869 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
870 * I am the new master-client so must send ACKs to DataBlocks. If I am not
871 * master-client, I'm a passive client, gathering what DataBlocks I may and
872 * making note of which ones I got in my bitmask.
873 * In theory, I never go from master->passive..
874 * .. this comes in with pkt already pointing just past opc
876 static void parse_multicast_oack(char *pkt
, int len
)
880 char *mc_adr
, *port
, *mc
;
882 mc_adr
= port
= mc
= NULL
;
883 /* march along looking for 'multicast\0', which has to start at least
884 * 14 bytes back from the end.
886 for (i
= 0; i
< len
-14; i
++)
887 if (strcmp(pkt
+i
, "multicast") == 0)
889 if (i
>= (len
-14)) /* non-Multicast OACK, ign. */
892 i
+= 10; /* strlen multicast */
894 for (; i
< len
; i
++) {
895 if (*(pkt
+i
) == ',') {
905 if (!port
|| !mc_adr
|| !mc
)
907 if (Multicast
&& MasterClient
) {
908 printf("I got a OACK as master Client, WRONG!\n");
911 /* ..I now accept packets destined for this MCAST addr, port */
914 printf("Internal failure! no mcast.\n");
920 /* I malloc instead of pre-declare; so that if the file ends
921 * up being too big for this bitmap I can retry
923 Bitmap
= malloc(Mapsize
);
925 printf("No Bitmap, no multicast. Sorry.\n");
929 memset(Bitmap
, 0, Mapsize
);
933 addr
= string_to_ip(mc_adr
);
934 if (Mcast_addr
!= addr
) {
936 eth_mcast_join(Mcast_addr
, 0);
938 if (eth_mcast_join(Mcast_addr
, 1)) {
939 printf("Fail to set mcast, revert to TFTP\n");
945 MasterClient
= (unsigned char)simple_strtoul((char *)mc
, NULL
, 10);
946 Mcast_port
= (unsigned short)simple_strtoul(port
, NULL
, 10);
947 printf("Multicast: %s:%d [%d]\n", mc_adr
, Mcast_port
, MasterClient
);
951 #endif /* Multicast TFTP */