Beispiel #1
0
        public bool Data(Mail mail)
        {
            //トランザクションでない場合エラー
            if (Status != SmtpClientStatus.Transaction)
            {
                SetLastError("Data() Status != Transaction");
                return(false);
            }
            //DATA送信
            if (!SendCmd("DATA"))
            {
                return(false);
            }
            //354受信
            if (!RecvStatus(354))
            {
                return(false);
            }
            var lines = Inet.GetLines(mail.GetBytes());

            foreach (var l in lines)
            {
                //ドットのみの行の場合、ドットを追加する
                if (l.Length == 3 && l[0] == '.' && l[1] == '\r' && l[2] == '\n')
                {
                    var buf = new byte[1] {
                        l[0]
                    };
                    _sockTcp.Send(buf);
                }
                if (l.Length != _sockTcp.Send(l))
                {
                    SetLastError(String.Format("Faild in SmtpClient Data()"));
                    ConfirmConnect();//接続確認
                    return(false);
                }
            }
            //最終行が改行で終わっているかどうかの確認
            var last = lines[lines.Count - 1];

            if (last.Length < 2 || last[last.Length - 2] != '\r' || last[last.Length - 1] != '\n')
            {
                SendCmd("");//改行を送る
            }
            if (!SendCmd("."))
            {
                return(false);
            }
            //250受信
            if (!RecvStatus(250))
            {
                return(false);
            }
            return(true);
        }
Beispiel #2
0
        private int SendBinary(SockTcp sockTcp, String fileName)
        {
            var sb = new StringBuilder();

            sb.Append(string.Format("SendBinary({0}) ", fileName));

            int size = 0;

            using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)){
                using (var br = new BinaryReader(fs)){
                    var buf = new byte[3000000];
                    while (IsLife())
                    {
                        var len = br.Read(buf, 0, 3000000);
                        if (len <= 0)
                        {
                            break;
                        }
                        //if (oneSsl != null) {
                        //}else{
                        sockTcp.Send(buf, len);
                        //}
                        //�g���[�X�\��
                        sb.Append(string.Format("Binary={0}byte ", len));
                        size += len;
                    }
                }
            }

            //noEncode = true; //�o�C�i���ł��鎖���������Ă���
            //Trace(TraceKind.Send, Encoding.ASCII.GetBytes(sb.ToString()), true); //�g���[�X�\��
            return(size);
        }
Beispiel #3
0
        public void Echoサーバに送信して溜まったデータサイズ_lengthを確認する()
        {
            //setUp
            const string addr = "127.0.0.1";
            const int    port = 9982;
            var          sv   = new EchoServer(addr, port);

            sv.Start();

            var       sut = new SockTcp(new Kernel(), new Ip(addr), port, 100, null);
            const int max = 1000;

            for (int i = 0; i < 3; i++)
            {
                sut.Send(new byte[max]);
            }

            Thread.Sleep(200);

            int expected = max * 3;

            //exercise
            var actual = sut.Length();

            //verify
            Assert.That(actual, Is.EqualTo(expected));

            //tearDown
            sut.Close();
            sv.Stop();
        }
Beispiel #4
0
        public void OneServerを継承したEchoServer_TCP版_を使用して接続する()
        {
            const string addr    = "127.0.0.1";
            const int    port    = 9999;
            const int    timeout = 300;
            Ip           ip      = null;

            try{
                ip = new Ip(addr);
            } catch (ValidObjException ex) {
                Assert.Fail(ex.Message);
            }
            var oneBind = new OneBind(ip, ProtocolKind.Tcp);
            var conf    = TestUtil.CreateConf("OptionSample");

            conf.Set("port", port);
            conf.Set("multiple", 10);
            conf.Set("acl", new Dat(new CtrlType[0]));
            conf.Set("enableAcl", 1);
            conf.Set("timeOut", timeout);

            var echoServer = new EchoServer(conf, oneBind);

            echoServer.Start();

            //TCPクライアント

            const int max = 10000;
            var       buf = new byte[max];

            buf[8] = 100; //CheckData
            for (int i = 0; i < 3; i++)
            {
                var sockTcp = new SockTcp(new Kernel(), ip, port, timeout, null);

                sockTcp.Send(buf);

                while (sockTcp.Length() == 0)
                {
                    Thread.Sleep(2);
                }

                var len = sockTcp.Length();
                if (0 < len)
                {
                    var b = sockTcp.Recv(len, timeout, this);
                    Assert.That(b[8], Is.EqualTo(buf[8]));//CheckData
                }
                Assert.That(max, Is.EqualTo(len));

                sockTcp.Close();
            }

            echoServer.Dispose();
        }
