コード例 #1
0
/*!
 * \brief Network interface setup.
 *
 * \param dev     Identifies the network device to setup.
 * \param ip_addr Specified IP address in network byte order.
 * \param ip_mask Specified IP network mask in network byte order.
 * \param gateway Optional default gateway.
 *
 * \return 0 on success, -1 otherwise.
 *
 * \note Typical applications do not use this function, but call
 *       NutDhcpIfConfig() or NutNetIfConfig().
 */
int NutNetIfSetup(NUTDEVICE * dev, u_long ip_addr, u_long ip_mask, u_long gateway)
{
    IFNET *nif;

    nif = dev->dev_icb;

    /*
     * Use specified or previously used IP address.
     */
    if (ip_addr == 0 && (ip_addr = confnet.cdn_ip_addr) == 0)
        return -1;
    nif->if_local_ip = ip_addr;

    /*
     * Use specified or default mask.
     */
    if (ip_mask == 0)
        ip_mask = inet_addr("255.255.255.0");
    nif->if_mask = ip_mask;

    /*
     * Add routing entries.
     */
    NutIpRouteAdd(ip_addr & ip_mask, ip_mask, 0, dev);
    if (gateway)
        NutIpRouteAdd(0, 0, gateway, dev);

    /*
     * Save configuration in EEPROM.
     */
    memcpy(confnet.cd_name, dev->dev_name, sizeof(confnet.cd_name));
    /* Never save MAC address 0. */
    if (nif->if_mac[0] || nif->if_mac[1] || nif->if_mac[2] || nif->if_mac[3] || nif->if_mac[4] || nif->if_mac[5]) {
        memcpy(confnet.cdn_mac, nif->if_mac, sizeof(nif->if_mac));
    }
    confnet.cdn_ip_addr = ip_addr;
    confnet.cdn_ip_mask = ip_mask;

    /*
     * Set gateway, if one was provided by the caller. Remove
     * gateway, if it's outside of our network.
     */
    if (gateway || (confnet.cdn_gateway & ip_mask) != (ip_addr & ip_mask))
        confnet.cdn_gateway = gateway;

    return NutNetSaveConfig();
}
コード例 #2
0
static int ProtocolPortConfigure(void)
{
#ifdef PPP_DEV
    PPPDCB *dcb;

    /*
     * We are connected, configure our PPP network interface.
     * This will initiate the PPP configuration negotiation
     * and authentication with the server.
     */
    printf("Configure network interface...");
    if (NutNetIfConfig("ppp", 0, 0, 0)) {
        puts("failed");
        return -1;
    }
    puts("done");

    /*
     * Set name server and default route. Actually the PPP interface
     * should do this, but the current release doesn't.
     */
    dcb = devPpp.dev_dcb;
    NutDnsConfig2(0, 0, dcb->dcb_ip_dns1, dcb->dcb_ip_dns2);
    NutIpRouteAdd(0, 0, dcb->dcb_remote_ip, &devPpp);

    /*
     * Display our IP settings.
     */
    printf("     Local IP: %s\n", inet_ntoa(dcb->dcb_local_ip));
    printf("    Remote IP: %s\n", inet_ntoa(dcb->dcb_remote_ip));
    printf("  Primary DNS: %s\n", inet_ntoa(dcb->dcb_ip_dns1));
    printf("Secondary DNS: %s\n", inet_ntoa(dcb->dcb_ip_dns2));
#else
    /* We use DHCP on Ethernet. */
    printf("Configure network interface...");
    if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {
        puts("failed");
        return -1;
    }
    puts("done");
#endif

    return 0;
}
コード例 #3
0
ファイル: audiostream.c プロジェクト: Bluepr1nt/C-huiswerk
/*!
* \brief Configure Ethernut LAN interface.
*
* If the EEPROM contains a valid network configuration, then it will
* be used. Otherwise DHCP is queried using a hard coded MAC address
* (global macro MY_MAC). If there is no DHCP server available, then
* the hard coded IP settings will be used (global macros MY_IPADDR,
* MY_IPMASK and MY_IPGATE).
*
* \param devname Symbolic name of the network device.
*/
int ConfigureLan(char *devname) {
    /*
     * Calling DHCP without MAC address assumes, that we have a valid
     * configuration in EEPROM.
     */
    puts("configure devname");
    printf("Configure %s...", devname);
    if (NutDhcpIfConfig(devname, NULL, 60000))
        {
            u_char mac[6] = MY_MAC;
            puts("Failed to get IP from dhcp");
            /*
             * DHCP failed. Either because the EEPROM contained no valid
             * MAC address or because we can't contact DHCP. We try again
             * with our hard coded MAC address.
             */
            puts("hard coded MAC...");
            if (NutDhcpIfConfig(devname, mac, 60000)) {
                u_long ip_addr = inet_addr(MY_IPADDR);
                u_long ip_mask = inet_addr(MY_IPMASK);
                u_long ip_gate = inet_addr(MY_IPGATE);

                /*
                 * Network configuration failed again. Give up DHCP and
                 * try the hard coded IP configuration.
                 */
                puts("hard coded IP...");
                if (NutNetIfConfig(devname, mac, ip_addr, ip_mask)) {

                    /*
                     * If even this one fails, then something is completely
                     * wrong. Return an error.
                     */
                    puts("Error: No LAN device");
                    return -1;
                }
                if (ip_gate) {

                    /*
                     * Without DHCP we had to set the default gateway manually.
                     */
                    puts("hard coded gate...");
                    if (NutIpRouteAdd(0, 0, ip_gate, &DEV_ETHER) == 0) {
                        puts("Error: Can't set gateway");
                        return -1;
                    }
                }
            }
        }
        else{
            puts("Got IP from DHCP");
        }

        /*
         * Display the result of our LAN configuration.
         */
        puts("OK");
        printf("MAC : %02X-%02X-%02X-%02X-%02X-%02X\n", confnet.cdn_mac[0], confnet.cdn_mac[1],
               confnet.cdn_mac[2], confnet.cdn_mac[3], confnet.cdn_mac[4], confnet.cdn_mac[5]);
        printf("IP  : %s\n", inet_ntoa(confnet.cdn_ip_addr));
        printf("Mask: %s\n", inet_ntoa(confnet.cdn_ip_mask));
        printf("Gate: %s\n\n", inet_ntoa(confnet.cdn_gateway));

        return 0;
    }
