Beispiel #1
0
 /**
  * Determine if two address are on the same network.
  *
  * @arg addr1 IP address 1
  * @arg addr2 IP address 2
  * @arg mask network identifier mask
  * @return !0 if the network identifiers of both address match
  */
 public static bool ip_addr_netcmp(ip_addr addr1, ip_addr addr2, ip_addr mask)
 {
     return(((addr1).addr &
             (mask).addr) ==
            ((addr2).addr &
             (mask).addr));
 }
Beispiel #2
0
        public const int IP_LOOPBACKNET = 127;                         /* official! */

        /** Set an IP address given by the four byte-parts.
         *      Little-endian version that prevents the use of lwip.lwip_htonl. */
        public static void IP4_ADDR(ip_addr ipaddr, byte a, byte b, byte c, byte d)
        {
            ipaddr.addr = ((uint)((d) & 0xff) << 24) |
                          ((uint)((c) & 0xff) << 16) |
                          ((uint)((b) & 0xff) << 8) |
                          (uint)((a) & 0xff);
        }
Beispiel #3
0
        internal err_t output(lwip ip, pbuf p, ip_addr src, ip_addr dest, byte ttl, byte tos, byte proto)
        {
            int pos = 0, rest = p.tot_len;

            byte[]  packet = new byte[rest];
            ip_addr srch   = new ip_addr(lwip_ntohl(src.addr));
            ip_addr desth  = new ip_addr(lwip_ntohl(dest.addr));

            for (pbuf q = p; q != null; q = q.next)
            {
                int len = rest;
                if (len > q.len)
                {
                    len = q.len;
                }

                Buffer.BlockCopy(q.payload.data, q.payload.offset, packet, pos, len);
                pos  += len;
                rest -= len;
            }

            m_output(ip, packet, srch, desth, proto);

            return(err_t.ERR_OK);
        }
Beispiel #4
0
        internal void input(pointer packet, int length, ip_addr srcn, ip_addr destn, byte proto)
        {
            ip_addr src = new ip_addr(lwip.lwip_htonl(srcn.addr));
            ip_addr dest = new ip_addr(lwip.lwip_htonl(destn.addr));
            pbuf    p = pbuf_alloc(pbuf_layer.PBUF_RAW, (ushort)length, pbuf_type.PBUF_POOL);
            int     pos = 0, rest = length;

            for (pbuf q = p; q != null; q = q.next)
            {
                int len = rest;
                if (len > q.len)
                {
                    len = q.len;
                }

                pointer.memcpy(q.payload, new pointer(packet, pos), len);
                pos  += len;
                rest -= len;
            }

            if (ip_input(p, src, dest, proto) != err_t.ERR_OK)
            {
                pbuf_free(p);
            }
        }
Beispiel #5
0
        public ER Connect(T_IPV4EP p_myaddr, T_IPV4EP p_dstaddr, TMO tmout)
        {
            err_t   err;
            ip_addr ma = new ip_addr(0);
            ip_addr sa = new ip_addr(0);

            ma.addr = lwip.lwip_htonl(p_myaddr.ipaddr);
            err     = m_lwIP.tcp.tcp_bind(m_Pcb, ma, lwip.lwip_htons(p_myaddr.portno));
            if (err != err_t.ERR_OK)
            {
                return(ER.E_OBJ);
            }

            sa.addr = lwip.lwip_htonl(p_dstaddr.ipaddr);
            err     = m_lwIP.tcp.tcp_connect(m_Pcb, sa, lwip.lwip_htons(p_dstaddr.portno), Connected);
            if (err != err_t.ERR_OK)
            {
                return(ER.E_OBJ);
            }

            Task task = m_Nucleus.GetTask(ID.TSK_SELF);

            if ((tmout != 0) && (task == null))
            {
                return(ER.E_CTX);
            }

            if (task == null)
            {
                return(ER.E_TMOUT);
            }

            if (tmout == 0)
            {
                return(ER.E_TMOUT);
            }

            SetState(true, new TMO(tcp.TCP_TMR_INTERVAL), task, Intarval);

            ER ret = task.Wait(m_SendTaskQueue, TSKWAIT.TTW_TCP, m_CepID, tmout);

            switch (ret)
            {
            case ER.E_OK:
                if ((m_TPcb == null) || (m_TPcb.state != tcp_state.ESTABLISHED))
                {
                    return(ER.E_RLWAI);
                }
                break;

            case ER.E_TMOUT:
                return(ER.E_TMOUT);

            default:
                return(ER.E_RLWAI);
            }

            return(ER.E_OK);
        }
Beispiel #6
0
 public static void ip_addr_debug_print(uint debug, ip_addr ipaddr)
 {
     uITron3.lwip.LWIP_DEBUGF(debug, "{0}.{1}.{2}.{3}",
                              ipaddr != null ? ip_addr.ip4_addr1_16(ipaddr) : 0,
                              ipaddr != null ? ip_addr.ip4_addr2_16(ipaddr) : 0,
                              ipaddr != null ? ip_addr.ip4_addr3_16(ipaddr) : 0,
                              ipaddr != null ? ip_addr.ip4_addr4_16(ipaddr) : 0);
 }
Beispiel #7
0
 internal static string to_string(ip_addr ipaddr)
 {
     return(String.Format("{0}.{1}.{2}.{3}",
                          ipaddr != null ? ip_addr.ip4_addr1_16(ipaddr) : 0,
                          ipaddr != null ? ip_addr.ip4_addr2_16(ipaddr) : 0,
                          ipaddr != null ? ip_addr.ip4_addr3_16(ipaddr) : 0,
                          ipaddr != null ? ip_addr.ip4_addr4_16(ipaddr) : 0));
 }
