Exemplo n.º 1
0
        public IntPtr ConnectTo(String host, ushort port, TNET_Socket.tnet_socket_type_t type)
        {
            if (!mStarted)
            {
                TSK_Debug.Error("Transport not started");
                return(IntPtr.Zero);
            }

            if (this.Type != type)
            {
                TSK_Debug.Error("Master/destination types mismatch [{0}/{1}]", this.Type, type);
                return(IntPtr.Zero);
            }

            EndPoint endpoint = TNET_Socket.CreateEndPoint(host, port);

            if (endpoint != null)
            {
                //var args = new SocketAsyncEventArgs();
                //args.RemoteEndPoint = endpoint;
                //args.Completed += this.SocketAsyncEventArgs_Callback;
                // args.SetBuffer(buffer, 0, buffer.Length);

                return(IntPtr.Zero);
            }

            return(IntPtr.Zero);
        }
Exemplo n.º 2
0
 public Boolean GetLocalIpAndPort(Int64 socketId, out String ip, out ushort port)
 {
     if (mSockets.ContainsKey(socketId))
     {
         TNET_Socket socket = mSockets[socketId];
         return(socket.GetLocalIpAndPort(out ip, out port));
     }
     return(mMasterSocket.GetLocalIpAndPort(out ip, out port));
 }
Exemplo n.º 3
0
        public TNET_Transport(String host, ushort port, TNET_Socket.tnet_socket_type_t type, String description)
        {
            mHost        = host;
            mPort        = port;
            mType        = type;
            mDescription = description;

            mSockets = new Dictionary <Int64, TNET_Socket>();

            mMasterSocket = new TNET_Socket(host, port, type);
            if (mMasterSocket != null)
            {
                mMasterSocket.RecvSocketCompleted += this.SocketAsyncEventArgs_Callback;
                mSockets.Add(mMasterSocket.Id, mMasterSocket);
            }
        }
Exemplo n.º 4
0
 public Int32 SendTo(Int64 localSocket, EndPoint remoteEP, byte[] buffer)
 {
     if (mSockets.ContainsKey(localSocket))
     {
         TNET_Socket socketFrom = mSockets[localSocket];
         if (socketFrom != null)
         {
             return(socketFrom.SendTo(remoteEP, buffer));
         }
     }
     else
     {
         TSK_Debug.Error("Cannot find socket with handle = {0}", localSocket);
     }
     return(-1);
 }
Exemplo n.º 5
0
        public TNET_Transport(String host, ushort port, TNET_Socket.tnet_socket_type_t type, String description)
        {
            mHost = host;
            mPort = port;
            mType = type;
            mDescription = description;

            mSockets = new Dictionary<Int64, TNET_Socket>();

            mMasterSocket = new TNET_Socket(host, port, type);
            if (mMasterSocket != null)
            {
                mMasterSocket.RecvSocketCompleted += this.SocketAsyncEventArgs_Callback;
                mSockets.Add(mMasterSocket.Id, mMasterSocket);
            }
        }
Exemplo n.º 6
0
        public static IPAddress GetBestLocalIPAddress(String host, TNET_Socket.tnet_socket_type_t type)
        {
            try
            {
#if WINDOWS_PHONE
                String      localHostNameOrAddress = (host == TNET_Socket.TNET_SOCKET_HOST_ANY) ? "localhost" : host;                      // Not use yet
                IPAddress[] ipAddresses            = new IPAddress[] { TNET_Socket.IsIPv6Type(type) ? IPAddress.IPv6Any : IPAddress.Any }; // FIXME: didn't found how to get local IP on WP7
#else
                String      localHostNameOrAddress = (host == TNET_Socket.TNET_SOCKET_HOST_ANY) ? Dns.GetHostName() : host;
                IPAddress[] ipAddresses            = Dns.GetHostAddresses(localHostNameOrAddress);
#endif
                IPAddress ipAddress = null;
                Boolean   useIPv6   = TNET_Socket.IsIPv6Type(type);
                if (ipAddresses != null && ipAddresses.Length > 0)
                {
                    ipAddress = ipAddresses[0];
                    foreach (IPAddress ia in ipAddresses)
                    {
                        if ((ia.AddressFamily == AddressFamily.InterNetwork && (IPAddress.Loopback.ToString().Equals(ia.ToString()))) ||
                            (ia.AddressFamily == AddressFamily.InterNetworkV6 && (IPAddress.IPv6Loopback.ToString().Equals(ia.ToString()))))
                        {
                            continue;
                        }
                        if ((ia.AddressFamily == AddressFamily.InterNetwork && !useIPv6) || (ia.AddressFamily == AddressFamily.InterNetworkV6 && !ia.IsIPv6LinkLocal && useIPv6))
                        {
                            ipAddress = ia;
                            break;
                        }
                    }
                }

                if (ipAddress == null)
                {
                    TSK_Debug.Error("{0} is an invalid hostname or address", localHostNameOrAddress);
                    ipAddress = TNET_Socket.IsIPv6Type(type) ? IPAddress.IPv6Any : IPAddress.Any;
                }

                return(ipAddress);
            }
            catch (Exception e)
            {
                TSK_Debug.Error("GetBestLocalIPAddress(host={0}) failed: ", host, e);
            }
            return(TNET_Socket.IsIPv6Type(type) ? IPAddress.IPv6Any : IPAddress.Any);
        }