コード例 #4
0
ファイル: httpserv.c プロジェクト: niziak/ethernut-4.9
/*! \fn Service(void *arg)
 * \brief HTTP service thread.
 *
 * The endless loop in this thread waits for a client connect,
 * processes the HTTP request and disconnects. Nut/Net doesn't
 * support a server backlog. If one client has established a
 * connection, further connect attempts will be rejected.
 * Typically browsers open more than one connection in order
 * to load images concurrently. So we run this routine by
 * several threads.
 *
 */
THREAD(Service, arg)
{
    TCPSOCKET *sock;
    FILE *stream;
    uint8_t id = (uint8_t) ((uintptr_t) arg);

    /*
     * Now loop endless for connections.
     */
    for (;;) {

        /*
         * Create a socket.
         */
        if ((sock = NutTcpCreateSocket()) == 0) {
            printf("[%u] Creating socket failed\n", id);
            NutSleep(5000);
            continue;
        }

        /*
         * Listen on port 80. This call will block until we get a connection
         * from a client.
         */
        NutTcpAccept(sock, 80);
#if defined(__AVR__)
        printf("[%u] Connected, %u bytes free\n", id, NutHeapAvailable());
#else
        printf("[%u] Connected, %lu bytes free\n", id, NutHeapAvailable());
#endif

        /*
         * Wait until at least 8 kByte of free RAM is available. This will
         * keep the client connected in low memory situations.
         */
#if defined(__AVR__)
        while (NutHeapAvailable() < 8192) {
#else
        while (NutHeapAvailable() < 4096) {
#endif
            printf("[%u] Low mem\n", id);
            NutSleep(1000);
        }

        /*
         * Associate a stream with the socket so we can use standard I/O calls.
         */
        if ((stream = _fdopen((int) ((uintptr_t) sock), "r+b")) == 0) {
            printf("[%u] Creating stream device failed\n", id);
        } else {
            /*
             * This API call saves us a lot of work. It will parse the
             * client's HTTP request, send any requested file from the
             * registered file system or handle CGI requests by calling
             * our registered CGI routine.
             */
            NutHttpProcessRequest(stream);

            /*
             * Destroy the virtual stream device.
             */
            fclose(stream);
        }

        /*
         * Close our socket.
         */
        NutTcpCloseSocket(sock);
        printf("[%u] Disconnected\n", id);
    }
}
#endif /* DEV_ETHER */

/*!
 * \brief Main application routine.
 *
 * Nut/OS automatically calls this entry after initialization.
 */
int main(void)
{
    uint32_t baud = 115200;
    uint8_t i;

    /*
     * Initialize the uart device.
     */
    NutRegisterDevice(&DEV_DEBUG, 0, 0);
    freopen(DEV_DEBUG_NAME, "w", stdout);
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
    NutSleep(200);
    printf("\n\nNut/OS %s HTTP Daemon...", NutVersionString());

#ifdef DEV_ETHER

#ifdef NUTDEBUG
    NutTraceTcp(stdout, 0);
    NutTraceOs(stdout, 0);
    NutTraceHeap(stdout, 0);
    NutTracePPP(stdout, 0);
#endif

    /*
     * Register Ethernet controller.
     */
    if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
        puts("Registering device failed");
    }

    printf("Configure %s...", DEV_ETHER_NAME);
    if (NutNetLoadConfig(DEV_ETHER_NAME)) {
        uint8_t mac[] = MY_MAC;

        printf("initial boot...");
#ifdef USE_DHCP
        if (NutDhcpIfConfig(DEV_ETHER_NAME, mac, 60000)) 
#endif
        {
            uint32_t ip_addr = inet_addr(MY_IPADDR);
            uint32_t ip_mask = inet_addr(MY_IPMASK);
            uint32_t ip_gate = inet_addr(MY_IPGATE);

            printf("No DHCP...");
            if (NutNetIfConfig(DEV_ETHER_NAME, mac, ip_addr, ip_mask) == 0) {
                /* Without DHCP we had to set the default gateway manually.*/
                if(ip_gate) {
                    printf("hard coded gate...");
                    NutIpRouteAdd(0, 0, ip_gate, &DEV_ETHER);
                }
                puts("OK");
            }
            else {
                puts("failed");
            }
        }
    }
    else {
#ifdef USE_DHCP
        if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000)) {
            puts("failed");
        }
        else {
            puts("OK");
        }
