Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages   Examples  

inetq/inetq.c

Requests an URL from the Internet. Demonstrates DNS query and default route usage.

#define DNSSERVERIP     "192.168.192.2"
#define INETSERVER  "www.kornet.net"
#define INETSERVERPORT  80
#define INETURL         "/"
#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 <stdio.h>
#include <io.h>

#include <dev/uartavr.h>
#include <dev/nicrtl.h>

#include <sys/heap.h>
#include <sys/thread.h>
#include <sys/timer.h>
#include <sys/socket.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.
 */
int main(void)
{
    u_long baud = 115200;
    TCPSOCKET *sock;
    FILE *stream;
    u_long rip;
    u_long ip_addr; /* ICCAVR bugfix */

    /*
     * Initialize the uart device.
     */
    NutRegisterDevice(&devUart0, 0, 0);
    freopen("uart0", "w", stdout);
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
    NutSleep(200);
    puts("\nInetQuery 1.0");

    /*
    * Register Realtek controller at address 8300 hex
    * and interrupt 5 and configure lan interface.
    */
    puts("Configuring Ethernet interface");
    NutRegisterDevice(&devEth0, 0x8300, 5);
    ip_addr = inet_addr(MY_IP);
    NutNetIfConfig("eth0", my_mac, ip_addr, 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) {
            printf("Connecting %s:%u\r\n", inet_ntoa(rip), INETSERVERPORT);
            if(NutTcpConnect(sock, rip, INETSERVERPORT) == 0) {
        if((stream = _fdopen((int)sock, "r+b")) != 0) {
                    fprintf(stream, "GET %s HTTP/1.0\r\n", INETURL);
                    fputs("User-Agent: Ethernut/2.3 [en] (NutOS)\r\n", stream);
                    fputs("\r\n", stream);
                    fflush(stream);
                    while(fgets(buff, sizeof(buff), stream)) 
                        printf("%s", buff);
                    fclose(stream);
                }
                else
                    puts("Creating stream device failed");

            }
            printf("Disonnecting %s:%u\n", inet_ntoa(rip), INETSERVERPORT);
            NutTcpCloseSocket(sock);
        }
    }
    else
        printf("Can't resolve %s\n", INETSERVER);

    for(;;)
        NutSleep(1000);
}


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