inetq.c

Go to the documentation of this file.
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/