Beispiel #8
0
        /**
         * Simple interface to ip_output_if. It finds the outgoing network
         * interface and calls upon ip_output_if to do the actual work.
         *
         * @param p the packet to send (p.payload points to the data, e.g. next
         *                      protocol header; if dest == ip.IP_HDRINCL, p already includes an IP
         *                      header and p.payload points to that IP header)
         * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
         *         IP  address of the netif used to send is used as source address)
         * @param dest the destination IP address to send the packet to
         * @param ttl the TTL value to be set in the IP header
         * @param tos the TOS value to be set in the IP header
         * @param proto the PROTOCOL to be set in the IP header
         *
         * @return err_t.ERR_RTE if no route is found
         *         see ip_output_if() for more return values
         */
        public err_t ip_output(pbuf p, ip_addr src, ip_addr dest,
                               byte ttl, byte tos, byte proto)
        {
            /* pbufs passed to IP must have a @ref-count of 1 as their payload pointer
             * gets altered as the packet is passed down the stack */
            lwip.LWIP_ASSERT("p.ref == 1", p.@ref == 1);

            return(ip_output_if(p, src, dest, ttl, tos, proto));
        }
Beispiel #9
0
        /* lwip.inet_chksum_pseudo:
         *
         * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
         * IP addresses are expected to be in network byte order.
         *
         * @param p chain of pbufs over that a checksum should be calculated (ip data part)
         * @param src source ip address (used for checksum of pseudo header)
         * @param dst destination ip address (used for checksum of pseudo header)
         * @param proto ip protocol (used for checksum of pseudo header)
         * @param proto_len length of the ip data part (used for checksum of pseudo header)
         * @return checksum (as ushort) to be saved directly in the protocol header
         */
        public static ushort inet_chksum_pseudo_partial(pbuf p,
                                                        ip_addr src, ip_addr dest,
                                                        byte proto, ushort proto_len, ushort chksum_len)
        {
            uint   acc;
            uint   addr;
            pbuf   q;
            byte   swapped;
            ushort chklen;

            acc     = 0;
            swapped = 0;
            /* iterate through all pbuf in chain */
            for (q = p; (q != null) && (chksum_len > 0); q = q.next)
            {
                lwip.LWIP_DEBUGF(opt.INET_DEBUG, "inet_chksum_pseudo(): checksumming pbuf {0} (has next {1}) \n",
                                 q, q.next);
                chklen = q.len;
                if (chklen > chksum_len)
                {
                    chklen = chksum_len;
                }
                acc        += LWIP_CHKSUM(q.payload, chklen);
                chksum_len -= chklen;
                lwip.LWIP_ASSERT("delete me", chksum_len < 0x7fff);
                /*lwip.LWIP_DEBUGF(opt.INET_DEBUG, "inet_chksum_pseudo(): unwrapped lwip_chksum()={0} \n", acc);*/
                /* fold the upper bit down */
                acc = FOLD_U32T(acc);
                if (q.len % 2 != 0)
                {
                    swapped = (byte)(1 - swapped);
                    acc     = SWAP_BYTES_IN_WORD((ushort)acc);
                }
                /*lwip.LWIP_DEBUGF(opt.INET_DEBUG, "inet_chksum_pseudo(): wrapped lwip_chksum()={0} \n", acc);*/
            }

            if (swapped != 0)
            {
                acc = SWAP_BYTES_IN_WORD((ushort)acc);
            }
            addr = ip_addr.ip4_addr_get_u32(src);
            acc += (addr & 0xffffU);
            acc += ((addr >> 16) & 0xffffU);
            addr = ip_addr.ip4_addr_get_u32(dest);
            acc += (addr & 0xffffU);
            acc += ((addr >> 16) & 0xffffU);
            acc += (uint)lwip.lwip_htons((ushort)proto);
            acc += (uint)lwip.lwip_htons(proto_len);

            /* Fold 32-bit sum to 16 bits
             * calling this twice is propably faster than if statements... */
            acc = FOLD_U32T(acc);
            acc = FOLD_U32T(acc);
            lwip.LWIP_DEBUGF(opt.INET_DEBUG, "inet_chksum_pseudo(): pbuf chain lwip_chksum()={0}\n", acc);
            return((ushort)~(acc & 0xffffUL));
        }
Beispiel #10
0
        /**
         * Ascii internet address interpretation routine.
         * The value returned is in network order.
         *
         * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
         * @return ip address in network order
         */
        public static uint ipaddr_addr(string cp)
        {
            ip_addr val = new ip_addr(0);

            if (ipaddr_aton(cp, val) != 0)
            {
                return(ip_addr.ip4_addr_get_u32(val));
            }
            return(IPADDR_NONE);
        }
Beispiel #11
0
        private void ip_output(lwip netif, byte[] packet, ip_addr src, ip_addr dest, byte proto)
        {
            if (m_IPPacketBridge == null)
            {
                return;
            }

            byte[] data = Itron.GetIp4Packet(new pointer(packet, 0), packet.Length, src, dest, proto);

            m_IPPacketBridge.OutputData(data);
        }
