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