Beispiel #5
0
        public void Echoサーバにsendで送信てtcpQueueのlength分ずつRecvする()
        {
            const string addr = "127.0.0.1";
            const int    port = 9981;

            var echoServer = new EchoServer(addr, port);

            echoServer.Start();

            const int timeout = 100;
            Ip        ip      = null;

            try{
                ip = new Ip(addr);
            } catch (ValidObjException ex) {
                Assert.Fail(ex.Message);
            }
            var sockTcp = new SockTcp(new Kernel(), ip, port, timeout, null);

            const int max  = 1000;
            const int loop = 3;
            var       tmp  = new byte[max];

            for (var i = 0; i < max; i++)
            {
                tmp[i] = (byte)i;
            }

            int recvCount = 0;

            for (var i = 0; i < loop; i++)
            {
                var len = sockTcp.Send(tmp);
                Assert.That(len, Is.EqualTo(tmp.Length));

                Thread.Sleep(10);

                var b = sockTcp.Recv(len, timeout, this);
                recvCount += b.Length;
                for (int m = 0; m < max; m += 10)
                {
                    Assert.That(b[m], Is.EqualTo(tmp[m])); //送信したデータと受信したデータが同一かどうかのテスト
                }
            }
            Assert.That(loop * max, Is.EqualTo(recvCount)); //送信したデータ数と受信したデータ数が一致するかどうかのテスト

            sockTcp.Close();
            echoServer.Stop();
        }
Beispiel #6
0
 private void Tcp(SockTcp sockTcp)
 {
     while (IsLife() && sockTcp.SockState == Bjd.sock.SockState.Connect)
     {
         Thread.Sleep(0); //これが無いと、別スレッドでlifeをfalseにできない
         var len = sockTcp.Length();
         if (0 < len)
         {
             const int timeout = 10;
             var       buf     = sockTcp.Recv(len, timeout, this);
             sockTcp.Send(buf);
             break; //echoしたらセッションを閉じる
         }
     }
 }