Beispiel #12
0
        public UdpCep(ID udpid, ref T_UDP_CCEP pk_cudp, Nucleus pNucleus, lwip lwip)
        {
            m_CepID   = udpid;
            m_cudp    = pk_cudp;
            m_Nucleus = pNucleus;
            m_lwIP    = lwip;

            ip_addr addr = new ip_addr(pk_cudp.myaddr.ipaddr);

            m_Pcb = m_lwIP.udp.udp_new();
            m_lwIP.udp.udp_bind(m_Pcb, addr, pk_cudp.myaddr.portno);
            udp.udp_recv(m_Pcb, recv, this);
        }
Beispiel #13
0
        /* lwip.inet_chksum_pseudo:
         *
         * Calculates the pseudo Internet checksum used by TCP and UDP for a pbuf chain.
         * IP addresses are expected to be in network byte order.
         *
         * @param p chain of pbufs over that a checksum should be calculated (ip data part)
         * @param src source ip address (used for checksum of pseudo header)
         * @param dst destination ip address (used for checksum of pseudo header)
         * @param proto ip protocol (used for checksum of pseudo header)
         * @param proto_len length of the ip data part (used for checksum of pseudo header)
         * @return checksum (as ushort) to be saved directly in the protocol header
         */
        public static ushort inet_chksum_pseudo(pbuf p, ip_addr src, ip_addr dest,
                                                byte proto, ushort proto_len)
        {
            uint acc;
            uint addr;
            pbuf q;
            byte swapped;

            acc     = 0;
            swapped = 0;
            /* iterate through all pbuf in chain */
            for (q = p; q != null; q = q.next)
            {
                lwip.LWIP_DEBUGF(opt.INET_DEBUG, "inet_chksum_pseudo(): checksumming pbuf {0} (has next {1}) \n",
                                 q, q.next);
                acc += LWIP_CHKSUM(q.payload, q.len);
                /*lwip.LWIP_DEBUGF(opt.INET_DEBUG, "inet_chksum_pseudo(): unwrapped lwip_chksum()={0} \n", acc);*/

                /* just executing this next line is probably faster that the if statement needed
                 * to check whether we really need to execute it, and does no harm */
                acc = FOLD_U32T(acc);
                if (q.len % 2 != 0)
                {
                    swapped = (byte)(1 - swapped);
                    acc     = SWAP_BYTES_IN_WORD((ushort)acc);
                }
                /*lwip.LWIP_DEBUGF(opt.INET_DEBUG, "inet_chksum_pseudo(): wrapped lwip_chksum()={0} \n", acc);*/
            }

            if (swapped != 0)
            {
                acc = SWAP_BYTES_IN_WORD((ushort)acc);
            }
            addr = ip_addr.ip4_addr_get_u32(src);
            acc += (addr & 0xffffU);
            acc += ((addr >> 16) & 0xffffU);
            addr = ip_addr.ip4_addr_get_u32(dest);
            acc += (addr & 0xffffU);
            acc += ((addr >> 16) & 0xffffU);
            acc += (uint)lwip.lwip_htons((ushort)proto);
            acc += (uint)lwip.lwip_htons(proto_len);

            /* Fold 32-bit sum to 16 bits
             * calling this twice is propably faster than if statements... */
            acc = FOLD_U32T(acc);
            acc = FOLD_U32T(acc);
            lwip.LWIP_DEBUGF(opt.INET_DEBUG, "inet_chksum_pseudo(): pbuf chain lwip_chksum()={0}\n", acc);
            return((ushort)~(acc & 0xffffU));
        }
Beispiel #14
0
        /**
         * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
         *
         * @param addr ip address in network order to convert
         * @param buf target buffer where the pointer is stored
         * @param buflen length of buf
         * @return either pointer to buf which now holds the ASCII
         *         representation of addr or null if buf was too small
         */
        public static string ipaddr_ntoa_r(ip_addr addr, char[] buf)
        {
            byte[] s_addr;
            char[] inv = new char[3];
            int    rp;
            int    ap;
            byte   rem;
            byte   n;
            byte   i;
            int    len = 0;

            s_addr = BitConverter.GetBytes(ip_addr.ip4_addr_get_u32(addr));

            rp = 0;
            ap = 0;
            for (n = 0; n < 4; n++)
            {
                i = 0;
                do
                {
                    rem         = (byte)(s_addr[ap] % 10);
                    s_addr[ap] /= (byte)10;
                    inv[i++]    = (char)('0' + rem);
                } while (s_addr[ap] != 0);
                while ((i--) != 0)
                {
                    if (len++ >= buf.Length)
                    {
                        return(null);
                    }
                    buf[rp++] = inv[i];
                }
                if (len++ >= buf.Length)
                {
                    return(null);
                }
                buf[rp++] = '.';
                ap++;
            }
            buf[--rp] = '\0';
            return(buf.ToString());
        }
