1 /******************************************************************************
2 * Copyright (c) 2004, 2008 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
13 #include <netlib/ipv4.h>
14 #include <netlib/dhcp.h>
15 #include <netlib/ethernet.h>
16 #include <sys/socket.h>
21 #include <netapps/args.h>
44 ("\nping device-path:[device-args,]server-ip,[client-ip],[gateway-ip][,timeout]\n");
49 parse_args(const char *args, struct ping_args *ping_args)
51 unsigned int argc = get_args_count(args);
53 ping_args->timeout = 10;
55 /* at least server-ip has to be specified */
58 /* probably only server ip is specified */
59 argncpy(args, 0, buf, 64);
60 if (!strtoip(buf, ping_args->server_ip.string))
64 /* get first option from list */
65 argncpy(args, 0, buf, 64);
66 if (!strtoip(buf, ping_args->server_ip.string)) {
67 /* it is not an IP address
68 * therefore it has to be device-args
69 * device-args are not supported and just ignored */
70 args = get_arg_ptr(args, 1);
74 argncpy(args, 0, buf, 64);
75 if (!strtoip(buf, ping_args->server_ip.string)) {
76 /* this should have been the server IP address */
79 args = get_arg_ptr(args, 1);
84 argncpy(args, 0, buf, 64);
85 if (!strtoip(buf, ping_args->client_ip.string)) {
86 /* this should have been the client (our) IP address */
89 args = get_arg_ptr(args, 1);
93 argncpy(args, 0, buf, 64);
94 if (!strtoip(buf, ping_args->gateway_ip.string)) {
95 /* this should have been the gateway IP address */
98 args = get_arg_ptr(args, 1);
102 argncpy(args, 0, buf, 64);
103 ping_args->timeout = strtol(args, 0, 10);
108 ping(int argc, char *argv[])
110 short arp_failed = 0;
113 struct ping_args ping_args;
116 memset(&ping_args, 0, sizeof(struct ping_args));
119 if (parse_args(argv[1], &ping_args)) {
128 memset(&fn_ip, 0, sizeof(filename_ip_t));
130 /* Get mac_addr from device */
131 printf("\n Reading MAC address from device: ");
132 fd_device = socket(0, 0, 0, (char *) own_mac);
133 if (fd_device == -1) {
134 printf("\nE3000: Could not read MAC address\n");
136 } else if (fd_device == -2) {
137 printf("\nE3006: Could not initialize network device\n");
141 fn_ip.fd = fd_device;
143 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
144 own_mac[0], own_mac[1], own_mac[2],
145 own_mac[3], own_mac[4], own_mac[5]);
147 // init ethernet layer
148 set_mac_address(own_mac);
149 // identify the BOOTP/DHCP server via broadcasts
150 // don't do this, when using DHCP !!!
151 // fn_ip.server_ip = 0xFFFFFFFF;
152 // memset(fn_ip.server_mac, 0xff, 6);
154 if (!ping_args.client_ip.integer) {
155 /* Get ip address for our mac address */
156 printf(" Requesting IP address via DHCP: ");
157 arp_failed = dhcp(0, &fn_ip, 30, F_IPV4);
159 if (arp_failed == -1) {
160 printf("\n DHCP: Could not get ip address\n");
165 memcpy(&fn_ip.own_ip, &ping_args.client_ip.integer, 4);
167 printf(" Own IP address: ");
170 // reinit network stack
171 set_ipv4_address(fn_ip.own_ip);
173 printf("%d.%d.%d.%d\n",
174 ((fn_ip.own_ip >> 24) & 0xFF), ((fn_ip.own_ip >> 16) & 0xFF),
175 ((fn_ip.own_ip >> 8) & 0xFF), (fn_ip.own_ip & 0xFF));
177 memcpy(&fn_ip.server_ip, &ping_args.server_ip.integer, 4);
178 printf(" Ping to %d.%d.%d.%d ", ((fn_ip.server_ip >> 24) & 0xFF),
179 ((fn_ip.server_ip >> 16) & 0xFF),
180 ((fn_ip.server_ip >> 8) & 0xFF), (fn_ip.server_ip & 0xFF));
183 ping_ipv4(fd_device, fn_ip.server_ip);
185 set_timer(TICKS_SEC / 10 * ping_args.timeout);
186 while(get_timer() > 0) {
187 receive_ether(fd_device);
188 if(pong_ipv4() == 0) {