Beispiel #7
0
        //�ڑ��P�ʂ̏���
        override protected void OnSubThread(SockObj sockObj)
        {
            string hostName;

            var     client = (SockTcp)sockObj;
            SockTcp server = null;

            //***************************************************************
            //�O�����i�ڑ���E���[�U���E�p�X���[�h�̎擾)
            //***************************************************************
            //{
            //    //�ڑ���i�z�X�g�j���擾
            //    client.AsciiSend("open>");
            //    var sb = new StringBuilder();
            //    while (IsLife()) {
            //        var b = client.Recv(1,Timeout,this);//timeout=60sec
            //        if (b == null)
            //            break;

            //        var c = Convert.ToChar(b[0]);
            //        if (c == '\r'){
            //            continue;
            //        }
            //        if (c == '\n')
            //            break;
            //        //Ver6.0.6 TeraTerm�Ή�
            //        if (c == 0 && sb.Length != 0)
            //        {
            //            break;
            //        }

            //        if ((c == '.') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9'))
            //        {
            //            client.Send(b);//�G�R�[
            //            sb.Append(c);
            //        }

            //    }
            //    hostName = sb.ToString();
            //}
            var buf = new List <byte>();

            {
                //�ڑ���i�z�X�g�j���擾
                client.Send(Encoding.ASCII.GetBytes("open>"));

                var iac = false;
                var ego = false;
                var sb  = new StringBuilder();
                while (IsLife())
                {
                    var b = client.Recv(1, Timeout, this); //timeout=60sec
                    if (b == null)
                    {
                        break;
                    }
                    var d = Convert.ToChar(b[0]);
                    if (d == 0)
                    {
                        continue;
                    }
                    if (d == '\xFF')
                    {
                        iac = true;
                        buf.Add(b[0]);
                        continue;
                    }
                    if (iac)
                    {
                        if (d == '\xFA')
                        {
                            ego = true;
                            iac = false;
                            buf.Add(b[0]);
                            continue;
                        }
                        if (d == '\xF0')
                        {
                            ego = false;
                            iac = false;
                            buf.Add(b[0]);
                            continue;
                        }
                        if (d == '\xFB' || d == '\xFC' || d == '\xFD' || d == '\xFE')
                        {
                            buf.Add(b[0]);
                            continue;
                        }
                        iac = false;
                        buf.Add(b[0]);
                        continue;
                    }
                    if (ego)
                    {
                        buf.Add(b[0]);
                        continue;
                    }
                    client.Send(b); //�G�R�[

                    if (d == '\r' || d == '\n')
                    {
                        break;
                    }

                    if (d == '\b')
                    {
                        sb.Remove(sb.Length - 1, 1);
                    }
                    else
                    {
                        sb.Append(d);
                    }
                }
                hostName = sb.ToString();
            }
            // ���݂̔j��
            while (IsLife() && client.Length() > 0)
            {
                var b = client.Recv(1, Timeout, this); //timeout=60sec
            }


            //***************************************************************
            // �T�[�o�Ƃ̐ڑ�
            //***************************************************************
            {
                const int port = 23;
                //var ipList = new List<Ip>{new Ip(hostName)};
                //if (ipList[0].ToString() == "0.0.0.0") {
                //    ipList = Kernel.DnsCache.Get(hostName);
                //    if (ipList.Count == 0) {
                //        Logger.Set(LogKind.Normal,null,2,string.Format("open>{0}",hostName));
                //        goto end;
                //    }
                //}
                var ipList = Kernel.GetIpList(hostName);
                if (ipList.Count == 0)
                {
                    Logger.Set(LogKind.Normal, null, 2, string.Format("open>{0}", hostName));
                    goto end;
                }
                foreach (var ip in ipList)
                {
                    server = Inet.Connect(Kernel, ip, port, Timeout, null);
                    if (server != null)
                    {
                        break;
                    }
                }

                if (server == null)
                {
                    Logger.Set(LogKind.Normal, null, 3, string.Format("open>{0}", hostName));
                    goto end;
                }

                //Ver6.0.6
                if (server.SockState != Bjd.sock.SockState.Connect)
                {
                    Logger.Set(LogKind.Normal, null, 3, string.Format("open>{0}", hostName));
                    goto end;
                }
            }
            Logger.Set(LogKind.Normal, server, 1, string.Format("open>{0}", hostName));

            server.Send(buf.ToArray(), buf.Count);
            //***************************************************************
            // �p�C�v
            //***************************************************************
            var tunnel = new Tunnel(Logger, (int)Conf.Get("idleTime"), Timeout);

            tunnel.Pipe(server, client, this);
end:
            client.Close();
            if (server != null)
            {
                server.Close();
            }
        }