Beispiel #15
0
        /**
         * Determine if an address is a broadcast address on a network interface
         *
         * @param addr address to be checked
         * @param netif the network interface against which the address is checked
         * @return returns non-zero if the address is a broadcast address
         */
        public static bool ip4_addr_isbroadcast(uint addr, lwip netif)
        {
            ip_addr ipaddr = new ip_addr(0);

            ip_addr.ip4_addr_set_u32(ipaddr, addr);

            /* all ones (broadcast) or all zeroes (old skool broadcast) */
            if ((~addr == IPADDR_ANY) ||
                (addr == IPADDR_ANY))
            {
                return(true);
                /* no broadcast support on this network interface? */
            }
            else if ((netif.flags & lwip.NETIF_FLAG_BROADCAST) == 0)
            {
                /* the given address cannot be a broadcast address
                 * nor can we check against any broadcast addresses */
                return(false);
                /* address matches network interface address exactly? => no broadcast */
            }
            else if (addr == ip_addr.ip4_addr_get_u32(netif.ip_addr))
            {
                return(false);
                /*  on the same (sub) network... */
            }
            else if (ip_addr.ip_addr_netcmp(ipaddr, netif.ip_addr, netif.netmask)
                     /* ...and host identifier bits are all ones? =>... */
                     && ((addr & ~ip_addr.ip4_addr_get_u32(netif.netmask)) ==
                         (IPADDR_BROADCAST & ~ip_addr.ip4_addr_get_u32(netif.netmask))))
            {
                /* => network broadcast address */
                return(true);
            }
            else
            {
                return(false);
            }
        }
Beispiel #16
0
        private void recv(object arg, udp_pcb pcb, pbuf p, ip_addr addr, ushort port)
        {
            System.Diagnostics.Debug.Assert((arg == this) && (pcb == m_Pcb));
            pointer  data      = new pointer(new byte[sizeof(ushort) + T_IPV4EP.length + p.tot_len], 0);
            T_IPV4EP p_dstaddr = new T_IPV4EP(data, sizeof(ushort));
            pointer  p_dat     = new pointer(data, sizeof(ushort) + T_IPV4EP.length);

            data.SetValue(p.tot_len);
            p_dstaddr.ipaddr = lwip.lwip_ntohl(addr.addr);
            p_dstaddr.portno = lwip.lwip_ntohs(port);

            for (pbuf q = p; q != null; q = q.next)
            {
                pointer.memcpy(p_dat, q.payload, q.len);
                p_dat += q.len;
            }

            lock (m_CallBack) {
                m_CallBack.Enqueue(data);
            }

            SetState(true, TMO.TMO_POL, null, OnTimeOut);
        }
Beispiel #17
0
        public ER SendData(T_IPV4EP p_dstaddr, pointer p_dat, int len, TMO tmout)
        {
            pbuf    buf;
            ip_addr addr = new ip_addr(lwip.lwip_htonl(p_dstaddr.ipaddr));

            buf = m_lwIP.pbuf_alloc(pbuf_layer.PBUF_TRANSPORT, (ushort)len, pbuf_type.PBUF_POOL);
            if (buf == null)
            {
                return(ER.E_NOMEM);
            }

            int pos = 0;

            for (pbuf q = buf; q != null; q = q.next)
            {
                pointer.memcpy(q.payload, new pointer(p_dat, pos), q.len);
                pos += q.len;
            }

            m_lwIP.udp.udp_sendto(m_Pcb, buf, addr, p_dstaddr.portno);

            return(ER.E_OK);
        }
Beispiel #18
0
 public static bool ip_addr_ismulticast(ip_addr addr1)
 {
     return (((addr1).addr & lwip.PP_HTONL(0xf0000000U)) == lwip.PP_HTONL(0xe0000000U));
 }
Beispiel #19
0
 public static bool ip_addr_isbroadcast(ip_addr ipaddr, lwip netif)
 {
     return ip_addr.ip4_addr_isbroadcast(ipaddr.addr, netif);
 }
Beispiel #20
0
 /** Get the network address by combining host address with netmask */
 public static void ip_addr_get_network(ip_addr target, ip_addr host, ip_addr netmask)
 {
     target.addr = host.addr & netmask.addr;
 }
Beispiel #21
0
 /** Copy IP address - faster than ip_addr.ip_addr_set: no null check */
 public static void ip_addr_copy(ip_addr dest, ip_addr src)
 {
     dest.addr = src.addr;
 }
Beispiel #22
0
        /**
         * Same as ipaddr_ntoa, but reentrant since a user-supplied buffer is used.
         *
         * @param addr ip address in network order to convert
         * @param buf target buffer where the pointer is stored
         * @param buflen length of buf
         * @return either pointer to buf which now holds the ASCII
         *         representation of addr or null if buf was too small
         */
        public static string ipaddr_ntoa_r(ip_addr addr, char[] buf)
        {
            byte[] s_addr;
            char[] inv = new char[3];
            int rp;
            int ap;
            byte rem;
            byte n;
            byte i;
            int len = 0;

            s_addr = BitConverter.GetBytes(ip_addr.ip4_addr_get_u32(addr));

            rp = 0;
            ap = 0;
            for (n = 0; n < 4; n++) {
                i = 0;
                do {
                    rem = (byte)(s_addr[ap] % 10);
                    s_addr[ap] /= (byte)10;
                    inv[i++] = (char)('0' + rem);
                } while (s_addr[ap] != 0);
                while ((i--) != 0) {
                    if (len++ >= buf.Length) {
                        return null;
                    }
                    buf[rp++] = inv[i];
                }
                if (len++ >= buf.Length) {
                    return null;
                }
                buf[rp++] = '.';
                ap++;
            }
            buf[--rp] = '\0';
            return buf.ToString();
        }
Beispiel #23
0
 public static ushort ip4_addr4_16(ip_addr ipaddr)
 {
     return((ushort)ip_addr.ip4_addr4(ipaddr));
 }
Beispiel #24
0
 /** Set address to loopback address */
 public static void ip_addr_set_loopback(ip_addr ipaddr)
 {
     ipaddr.addr = lwip.PP_HTONL(IPADDR_LOOPBACK);
 }
