inetq/inetq.c

Copyright (C) 2001-2006 by egnite Software GmbH. All rights reserved.

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/

$Log$ Revision 1.6 2009/02/18 12:18:58 olereinhardt 2009-02-18 Ole Reinhardt <ole.reinhardt@thermotemp.de>

Fixed compilier warnings. Especialy signedness of char buffers as well as unused code on arm platform and main functions without return value

Revision 1.5 2006/07/21 09:07:48 haraldkipp Fixed warnings about wrong signedness.

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 
00092 #define DNSSERVERIP     "192.168.192.2"
00093 #define INETSERVER  "www.kornet.net"
00094 #define INETSERVERPORT  80
00095 #define INETURL         "/"
00096 #define MY_MAC          {0x00,0x06,0x98,0x20,0x00,0x00}
00097 #define MY_IP           "192.168.192.100"
00098 #define MY_MASK         "255.255.255.0"
00099 #define MY_GATE         "192.168.192.3"
00100 
00101 #include <string.h>
00102 #include <stdio.h>
00103 #include <io.h>
00104 
00105 #include <dev/board.h>
00106 
00107 #include <sys/heap.h>
00108 #include <sys/thread.h>
00109 #include <sys/timer.h>
00110 #include <sys/socket.h>
00111 #include <sys/confnet.h>
00112 
00113 #include <arpa/inet.h>
00114 #include <net/route.h>
00115 #include <netdb.h>
00116 
00117 #include <pro/dhcp.h>
00118 
00119 static char buff[1024];
00120 static u_char my_mac[] = MY_MAC;
00121 
00122 /*
00123  * Main application routine. 
00124  *
00125  */
00126 int main(void)
00127 {
00128     u_long baud = 115200;
00129     TCPSOCKET *sock;
00130     FILE *stream;
00131     u_long rip;
00132     u_long ip_addr;
00133     int bite;
00134     size_t rc;
00135     size_t len;
00136     u_long start_time;
00137     u_long total_bytes;
00138 
00139     /*
00140      * Initialize the uart device.
00141      */
00142     NutRegisterDevice(&DEV_DEBUG, 0, 0);
00143     freopen(DEV_DEBUG_NAME, "w", stdout);
00144     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00145     puts("\nInetQuery 1.0");
00146 
00147     /*
00148      * Register Realtek controller at address 8300 hex and interrupt 5.
00149      */
00150     puts("Configuring Ethernet interface");
00151     NutRegisterDevice(&DEV_ETHER, 0, 0);
00152 
00153     /*
00154      * Try DHCP. First use MAC from EEPROM.
00155      */
00156     if (NutDhcpIfConfig("eth0", 0, 60000) && NutDhcpIfConfig("eth0", my_mac, 60000)) {
00157         /*
00158          * No DHCP server available. Use hard coded values.
00159          */
00160         ip_addr = inet_addr(MY_IP);
00161         NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK));
00162         NutIpRouteAdd(0, 0, inet_addr(MY_GATE), &DEV_ETHER);
00163         NutDnsConfig(0, 0, inet_addr(DNSSERVERIP));
00164     } else
00165         ip_addr = confnet.cdn_ip_addr;
00166     printf("%s ready\n", inet_ntoa(ip_addr));
00167 
00168 
00169     /*
00170      * Resolve hostname using DNS.
00171      */
00172     if ((rip = NutDnsGetHostByName((u_char*)INETSERVER)) != 0) {
00173 
00174         /*
00175          * Let's try a stdio stream first.
00176          */
00177         if ((sock = NutTcpCreateSocket()) != 0) {
00178 
00179             /*
00180              * Connect a HTTP server in the Internet.
00181              */
00182             printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT);
00183             if (NutTcpConnect(sock, rip, INETSERVERPORT) == 0) {
00184 
00185                 /*
00186                  * Assign a stream to our connected socket.
00187                  */
00188                 if ((stream = _fdopen((int) sock, "r+b")) != 0) {
00189                     /*
00190                      * Send HTTP request to the server.
00191                      */
00192                     fprintf(stream, "GET %s HTTP/1.0\r\n", INETURL);
00193                     fputs("User-Agent: Ethernut [en] (NutOS)\r\n", stream);
00194                     fputs("\r\n", stream);
00195                     fflush(stream);
00196 
00197                     /*
00198                      * Init measure values.
00199                      */
00200                     start_time = NutGetTickCount();
00201                     total_bytes = 0;
00202 
00203                     /*
00204                      * Read server response and send it to the UART.
00205                      */
00206                     while (fgets(buff, sizeof(buff), stream)) {
00207                         puts(buff);
00208                         total_bytes += strlen(buff);
00209                     }
00210                     printf("Transfered %lu bytes in %lu seconds\n", total_bytes, (NutGetTickCount() - start_time) / 16UL);
00211                     fclose(stream);
00212                 } else
00213                     puts("Creating stream device failed");
00214 
00215             } else {
00216                 printf("Bad news, %s refuses the connection.\n", INETSERVER);
00217             }
00218             printf("Disconnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT);
00219             NutTcpCloseSocket(sock);
00220         }
00221 
00222         NutSleep(5000);
00223 
00224         /*
00225          * Now let's use native calls.
00226          */
00227         if ((sock = NutTcpCreateSocket()) != 0) {
00228 
00229             /*
00230              * Connect a HTTP server in the Internet.
00231              */
00232             printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT);
00233             if (NutTcpConnect(sock, rip, INETSERVERPORT) == 0) {
00234 
00235                 /*
00236                  * Send HTTP request to the server. NutTcpSend() doesn't
00237                  * guarantee to send out all bytes, thus the loop.
00238                  */
00239                 strcpy(buff, "GET " INETURL " HTTP/1.0\r\nUser-Agent: Ethernut [en] (NutOS)\r\n\r\n");
00240                 len = (int) strlen(buff);
00241                 for (rc = 0; rc < len; rc += bite)
00242                     if ((bite = NutTcpSend(sock, buff + rc, len - rc)) <= 0)
00243                         break;
00244 
00245                 /*
00246                  * Init measure values.
00247                  */
00248                 start_time = NutGetTickCount();
00249                 total_bytes = 0;
00250 
00251                 /*
00252                  * Read server response and send it to the UART.
00253                  */
00254                 while ((bite = NutTcpReceive(sock, buff, sizeof(buff) - 1)) > 0) {
00255                     total_bytes += bite;
00256                     buff[bite] = 0;
00257                     puts(buff);
00258                 }
00259                 printf("Transfered %lu bytes in %lu seconds\n", total_bytes, (NutGetTickCount() - start_time) / 16UL);
00260             } else {
00261                 printf("Bad news, %s refuses the connection.\n", INETSERVER);
00262             }
00263             printf("Disconnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT);
00264             NutTcpCloseSocket(sock);
00265         }
00266     } else
00267         printf("Great news, %s has been removed!\n", INETSERVER);
00268 
00269     for (;;)
00270         NutSleep(1000);
00271     return 0;
00272 }

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/