|
|
1.1 root 1: /******************************************************************************
2: * Copyright (c) 2004, 2008 IBM Corporation
3: * All rights reserved.
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
8: *
9: * Contributors:
10: * IBM Corporation - initial implementation
11: *****************************************************************************/
12:
13: #include <netlib/ipv4.h>
14: #include <netlib/dhcp.h>
15: #include <netlib/ethernet.h>
16: #include <sys/socket.h>
17: #include <string.h>
18: #include <stdio.h>
19: #include <stdlib.h>
20: #include <time.h>
21: #include <netapps/args.h>
22:
23: struct ping_args {
24: union {
25: char string[4];
26: unsigned int integer;
27: } server_ip;
28: union {
29: char string[4];
30: unsigned int integer;
31: } client_ip;
32: union {
33: char string[4];
34: unsigned int integer;
35: } gateway_ip;
36: unsigned int timeout;
37: };
38:
39: static void
40: usage()
41: {
42: printf
43: ("\nping device-path:[device-args,]server-ip,[client-ip],[gateway-ip][,timeout]\n");
44:
45: }
46:
47: static int
48: parse_args(const char *args, struct ping_args *ping_args)
49: {
50: unsigned int argc = get_args_count(args);
51: char buf[64];
52: ping_args->timeout = 10;
53: if (argc == 0)
54: /* at least server-ip has to be specified */
55: return -1;
56: if (argc == 1) {
57: /* probably only server ip is specified */
58: argncpy(args, 0, buf, 64);
59: if (!strtoip(buf, ping_args->server_ip.string))
60: return -1;
61: return 0;
62: }
63: /* get first option from list */
64: argncpy(args, 0, buf, 64);
65: if (!strtoip(buf, ping_args->server_ip.string)) {
66: /* it is not an IP address
67: * therefore it has to be device-args
68: * device-args are not supported and just ignored */
69: args = get_arg_ptr(args, 1);
70: argc--;
71: }
72:
73: argncpy(args, 0, buf, 64);
74: if (!strtoip(buf, ping_args->server_ip.string)) {
75: /* this should have been the server IP address */
76: return -1;
77: } else {
78: args = get_arg_ptr(args, 1);
79: if (!--argc)
80: return 0;
81: }
82:
83: argncpy(args, 0, buf, 64);
84: if (!strtoip(buf, ping_args->client_ip.string)) {
85: /* this should have been the client (our) IP address */
86: return -1;
87: } else {
88: args = get_arg_ptr(args, 1);
89: if (!--argc)
90: return 0;
91: }
92: argncpy(args, 0, buf, 64);
93: if (!strtoip(buf, ping_args->gateway_ip.string)) {
94: /* this should have been the gateway IP address */
95: return -1;
96: } else {
97: args = get_arg_ptr(args, 1);
98: if (!--argc)
99: return 0;
100: }
101: argncpy(args, 0, buf, 64);
102: ping_args->timeout = strtol(args, 0, 10);
103: return 0;
104: }
105:
106: int
107: ping(int argc, char *argv[])
108: {
109: short arp_failed = 0;
110: filename_ip_t fn_ip;
111: int fd_device;
112: struct ping_args ping_args;
113: uint8_t own_mac[6];
114:
115: memset(&ping_args, 0, sizeof(struct ping_args));
116:
117: if (argc == 2) {
118: if (parse_args(argv[1], &ping_args)) {
119: usage();
120: return -1;
121: }
122: } else {
123: usage();
124: return -1;
125: }
126:
127: memset(&fn_ip, 0, sizeof(filename_ip_t));
128:
129: /* Get mac_addr from device */
130: printf("\n Reading MAC address from device: ");
131: fd_device = socket(0, 0, 0, (char *) own_mac);
132: if (fd_device == -1) {
133: printf("\nE3000: Could not read MAC address\n");
134: return -100;
135: } else if (fd_device == -2) {
136: printf("\nE3006: Could not initialize network device\n");
137: return -101;
138: }
139:
140: printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
141: own_mac[0], own_mac[1], own_mac[2],
142: own_mac[3], own_mac[4], own_mac[5]);
143:
144: // init ethernet layer
145: set_mac_address(own_mac);
146: // identify the BOOTP/DHCP server via broadcasts
147: // don't do this, when using DHCP !!!
148: // fn_ip.server_ip = 0xFFFFFFFF;
149: // memset(fn_ip.server_mac, 0xff, 6);
150:
151: if (!ping_args.client_ip.integer) {
152: /* Get ip address for our mac address */
153: printf(" Requesting IP address via DHCP: ");
154: arp_failed = dhcp(0, &fn_ip, 30);
155:
156: if (arp_failed == -1) {
157: printf("\n DHCP: Could not get ip address\n");
158: return -1;
159: }
160:
161: } else {
162: memcpy(&fn_ip.own_ip, &ping_args.client_ip.integer, 4);
163: arp_failed = 1;
164: printf(" Own IP address: ");
165: }
166:
167: // reinit network stack
168: set_ipv4_address(fn_ip.own_ip);
169:
170: printf("%d.%d.%d.%d\n",
171: ((fn_ip.own_ip >> 24) & 0xFF), ((fn_ip.own_ip >> 16) & 0xFF),
172: ((fn_ip.own_ip >> 8) & 0xFF), (fn_ip.own_ip & 0xFF));
173:
174: memcpy(&fn_ip.server_ip, &ping_args.server_ip.integer, 4);
175: printf(" Ping to %d.%d.%d.%d ", ((fn_ip.server_ip >> 24) & 0xFF),
176: ((fn_ip.server_ip >> 16) & 0xFF),
177: ((fn_ip.server_ip >> 8) & 0xFF), (fn_ip.server_ip & 0xFF));
178:
179:
180: ping_ipv4(fn_ip.server_ip);
181:
182: set_timer(TICKS_SEC / 10 * ping_args.timeout);
183: while(get_timer() > 0) {
184: receive_ether();
185: if(pong_ipv4() == 0) {
186: printf("success\n");
187: return 0;
188: }
189: }
190:
191: printf("failed\n");
192: return -1;
193: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.