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 /* Well known TFTP port # */
16 #define WELL_KNOWN_PORT 69
17 /* Millisecs to timeout for lost pkt */
18 #define TIMEOUT 5000UL
19 #ifndef CONFIG_NET_RETRY_COUNT
20 /* # of timeouts before giving up */
21 # define TIMEOUT_COUNT 10
23 # define TIMEOUT_COUNT (CONFIG_NET_RETRY_COUNT * 2)
25 /* Number of "loading" hashes per line (for checking the image size) */
26 #define HASHES_PER_LINE 65
38 static ulong TftpTimeoutMSecs
= TIMEOUT
;
39 static int TftpTimeoutCountMax
= TIMEOUT_COUNT
;
42 * These globals govern the timeout behavior when attempting a connection to a
43 * TFTP server. TftpRRQTimeoutMSecs specifies the number of milliseconds to
44 * wait for the server to respond to initial connection. Second global,
45 * TftpRRQTimeoutCountMax, gives the number of such connection retries.
46 * TftpRRQTimeoutCountMax must be non-negative and TftpRRQTimeoutMSecs must be
47 * positive. The globals are meant to be set (and restored) by code needing
48 * non-standard timeout behavior when initiating a TFTP transfer.
50 ulong TftpRRQTimeoutMSecs
= TIMEOUT
;
51 int TftpRRQTimeoutCountMax
= TIMEOUT_COUNT
;
54 TFTP_ERR_UNDEFINED
= 0,
55 TFTP_ERR_FILE_NOT_FOUND
= 1,
56 TFTP_ERR_ACCESS_DENIED
= 2,
57 TFTP_ERR_DISK_FULL
= 3,
58 TFTP_ERR_UNEXPECTED_OPCODE
= 4,
59 TFTP_ERR_UNKNOWN_TRANSFER_ID
= 5,
60 TFTP_ERR_FILE_ALREADY_EXISTS
= 6,
63 static IPaddr_t TftpRemoteIP
;
64 /* The UDP port at their end */
65 static int TftpRemotePort
;
66 /* The UDP port at our end */
67 static int TftpOurPort
;
68 static int TftpTimeoutCount
;
69 /* packet sequence number */
70 static ulong TftpBlock
;
71 /* last packet sequence number received */
72 static ulong TftpLastBlock
;
73 /* count of sequence number wraparounds */
74 static ulong TftpBlockWrap
;
75 /* memory offset due to wrapping */
76 static ulong TftpBlockWrapOffset
;
78 #ifdef CONFIG_TFTP_TSIZE
79 /* The file size reported by the server */
81 /* The number of hashes we printed */
82 static short TftpNumchars
;
84 #ifdef CONFIG_CMD_TFTPPUT
85 static int TftpWriting
; /* 1 if writing, else 0 */
86 static int TftpFinalBlock
; /* 1 if we have sent the last block */
91 #define STATE_SEND_RRQ 1
93 #define STATE_TOO_LARGE 3
94 #define STATE_BAD_MAGIC 4
96 #define STATE_RECV_WRQ 6
97 #define STATE_SEND_WRQ 7
99 /* default TFTP block size */
100 #define TFTP_BLOCK_SIZE 512
101 /* sequence number is 16 bit */
102 #define TFTP_SEQUENCE_SIZE ((ulong)(1<<16))
104 #define DEFAULT_NAME_LEN (8 + 4 + 1)
105 static char default_filename
[DEFAULT_NAME_LEN
];
107 #ifndef CONFIG_TFTP_FILE_NAME_MAX_LEN
110 #define MAX_LEN CONFIG_TFTP_FILE_NAME_MAX_LEN
113 static char tftp_filename
[MAX_LEN
];
115 #ifdef CONFIG_SYS_DIRECT_FLASH_TFTP
116 extern flash_info_t flash_info
[];
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 extern IPaddr_t Mcast_addr
;
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 */
160 static __inline__
void
161 store_block(unsigned 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 NetState
= NETLOOP_FAIL
;
187 #endif /* CONFIG_SYS_DIRECT_FLASH_TFTP */
189 (void)memcpy((void *)(load_addr
+ offset
), src
, len
);
191 #ifdef CONFIG_MCAST_TFTP
193 ext2_set_bit(block
, Bitmap
);
196 if (NetBootFileXferSize
< newsize
)
197 NetBootFileXferSize
= newsize
;
200 /* Clear our state ready for a new transfer */
201 static void new_transfer(void)
205 TftpBlockWrapOffset
= 0;
206 #ifdef CONFIG_CMD_TFTPPUT
211 #ifdef CONFIG_CMD_TFTPPUT
213 * Load the next block from memory to be sent over tftp.
215 * @param block Block number to send
216 * @param dst Destination buffer for data
217 * @param len Number of bytes in block (this one and every other)
218 * @return number of bytes loaded
220 static int load_block(unsigned block
, uchar
*dst
, unsigned len
)
222 /* We may want to get the final block from the previous set */
223 ulong offset
= ((int)block
- 1) * len
+ TftpBlockWrapOffset
;
226 tosend
= min(NetBootFileXferSize
- offset
, tosend
);
227 (void)memcpy(dst
, (void *)(save_addr
+ offset
), tosend
);
228 debug("%s: block=%d, offset=%ld, len=%d, tosend=%ld\n", __func__
,
229 block
, offset
, len
, tosend
);
234 static void TftpSend(void);
235 static void TftpTimeout(void);
237 /**********************************************************************/
239 static void show_block_marker(void)
241 #ifdef CONFIG_TFTP_TSIZE
243 ulong pos
= TftpBlock
* TftpBlkSize
+ TftpBlockWrapOffset
;
245 while (TftpNumchars
< pos
* 50 / TftpTsize
) {
252 if (((TftpBlock
- 1) % 10) == 0)
254 else if ((TftpBlock
% (10 * HASHES_PER_LINE
)) == 0)
260 * restart the current transfer due to an error
262 * @param msg Message to print for user
264 static void restart(const char *msg
)
266 printf("\n%s; starting again\n", msg
);
267 #ifdef CONFIG_MCAST_TFTP
274 * Check if the block number has wrapped, and update progress
276 * TODO: The egregious use of global variables in this file should be tidied.
278 static void update_block_number(void)
281 * RFC1350 specifies that the first data packet will
282 * have sequence number 1. If we receive a sequence
283 * number of 0 this means that there was a wrap
284 * around of the (16 bit) counter.
286 if (TftpBlock
== 0) {
288 TftpBlockWrapOffset
+= TftpBlkSize
* TFTP_SEQUENCE_SIZE
;
289 TftpTimeoutCount
= 0; /* we've done well, reset thhe timeout */
295 /* The TFTP get or put is complete */
296 static void tftp_complete(void)
298 #ifdef CONFIG_TFTP_TSIZE
299 /* Print hash marks for the last packet received */
300 while (TftpTsize
&& TftpNumchars
< 49) {
306 NetState
= NETLOOP_SUCCESS
;
317 #ifdef CONFIG_MCAST_TFTP
318 /* Multicast TFTP.. non-MasterClients do not ACK data. */
320 && (TftpState
== STATE_DATA
)
321 && (MasterClient
== 0))
325 * We will always be sending some sort of packet, so
326 * cobble together the packet headers now.
328 pkt
= (uchar
*)(NetTxPacket
+ NetEthHdrSize() + IP_HDR_SIZE
);
335 *s
++ = htons(TftpState
== STATE_SEND_RRQ
? TFTP_RRQ
:
338 strcpy((char *)pkt
, tftp_filename
);
339 pkt
+= strlen(tftp_filename
) + 1;
340 strcpy((char *)pkt
, "octet");
341 pkt
+= 5 /*strlen("octet")*/ + 1;
342 strcpy((char *)pkt
, "timeout");
343 pkt
+= 7 /*strlen("timeout")*/ + 1;
344 sprintf((char *)pkt
, "%lu", TftpTimeoutMSecs
/ 1000);
345 debug("send option \"timeout %s\"\n", (char *)pkt
);
346 pkt
+= strlen((char *)pkt
) + 1;
347 #ifdef CONFIG_TFTP_TSIZE
348 pkt
+= sprintf((char *)pkt
, "tsize%c%lu%c",
349 0, NetBootFileXferSize
, 0);
351 /* try for more effic. blk size */
352 pkt
+= sprintf((char *)pkt
, "blksize%c%d%c",
353 0, TftpBlkSizeOption
, 0);
354 #ifdef CONFIG_MCAST_TFTP
355 /* Check all preconditions before even trying the option */
357 && (Bitmap
= malloc(Mapsize
))
358 && eth_get_dev()->mcast
) {
361 pkt
+= sprintf((char *)pkt
, "multicast%c%c", 0, 0);
363 #endif /* CONFIG_MCAST_TFTP */
368 #ifdef CONFIG_MCAST_TFTP
369 /* My turn! Start at where I need blocks I missed.*/
371 TftpBlock
= ext2_find_next_zero_bit(Bitmap
,
380 s
[0] = htons(TFTP_ACK
);
381 s
[1] = htons(TftpBlock
);
382 pkt
= (uchar
*)(s
+ 2);
383 #ifdef CONFIG_CMD_TFTPPUT
385 int toload
= TftpBlkSize
;
386 int loaded
= load_block(TftpBlock
, pkt
, toload
);
388 s
[0] = htons(TFTP_DATA
);
390 TftpFinalBlock
= (loaded
< toload
);
396 case STATE_TOO_LARGE
:
399 *s
++ = htons(TFTP_ERROR
);
403 strcpy((char *)pkt
, "File too large");
404 pkt
+= 14 /*strlen("File too large")*/ + 1;
408 case STATE_BAD_MAGIC
:
411 *s
++ = htons(TFTP_ERROR
);
414 strcpy((char *)pkt
, "File has bad magic");
415 pkt
+= 18 /*strlen("File has bad magic")*/ + 1;
420 NetSendUDPPacket(NetServerEther
, TftpRemoteIP
, TftpRemotePort
,
424 #ifdef CONFIG_CMD_TFTPPUT
425 static void icmp_handler(unsigned type
, unsigned code
, unsigned dest
,
426 IPaddr_t sip
, unsigned src
, uchar
*pkt
, unsigned len
)
428 if (type
== ICMP_NOT_REACH
&& code
== ICMP_NOT_REACH_PORT
) {
429 /* Oh dear the other end has gone away */
430 restart("TFTP server died");
436 TftpHandler(uchar
*pkt
, unsigned dest
, IPaddr_t sip
, unsigned src
,
443 if (dest
!= TftpOurPort
) {
444 #ifdef CONFIG_MCAST_TFTP
446 && (!Mcast_port
|| (dest
!= Mcast_port
)))
450 if (TftpState
!= STATE_SEND_RRQ
&& src
!= TftpRemotePort
&&
451 TftpState
!= STATE_RECV_WRQ
&& TftpState
!= STATE_SEND_WRQ
)
457 /* warning: don't use increment (++) in ntohs() macros!! */
461 switch (ntohs(proto
)) {
467 #ifdef CONFIG_CMD_TFTPPUT
469 if (TftpFinalBlock
) {
473 * Move to the next block. We want our block
474 * count to wrap just like the other end!
476 int block
= ntohs(*s
);
477 int ack_ok
= (TftpBlock
== block
);
479 TftpBlock
= (unsigned short)(block
+ 1);
480 update_block_number();
482 TftpSend(); /* Send next data block */
491 #ifdef CONFIG_CMD_TFTPSRV
495 TftpRemotePort
= src
;
496 TftpOurPort
= 1024 + (get_timer(0) % 3072);
498 TftpSend(); /* Send ACK(0) */
503 debug("Got OACK: %s %s\n",
505 pkt
+ strlen((char *)pkt
) + 1);
506 TftpState
= STATE_OACK
;
507 TftpRemotePort
= src
;
509 * Check for 'blksize' option.
510 * Careful: "i" is signed, "len" is unsigned, thus
511 * something like "len-8" may give a *huge* number
513 for (i
= 0; i
+8 < len
; i
++) {
514 if (strcmp((char *)pkt
+i
, "blksize") == 0) {
515 TftpBlkSize
= (unsigned short)
516 simple_strtoul((char *)pkt
+i
+8, NULL
,
518 debug("Blocksize ack: %s, %d\n",
519 (char *)pkt
+i
+8, TftpBlkSize
);
521 #ifdef CONFIG_TFTP_TSIZE
522 if (strcmp((char *)pkt
+i
, "tsize") == 0) {
523 TftpTsize
= simple_strtoul((char *)pkt
+i
+6,
525 debug("size = %s, %d\n",
526 (char *)pkt
+i
+6, TftpTsize
);
530 #ifdef CONFIG_MCAST_TFTP
531 parse_multicast_oack((char *)pkt
, len
-1);
532 if ((Multicast
) && (!MasterClient
))
533 TftpState
= STATE_DATA
; /* passive.. */
536 #ifdef CONFIG_CMD_TFTPPUT
538 /* Get ready to send the first block */
539 TftpState
= STATE_DATA
;
543 TftpSend(); /* Send ACK or first data block */
549 TftpBlock
= ntohs(*(ushort
*)pkt
);
551 update_block_number();
553 if (TftpState
== STATE_SEND_RRQ
)
554 debug("Server did not acknowledge timeout option!\n");
556 if (TftpState
== STATE_SEND_RRQ
|| TftpState
== STATE_OACK
||
557 TftpState
== STATE_RECV_WRQ
) {
558 /* first block received */
559 TftpState
= STATE_DATA
;
560 TftpRemotePort
= src
;
563 #ifdef CONFIG_MCAST_TFTP
564 if (Multicast
) { /* start!=1 common if mcast */
565 TftpLastBlock
= TftpBlock
- 1;
568 if (TftpBlock
!= 1) { /* Assertion */
569 printf("\nTFTP error: "
570 "First block is not block 1 (%ld)\n"
571 "Starting again\n\n",
578 if (TftpBlock
== TftpLastBlock
) {
580 * Same block again; ignore it.
585 TftpLastBlock
= TftpBlock
;
586 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
587 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
589 store_block(TftpBlock
- 1, pkt
+ 2, len
);
592 * Acknowledge the block just received, which will prompt
593 * the remote for the next one.
595 #ifdef CONFIG_MCAST_TFTP
596 /* if I am the MasterClient, actively calculate what my next
597 * needed block is; else I'm passive; not ACKING
600 if (len
< TftpBlkSize
) {
601 TftpEndingBlock
= TftpBlock
;
602 } else if (MasterClient
) {
603 TftpBlock
= PrevBitmapHole
=
604 ext2_find_next_zero_bit(
608 if (TftpBlock
> ((Mapsize
*8) - 1)) {
609 printf("tftpfile too big\n");
610 /* try to double it and retry */
616 TftpLastBlock
= TftpBlock
;
622 #ifdef CONFIG_MCAST_TFTP
624 if (MasterClient
&& (TftpBlock
>= TftpEndingBlock
)) {
625 puts("\nMulticast tftp done\n");
627 NetState
= NETLOOP_SUCCESS
;
632 if (len
< TftpBlkSize
)
637 printf("\nTFTP error: '%s' (%d)\n",
638 pkt
+ 2, ntohs(*(ushort
*)pkt
));
640 switch (ntohs(*(ushort
*)pkt
)) {
641 case TFTP_ERR_FILE_NOT_FOUND
:
642 case TFTP_ERR_ACCESS_DENIED
:
643 puts("Not retrying...\n");
645 NetState
= NETLOOP_FAIL
;
647 case TFTP_ERR_UNDEFINED
:
648 case TFTP_ERR_DISK_FULL
:
649 case TFTP_ERR_UNEXPECTED_OPCODE
:
650 case TFTP_ERR_UNKNOWN_TRANSFER_ID
:
651 case TFTP_ERR_FILE_ALREADY_EXISTS
:
653 puts("Starting again\n\n");
654 #ifdef CONFIG_MCAST_TFTP
668 if (++TftpTimeoutCount
> TftpTimeoutCountMax
) {
669 restart("Retry count exceeded");
672 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
673 if (TftpState
!= STATE_RECV_WRQ
)
679 void TftpStart(enum proto_t protocol
)
681 char *ep
; /* Environment pointer */
684 * Allow the user to choose TFTP blocksize and timeout.
685 * TFTP protocol has a minimal timeout of 1 second.
687 ep
= getenv("tftpblocksize");
689 TftpBlkSizeOption
= simple_strtol(ep
, NULL
, 10);
691 ep
= getenv("tftptimeout");
693 TftpTimeoutMSecs
= simple_strtol(ep
, NULL
, 10);
695 if (TftpTimeoutMSecs
< 1000) {
696 printf("TFTP timeout (%ld ms) too low, "
697 "set minimum = 1000 ms\n",
699 TftpTimeoutMSecs
= 1000;
702 debug("TFTP blocksize = %i, timeout = %ld ms\n",
703 TftpBlkSizeOption
, TftpTimeoutMSecs
);
705 TftpRemoteIP
= NetServerIP
;
706 if (BootFile
[0] == '\0') {
707 sprintf(default_filename
, "%02lX%02lX%02lX%02lX.img",
709 (NetOurIP
>> 8) & 0xFF,
710 (NetOurIP
>> 16) & 0xFF,
711 (NetOurIP
>> 24) & 0xFF);
713 strncpy(tftp_filename
, default_filename
, MAX_LEN
);
714 tftp_filename
[MAX_LEN
-1] = 0;
716 printf("*** Warning: no boot file name; using '%s'\n",
719 char *p
= strchr(BootFile
, ':');
722 strncpy(tftp_filename
, BootFile
, MAX_LEN
);
723 tftp_filename
[MAX_LEN
-1] = 0;
725 TftpRemoteIP
= string_to_ip(BootFile
);
726 strncpy(tftp_filename
, p
+ 1, MAX_LEN
);
727 tftp_filename
[MAX_LEN
-1] = 0;
731 printf("Using %s device\n", eth_get_name());
732 printf("TFTP %s server %pI4; our IP address is %pI4",
733 protocol
== TFTPPUT
? "to" : "from", &TftpRemoteIP
, &NetOurIP
);
735 /* Check if we need to send across this subnet */
736 if (NetOurGatewayIP
&& NetOurSubnetMask
) {
737 IPaddr_t OurNet
= NetOurIP
& NetOurSubnetMask
;
738 IPaddr_t RemoteNet
= TftpRemoteIP
& NetOurSubnetMask
;
740 if (OurNet
!= RemoteNet
)
741 printf("; sending through gateway %pI4",
746 printf("Filename '%s'.", tftp_filename
);
748 if (NetBootFileSize
) {
749 printf(" Size is 0x%x Bytes = ", NetBootFileSize
<<9);
750 print_size(NetBootFileSize
<<9, "");
754 #ifdef CONFIG_CMD_TFTPPUT
755 TftpWriting
= (protocol
== TFTPPUT
);
757 printf("Save address: 0x%lx\n", save_addr
);
758 printf("Save size: 0x%lx\n", save_size
);
759 NetBootFileXferSize
= save_size
;
761 TftpState
= STATE_SEND_WRQ
;
766 printf("Load address: 0x%lx\n", load_addr
);
767 puts("Loading: *\b");
768 TftpState
= STATE_SEND_RRQ
;
771 TftpTimeoutCountMax
= TftpRRQTimeoutCountMax
;
773 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
774 NetSetHandler(TftpHandler
);
775 #ifdef CONFIG_CMD_TFTPPUT
776 net_set_icmp_handler(icmp_handler
);
778 TftpRemotePort
= WELL_KNOWN_PORT
;
779 TftpTimeoutCount
= 0;
780 /* Use a pseudo-random port unless a specific port is set */
781 TftpOurPort
= 1024 + (get_timer(0) % 3072);
783 #ifdef CONFIG_TFTP_PORT
784 ep
= getenv("tftpdstp");
786 TftpRemotePort
= simple_strtol(ep
, NULL
, 10);
787 ep
= getenv("tftpsrcp");
789 TftpOurPort
= simple_strtol(ep
, NULL
, 10);
793 /* zero out server ether in case the server ip has changed */
794 memset(NetServerEther
, 0, 6);
795 /* Revert TftpBlkSize to dflt */
796 TftpBlkSize
= TFTP_BLOCK_SIZE
;
797 #ifdef CONFIG_MCAST_TFTP
800 #ifdef CONFIG_TFTP_TSIZE
808 #ifdef CONFIG_CMD_TFTPSRV
810 TftpStartServer(void)
812 tftp_filename
[0] = 0;
814 printf("Using %s device\n", eth_get_name());
815 printf("Listening for TFTP transfer on %pI4\n", &NetOurIP
);
816 printf("Load address: 0x%lx\n", load_addr
);
818 puts("Loading: *\b");
820 TftpTimeoutCountMax
= TIMEOUT_COUNT
;
821 TftpTimeoutCount
= 0;
822 TftpTimeoutMSecs
= TIMEOUT
;
823 NetSetTimeout(TftpTimeoutMSecs
, TftpTimeout
);
825 /* Revert TftpBlkSize to dflt */
826 TftpBlkSize
= TFTP_BLOCK_SIZE
;
828 TftpOurPort
= WELL_KNOWN_PORT
;
830 #ifdef CONFIG_TFTP_TSIZE
835 TftpState
= STATE_RECV_WRQ
;
836 NetSetHandler(TftpHandler
);
838 #endif /* CONFIG_CMD_TFTPSRV */
840 #ifdef CONFIG_MCAST_TFTP
841 /* Credits: atftp project.
844 /* pick up BcastAddr, Port, and whether I am [now] the master-client. *
846 * +-------+-----------+---+-------~~-------+---+
847 * | opc | multicast | 0 | addr, port, mc | 0 |
848 * +-------+-----------+---+-------~~-------+---+
849 * The multicast addr/port becomes what I listen to, and if 'mc' is '1' then
850 * I am the new master-client so must send ACKs to DataBlocks. If I am not
851 * master-client, I'm a passive client, gathering what DataBlocks I may and
852 * making note of which ones I got in my bitmask.
853 * In theory, I never go from master->passive..
854 * .. this comes in with pkt already pointing just past opc
856 static void parse_multicast_oack(char *pkt
, int len
)
860 char *mc_adr
, *port
, *mc
;
862 mc_adr
= port
= mc
= NULL
;
863 /* march along looking for 'multicast\0', which has to start at least
864 * 14 bytes back from the end.
866 for (i
= 0; i
< len
-14; i
++)
867 if (strcmp(pkt
+i
, "multicast") == 0)
869 if (i
>= (len
-14)) /* non-Multicast OACK, ign. */
872 i
+= 10; /* strlen multicast */
874 for (; i
< len
; i
++) {
875 if (*(pkt
+i
) == ',') {
885 if (!port
|| !mc_adr
|| !mc
)
887 if (Multicast
&& MasterClient
) {
888 printf("I got a OACK as master Client, WRONG!\n");
891 /* ..I now accept packets destined for this MCAST addr, port */
894 printf("Internal failure! no mcast.\n");
900 /* I malloc instead of pre-declare; so that if the file ends
901 * up being too big for this bitmap I can retry
903 Bitmap
= malloc(Mapsize
);
905 printf("No Bitmap, no multicast. Sorry.\n");
909 memset(Bitmap
, 0, Mapsize
);
913 addr
= string_to_ip(mc_adr
);
914 if (Mcast_addr
!= addr
) {
916 eth_mcast_join(Mcast_addr
, 0);
918 if (eth_mcast_join(Mcast_addr
, 1)) {
919 printf("Fail to set mcast, revert to TFTP\n");
925 MasterClient
= (unsigned char)simple_strtoul((char *)mc
, NULL
, 10);
926 Mcast_port
= (unsigned short)simple_strtoul(port
, NULL
, 10);
927 printf("Multicast: %s:%d [%d]\n", mc_adr
, Mcast_port
, MasterClient
);
931 #endif /* Multicast TFTP */