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 #ifdef DEV_ETHER
00148     /*
00149      * Register Realtek controller at address 8300 hex and interrupt 5.
00150      */
00151     puts("Configuring Ethernet interface");
00152     NutRegisterDevice(&DEV_ETHER, 0, 0);
00153 
00154     /*
00155      * Try DHCP. First use MAC from EEPROM.
00156      */
00157     if (NutDhcpIfConfig("eth0", 0, 60000) && NutDhcpIfConfig("eth0", my_mac, 60000)) {
00158         /*
00159          * No DHCP server available. Use hard coded values.
00160          */
00161         ip_addr = inet_addr(MY_IP);
00162         NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK));
00163         NutIpRouteAdd(0, 0, inet_addr(MY_GATE), &DEV_ETHER);
00164         NutDnsConfig(0, 0, inet_addr(DNSSERVERIP));
00165     } else
00166         ip_addr = confnet.cdn_ip_addr;
00167     printf("%s ready\n", inet_ntoa(ip_addr));
00168 
00169 
00170     /*
00171      * Resolve hostname using DNS.
00172      */
00173     if ((rip = NutDnsGetHostByName((u_char*)INETSERVER)) != 0) {
00174 
00175         /*
00176          * Let's try a stdio stream first.
00177          */
00178         if ((sock = NutTcpCreateSocket()) != 0) {
00179 
00180             /*
00181              * Connect a HTTP server in the Internet.
00182              */
00183             printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT);
00184             if (NutTcpConnect(sock, rip, INETSERVERPORT) == 0) {
00185 
00186                 /*
00187                  * Assign a stream to our connected socket.
00188                  */
00189                 if ((stream = _fdopen((int) sock, "r+b")) != 0) {
00190                     /*
00191                      * Send HTTP request to the server.
00192                      */
00193                     fprintf(stream, "GET %s HTTP/1.0\r\n", INETURL);
00194                     fputs("User-Agent: Ethernut [en] (NutOS)\r\n", stream);
00195                     fputs("\r\n", stream);
00196                     fflush(stream);
00197 
00198                     /*
00199                      * Init measure values.
00200                      */
00201                     start_time = NutGetTickCount();
00202                     total_bytes = 0;
00203 
00204                     /*
00205                      * Read server response and send it to the UART.
00206                      */
00207                     while (fgets(buff, sizeof(buff), stream)) {
00208                         puts(buff);
00209                         total_bytes += strlen(buff);
00210                     }
00211                     printf("Transfered %lu bytes in %lu seconds\n", total_bytes, (NutGetTickCount() - start_time) / 16UL);
00212                     fclose(stream);
00213                 } else
00214                     puts("Creating stream device failed");
00215 
00216             } else {
00217                 printf("Bad news, %s refuses the connection.\n", INETSERVER);
00218             }
00219             printf("Disconnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT);
00220             NutTcpCloseSocket(sock);
00221         }
00222 
00223         NutSleep(5000);
00224 
00225         /*
00226          * Now let's use native calls.
00227          */
00228         if ((sock = NutTcpCreateSocket()) != 0) {
00229 
00230             /*
00231              * Connect a HTTP server in the Internet.
00232              */
00233             printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT);
00234             if (NutTcpConnect(sock, rip, INETSERVERPORT) == 0) {
00235 
00236                 /*
00237                  * Send HTTP request to the server. NutTcpSend() doesn't
00238                  * guarantee to send out all bytes, thus the loop.
00239                  */
00240                 strcpy(buff, "GET " INETURL " HTTP/1.0\r\nUser-Agent: Ethernut [en] (NutOS)\r\n\r\n");
00241                 len = (int) strlen(buff);
00242                 for (rc = 0; rc < len; rc += bite)
00243                     if ((bite = NutTcpSend(sock, buff + rc, len - rc)) <= 0)
00244                         break;
00245 
00246                 /*
00247                  * Init measure values.
00248                  */
00249                 start_time = NutGetTickCount();
00250                 total_bytes = 0;
00251 
00252                 /*
00253                  * Read server response and send it to the UART.
00254                  */
00255                 while ((bite = NutTcpReceive(sock, buff, sizeof(buff) - 1)) > 0) {
00256                     total_bytes += bite;
00257                     buff[bite] = 0;
00258                     puts(buff);
00259                 }
00260                 printf("Transfered %lu bytes in %lu seconds\n", total_bytes, (NutGetTickCount() - start_time) / 16UL);
00261             } else {
00262                 printf("Bad news, %s refuses the connection.\n", INETSERVER);
00263             }
00264             printf("Disconnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT);
00265             NutTcpCloseSocket(sock);
00266         }
00267     } else
00268         printf("Great news, %s has been removed!\n", INETSERVER);
00269 #endif
00270 
00271     for (;;)
00272         NutSleep(1000);
00273     return 0;
00274 }

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