#else
        if (NutNetIfConfig(DEV_ETHER_NAME, 0, 0, confnet.cdn_ip_mask)) {
            puts("failed");
        }
        else {
            puts("OK");
        }
#endif
    }
    printf("%s ready\n", inet_ntoa(confnet.cdn_ip_addr));

#ifdef USE_DISCOVERY
    NutRegisterDiscovery((uint32_t)-1, 0, DISF_INITAL_ANN);
#endif

    /*
     * Register our device for the file system.
     */
    NutRegisterDevice(&MY_FSDEV, 0, 0);

#ifdef MY_BLKDEV
    /* Register block device. */
    printf("Registering block device '" MY_BLKDEV_NAME "'...");
    if (NutRegisterDevice(&MY_BLKDEV, 0, 0)) {
        puts("failed");
        for (;;);
    }
    puts("OK");

    /* Mount partition. */
    printf("Mounting block device '" MY_BLKDEV_NAME ":1/" MY_FSDEV_NAME "'...");
    if (_open(MY_BLKDEV_NAME ":1/" MY_FSDEV_NAME, _O_RDWR | _O_BINARY) == -1) {
        puts("failed");
        for (;;);
    }
    puts("OK");
#endif

#ifdef MY_HTTPROOT
    /* Register root path. */
    printf("Registering HTTP root '" MY_HTTPROOT "'...");
    if (NutRegisterHttpRoot(MY_HTTPROOT)) {
        puts("failed");
        for (;;);
    }
    puts("OK");
#endif

    NutRegisterCgiBinPath("cgi-bin/;user/cgi-bin/;admin/cgi-bin/");


    /*
     * Register our CGI sample. This will be called
     * by http://host/cgi-bin/test.cgi?anyparams
     */
    NutRegisterCgi("test.cgi", ShowQuery);

#if defined(USE_SSI)
    /* 
     * Register a cgi included by the ssi demo. This will show how dynamic 
     * content is included in a ssi page and how the request parameters for 
     * a site are passed down to the included cgi.
     */    
    NutRegisterCgi("ssi-demo.cgi", SSIDemoCGI);
