Requests an URL from the Internet. Demonstrates DNS query and default route usage.
#define DNSSERVERIP "192.168.192.2" #define INETSERVER "www.google.com" #define INETSERVERPORT 80 #define INETURL "/search?q=ethernut" #define MY_MAC {0x00,0x06,0x98,0x20,0x00,0x00} #define MY_IP "192.168.192.100" #define MY_MASK "255.255.255.0" #define MY_GATE "192.168.192.3" #include <string.h> #include <dev/uartavr.h> #include <dev/nicrtl.h> #include <sys/heap.h> #include <sys/thread.h> #include <sys/timer.h> #include <sys/print.h> #include <sys/kprint.h> #include <netinet/sostream.h> #include <arpa/inet.h> #include <net/route.h> #include <netdb.h> static u_char buff[1024]; static u_char my_mac[] = MY_MAC; /* * Main application routine. * * Nut/OS automatically calls this entry after initialization. */ THREAD(NutMain, arg) { NUTDEVICE *uart0; u_long baud = 115200; TCPSOCKET *sock; NUTDEVICE *sostream; u_long rip; NutRegisterDevice(&devUart0, 0, 0); uart0 = NutDeviceOpen("uart0"); NutDeviceIOCtl(uart0, UART_SETSPEED, &baud); NutKPrintString("\r\nInetQuery 1.0\r\n"); /* * Register Realtek controller at address 8300 hex * and interrupt 5 and configure lan interface. */ NutKPrintString("Configuring Ethernet interface\r\n"); NutRegisterDevice(&devEth0, 0x8300, 5); NutNetIfConfig("eth0", my_mac, inet_addr(MY_IP), inet_addr(MY_MASK)); NutIpRouteAdd(0, 0, inet_addr(MY_GATE), &devEth0); NutDnsConfig(0, 0, inet_addr(DNSSERVERIP)); if((rip = NutDnsGetHostByName(INETSERVER)) != 0) { /* * Create a socket. */ if((sock = NutTcpCreateSocket()) != 0) { NutKPrintFormat("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT); if(NutTcpConnect(sock, rip, INETSERVERPORT) == 0) { if((sostream = NutSoStreamCreate(sock)) != 0) { NutPrintFormat(sostream, "GET %s HTTP/1.0\r\n", INETURL); NutPrintString(sostream, "User-Agent: Ethernut/2.3 [en] (NutOS)\r\n"); NutPrintString(sostream, "\r\n"); NutPrintFlush(sostream); while(NutDeviceGetLine(sostream, buff, sizeof(buff) - 1) >= 0) { NutKPrintString(buff); NutKPrintString("\r\n"); } NutSoStreamDestroy(sostream); } else NutKPrintString("Creating stream device failed\r\n"); } NutKPrintFormat("Disonnecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT); NutTcpCloseSocket(sock); } } else NutKPrintFormat("Can't resolve %s\r\n", INETSERVER); for(;;) NutSleep(1000); }