inetq/inetq.c

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

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