Ejemplo n.º 1
0
        /// <summary>
        /// Start kmod and connect to it.
        /// </summary>
        private void StartKmod()
        {
            FileStream  file       = null;
            Socket      listenSock = null;
            RegistryKey kwmRegKey  = null;

            try
            {
                // Get the path to the kmod executable in the registry.
                kwmRegKey = KwmReg.GetKwmLMRegKey();
                String Kmod = "\"" + (String)kwmRegKey.GetValue("InstallDir", @"C:\Program Files\Teambox\Teambox Manager") + "\\kmod\\kmod.exe\"";

                // The directory where KMOD will save logs and its database for use with the kwm.
                String KmodDir = KwmPath.GetKmodDirPath();
                Directory.CreateDirectory(KmodDir);

                // Start listening for kmod to connect when it'll be started.
                IPEndPoint endPoint = new IPEndPoint(IPAddress.Loopback, 0);
                listenSock = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                listenSock.Bind(endPoint);
                listenSock.Listen(1);
                int port = ((IPEndPoint)listenSock.LocalEndPoint).Port;

                // Start KMOD in debugging mode if our settings say so.
                String debug   = KwmCfg.Cur.KtlstunnelLoggingLevel > 0 ? " -l 3" : "";
                String args    = " -C kmod_connect -p " + port + debug + " -m 20000 -k \"" + KmodDir + "\"";
                String cmdLine = Kmod + args;
                KLogging.Log("About to start kmod.exe: " + cmdLine);
                m_kmodProc = new KProcess(cmdLine);
                m_kmodProc.InheritHandles = false;
                m_kmodProc.CreationFlags  = (uint)KSyscalls.CREATION_FLAGS.CREATE_NO_WINDOW;
                m_kmodProc.Start();

                // Wait for KMOD to connect for about 10 seconds.
                DateTime startTime = DateTime.Now;
                while (true)
                {
                    SelectSockets select = new SelectSockets();
                    select.AddRead(listenSock);
                    SetSelectTimeout(startTime, 10000, select, "no KMOD connection received");
                    Block(select);

                    if (select.InRead(listenSock))
                    {
                        m_kmodSock          = listenSock.Accept();
                        m_kmodSock.Blocking = false;
                        break;
                    }
                }

                // Read the authentication data.
                byte[] authSockData = new byte[32];
                byte[] authFileData = new byte[32];

                startTime = DateTime.Now;
                int nbRead = 0;
                while (nbRead != 32)
                {
                    SelectSockets select = new SelectSockets();
                    select.AddRead(m_kmodSock);
                    SetSelectTimeout(startTime, 2000, select, "no authentication data received");
                    Block(select);
                    int r = KSocket.SockRead(m_kmodSock, authSockData, nbRead, 32 - nbRead);
                    if (r > 0)
                    {
                        nbRead += r;
                    }
                }

                file = File.Open(KmodDir + "\\connect_secret", FileMode.Open);
                file.Read(authFileData, 0, 32);
                if (!KUtil.ByteArrayEqual(authFileData, authSockData))
                {
                    throw new Exception("invalid authentication data received");
                }

                // Set the transport.
                m_transport = new K3pTransport(m_kmodSock);
            }

            finally
            {
                if (file != null)
                {
                    file.Close();
                }
                if (listenSock != null)
                {
                    listenSock.Close();
                }
                if (kwmRegKey != null)
                {
                    kwmRegKey.Close();
                }
            }
        }
Ejemplo n.º 2
0
        public void doXfer()
        {
            bool loop = true;

            while (loop)
            {
                loop = false;

                if (inState == InState.RecvHdr)
                {
                    int r = KSocket.SockRead(sock, inBuf, inPos, inBuf.Length - inPos);

                    if (r > 0)
                    {
                        loop   = true;
                        inPos += r;

                        if (inPos == inBuf.Length)
                        {
                            inMsg = new K3pElement();

                            inMsg.ParseType(inBuf);

                            switch (inMsg.Type)
                            {
                            case K3pElement.K3pType.INS:
                                inState = InState.RecvIns;
                                inPos   = 0;
                                inBuf   = new byte[8];
                                break;

                            case K3pElement.K3pType.INT:
                                inState = InState.RecvInt;
                                inPos   = 0;
                                inBuf   = new byte[20];
                                break;

                            case K3pElement.K3pType.STR:
                                inState = InState.RecvStrSize;
                                inBuf   = new byte[20];
                                inPos   = 0;
                                break;
                            }
                        }
                    }
                }

                if (inState == InState.RecvIns)
                {
                    int r = KSocket.SockRead(sock, inBuf, inPos, inBuf.Length - inPos);

                    if (r > 0)
                    {
                        loop   = true;
                        inPos += r;

                        if (inPos == inBuf.Length)
                        {
                            inMsg.ParseIns(inBuf);
                            inState = InState.Received;
                        }
                    }
                }

                if (inState == InState.RecvInt || inState == InState.RecvStrSize)
                {
                    int r = KSocket.SockRead(sock, inBuf, inPos, 1);

                    if (r > 0)
                    {
                        loop   = true;
                        inPos += r;

                        if ((char)inBuf[inPos - 1] == '>')
                        {
                            byte[] tmpInBuf = new byte[inPos];
                            for (int i = 0; i < inPos; i++)
                            {
                                tmpInBuf[i] = inBuf[i];
                            }
                            inMsg.ParseInt(tmpInBuf);
                            if (inState == InState.RecvStrSize)
                            {
                                inState = InState.RecvStr;
                                inBuf   = new byte[inMsg.Int];
                                inPos   = 0;

                                if (inMsg.Int == 0)
                                {
                                    inMsg.ParseStr(inBuf);
                                    inState = InState.Received;
                                }
                            }
                            else
                            {
                                inState = InState.Received;
                            }
                        }
                        else if (inPos == inBuf.Length)
                        {
                            throw new K3pException("Expecting a '>' to end an INT in k3p message");
                        }
                    }
                }

                if (inState == InState.RecvStr)
                {
                    int r = KSocket.SockRead(sock, inBuf, inPos, inBuf.Length - inPos);

                    if (r > 0)
                    {
                        loop   = true;
                        inPos += r;

                        if (inPos == inBuf.Length)
                        {
                            inMsg.ParseStr(inBuf);
                            inState = InState.Received;
                        }
                    }
                }

                if (outState == OutState.Sending)
                {
                    int r = KSocket.SockWrite(sock, outBuf, outPos, outBuf.Length - outPos);

                    if (r > 0)
                    {
                        loop    = true;
                        outPos += r;

                        if (outPos == outBuf.Length)
                        {
                            outState = OutState.NoPacket;
                            break;
                        }
                    }
                }
            }
        }