usb ehci driver: Get rid of echi_alloc/free
[barebox-mini2440.git] / net / ping.c
blob14224c6b91418774e52a356e02ae0e81e71744be
1 #include <common.h>
2 #include <command.h>
3 #include <clock.h>
4 #include <net.h>
6 static ushort PingSeqNo;
8 static int PingSend(void)
10 static uchar mac[6];
11 IP_t *ip;
12 ushort *s;
13 uchar *pkt;
15 /* XXX always send arp request */
17 memcpy(mac, NetEtherNullAddr, 6);
19 #ifdef ET_DEBUG
20 printf("sending ARP for %08lx\n", NetPingIP);
21 #endif
23 NetArpWaitPacketIP = NetPingIP;
24 NetArpWaitPacketMAC = mac;
26 pkt = NetArpWaitTxPacket;
27 pkt += NetSetEther(pkt, mac, PROT_IP);
29 ip = (IP_t *)pkt;
32 * Construct an IP and ICMP header. (need to set no fragment bit - XXX)
34 ip->ip_hl_v = 0x45; /* IP_HDR_SIZE / 4 (not including UDP) */
35 ip->ip_tos = 0;
36 ip->ip_len = htons(IP_HDR_SIZE_NO_UDP + 8);
37 ip->ip_id = htons(NetIPID++);
38 ip->ip_off = htons(0x4000); /* No fragmentation */
39 ip->ip_ttl = 255;
40 ip->ip_p = 0x01; /* ICMP */
41 ip->ip_sum = 0;
42 NetCopyIP((void*)&ip->ip_src, &NetOurIP); /* already in network byte order */
43 NetCopyIP((void*)&ip->ip_dst, &NetPingIP); /* - "" - */
44 ip->ip_sum = ~NetCksum((uchar *)ip, IP_HDR_SIZE_NO_UDP / 2);
46 s = &ip->udp_src; /* XXX ICMP starts here */
47 s[0] = htons(0x0800); /* echo-request, code */
48 s[1] = 0; /* checksum */
49 s[2] = 0; /* identifier */
50 s[3] = htons(PingSeqNo++); /* sequence number */
51 s[1] = ~NetCksum((uchar *)s, 8/2);
53 /* size of the waiting packet */
54 NetArpWaitTxPacketSize = (pkt - NetArpWaitTxPacket) + IP_HDR_SIZE_NO_UDP + 8;
56 /* and do the ARP request */
57 NetArpWaitTry = 1;
58 NetArpWaitTimerStart = get_time_ns();
59 ArpRequest();
60 return 1; /* waiting */
63 static void
64 PingTimeout (void)
66 eth_halt();
67 NetState = NETLOOP_FAIL; /* we did not get the reply */
70 static void
71 PingHandler (uchar * pkt, unsigned dest, unsigned src, unsigned len)
73 IPaddr_t tmp;
74 IP_t *ip = (IP_t *)pkt;
76 tmp = NetReadIP((void *)&ip->ip_src);
77 if (tmp != NetPingIP)
78 return;
80 NetState = NETLOOP_SUCCESS;
83 void PingStart(void)
85 NetSetTimeout (10 * SECOND, PingTimeout);
86 NetSetHandler (PingHandler);
88 PingSend();
91 int do_ping (cmd_tbl_t *cmdtp, int argc, char *argv[])
93 if (argc < 2)
94 return -1;
96 if (string_to_ip(argv[1], &NetPingIP)) {
97 printf ("Usage:\n%s\n", cmdtp->usage);
98 return -1;
101 if (NetLoop(PING) < 0) {
102 printf("ping failed; host %s is not alive\n", argv[1]);
103 return 1;
106 printf("host %s is alive\n", argv[1]);
108 return 0;
111 U_BOOT_CMD_START(ping)
112 .maxargs = 2,
113 .cmd = do_ping,
114 .usage = "send icmp echo_request to network host",
115 U_BOOT_CMD_END