Beispiel #25
0
 public static bool ip_addr_netmask_valid(ip_addr netmask)
 {
     return(ip_addr.ip4_addr_netmask_valid((netmask).addr));
 }
Beispiel #26
0
 public static bool ip_addr_ismulticast(ip_addr addr1)
 {
     return(((addr1).addr & lwip.PP_HTONL(0xf0000000U)) == lwip.PP_HTONL(0xe0000000U));
 }
Beispiel #27
0
 static ip_addr()
 {
     IP_ADDR_ANY       = ip_addr_any;
     IP_ADDR_BROADCAST = ip_addr_broadcast;
 }
Beispiel #28
0
 public static bool ip_addr_islinklocal(ip_addr addr1)
 {
     return(((addr1).addr & lwip.PP_HTONL(0xffff0000U)) == lwip.PP_HTONL(0xa9fe0000U));
 }
Beispiel #29
0
 public static byte ip4_addr4(ip_addr ipaddr)
 {
     return(BitConverter.GetBytes(ipaddr.addr)[3]);
 }
Beispiel #30
0
 /**
  * Convert numeric IP address into decimal dotted ASCII representation.
  * returns ptr to static buffer; not reentrant!
  *
  * @param addr ip address in network order to convert
  * @return pointer to a global static (!) buffer that holds the ASCII
  *         represenation of addr
  */
 public static string ipaddr_ntoa(ip_addr addr)
 {
     char[] str = new char[16];
     return(ipaddr_ntoa_r(addr, str));
 }
Beispiel #31
0
        /**
         * Check whether "cp" is a valid ascii representation
         * of an Internet address and convert to a binary address.
         * Returns 1 if the address is valid, 0 if not.
         * This replaces inet_addr, the return value from which
         * cannot distinguish between failure and a local broadcast address.
         *
         * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
         * @param addr pointer to which to save the ip address in network order
         * @return 1 if cp could be converted to addr, 0 on failure
         */
        public static int ipaddr_aton(string cp, ip_addr addr)
        {
            uint val;
            byte @base;
            char c;

            uint[] parts = new uint[4];
            int    pp    = 0;
            int    p     = 0;

            cp += "\0";
            c   = cp[0];
            for (;;)
            {
                /*
                 * Collect number up to ``.''.
                 * Values are specified as for C:
                 * 0x=hex, 0=octal, 1-9=decimal.
                 */
                if (!isdigit(c))
                {
                    return(0);
                }
                val   = 0;
                @base = 10;
                if (c == '0')
                {
                    c = cp[++p];
                    if (c == 'x' || c == 'X')
                    {
                        @base = 16;
                        c     = cp[++p];
                    }
                    else
                    {
                        @base = 8;
                    }
                }
                for (;;)
                {
                    if (isdigit(c))
                    {
                        val = (val * @base) + (uint)(c - (byte)'0');
                        c   = cp[++p];
                    }
                    else if (@base == 16 && isxdigit(c))
                    {
                        val = (val << 4) | (uint)(c + 10 - (islower(c) ? (byte)'a' : (byte)'A'));
                        c   = cp[++p];
                    }
                    else
                    {
                        break;
                    }
                }
                if (c == '.')
                {
                    /*
                     * Internet format:
                     *  a.b.c.d
                     *  a.b.c   (with c treated as 16 bits)
                     *  a.b (with b treated as 24 bits)
                     */
                    if (pp >= 3)
                    {
                        return(0);
                    }
                    parts[pp++] = val;
                    c           = cp[++p];
                }
                else
                {
                    break;
                }
            }

            /*
             * Check for trailing characters.
             */
            if (c != '\0' && !isspace(c))
            {
                return(0);
            }

            /*
             * Concoct the address according to
             * the number of parts specified.
             */
            switch (pp + 1)
            {
            case 0:
                return(0);                  /* initial nondigit */

            case 1:                         /* a -- 32 bits */
                break;

            case 2:                         /* a.b -- 8.24 bits */
                if (val > 0xffffffUL)
                {
                    return(0);
                }
                val |= parts[0] << 24;
                break;

            case 3:                         /* a.b.c -- 8.8.16 bits */
                if (val > 0xffff)
                {
                    return(0);
                }
                val |= (parts[0] << 24) | (parts[1] << 16);
                break;

            case 4:                         /* a.b.c.d -- 8.8.8.8 bits */
                if (val > 0xff)
                {
                    return(0);
                }
                val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
                break;

            default:
                lwip.LWIP_ASSERT("unhandled", false);
                break;
            }
            if (addr != null)
            {
                ip_addr.ip4_addr_set_u32(addr, lwip.lwip_htonl(val));
            }
            return(1);
        }
Beispiel #32
0
 public static bool ip_addr_netmask_valid(ip_addr netmask)
 {
     return ip_addr.ip4_addr_netmask_valid((netmask).addr);
 }
Beispiel #33
0
 /** Set address to IPADDR_ANY (no need for lwip.lwip_htonl()) */
 public static void ip_addr_set_any(ip_addr ipaddr)
 {
     ipaddr.addr = IPADDR_ANY;
 }