Beispiel #8
0
        //接続単位の処理
        override protected void OnSubThread(SockObj sockObj)
        {
            var     client = (SockTcp)sockObj;
            SockTcp server = null;

            string _targetServer;
            int    _targetPort;
            var    clientBuf = new List <byte[]>();

            _targetServer = (string)Conf.Get("targetServer");
            _targetPort   = (int)Conf.Get("targetPort");
            if (_targetServer == "")
            {
                Logger.Set(LogKind.Error, client, 1, "");
                goto end;
            }
            if (_targetPort == 0)
            {
                Logger.Set(LogKind.Error, client, 2, "");
                goto end;
            }


            //***************************************************************
            //前処理(接続先・ユーザの取得と特別なユーザの置換)
            //***************************************************************
            {
                var keyWord = BeforeJob(client, clientBuf);//前処理
                if (keyWord == null)
                {
                    goto end;
                }


                //特別なユーザにヒットしているかどうかの確認
                var oneSpecialUser = _specialUser.Search(keyWord);
                if (oneSpecialUser != null)  //ヒットした場合
                //置換
                {
                    _targetServer = oneSpecialUser.Server; //サーバ
                    _targetPort   = oneSpecialUser.Port;   //ポート番号

                    for (int i = 0; i < clientBuf.Count; i++)
                    {
                        //string str = Inet.TrimCRLF(Encoding.ASCII.GetString(clientBuf[i]));
                        var str = Encoding.ASCII.GetString(clientBuf[i]);
                        if ((Protocol == MailProxyProtocol.Smtp && str.ToUpper().IndexOf("MAIL FROM:") == 0) ||
                            (Protocol == MailProxyProtocol.Pop3 && str.ToUpper().IndexOf("USER") == 0))
                        {
                            str          = Util.SwapStr(oneSpecialUser.Before, oneSpecialUser.After, str);
                            clientBuf[i] = Encoding.ASCII.GetBytes(str);
                            break;
                        }
                    }
                    Logger.Set(LogKind.Normal, client, 3, string.Format("{0}->{1} {2}:{3}", oneSpecialUser.Before, oneSpecialUser.After, _targetServer, _targetPort));
                }
            }

            //***************************************************************
            // サーバとの接続
            //***************************************************************
            {
                var port = _targetPort;
                //var ipList = new List<Ip>{new Ip(_targetServer)};
                //if (ipList[0].ToString() == "0.0.0.0") {
                //    ipList = Kernel.DnsCache.Get(_targetServer);
                //    if (ipList.Count == 0) {
                //        Logger.Set(LogKind.Normal, client, 4, string.Format("{0}:{1}", _targetServer, _targetPort));
                //        goto end;
                //    }
                //}

                var ipList = Kernel.GetIpList(_targetServer);
                if (ipList.Count == 0)
                {
                    Logger.Set(LogKind.Normal, client, 4, string.Format("{0}:{1}", _targetServer, _targetPort));
                    goto end;
                }

                foreach (var ip in ipList)
                {
                    server = Inet.Connect(Kernel, ip, port, Timeout, null);
                    if (server != null)
                    {
                        break;
                    }
                }
                if (server == null)
                {
                    Logger.Set(LogKind.Normal, client, 5, string.Format("{0}:{1}", _targetServer, _targetPort));
                    goto end;
                }
            }
            Logger.Set(LogKind.Normal, client, 4, string.Format("connect {0}:{1}", _targetServer, _targetPort));

            //***************************************************************
            //後処理(接続先・ユーザの取得と特別なユーザの置換)
            //***************************************************************

            foreach (var buf in clientBuf)
            {
                //byte[] serverBuf = server.LineRecv(timeout, OperateCrlf.No, ref life);
                server.LineRecv(Timeout, this);
                //クライアントからの受信分を送信する
                //Ver5.8.4
                //server.LineSend(buf);
                server.Send(buf);
            }

            //***************************************************************
            // パイプ
            //***************************************************************
            var tunnel = new Tunnel(Logger, (int)Conf.Get("idleTime"), Timeout);

            tunnel.Pipe(server, client, this);
end:
            if (client != null)
            {
                client.Close();
            }
            if (server != null)
            {
                server.Close();
            }
        }