#endif

    /*
     * Register some CGI samples, which display interesting
     * system informations.
     */
    NutRegisterCgi("threads.cgi", ShowThreads);
    NutRegisterCgi("timers.cgi", ShowTimers);
    NutRegisterCgi("sockets.cgi", ShowSockets);

    /*
     * Finally a CGI example to process a form.
     */
    NutRegisterCgi("form.cgi", ShowForm);

    /*
     * Protect the cgi-bin directory with
     * user and password.
     */
    NutRegisterAuth("admin", "root:root");
    NutRegisterAuth("user", "user:user");

    /*
     * Register SSI and ASP handler
     */
#if defined(USE_SSI)
    NutRegisterSsi();
#endif
#if defined(USE_ASP)
    NutRegisterAsp();
    NutRegisterAspCallback(ASPCallback);
#endif

    /*
     * Start four server threads.
     */
    for (i = 1; i <= 4; i++) {
        char thname[] = "httpd0";

        thname[5] = '0' + i;
        NutThreadCreate(thname, Service, (void *) (uintptr_t) i, 
            (HTTPD_SERVICE_STACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD);
    }
#endif /* DEV_ETHER */

    /*
     * We could do something useful here, like serving a watchdog.
     */
    NutThreadSetPriority(254);
    for (;;) {
        NutSleep(60000);
    }
    return 0;
}
コード例 #5
0
ファイル: icmp-udp.c プロジェクト: niziak/ethernut-4.9
/*
 * Main application routine. 
 *
 */
int main(void)
{
    uint32_t baud = 115200;
    UDPSOCKET *socket;

    uint32_t ip_addr;
    uint32_t ip_udp_echo;
    int    rc;
    int    error;
    int    packet_nr;
    uint16_t length;

    /*
     * Initialize the uart device.
     */
    NutRegisterDevice(&DEV_DEBUG, 0, 0);
    freopen(DEV_DEBUG_NAME, "w", stdout);
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
    puts("Demo for ICMP support in UDP sockets...\r\n");

#ifdef DEV_ETHER
#ifndef NUT_UDP_ICMP_SUPPORT
#warning ICMP support for UDP sockets not enabled in the configurator, please enable NUT_UDP_ICMP_SUPPORT
    puts("ICMP support for UDP sockets not enabled in the configurator\r\n");
    puts("Please enable NUT_UDP_ICMP_SUPPORT\r\n");
#endif    
    /*
     * Register the network device.
     */
    puts("Configuring Ethernet interface");
    NutRegisterDevice(&DEV_ETHER, 0, 0);

    ip_addr = inet_addr(MY_IP);
    NutNetIfConfig("eth0", my_mac, ip_addr, inet_addr(MY_MASK));
    NutIpRouteAdd(0, 0, inet_addr(MY_GATE), &DEV_ETHER);

    printf("%s ready\r\n", inet_ntoa(ip_addr));


    socket = NutUdpCreateSocket(UDP_ECHO_PORT);
    if (socket == 0) {
        printf("Could not create UDP socket in port '%d'\r\n", UDP_ECHO_PORT);
        puts("Demo halted...\r\n");
        while (1);
    } else {
        printf("Successfully created UDP socket on port '%d'\r\n", UDP_ECHO_PORT);
        length = UDP_BUFF_SIZE;
        if (NutUdpSetSockOpt(socket, SO_RCVBUF, &length, sizeof(length))) {;
            printf("Could not set UDP receive buffer size (%d)\r\n", length);
            puts("Demo halted...\r\n");
            while (1);
        }
    }
    
    if (NutThreadCreate("RCV", UDPReceiver, (void*)socket, 1024) == 0) {
            puts("Could not start receiver thread\r\n");
            puts("Demo halted...\r\n");
            while (1);
    } else {
        puts("Receiver thread started\r\n");
    }
    
    puts("Starting echo test (1 packet / second)\r\n");
    
    ip_udp_echo = inet_addr(UDP_ECHO_IP);
    packet_nr = 0;
    
    while (1) {
        packet_nr ++;
        sprintf(send_buffer, "Packet: %d", packet_nr);
        rc = NutUdpSendTo(socket, ip_udp_echo, UDP_ECHO_PORT, send_buffer, length);
        printf("--> Sended packet: \"%s\", to %s, rc: %d\r\n", send_buffer, inet_ntoa(ip_udp_echo), rc);
        if (rc < 0) {
            uint32_t remote_ip;
            uint16_t remote_port;
            
            error = NutUdpError(socket, &remote_ip, &remote_port);
            print_udp_icmp_error(remote_ip, remote_port, error);
            
        }
        
        NutSleep(1000);
    }
#endif /* DEV_ETHER */
    return 0;
}