Beispiel #34
0
        /** Like ip.ip_output, but takes and addr_hint pointer that is passed on to netif.addr_hint
         *  before calling ip_output_if.
         *
         * @param p the packet to send (p.payload points to the data, e.g. next
                    protocol header; if dest == ip.IP_HDRINCL, p already includes an IP
                    header and p.payload points to that IP header)
         * @param src the source IP address to send from (if src == IP_ADDR_ANY, the
         *         IP  address of the netif used to send is used as source address)
         * @param dest the destination IP address to send the packet to
         * @param ttl the TTL value to be set in the IP header
         * @param tos the TOS value to be set in the IP header
         * @param proto the PROTOCOL to be set in the IP header
         * @param addr_hint address hint pointer set to netif.addr_hint before
         *        calling ip_output_if()
         *
         * @return err_t.ERR_RTE if no route is found
         *         see ip_output_if() for more return values
         */
        public err_t ip_output_hinted(pbuf p, ip_addr src, ip_addr dest,
			byte ttl, byte tos, byte proto, object addr_hint)
        {
            err_t err;

            /* pbufs passed to IP must have a @ref-count of 1 as their payload pointer
               gets altered as the packet is passed down the stack */
            lwip.LWIP_ASSERT("p.ref == 1", p.@ref == 1);

            err = ip_output_if(p, src, dest, ttl, tos, proto);

            return err;
        }
Beispiel #35
0
 /** For backwards compatibility */
 public static string ip_ntoa(ip_addr ipaddr)
 {
     return ipaddr_ntoa(ipaddr);
 }
Beispiel #36
0
 public static bool ip_addr_isbroadcast(ip_addr ipaddr, lwip netif)
 {
     return(ip_addr.ip4_addr_isbroadcast(ipaddr.addr, netif));
 }
Beispiel #37
0
 /**
  * Convert numeric IP address into decimal dotted ASCII representation.
  * returns ptr to static buffer; not reentrant!
  *
  * @param addr ip address in network order to convert
  * @return pointer to a global static (!) buffer that holds the ASCII
  *         represenation of addr
  */
 public static string ipaddr_ntoa(ip_addr addr)
 {
     char[] str = new char[16];
     return ipaddr_ntoa_r(addr, str);
 }
Beispiel #38
0
 static ip_addr()
 {
     IP_ADDR_ANY = ip_addr_any;
     IP_ADDR_BROADCAST = ip_addr_broadcast;
 }
Beispiel #39
0
 public static bool ip_addr_cmp(ip_addr addr1, ip_addr addr2)
 {
     return ((addr1).addr == (addr2).addr);
 }
Beispiel #40
0
 /** Set an IP address given by the four byte-parts.
     Little-endian version that prevents the use of lwip.lwip_htonl. */
 public static void IP4_ADDR(ip_addr ipaddr, byte a, byte b, byte c, byte d)
 {
     ipaddr.addr = ((uint)((d) & 0xff) << 24) |
                   ((uint)((c) & 0xff) << 16) |
                   ((uint)((b) & 0xff) << 8) |
                    (uint)((a) & 0xff);
 }
Beispiel #41
0
 public static void ip_addr_debug_print(uint debug, ip_addr ipaddr)
 {
     uITron3.lwip.LWIP_DEBUGF(debug, "{0}.{1}.{2}.{3}",
                         ipaddr != null ? ip_addr.ip4_addr1_16(ipaddr) : 0,
                         ipaddr != null ? ip_addr.ip4_addr2_16(ipaddr) : 0,
                         ipaddr != null ? ip_addr.ip4_addr3_16(ipaddr) : 0,
                         ipaddr != null ? ip_addr.ip4_addr4_16(ipaddr) : 0);
 }
Beispiel #42
0
 public static byte ip4_addr4(ip_addr ipaddr)
 {
     return BitConverter.GetBytes(ipaddr.addr)[3];
 }
Beispiel #43
0
 public static bool ip_addr_isany(ip_addr addr1)
 {
     return ((addr1) == null || (addr1).addr == IPADDR_ANY);
 }
Beispiel #44
0
 public static bool ip_addr_isany(ip_addr addr1)
 {
     return((addr1) == null || (addr1).addr == IPADDR_ANY);
 }
Beispiel #45
0
 public static bool ip_addr_islinklocal(ip_addr addr1)
 {
     return (((addr1).addr & lwip.PP_HTONL(0xffff0000U)) == lwip.PP_HTONL(0xa9fe0000U));
 }
Beispiel #46
0
 public static bool ip_addr_cmp(ip_addr addr1, ip_addr addr2)
 {
     return((addr1).addr == (addr2).addr);
 }
Beispiel #47
0
 /**
  * Determine if two address are on the same network.
  *
  * @arg addr1 IP address 1
  * @arg addr2 IP address 2
  * @arg mask network identifier mask
  * @return !0 if the network identifiers of both address match
  */
 public static bool ip_addr_netcmp(ip_addr addr1, ip_addr addr2, ip_addr mask)
 {
     return (((addr1).addr &
         (mask).addr) ==
         ((addr2).addr &
         (mask).addr));
 }
Beispiel #48
0
 /** IPv4 only: set the IP address given as an uint */
 public static void ip4_addr_set_u32(ip_addr dest_ipaddr, uint src_u32)
 {
     dest_ipaddr.addr = src_u32;
 }
Beispiel #49
0
 /** Safely copy one IP address to another (src may be null) */
 public static void ip_addr_set(ip_addr dest, ip_addr src)
 {
     dest.addr = (src == null ? 0 : src.addr);
 }
Beispiel #50
0
 /** MEMCPY-like copying of IP addresses where addresses are known to be
  * 16-bit-aligned if the port is correctly configured (so a port could define
  * this to copying 2 ushort's) - no null-pointer-checking needed. */
 public static void IPADDR2_COPY(ip_addr dest, ip_addr src)
 {
     dest.addr = src.addr;
 }
