Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
For additional information see http://www.ethernut.de/
Revision 1.4 2005/11/22 09:14:13 haraldkipp Replaced specific device names by generalized macros.
Revision 1.3 2005/04/19 08:53:56 haraldkipp Added more detailed description
Revision 1.2 2003/11/04 17:46:52 haraldkipp Adapted to Ethernut 2
Revision 1.1 2003/08/05 18:59:05 haraldkipp Release 3.3 update
Revision 1.7 2003/05/08 12:00:53 harald Changed target to famous spammer
Revision 1.6 2003/02/04 18:19:37 harald Version 3 released
Revision 1.5 2003/02/04 16:24:32 harald Adapted to version 3
Revision 1.4 2002/06/12 10:55:54 harald *** empty log message ***
Revision 1.3 2002/06/04 19:11:29 harald *** empty log message ***
Revision 1.2 2002/05/08 16:02:30 harald First Imagecraft compilation
Requests an URL from the Internet and transfers the HTML source code to the serial device.
Your local Ethernet network must provide Internet access. Connect the RS232 port of the Ethernut with a free COM port of your PC and run a terminal emulator at 115200 Baud.
If your local network does not support DHCP, it may be required to modify the MY_IP, MY_MASK and MY_GATE below.
This sample demonstrates DNS query and default route usage.
00001 00085 #define DNSSERVERIP "192.168.192.2" 00086 #define INETSERVER "www.kornet.net" 00087 #define INETSERVERPORT 80 00088 #define INETURL "/" 00089 #define MY_MAC {0x00,0x06,0x98,0x20,0x00,0x00} 00090 #define MY_IP "192.168.192.100" 00091 #define MY_MASK "255.255.255.0" 00092 #define MY_GATE "192.168.192.3" 00093 00094 #include <string.h> 00095 #include <stdio.h> 00096 #include <io.h> 00097 00098 #include <dev/board.h> 00099 00100 #include <sys/heap.h> 00101 #include <sys/thread.h> 00102 #include <sys/timer.h> 00103 #include <sys/socket.h> 00104 #include <sys/confnet.h> 00105 00106 #include <arpa/inet.h> 00107 #include <net/route.h> 00108 #include <netdb.h> 00109 00110 #include <pro/dhcp.h> 00111 00112 static char buff[1024]; 00113 static u_char my_mac[] = MY_MAC; 00114 00115 /* 00116 * Main application routine. 00117 * 00118 */ 00119 int main(void) 00120 { 00121 u_long baud = 115200; 00122 TCPSOCKET *sock; 00123 FILE *stream; 00124 u_long rip; 00125 u_long ip_addr; 00126 int bite; 00127 size_t rc; 00128 size_t len; 00129 u_long start_time; 00130 u_long total_bytes; 00131 00132 /* 00133 * Initialize the uart device. 00134 */ 00135 NutRegisterDevice(&DEV_DEBUG, 0, 0); 00136 freopen(DEV_DEBUG_NAME, "w", stdout); 00137 _ioctl(_fileno(stdout), UART_SETSPEED, &baud); 00138 puts("\nInetQuery 1.0"); 00139 00140 /* 00141 * Register Realtek controller at address 8300 hex and interrupt 5. 00142 */ 00143 puts("Configuring Ethernet interface"); 00144 NutRegisterDevice(&DEV_ETHER, 0, 0); 00145 00146 /* 00147 * Try DHCP. First use MAC from EEPROM. 00148 */ 00149 if (NutDhcpIfConfig("eth0", 0, 60000) && NutDhcpIfConfig("eth0", my_mac, 60000)) { 00150 /* 00151 * No DHCP server available. Use hard coded values. 00152 */ 00153 ip_addr = inet_addr(MY_IP); 00154 NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK)); 00155 NutIpRouteAdd(0, 0, inet_addr(MY_GATE), &DEV_ETHER); 00156 NutDnsConfig(0, 0, inet_addr(DNSSERVERIP)); 00157 } else 00158 ip_addr = confnet.cdn_ip_addr; 00159 printf("%s ready\n", inet_ntoa(ip_addr)); 00160 00161 00162 /* 00163 * Resolve hostname using DNS. 00164 */ 00165 if ((rip = NutDnsGetHostByName(INETSERVER)) != 0) { 00166 00167 /* 00168 * Let's try a stdio stream first. 00169 */ 00170 if ((sock = NutTcpCreateSocket()) != 0) { 00171 00172 /* 00173 * Connect a HTTP server in the Internet. 00174 */ 00175 printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT); 00176 if (NutTcpConnect(sock, rip, INETSERVERPORT) == 0) { 00177 00178 /* 00179 * Assign a stream to our connected socket. 00180 */ 00181 if ((stream = _fdopen((int) sock, "r+b")) != 0) { 00182 /* 00183 * Send HTTP request to the server. 00184 */ 00185 fprintf(stream, "GET %s HTTP/1.0\r\n", INETURL); 00186 fputs("User-Agent: Ethernut [en] (NutOS)\r\n", stream); 00187 fputs("\r\n", stream); 00188 fflush(stream); 00189 00190 /* 00191 * Init measure values. 00192 */ 00193 start_time = NutGetTickCount(); 00194 total_bytes = 0; 00195 00196 /* 00197 * Read server response and send it to the UART. 00198 */ 00199 while (fgets(buff, sizeof(buff), stream)) { 00200 puts(buff); 00201 total_bytes += strlen(buff); 00202 } 00203 printf("Transfered %lu bytes in %lu seconds\n", total_bytes, (NutGetTickCount() - start_time) / 16UL); 00204 fclose(stream); 00205 } else 00206 puts("Creating stream device failed"); 00207 00208 } else { 00209 printf("Bad news, %s refuses the connection.\n", INETSERVER); 00210 } 00211 printf("Disconnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT); 00212 NutTcpCloseSocket(sock); 00213 } 00214 00215 NutSleep(5000); 00216 00217 /* 00218 * Now let's use native calls. 00219 */ 00220 if ((sock = NutTcpCreateSocket()) != 0) { 00221 00222 /* 00223 * Connect a HTTP server in the Internet. 00224 */ 00225 printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT); 00226 if (NutTcpConnect(sock, rip, INETSERVERPORT) == 0) { 00227 00228 /* 00229 * Send HTTP request to the server. NutTcpSend() doesn't 00230 * guarantee to send out all bytes, thus the loop. 00231 */ 00232 strcpy(buff, "GET " INETURL " HTTP/1.0\r\nUser-Agent: Ethernut [en] (NutOS)\r\n\r\n"); 00233 len = (int) strlen(buff); 00234 for (rc = 0; rc < len; rc += bite) 00235 if ((bite = NutTcpSend(sock, buff + rc, len - rc)) <= 0) 00236 break; 00237 00238 /* 00239 * Init measure values. 00240 */ 00241 start_time = NutGetTickCount(); 00242 total_bytes = 0; 00243 00244 /* 00245 * Read server response and send it to the UART. 00246 */ 00247 while ((bite = NutTcpReceive(sock, buff, sizeof(buff) - 1)) > 0) { 00248 total_bytes += bite; 00249 buff[bite] = 0; 00250 puts(buff); 00251 } 00252 printf("Transfered %lu bytes in %lu seconds\n", total_bytes, (NutGetTickCount() - start_time) / 16UL); 00253 } else { 00254 printf("Bad news, %s refuses the connection.\n", INETSERVER); 00255 } 00256 printf("Disconnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT); 00257 NutTcpCloseSocket(sock); 00258 } 00259 } else 00260 printf("Great news, %s has been removed!\n", INETSERVER); 00261 00262 for (;;) 00263 NutSleep(1000); 00264 }