Exemplo n.º 7
0
        public static IPAddress GetBestLocalIPAddress(String host, TNET_Socket.tnet_socket_type_t type)
        {
            try
            {
            #if WINDOWS_PHONE
                String localHostNameOrAddress = (host == TNET_Socket.TNET_SOCKET_HOST_ANY) ? "localhost" : host; // Not use yet
                IPAddress[] ipAddresses = new IPAddress[] { TNET_Socket.IsIPv6Type(type) ? IPAddress.IPv6Any : IPAddress.Any }; // FIXME: didn't found how to get local IP on WP7
            #else
                String localHostNameOrAddress = (host == TNET_Socket.TNET_SOCKET_HOST_ANY) ? Dns.GetHostName() : host;
                IPAddress[] ipAddresses = Dns.GetHostAddresses(localHostNameOrAddress);
            #endif
                IPAddress ipAddress = null;
                Boolean useIPv6 = TNET_Socket.IsIPv6Type(type);
                if (ipAddresses != null && ipAddresses.Length > 0)
                {
                    ipAddress = ipAddresses[0];
                    foreach (IPAddress ia in ipAddresses)
                    {
                        if ((ia.AddressFamily == AddressFamily.InterNetwork && (IPAddress.Loopback.ToString().Equals(ia.ToString())))
                            || (ia.AddressFamily == AddressFamily.InterNetworkV6 && (IPAddress.IPv6Loopback.ToString().Equals(ia.ToString()))))
                        {
                            continue;
                        }
                        if ((ia.AddressFamily == AddressFamily.InterNetwork && !useIPv6) || (ia.AddressFamily == AddressFamily.InterNetworkV6 && !ia.IsIPv6LinkLocal && useIPv6))
                        {
                            ipAddress = ia;
                            break;
                        }
                    }
                }

                if (ipAddress == null)
                {
                    TSK_Debug.Error("{0} is an invalid hostname or address", localHostNameOrAddress);
                    ipAddress = TNET_Socket.IsIPv6Type(type) ? IPAddress.IPv6Any : IPAddress.Any;
                }

                return ipAddress;
            }
            catch (Exception e)
            {
                TSK_Debug.Error("GetBestLocalIPAddress(host={0}) failed: ", host, e);
            }
            return TNET_Socket.IsIPv6Type(type) ? IPAddress.IPv6Any : IPAddress.Any;
        }
Exemplo n.º 8
0
        private void SocketAsyncEventArgs_Callback(object sender, SocketAsyncEventArgs e)
        {
            if (e.SocketError != SocketError.Success)
            {
                // FIXME: cleanup
                return;
            }

            if (NetworkEvent == null)
            {
                return;
            }

            switch (e.LastOperation)
            {
            case SocketAsyncOperation.Receive:
            case SocketAsyncOperation.ReceiveFrom:
            {
                TNET_Socket tskSocket = (e.UserToken as TNET_Socket);

                if (e.Buffer != null)
                {
                    byte[] bytes = new byte[e.BytesTransferred];
                    Buffer.BlockCopy(e.Buffer, 0, bytes, 0, bytes.Length);
                    TransportEventArgs eargs = new TransportEventArgs(TransportEventArgs.TransportEventTypes.Data, bytes, this, tskSocket.Id);
                    NetworkEvent(this, eargs);
                }
                // enqueue another event args
                tskSocket.ScheduleReceiveFromAsync();
                break;
            }

            case SocketAsyncOperation.SendTo:
            default:
            {
                break;
            }
            }
        }