Beispiel #51
0
 /** Safely copy one IP address to another and change byte order
  * from host- to network-order. */
 public static void ip_addr_set_hton(ip_addr dest, ip_addr src)
 {
     dest.addr =
         (src == null ? 0U :
         lwip.lwip_htonl(src.addr));
 }
Beispiel #52
0
        /**
         * Ascii internet address interpretation routine.
         * The value returned is in network order.
         *
         * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
         * @return ip address in network order
         */
        public static uint ipaddr_addr(string cp)
        {
            ip_addr val = new ip_addr(0);

            if (ipaddr_aton(cp, val) != 0) {
                return ip_addr.ip4_addr_get_u32(val);
            }
            return (IPADDR_NONE);
        }
Beispiel #53
0
 /** Set complete address to zero */
 public static void ip_addr_set_zero(ip_addr ipaddr)
 {
     ipaddr.addr = 0;
 }
Beispiel #54
0
        /**
         * Check whether "cp" is a valid ascii representation
         * of an Internet address and convert to a binary address.
         * Returns 1 if the address is valid, 0 if not.
         * This replaces inet_addr, the return value from which
         * cannot distinguish between failure and a local broadcast address.
         *
         * @param cp IP address in ascii represenation (e.g. "127.0.0.1")
         * @param addr pointer to which to save the ip address in network order
         * @return 1 if cp could be converted to addr, 0 on failure
         */
        public static int ipaddr_aton(string cp, ip_addr addr)
        {
            uint val;
            byte @base;
            char c;
            uint[] parts = new uint[4];
            int pp = 0;
            int p = 0;

            cp += "\0";
            c = cp[0];
            for (;;) {
                /*
                 * Collect number up to ``.''.
                 * Values are specified as for C:
                 * 0x=hex, 0=octal, 1-9=decimal.
                 */
                if (!isdigit(c))
                    return (0);
                val = 0;
                @base = 10;
                if (c == '0') {
                    c = cp[++p];
                    if (c == 'x' || c == 'X') {
                        @base = 16;
                        c = cp[++p];
                    }
                    else
                        @base = 8;
                }
                for (;;) {
                    if (isdigit(c)) {
                        val = (val * @base) + (uint)(c - (byte)'0');
                        c = cp[++p];
                    }
                    else if (@base == 16 && isxdigit(c)) {
                        val = (val << 4) | (uint)(c + 10 - (islower(c) ? (byte)'a' : (byte)'A'));
                        c = cp[++p];
                    }
                    else
                        break;
                }
                if (c == '.') {
                    /*
                     * Internet format:
                     *  a.b.c.d
                     *  a.b.c   (with c treated as 16 bits)
                     *  a.b (with b treated as 24 bits)
                     */
                    if (pp >= 3) {
                        return (0);
                    }
                    parts[pp++] = val;
                    c = cp[++p];
                }
                else
                    break;
            }
            /*
             * Check for trailing characters.
             */
            if (c != '\0' && !isspace(c)) {
                return (0);
            }
            /*
             * Concoct the address according to
             * the number of parts specified.
             */
            switch (pp + 1) {

            case 0:
                return (0);       /* initial nondigit */

            case 1:             /* a -- 32 bits */
                break;

            case 2:             /* a.b -- 8.24 bits */
                if (val > 0xffffffUL) {
                    return (0);
                }
                val |= parts[0] << 24;
                break;

            case 3:             /* a.b.c -- 8.8.16 bits */
                if (val > 0xffff) {
                    return (0);
                }
                val |= (parts[0] << 24) | (parts[1] << 16);
                break;

            case 4:             /* a.b.c.d -- 8.8.8.8 bits */
                if (val > 0xff) {
                    return (0);
                }
                val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
                break;
            default:
                lwip.LWIP_ASSERT("unhandled", false);
                break;
            }
            if (addr != null) {
                ip_addr.ip4_addr_set_u32(addr, lwip.lwip_htonl(val));
            }
            return (1);
        }
Beispiel #55
0
 internal static string to_string(ip_addr ipaddr)
 {
     return String.Format("{0}.{1}.{2}.{3}",
         ipaddr != null ? ip_addr.ip4_addr1_16(ipaddr) : 0,
         ipaddr != null ? ip_addr.ip4_addr2_16(ipaddr) : 0,
         ipaddr != null ? ip_addr.ip4_addr3_16(ipaddr) : 0,
         ipaddr != null ? ip_addr.ip4_addr4_16(ipaddr) : 0);
 }
Beispiel #56
0
 /** For backwards compatibility */
 public static string ip_ntoa(ip_addr ipaddr)
 {
     return(ipaddr_ntoa(ipaddr));
 }
