/*!
 * \brief Configure a network interface including the default gateway.
 *
 * Devices must have been registered by NutRegisterDevice() before
 * calling this function.
 *
 * For Ethernet devices applications may alternatively call
 * NutDhcpIfConfig(), which allows automatic configuration by DHCP or
 * the so called ARP method.
 *
 * \param name    Name of the device to configure.
 * \param params  Pointer to interface specific parameters.
 * \param ip_addr Specified IP address in network byte order. This must
 *                be a unique address within the Internet. If you do not
 *                directly communicate with other Internet hosts, you can
 *                use a locally assigned address. With PPP interfaces this
 *                may be set to 0.0.0.0, in which case the remote peer
 *                will be queried for an IP address.
 * \param ip_mask Specified IP network mask in network byte order.
 *                Typical Ethernet networks use 255.255.255.0, which
 *                allows upto 254 hosts. For PPP interfaces 255.255.255.255
 *                is the default.
 * \param gateway Specified IP address of gateway or next router in LAN.
 *
 * \return 0 on success, -1 otherwise.
 *
 * \note I do not like this function, because setting a gateway should
 *       be handled by NutIpRouteAdd(). It's not yet deprecated, but I
 *       recommend not to use it in application code.
 */
int NutNetIfConfig2(CONST char *name, void *params, u_long ip_addr, u_long ip_mask, u_long gateway)
{
    NUTDEVICE *dev;
    IFNET *nif;

    /*
     * Check if this is a registered network device.
     */
    if ((dev = NutDeviceLookup(name)) == 0 || dev->dev_type != IFTYP_NET)
        return -1;

    /*
     * Setup Ethernet interfaces.
     */
    nif = dev->dev_icb;
    if (nif->if_type == IFT_ETHER) {
        memcpy(nif->if_mac, params, sizeof(nif->if_mac));
        return NutNetIfSetup(dev, ip_addr, ip_mask, gateway);
    }

    /*
     * Setup PPP interfaces.
     */
    else if (nif->if_type == IFT_PPP) {
        PPPDCB *dcb = dev->dev_dcb;

        /*
         * Set the interface's IP address, make sure that the state
         * change queue is empty and switch hardware driver into
         * network mode.
         */
        dcb->dcb_local_ip = ip_addr;
        dcb->dcb_ip_mask = ip_mask ? ip_mask : 0xffffffff;
        NutEventBroadcast(&dcb->dcb_state_chg);
        _ioctl(dcb->dcb_fd, HDLC_SETIFNET, &dev);

        /* Ugly hack to get simple UART drivers working. */
        ppp_hackup = 1;

        /*
         * Wait for network layer up and configure the interface on
         * success.
         */
        if (NutEventWait(&dcb->dcb_state_chg, 60000) == 0 && dcb->dcb_ipcp_state == PPPS_OPENED) {
            return NutNetIfSetup(dev, dcb->dcb_local_ip, dcb->dcb_ip_mask, dcb->dcb_remote_ip);
        }
    }
    return -1;
}
示例#2
0
/*!
 * \brief Open a file.
 *
 * \param name The name of a registered device, optionally followed by a
 *             colon and a filename.
 * \param mode Operation mode. May be any of the following:
 * - _O_APPEND Always write at the end.
 * - _O_BINARY Raw mode.
 * - _O_CREAT Create file if it does not exist.
 * - _O_EXCL Open only if it does not exist.
 * - _O_RDONLY Read only.
 * - _O_RDWR Read and write.
 * - _O_TEXT End of line translation.
 * - _O_TRUNC Truncate file if it exists.
 * - _O_WRONLY Write only.
 *
 * \return File descriptor for the opened file or -1 to indicate an error.
 */
int _open(const char *name, int mode)
{
    NUTDEVICE *dev;
    char dev_name[9];
    uint_fast8_t nidx;
    const char *nptr = name;

    NUTASSERT(name != NULL);

    /*
     * Extract device name.
     */
    for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
        dev_name[nidx] = *nptr++;
    }
    dev_name[nidx] = 0;

    /*
     * Get device structure of registered device. In later releases we
     * try to open a file on a root device.
     */
    if ((dev = NutDeviceLookup(dev_name)) == 0) {
        errno = ENOENT;
        return -1;
    }

    /*
     * We should check, if the mode flags are device conformant.
     */

    /*
     * If a device name was specified, open this device. Otherwise open
     * a file on the device.
     */
    if (*nptr++ != ':')
        nptr = 0;
    return (int) ((uintptr_t) ((*dev->dev_open) (dev, nptr, mode, 0)));
}