Ejemplo n.º 1
0
        internal override int ReceiveFrom(byte[]      buffer,
                                          int offset,
                                          int size,
                                          SocketFlags socketFlags,
                                          ref EndPoint remoteEP)
        {
            ValidateBufferOffsets(buffer, offset, size);
            if (socketFlags != SocketFlags.None)
            {
                // Invalid flags for this operation
                throw new SocketException(SocketErrors.WSAEINVAL);
            }

            UdpConnectionContract /*.Imp*/ conn = (UdpConnectionContract)m_Conn.Acquire();

            try {
                if (conn.InState(UdpConnectionContract.State.Closed /*.Value*/))
                {
                    throw new SocketException(BAD_SOCKET_STATE_ERR);
                }

                Bytes  rdata = conn.Read();
                uint   addr  = 0;
                ushort port  = 0;
                remoteEP = new IPEndPoint(new IPAddress(addr), port);
                int amount = Math.Min(rdata.Length, size);
                Bitter.ToByteArray(rdata, 0, amount, buffer, offset);
                return(amount);
            }
            finally {
                m_Conn.Release(conn);
            }
        }
Ejemplo n.º 2
0
        //way too many copies.  But for now we'll live with it.
        private StatusCode AskDnsServer(IPv4 dnsServer,
                                        byte[]    outData,
                                        out byte[] rcvData)
        {
            UDP udp = new UDP();

            udp.Bind(IPv4.Any, 0);
            udp.Connect(dnsServer, Dns.Format.ServerPort);

            Bytes packet = Bitter.FromByteArray(outData);

            udp.WriteData(packet);

            Bytes buffer;

            buffer = udp.PollReadData(TimeSpan.FromTicks(timeout.Ticks));

            //This is silly..I'll come back and clean this up.
            udp.Close();

            if (buffer == null)
            {
                rcvData = null;
                return(StatusCode.Timeout);
            }
            rcvData = new byte[buffer.Length];
            Bitter.ToByteArray(buffer, 0, buffer.Length, rcvData, 0);
            //delete buffer;

            return(StatusCode.Success);
        }
Ejemplo n.º 3
0
/*
 *      public void GetFragmentRange(out UIntPtr virtualAddr,
 *                                   out int lengthBytes)
 *      {
 *          {
 *              if (this.data != null) {
 *                  virtualAddr = Bitter.ToAddress(this.data, this.start);
 *                  lengthBytes = this.length;
 *              }
 *              else {
 *                  virtualAddr = UIntPtr.Zero;
 *                  lengthBytes = 0;
 *              }
 *          }
 *      }
 */

        public int Copy(byte[] destination, int offset)
        {
            {
                if (this.data != null)
                {
                    Bitter.ToByteArray(this.data, this.start, this.length,
                                       destination, offset);
                    return(this.length);
                }
                else
                {
                    return(0);
                }
            }
        }
Ejemplo n.º 4
0
 public static SortedList Parse(Bytes buffer)
 {
     // This is an unnecessary copy
     byte [] data = Bitter.ToByteArray(buffer);
     return(Parse(data, 0, data.Length));
 }
Ejemplo n.º 5
0
        public static DhcpFormat Parse(Bytes buffer)
        {
            DhcpFormat p = new DhcpFormat(BootType.NotSpecified);

            p.optionsUsedLength = 0;

            if (buffer.Length < DhcpFormat.MinLength)
            {
                throw new InvalidDhcpFormatException("Format less than minimum size");
            }
            int offset = 0;

            p.op = buffer[offset++];
            if (p.op != (byte)BootType.Request &&
                p.op != (byte)BootType.Reply)
            {
                throw new InvalidDhcpFormatException("Bad Type (op = {0})", p.op);
            }
            p.htype = buffer[offset++];
            // No check

            p.hlen = buffer[offset++];
            if (p.hlen > HardwareAddressLength)
            {
                throw new InvalidDhcpFormatException("Bad address length (hlen {0})",
                                                     p.hlen);
            }
            p.hops = buffer[offset++];
            // No check

            p.xid   = NetworkBitConverter.ToUInt32(buffer, offset);
            offset += 4;

            p.secs  = NetworkBitConverter.ToUInt16(buffer, offset);
            offset += 2;

            p.flags = NetworkBitConverter.ToUInt16(buffer, offset);
            offset += 2;

            p.ciaddr = NetworkBitConverter.ToUInt32(buffer, offset);
            offset  += 4;

            p.yiaddr = NetworkBitConverter.ToUInt32(buffer, offset);
            offset  += 4;

            p.siaddr = NetworkBitConverter.ToUInt32(buffer, offset);
            offset  += 4;

            p.giaddr = NetworkBitConverter.ToUInt32(buffer, offset);
            offset  += 4;

            Bitter.ToByteArray(buffer, offset, HardwareAddressLength, p.chaddr, 0);
            offset += HardwareAddressLength;
            Bitter.ToByteArray(buffer, offset, ServerNameLength, p.sname, 0);
            offset += ServerNameLength;
            Bitter.ToByteArray(buffer, offset, BootFileLength, p.file, 0);
            offset += BootFileLength;

            p.cookie = NetworkBitConverter.ToUInt32(buffer, offset);
            offset  += 4;
            if (p.cookie != DhcpCookie)
            {
                throw new InvalidDhcpFormatException("Bad cookie (0x{0x:x8})",
                                                     p.cookie);
            }
            int available = buffer.Length - offset;

            if (available > p.options.Length)
            {
                p.options = new byte [available];
            }
            p.optionsUsedLength = available;
            Bitter.ToByteArray(buffer, offset, available, p.options, 0);

            return(p);
        }