Beispiel #57
0
        internal err_t ip_input(pbuf p, ip_addr src, ip_addr dest, byte proto)
        {
            ++lwip_stats.ip.recv;

            /* copy IP addresses to aligned ip_addr */
            ip_addr.ip_addr_copy(current_iphdr_dest, dest);
            ip_addr.ip_addr_copy(current_iphdr_src, src);

            /* broadcast or multicast packet source address? Compliant with RFC 1122: 3.2.1.3 */
#if IP_ACCEPT_LINK_LAYER_ADDRESSING
            /* DHCP servers need 0.0.0.0 to be allowed as source address (RFC 1.1.2.2: 3.2.1.3/a) */
            if (check_ip_src != 0 && !ip_addr.ip_addr_isany(ip.current_iphdr_src))
#endif // IP_ACCEPT_LINK_LAYER_ADDRESSING
            {
                if ((ip_addr.ip_addr_isbroadcast(current_iphdr_src, this)) ||
                    (ip_addr.ip_addr_ismulticast(current_iphdr_src)))
                {
                    /* packet source is not valid */
                    lwip.LWIP_DEBUGF(opt.IP_DEBUG | lwip.LWIP_DBG_TRACE | lwip.LWIP_DBG_LEVEL_WARNING, "ip_input: packet source is not valid.\n");
                    /* free (drop) packet pbufs */
                    pbuf_free(p);
                    ++lwip_stats.ip.drop;
                    //snmp.snmp_inc_ipinaddrerrors();
                    //snmp.snmp_inc_ipindiscards();
                    return(err_t.ERR_OK);
                }
            }

            /* send to upper layers */
            lwip.LWIP_DEBUGF(opt.IP_DEBUG, "ip_input: \n");
#if IP_DEBUG
            ip_debug_print(p);
#endif
            lwip.LWIP_DEBUGF(opt.IP_DEBUG, "ip_input: p.len {0} p.tot_len {1}\n", p.len, p.tot_len);

            //lwip.current_header = iphdr;

#if LWIP_RAW
            /* raw input did not eat the packet? */
            if (raw.raw_input(p, inp) == 0)
#endif // LWIP_RAW
            {
                switch (proto)
                {
#if LWIP_UDP
                case lwip.IP_PROTO_UDP:
#if LWIP_UDPLITE
                case lwip.IP_PROTO_UDPLITE:
#endif // LWIP_UDPLITE
                    //snmp.snmp_inc_ipindelivers();
                    udp.udp_input(p, this, proto == lwip.IP_PROTO_UDPLITE);
                    break;
#endif // LWIP_UDP
#if LWIP_TCP
                case lwip.IP_PROTO_TCP:
                    //snmp.snmp_inc_ipindelivers();
                    tcp.tcp_input(p, this);
                    break;
#endif // LWIP_TCP
#if LWIP_ICMP
                case lwip.IP_PROTO_ICMP:
                    //snmp.snmp_inc_ipindelivers();
                    lwip.icmp.icmp_input(p, inp);
                    break;
#endif // LWIP_ICMP
#if LWIP_IGMP
                case lwip.IP_PROTO_IGMP:
                    lwip.igmp.igmp_input(p, inp, lwip.current_iphdr_dest);
                    break;
#endif // LWIP_IGMP
                default:
#if LWIP_ICMP
                    /* send ICMP destination protocol unreachable unless is was a broadcast */
                    if (!ip_addr.ip_addr_isbroadcast(lwip.current_iphdr_dest, inp) &&
                        !ip_addr.ip_addr_ismulticast(lwip.current_iphdr_dest))
                    {
                        p.payload = iphdr;
                        lwip.icmp.icmp_dest_unreach(p, icmp_dur_type.ICMP_DUR_PROTO);
                    }
#endif // LWIP_ICMP
                    pbuf_free(p);

                    LWIP_DEBUGF(opt.IP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, "Unsupported transport protocol {0}\n", proto);

                    ++lwip_stats.ip.proterr;
                    ++lwip_stats.ip.drop;
                    //snmp.snmp_inc_ipinunknownprotos();
                    break;
                }
            }

            //lwip.current_header = null;
            ip_addr.ip_addr_set_any(current_iphdr_src);
            ip_addr.ip_addr_set_any(current_iphdr_dest);

            return(err_t.ERR_OK);
        }
Beispiel #58
0
 /** IPv4 only: get the IP address as an uint */
 public static uint ip4_addr_get_u32(ip_addr src_ipaddr)
 {
     return src_ipaddr.addr;
 }
Beispiel #59
0
 public static ushort ip4_addr4_16(ip_addr ipaddr)
 {
     return ((ushort)ip_addr.ip4_addr4(ipaddr));
 }
Beispiel #60
0
        /**
         * Determine if an address is a broadcast address on a network interface
         *
         * @param addr address to be checked
         * @param netif the network interface against which the address is checked
         * @return returns non-zero if the address is a broadcast address
         */
        public static bool ip4_addr_isbroadcast(uint addr, lwip netif)
        {
            ip_addr ipaddr = new ip_addr(0);
            ip_addr.ip4_addr_set_u32(ipaddr, addr);

            /* all ones (broadcast) or all zeroes (old skool broadcast) */
            if ((~addr == IPADDR_ANY) ||
                (addr == IPADDR_ANY)) {
                return true;
                /* no broadcast support on this network interface? */
            }
            else if ((netif.flags & lwip.NETIF_FLAG_BROADCAST) == 0) {
                /* the given address cannot be a broadcast address
                 * nor can we check against any broadcast addresses */
                return false;
                /* address matches network interface address exactly? => no broadcast */
            }
            else if (addr == ip_addr.ip4_addr_get_u32(netif.ip_addr)) {
                return false;
                /*  on the same (sub) network... */
            }
            else if (ip_addr.ip_addr_netcmp(ipaddr, netif.ip_addr, netif.netmask)
                  /* ...and host identifier bits are all ones? =>... */
                  && ((addr & ~ip_addr.ip4_addr_get_u32(netif.netmask)) ==
                   (IPADDR_BROADCAST & ~ip_addr.ip4_addr_get_u32(netif.netmask)))) {
                /* => network broadcast address */
                return true;
            }
            else {
                return false;
            }
        }