1 |
27 |
unneback |
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
2 |
|
|
<html>
|
3 |
|
|
<head>
|
4 |
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
5 |
|
|
<meta name="GENERATOR" content="Mozilla/4.7 [en] (X11; I; Linux 2.2.14 ppc) [Netscape]">
|
6 |
|
|
</head>
|
7 |
|
|
<body>
|
8 |
|
|
|
9 |
|
|
<center>
|
10 |
|
|
<h1>
|
11 |
|
|
Sample Networking Program</h1></center>
|
12 |
|
|
|
13 |
|
|
<p><br>This example is derived from the test program <tt>".../net/tcpip/v1_3_1/tests/ping_test.c"</tt>.
|
14 |
|
|
<br>
|
15 |
|
|
<blockquote><tt><font size=-1>//==========================================================================</font></tt>
|
16 |
|
|
<br><tt><font size=-1>//</font></tt>
|
17 |
|
|
<br><tt><font size=-1>// tests/ping_test.c</font></tt>
|
18 |
|
|
<br><tt><font size=-1>//</font></tt>
|
19 |
|
|
<br><tt><font size=-1>// Simple test of PING
|
20 |
|
|
(ICMP) and networking support</font></tt>
|
21 |
|
|
<br><tt><font size=-1>//</font></tt>
|
22 |
|
|
<br><tt><font size=-1>//==========================================================================</font></tt>
|
23 |
|
|
<br><tt><font size=-1>//####BSDCOPYRIGHTBEGIN####</font></tt>
|
24 |
|
|
<br><tt><font size=-1>//</font></tt>
|
25 |
|
|
<br><tt><font size=-1>// -------------------------------------------</font></tt>
|
26 |
|
|
<br><tt><font size=-1>//</font></tt>
|
27 |
|
|
<br><tt><font size=-1>// Portions of this software may have been derived
|
28 |
|
|
from OpenBSD or other sources,</font></tt>
|
29 |
|
|
<br><tt><font size=-1>// and are covered by the appropriate copyright disclaimers
|
30 |
|
|
included herein.</font></tt>
|
31 |
|
|
<br><tt><font size=-1>//</font></tt>
|
32 |
|
|
<br><tt><font size=-1>// -------------------------------------------</font></tt>
|
33 |
|
|
<br><tt><font size=-1>//</font></tt>
|
34 |
|
|
<br><tt><font size=-1>//####BSDCOPYRIGHTEND####</font></tt>
|
35 |
|
|
<br><tt><font size=-1>//==========================================================================</font></tt>
|
36 |
|
|
<br><tt><font size=-1>//#####DESCRIPTIONBEGIN####</font></tt>
|
37 |
|
|
<br><tt><font size=-1>//</font></tt>
|
38 |
|
|
<br><tt><font size=-1>// Author(s): gthomas</font></tt>
|
39 |
|
|
<br><tt><font size=-1>// Contributors: gthomas</font></tt>
|
40 |
|
|
<br><tt><font size=-1>// Date:
|
41 |
|
|
2000-01-10</font></tt>
|
42 |
|
|
<br><tt><font size=-1>// Purpose:</font></tt>
|
43 |
|
|
<br><tt><font size=-1>// Description:</font></tt>
|
44 |
|
|
<br><tt><font size=-1>//</font></tt>
|
45 |
|
|
<br><tt><font size=-1>//</font></tt>
|
46 |
|
|
<br><tt><font size=-1>//####DESCRIPTIONEND####</font></tt>
|
47 |
|
|
<br><tt><font size=-1>//</font></tt>
|
48 |
|
|
<br><tt><font size=-1>//==========================================================================</font></tt><tt><font size=-1></font></tt>
|
49 |
|
|
<p><tt><font size=-1>// PING test code</font></tt><b><tt><font size=-1></font></tt></b>
|
50 |
|
|
<p><b><tt><font size=-1>#include <network.h></font></tt></b></blockquote>
|
51 |
|
|
This single include directive is all that is required to pick up the networking
|
52 |
|
|
support, including complete configuration information.
|
53 |
|
|
<blockquote><tt><font size=-1>#define STACK_SIZE CYGNUM_HAL_STACK_SIZE_TYPICAL</font></tt>
|
54 |
|
|
<br><tt><font size=-1>static char stack[STACK_SIZE];</font></tt>
|
55 |
|
|
<br><tt><font size=-1>static cyg_thread thread_data;</font></tt>
|
56 |
|
|
<br><tt><font size=-1>static cyg_handle_t thread_handle;</font></tt><tt><font size=-1></font></tt>
|
57 |
|
|
<p><tt><font size=-1>#define NUM_PINGS 16</font></tt>
|
58 |
|
|
<br><tt><font size=-1>#define MAX_PACKET 4096</font></tt>
|
59 |
|
|
<br><tt><font size=-1>static unsigned char pkt1[MAX_PACKET], pkt2[MAX_PACKET];</font></tt><tt><font size=-1></font></tt>
|
60 |
|
|
<p><tt><font size=-1>#define UNIQUEID 0x1234</font></tt><tt><font size=-1></font></tt>
|
61 |
|
|
<p><tt><font size=-1>void</font></tt>
|
62 |
|
|
<br><tt><font size=-1>cyg_test_exit(void)</font></tt>
|
63 |
|
|
<br><tt><font size=-1>{</font></tt>
|
64 |
|
|
<br><tt><font size=-1> diag_printf("... Done\n");</font></tt>
|
65 |
|
|
<br><tt><font size=-1> while (1) ;</font></tt>
|
66 |
|
|
<br><tt><font size=-1>}</font></tt><tt><font size=-1></font></tt>
|
67 |
|
|
<p><tt><font size=-1>void</font></tt>
|
68 |
|
|
<br><tt><font size=-1>pexit(char *s)</font></tt>
|
69 |
|
|
<br><tt><font size=-1>{</font></tt>
|
70 |
|
|
<br><tt><font size=-1> perror(s);</font></tt>
|
71 |
|
|
<br><tt><font size=-1> cyg_test_exit();</font></tt>
|
72 |
|
|
<br><tt><font size=-1>}</font></tt><tt><font size=-1></font></tt>
|
73 |
|
|
<p><tt><font size=-1>// Compute INET checksum</font></tt>
|
74 |
|
|
<br><tt><font size=-1>int</font></tt>
|
75 |
|
|
<br><tt><font size=-1>inet_cksum(u_short *addr, int len)</font></tt>
|
76 |
|
|
<br><tt><font size=-1>{</font></tt>
|
77 |
|
|
<br><tt><font size=-1> register int nleft = len;</font></tt>
|
78 |
|
|
<br><tt><font size=-1> register u_short *w = addr;</font></tt>
|
79 |
|
|
<br><tt><font size=-1> register u_short answer;</font></tt>
|
80 |
|
|
<br><tt><font size=-1> register u_int sum = 0;</font></tt>
|
81 |
|
|
<br><tt><font size=-1> u_short odd_byte = 0;</font></tt><tt><font size=-1></font></tt>
|
82 |
|
|
<p><tt><font size=-1> /*</font></tt>
|
83 |
|
|
<br><tt><font size=-1> * Our algorithm is
|
84 |
|
|
simple, using a 32 bit accumulator (sum),</font></tt>
|
85 |
|
|
<br><tt><font size=-1> * we add sequential
|
86 |
|
|
16 bit words to it, and at the end, fold</font></tt>
|
87 |
|
|
<br><tt><font size=-1> * back all the carry
|
88 |
|
|
bits from the top 16 bits into the lower</font></tt>
|
89 |
|
|
<br><tt><font size=-1> * 16 bits.</font></tt>
|
90 |
|
|
<br><tt><font size=-1> */</font></tt>
|
91 |
|
|
<br><tt><font size=-1> while( nleft > 1 ) {</font></tt>
|
92 |
|
|
<br><tt><font size=-1> sum +=
|
93 |
|
|
*w++;</font></tt>
|
94 |
|
|
<br><tt><font size=-1> nleft
|
95 |
|
|
-= 2;</font></tt>
|
96 |
|
|
<br><tt><font size=-1> }</font></tt><tt><font size=-1></font></tt>
|
97 |
|
|
<p><tt><font size=-1> /* mop up an odd byte, if necessary
|
98 |
|
|
*/</font></tt>
|
99 |
|
|
<br><tt><font size=-1> if( nleft == 1 ) {</font></tt>
|
100 |
|
|
<br><tt><font size=-1> *(u_char
|
101 |
|
|
*)(&odd_byte) = *(u_char *)w;</font></tt>
|
102 |
|
|
<br><tt><font size=-1> sum +=
|
103 |
|
|
odd_byte;</font></tt>
|
104 |
|
|
<br><tt><font size=-1> }</font></tt><tt><font size=-1></font></tt>
|
105 |
|
|
<p><tt><font size=-1> /*</font></tt>
|
106 |
|
|
<br><tt><font size=-1> * add back carry outs from
|
107 |
|
|
top 16 bits to low 16 bits</font></tt>
|
108 |
|
|
<br><tt><font size=-1> */</font></tt>
|
109 |
|
|
<br><tt><font size=-1> sum = (sum >> 16) + (sum &
|
110 |
|
|
0x0000ffff); /* add hi 16 to low 16 */</font></tt>
|
111 |
|
|
<br><tt><font size=-1> sum += (sum >> 16);
|
112 |
|
|
/* add carry */</font></tt>
|
113 |
|
|
<br><tt><font size=-1> answer = ~sum;
|
114 |
|
|
/* truncate to 16 bits */</font></tt>
|
115 |
|
|
<br><tt><font size=-1> return (answer);</font></tt>
|
116 |
|
|
<br><tt><font size=-1>}</font></tt><tt><font size=-1></font></tt>
|
117 |
|
|
<p><tt><font size=-1>static int</font></tt>
|
118 |
|
|
<br><tt><font size=-1>show_icmp(unsigned char *pkt, int len,</font></tt>
|
119 |
|
|
<br><tt><font size=-1>
|
120 |
|
|
struct sockaddr_in *from, struct sockaddr_in *to)</font></tt>
|
121 |
|
|
<br><tt><font size=-1>{</font></tt>
|
122 |
|
|
<br><tt><font size=-1> cyg_tick_count_t *tp, tv;</font></tt>
|
123 |
|
|
<br><tt><font size=-1> struct ip *ip;</font></tt>
|
124 |
|
|
<br><tt><font size=-1> struct icmp *icmp;</font></tt>
|
125 |
|
|
<br><tt><font size=-1> tv = cyg_current_time();</font></tt>
|
126 |
|
|
<br><tt><font size=-1> ip = (struct ip *)pkt;</font></tt>
|
127 |
|
|
<br><tt><font size=-1> if ((len < sizeof(*ip)) ||
|
128 |
|
|
ip->ip_v != IPVERSION) {</font></tt>
|
129 |
|
|
<br><tt><font size=-1> diag_printf("%s:
|
130 |
|
|
Short packet or not IP! - Len: %d, Version: %d\n",</font></tt>
|
131 |
|
|
<br><tt><font size=-1>
|
132 |
|
|
inet_ntoa(from->sin_addr), len, ip->ip_v);</font></tt>
|
133 |
|
|
<br><tt><font size=-1> return
|
134 |
|
|
0;</font></tt>
|
135 |
|
|
<br><tt><font size=-1> }</font></tt>
|
136 |
|
|
<br><tt><font size=-1> icmp = (struct icmp *)(pkt + sizeof(*ip));</font></tt>
|
137 |
|
|
<br><tt><font size=-1> len -= (sizeof(*ip) + 8);</font></tt>
|
138 |
|
|
<br><tt><font size=-1> tp = (cyg_tick_count_t *)&icmp->icmp_data;</font></tt>
|
139 |
|
|
<br><tt><font size=-1> if (icmp->icmp_type != ICMP_ECHOREPLY)
|
140 |
|
|
{</font></tt>
|
141 |
|
|
<br><tt><font size=-1> diag_printf("%s:
|
142 |
|
|
Invalid ICMP - type: %d\n",</font></tt>
|
143 |
|
|
<br><tt><font size=-1>
|
144 |
|
|
inet_ntoa(from->sin_addr), icmp->icmp_type);</font></tt>
|
145 |
|
|
<br><tt><font size=-1> return
|
146 |
|
|
0;</font></tt>
|
147 |
|
|
<br><tt><font size=-1> }</font></tt>
|
148 |
|
|
<br><tt><font size=-1> if (icmp->icmp_id != UNIQUEID)
|
149 |
|
|
{</font></tt>
|
150 |
|
|
<br><tt><font size=-1> diag_printf("%s:
|
151 |
|
|
ICMP received for wrong id - sent: %x, recvd: %x\n",</font></tt>
|
152 |
|
|
<br><tt><font size=-1>
|
153 |
|
|
inet_ntoa(from->sin_addr), UNIQUEID, icmp->icmp_id);</font></tt>
|
154 |
|
|
<br><tt><font size=-1> }</font></tt>
|
155 |
|
|
<br><tt><font size=-1> diag_printf("%d bytes from %s:
|
156 |
|
|
", len, inet_ntoa(from->sin_addr));</font></tt>
|
157 |
|
|
<br><tt><font size=-1> diag_printf("icmp_seq=%d", icmp->icmp_seq);</font></tt>
|
158 |
|
|
<br><tt><font size=-1> diag_printf(", time=%dms\n", (int)(tv
|
159 |
|
|
- *tp)*10);</font></tt>
|
160 |
|
|
<br><tt><font size=-1> return (from->sin_addr.s_addr
|
161 |
|
|
== to->sin_addr.s_addr);</font></tt>
|
162 |
|
|
<br><tt><font size=-1>}</font></tt><tt><font size=-1></font></tt>
|
163 |
|
|
<p><tt><font size=-1>static void</font></tt>
|
164 |
|
|
<br><tt><font size=-1>ping_host(int s, struct sockaddr_in *host)</font></tt>
|
165 |
|
|
<br><tt><font size=-1>{</font></tt>
|
166 |
|
|
<br><tt><font size=-1> struct icmp *icmp = (struct icmp
|
167 |
|
|
*)pkt1;</font></tt>
|
168 |
|
|
<br><tt><font size=-1> int icmp_len = 64;</font></tt>
|
169 |
|
|
<br><tt><font size=-1> int seq, ok_recv, bogus_recv;</font></tt>
|
170 |
|
|
<br><tt><font size=-1> cyg_tick_count_t *tp;</font></tt>
|
171 |
|
|
<br><tt><font size=-1> long *dp;</font></tt>
|
172 |
|
|
<br><tt><font size=-1> struct sockaddr_in from;</font></tt>
|
173 |
|
|
<br><tt><font size=-1> int i, len, fromlen;</font></tt><tt><font size=-1></font></tt>
|
174 |
|
|
<p><tt><font size=-1> ok_recv = 0;</font></tt>
|
175 |
|
|
<br><tt><font size=-1> bogus_recv = 0;</font></tt>
|
176 |
|
|
<br><tt><font size=-1> diag_printf("PING server %s\n",
|
177 |
|
|
<b>inet_ntoa(host->sin_addr)</b>);</font></tt></blockquote>
|
178 |
|
|
This function takes an IP address and returns a string representation.
|
179 |
|
|
Useful for printing the address.
|
180 |
|
|
<blockquote><tt><font size=-1> for (seq = 0; seq
|
181 |
|
|
< NUM_PINGS; seq++) {</font></tt>
|
182 |
|
|
<br><tt><font size=-1> // Build
|
183 |
|
|
ICMP packet</font></tt>
|
184 |
|
|
<br><tt><font size=-1> icmp->icmp_type
|
185 |
|
|
= ICMP_ECHO;</font></tt>
|
186 |
|
|
<br><tt><font size=-1> icmp->icmp_code
|
187 |
|
|
= 0;</font></tt>
|
188 |
|
|
<br><tt><font size=-1> icmp->icmp_cksum
|
189 |
|
|
= 0;</font></tt>
|
190 |
|
|
<br><tt><font size=-1> icmp->icmp_seq
|
191 |
|
|
= seq;</font></tt>
|
192 |
|
|
<br><tt><font size=-1> icmp->icmp_id
|
193 |
|
|
= 0x1234;</font></tt>
|
194 |
|
|
<br><tt><font size=-1> // Set
|
195 |
|
|
up ping data</font></tt>
|
196 |
|
|
<br><tt><font size=-1> tp = (cyg_tick_count_t
|
197 |
|
|
*)&icmp->icmp_data;</font></tt>
|
198 |
|
|
<br><tt><font size=-1> *tp++
|
199 |
|
|
= cyg_current_time();</font></tt>
|
200 |
|
|
<br><tt><font size=-1> dp = (long
|
201 |
|
|
*)tp;</font></tt>
|
202 |
|
|
<br><tt><font size=-1> for (i
|
203 |
|
|
= sizeof(*tp); i < icmp_len; i += sizeof(*dp)) {</font></tt>
|
204 |
|
|
<br><tt><font size=-1>
|
205 |
|
|
*dp++ = i;</font></tt>
|
206 |
|
|
<br><tt><font size=-1> }</font></tt>
|
207 |
|
|
<br><tt><font size=-1> // Add
|
208 |
|
|
checksum</font></tt>
|
209 |
|
|
<br><tt><font size=-1> icmp->icmp_cksum
|
210 |
|
|
= inet_cksum( (u_short *)icmp, icmp_len+8);</font></tt>
|
211 |
|
|
<br><tt><font size=-1> // Send
|
212 |
|
|
it off</font></tt>
|
213 |
|
|
<br><tt><font size=-1> <b>if
|
214 |
|
|
(sendto(s, icmp, icmp_len+8, 0, (struct sockaddr *)host, sizeof(*host))
|
215 |
|
|
< 0) {</b></font></tt></blockquote>
|
216 |
|
|
This function sends a single packet, in this case via the ICMP protocol.
|
217 |
|
|
The destination address is specified by the <tt>host</tt> argument.
|
218 |
|
|
<blockquote><tt><font size=-1>
|
219 |
|
|
perror("sendto");</font></tt>
|
220 |
|
|
<br><tt><font size=-1>
|
221 |
|
|
continue;</font></tt>
|
222 |
|
|
<br><tt><font size=-1> }</font></tt>
|
223 |
|
|
<br><tt><font size=-1> // Wait
|
224 |
|
|
for a response</font></tt>
|
225 |
|
|
<br><tt><font size=-1> fromlen
|
226 |
|
|
= sizeof(from);</font></tt>
|
227 |
|
|
<br><tt><font size=-1> <b>len
|
228 |
|
|
= recvfrom(s, pkt2, sizeof(pkt2), 0, (struct sockaddr *)&from, &fromlen);</b></font></tt></blockquote>
|
229 |
|
|
This function waits for a packet to be sent to this interface. Note:
|
230 |
|
|
this operation can fail if no packet is received with a given amount of
|
231 |
|
|
time (this timeout value is setup in the <tt>ping_test()</tt> routine below).
|
232 |
|
|
<blockquote><tt><font size=-1>
|
233 |
|
|
if (len < 0) {</font></tt>
|
234 |
|
|
<br><tt><font size=-1>
|
235 |
|
|
perror("recvfrom");</font></tt>
|
236 |
|
|
<br><tt><font size=-1> } else
|
237 |
|
|
{</font></tt>
|
238 |
|
|
<br><tt><font size=-1>
|
239 |
|
|
if (show_icmp(pkt2, len, &from, host)) {</font></tt>
|
240 |
|
|
<br><tt><font size=-1>
|
241 |
|
|
ok_recv++;</font></tt>
|
242 |
|
|
<br><tt><font size=-1>
|
243 |
|
|
} else {</font></tt>
|
244 |
|
|
<br><tt><font size=-1>
|
245 |
|
|
bogus_recv++;</font></tt>
|
246 |
|
|
<br><tt><font size=-1>
|
247 |
|
|
}</font></tt>
|
248 |
|
|
<br><tt><font size=-1> }</font></tt>
|
249 |
|
|
<br><tt><font size=-1> }</font></tt>
|
250 |
|
|
<br><tt><font size=-1> diag_printf("Sent %d packets,
|
251 |
|
|
received %d OK, %d bad\n", NUM_PINGS, ok_recv, bogus_recv);</font></tt>
|
252 |
|
|
<br><tt><font size=-1>}</font></tt><tt><font size=-1></font></tt>
|
253 |
|
|
<p><tt><font size=-1>static void</font></tt>
|
254 |
|
|
<br><tt><font size=-1>ping_test(struct bootp *bp)</font></tt>
|
255 |
|
|
<br><tt><font size=-1>{</font></tt>
|
256 |
|
|
<br><tt><font size=-1> struct protoent *p;</font></tt>
|
257 |
|
|
<br><tt><font size=-1> struct timeval tv;</font></tt>
|
258 |
|
|
<br><tt><font size=-1> struct sockaddr_in host;</font></tt>
|
259 |
|
|
<br><tt><font size=-1> int s;</font></tt><tt><font size=-1></font></tt>
|
260 |
|
|
<p><tt><font size=-1> <b>if ((p = getprotobyname("icmp"))
|
261 |
|
|
== (struct protoent *)0) {</b></font></tt></blockquote>
|
262 |
|
|
This function gets information about the ICMP protocol. This will
|
263 |
|
|
be used below to set up the "socket" (the handle to a network connection).
|
264 |
|
|
<blockquote><tt><font size=-1>
|
265 |
|
|
perror("getprotobyname");</font></tt>
|
266 |
|
|
<br><tt><font size=-1> return;</font></tt>
|
267 |
|
|
<br><tt><font size=-1> }</font></tt>
|
268 |
|
|
<br><tt><font size=-1> <b>s = socket(AF_INET, SOCK_RAW,
|
269 |
|
|
p->p_proto);</b></font></tt></blockquote>
|
270 |
|
|
Create the socket "s". A socket is an abstract object (sometimes
|
271 |
|
|
called a handle or endpoint). Sockets come in many flavors, RAW sockets
|
272 |
|
|
are used for communicating with non-structured protocols. DATAGRAM
|
273 |
|
|
sockets are used for packet oriented protocols, such as UDP. STREAM
|
274 |
|
|
sockets are used for reliable, sequenced byte streams, such as those provided
|
275 |
|
|
by TCP.
|
276 |
|
|
<blockquote><tt><font size=-1> if (s < 0) {</font></tt>
|
277 |
|
|
<br><tt><font size=-1> perror("socket");</font></tt>
|
278 |
|
|
<br><tt><font size=-1> return;</font></tt>
|
279 |
|
|
<br><tt><font size=-1> }</font></tt>
|
280 |
|
|
<br><tt><font size=-1> tv.tv_sec = 1;</font></tt>
|
281 |
|
|
<br><tt><font size=-1> tv.tv_usec = 0;</font></tt>
|
282 |
|
|
<br><tt><font size=-1> <b>setsockopt(s, SOL_SOCKET, SO_RCVTIMEO,
|
283 |
|
|
&tv, sizeof(tv));</b></font></tt></blockquote>
|
284 |
|
|
This function instructs the system that receive operations (see <tt>ping_host()</tt>
|
285 |
|
|
above) must complete within 1 second or issue a timed out error condition.
|
286 |
|
|
<blockquote><tt><font size=-1> // Set up host address</font></tt>
|
287 |
|
|
<br><tt><font size=-1> host.sin_family = AF_INET;</font></tt>
|
288 |
|
|
<br><tt><font size=-1> host.sin_addr = bp->bp_siaddr;</font></tt>
|
289 |
|
|
<br><tt><font size=-1> host.sin_port = 0;</font></tt>
|
290 |
|
|
<br><tt><font size=-1> ping_host(s, &host);</font></tt>
|
291 |
|
|
<br><tt><font size=-1> // Now try a bogus host</font></tt>
|
292 |
|
|
<br><tt><font size=-1> host.sin_addr.s_addr = htonl(ntohl(host.sin_addr.s_addr)
|
293 |
|
|
+ 32);</font></tt>
|
294 |
|
|
<br><tt><font size=-1> ping_host(s, &host);</font></tt>
|
295 |
|
|
<br><tt><font size=-1>}</font></tt>
|
296 |
|
|
<br><tt><font size=-1></font></tt> </blockquote>
|
297 |
|
|
This function drives the test.
|
298 |
|
|
<blockquote><tt><font size=-1>void</font></tt>
|
299 |
|
|
<br><tt><font size=-1>net_test(cyg_addrword_t p)</font></tt>
|
300 |
|
|
<br><tt><font size=-1>{</font></tt>
|
301 |
|
|
<br><tt><font size=-1> diag_printf("Start PING test\n");</font></tt>
|
302 |
|
|
<br><tt><font size=-1> <b>init_all_network_interfaces();</b></font></tt></blockquote>
|
303 |
|
|
This is a call to the network initialization code. Note that it must
|
304 |
|
|
be executed from a thread context, thus we can't put it in the <tt>cyg_start()
|
305 |
|
|
</tt>function which follows. The <tt>init_all_network_interfaces()</tt>
|
306 |
|
|
routine will cause the networking system to be initialized and any hardware
|
307 |
|
|
interfaces will be set up. Using the system configuration information,
|
308 |
|
|
an interface can be started using either <b>BOOTP</b>, or a predefined
|
309 |
|
|
configuration with static IP information. It is also possible to
|
310 |
|
|
indicate that an interface will be configured by the user code, outside
|
311 |
|
|
of "<tt>init_all_network_interfaces()</tt>".
|
312 |
|
|
<br><tt><font size=-1> </font></tt>
|
313 |
|
|
<br><tt><font size=-1> #ifdef CYGHWR_NET_DRIVER_ETH0</font></tt>
|
314 |
|
|
<br><tt><font size=-1> if (eth0_up)
|
315 |
|
|
{</font></tt>
|
316 |
|
|
<blockquote><tt><font size=-1>
|
317 |
|
|
ping_test(&eth0_bootp_data);</font></tt>
|
318 |
|
|
<br><tt><font size=-1> }</font></tt>
|
319 |
|
|
<br><tt><font size=-1>#endif</font></tt></blockquote>
|
320 |
|
|
Notice that there is a BOOTP information structure kept about all hardware
|
321 |
|
|
interfaces. This is created and initialized by the call to <tt>init_all_network_interfaces()</tt>,
|
322 |
|
|
unless manual configuration is selected. This information can be
|
323 |
|
|
used by the application to learn things such as the local IP address, server
|
324 |
|
|
name, etc.
|
325 |
|
|
<blockquote><tt><font size=-1>#ifdef CYGHWR_NET_DRIVER_ETH1</font></tt>
|
326 |
|
|
<br><tt><font size=-1> if (eth1_up) {</font></tt>
|
327 |
|
|
<br><tt><font size=-1> ping_test(&eth1_bootp_data);</font></tt>
|
328 |
|
|
<br><tt><font size=-1> }</font></tt>
|
329 |
|
|
<br><tt><font size=-1>#endif</font></tt></blockquote>
|
330 |
|
|
Multiple hardware interfaces can be supported, depending on the hardware
|
331 |
|
|
platform. If the platform provides a single ethernet device then
|
332 |
|
|
<b><tt>CYGHWR_NET_DRIVER_ETH0 </tt></b>will be defined. If there
|
333 |
|
|
is a second interface, then will <b><tt>CYGHWR_NET_DRIVER_ETH1 </tt></b>be
|
334 |
|
|
defined.
|
335 |
|
|
<blockquote><tt><font size=-1> cyg_test_exit();</font></tt>
|
336 |
|
|
<br><tt><font size=-1>}</font></tt>
|
337 |
|
|
<br><tt><font size=-1></font></tt> </blockquote>
|
338 |
|
|
The following function creates an initial thread which runs the program.
|
339 |
|
|
This could just as easily be done by naming the <tt>net_test()</tt> function
|
340 |
|
|
<tt>main()</tt>.
|
341 |
|
|
<blockquote><tt><font size=-1>void</font></tt>
|
342 |
|
|
<br><tt><font size=-1>cyg_start(void)</font></tt>
|
343 |
|
|
<br><tt><font size=-1>{</font></tt>
|
344 |
|
|
<br><tt><font size=-1> // Create a main thread, so we
|
345 |
|
|
can run the scheduler and have time 'pass'</font></tt>
|
346 |
|
|
<br><tt><font size=-1> cyg_thread_create(10,
|
347 |
|
|
// Priority - just a number</font></tt>
|
348 |
|
|
<br><tt><font size=-1>
|
349 |
|
|
net_test, // entry</font></tt>
|
350 |
|
|
<br><tt><font size=-1>
|
351 |
|
|
0,
|
352 |
|
|
// entry parameter</font></tt>
|
353 |
|
|
<br><tt><font size=-1>
|
354 |
|
|
"Network test", // Name</font></tt>
|
355 |
|
|
<br><tt><font size=-1>
|
356 |
|
|
&stack[0], // Stack</font></tt>
|
357 |
|
|
<br><tt><font size=-1>
|
358 |
|
|
STACK_SIZE, // Size</font></tt>
|
359 |
|
|
<br><tt><font size=-1>
|
360 |
|
|
&thread_handle, // Handle</font></tt>
|
361 |
|
|
<br><tt><font size=-1>
|
362 |
|
|
&thread_data // Thread data structure</font></tt>
|
363 |
|
|
<br><tt><font size=-1>
|
364 |
|
|
);</font></tt>
|
365 |
|
|
<br><tt><font size=-1> cyg_thread_resume(thread_handle);
|
366 |
|
|
// Start it</font></tt>
|
367 |
|
|
<br><tt><font size=-1> cyg_scheduler_start();</font></tt>
|
368 |
|
|
<br><tt><font size=-1>}</font></tt></blockquote>
|
369 |
|
|
|
370 |
|
|
</body>
|
371 |
|
|
</html>
|