Exemplo n.º 9
0
        public IntPtr ConnectTo(String host, ushort port, TNET_Socket.tnet_socket_type_t type)
        {
            if (!mStarted)
            {
                TSK_Debug.Error("Transport not started");
                return IntPtr.Zero;
            }

            if (this.Type != type)
            {
                TSK_Debug.Error("Master/destination types mismatch [{0}/{1}]", this.Type, type);
                return IntPtr.Zero;
            }

            EndPoint endpoint = TNET_Socket.CreateEndPoint(host, port);
            if (endpoint != null)
            {
                //var args = new SocketAsyncEventArgs();
                //args.RemoteEndPoint = endpoint;
                //args.Completed += this.SocketAsyncEventArgs_Callback;
                // args.SetBuffer(buffer, 0, buffer.Length);

                return IntPtr.Zero;
            }

            return IntPtr.Zero;
        }
        internal Boolean AddTransport(String localHost, ushort localPort, TNET_Socket.tnet_socket_type_t type, String description)
        {
            TSIP_Transport transport = TNET_Socket.IsStreamType(type) ? (TSIP_Transport)new TSIP_TransportTCP(mStack, localHost, localPort, TNET_Socket.IsIPv6Type(type), description)
                : (TSIP_Transport)new TSIP_TransportUDP(mStack, localHost, localPort, TNET_Socket.IsIPv6Type(type), description);

            if (transport != null)
            {
                // FIXME:DO not forget to call "transport.NetworkEvent -= this.TSIP_Transport_NetworkEvent;" before removing the transport
                transport.NetworkEvent += this.TSIP_Transport_NetworkEvent;
                mTransports.Add(transport);
            }

            return (transport != null);
        }
Exemplo n.º 11
0
        public TNET_Socket(String host, ushort port, tnet_socket_type_t type, Boolean nonblocking, Boolean bindsocket)
        {
            mId          = ++sUniqueId;
            mType        = type;
            mHost        = host;
            mPort        = port;
            mSocketEvent = new ManualResetEvent(false);

            mSendSocketEventArgs            = new SocketAsyncEventArgs();
            mSendSocketEventArgs.Completed += delegate(object sender, SocketAsyncEventArgs e)
            {
                if (e.SocketError != SocketError.Success)
                {
                    mSocketEvent.Set();
                    return;
                }

                // On WP7 Socket.ReceiveFromAsync() will only works after at least ONE SendTo()
                if (e.LastOperation == SocketAsyncOperation.SendTo)
                {
                    if (mRecvSocketEventArgs == null && RecvSocketCompleted != null)
                    {
                        mRecvSocketEventArgs = new SocketAsyncEventArgs();

                        // setting the remote endpoint will not filter on the remote host:port
                        // => will continue to work even if the remote port is different than 5060
                        // IPEndPoint is not serializable => create new one
                        mRecvSocketEventArgs.RemoteEndPoint = new IPEndPoint(IPAddress.Any, 5060); // e.RemoteEndPoint
                        mRecvSocketEventArgs.Completed     += this.RecvSocketCompleted;

                        mRecvSocketEventArgs.SetBuffer(buffers, 0, buffers.Length);
                        mRecvSocketEventArgs.UserToken = e.UserToken;
                        (mRecvSocketEventArgs.UserToken as TNET_Socket).Socket.ReceiveFromAsync(mRecvSocketEventArgs);
                    }
                }

                mSocketEvent.Set();
            };

            AddressFamily addressFamily = TNET_Socket.IsIPv6Type(type) ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
            SocketType    socketType    = TNET_Socket.IsStreamType(type) ? SocketType.Stream : SocketType.Dgram;

#if WINDOWS_PHONE
            ProtocolType protocolType = TNET_Socket.IsStreamType(type) ? ProtocolType.Tcp : ProtocolType.Udp;
#else
            ProtocolType protocolType = TNET_Socket.IsIPv6Type(type) ? ProtocolType.IP : ProtocolType.IP; // Leave it like this
#endif
            mSocket = new Socket(addressFamily, socketType, protocolType);
#if !WINDOWS_PHONE
            mSocket.Blocking            = !nonblocking;
            mSocket.UseOnlyOverlappedIO = true;
#endif

            mSendSocketEventArgs.UserToken = this;

            if (bindsocket)
            {
                IPAddress ipAddress = TNET_Utils.GetBestLocalIPAddress(host, type);
                TSK_Debug.Info("Local address={0}", ipAddress);
                mHost = ipAddress.ToString();
                if (bindsocket)
                {
                    ushort     localPort = (port == TNET_SOCKET_PORT_ANY) ? (ushort)0 : port;
                    IPEndPoint localIEP  = new IPEndPoint(ipAddress, localPort);
#if !WINDOWS_PHONE
                    mSocket.Bind(localIEP);
#endif
                    if (TNET_Socket.IsDatagramType(type))
                    {
 #if !WINDOWS_PHONE
                        mPort = (ushort)(mSocket.LocalEndPoint as IPEndPoint).Port;
#endif
                    }
                }
            }
        }