protected override string BeforeJob(SockTcp client,List<byte[]> clientBuf) { Protocol = MailProxyProtocol.Pop3; //挨拶文をサーバに変わって送出する client.AsciiSend("+OK "); //USER コマンドを受け付けるまでループ(最大5回)する for(var i=0;i<5;i++) { var buf = client.LineRecv(Timeout,this); if(buf != null) { var str = Inet.TrimCrlf(Encoding.ASCII.GetString(buf)); if(str.ToUpper().IndexOf("USER") == 0) { clientBuf.Add(buf); var tmp = str.Split(' '); if(tmp.Length >= 2) { return tmp[1];//ユーザ名 } } else if(str.ToUpper().IndexOf("QUIT") == 0) { return null; } else { client.AsciiSend("-ERR "); } }else{ Thread.Sleep(300); } } return null; }
public void EchoサーバにlineSendで1行送信してlineRecvで1行受信する() { //setUp const string addr = "127.0.0.1"; const int port = 9993; var sv = new EchoServer(addr, port); sv.Start(); var sut = new SockTcp(new Kernel(), new Ip(addr), port, 100, null); sut.LineSend(Encoding.UTF8.GetBytes("本日は晴天なり")); Thread.Sleep(10); var expected = "本日は晴天なり\r\n"; //exercise var bytes = sut.LineRecv(1, this); var actual = Encoding.UTF8.GetString(bytes); //verify Assert.That(actual, Is.EqualTo(expected)); //tearDown sut.Close(); sv.Stop(); }
protected override string BeforeJob(SockTcp client,List<byte[]> clientBuf) { Protocol = MailProxyProtocolKind.Smtp; //挨拶文をサーバに変わって送出する client.AsciiSend("220 SMTP-Proxy"); while(clientBuf.Count<5) { var buf = client.LineRecv(Timeout,this); if(buf == null) return null;//タイムアウト //Ver5.8.6 //var str = Inet.TrimCrlf(Encoding.ASCII.GetString(buf)); buf = Inet.TrimCrlf(buf); var str = Encoding.ASCII.GetString(buf); //Ver5,3,4 RESTコマンドは蓄積がプロトコル上できないのでサーバへは送らない if(str.ToUpper().IndexOf("RSET")!=0) clientBuf.Add(buf); if(str.ToUpper().IndexOf("QUIT") != -1) { return null; } if(clientBuf.Count > 1) { if(str.ToUpper().IndexOf("MAIL FROM:") != -1) { var mailAddress = str.Substring(str.IndexOf(":") + 1); mailAddress = mailAddress.Trim(); mailAddress = mailAddress.Trim(new[] { '<','>' }); return mailAddress;//メールアドレス } } client.AsciiSend("250 OK"); } return null; }
public Session(SockTcp sockCtrl) { SockCtrl = sockCtrl; //PASV接続用ポート番号の初期化 (開始番号は2000~9900) var rnd = new Random(); Port = (rnd.Next(79) + 20)*100; }
//データ取得(内部データは、初期化される) public bool Recv(Logger logger, SockTcp sockTcp, int timeout, ILife iLife) { _logger = logger; //int limit = 3600;//文字数制限 var str = sockTcp.AsciiRecv(timeout, iLife); if (str == null){ return false; } return Interpretation(Inet.TrimCrlf(str)); }
public void SetUp() { var kernel = new Kernel(); var ip = new Ip("127.0.0.1"); const int port = 0; Ssl ssl = null; var tcpObj = new SockTcp(new Kernel(), ip,port,3,ssl); var upperProxy = new UpperProxy(false, "", 0, null,false,"","");//上位プロキシ未使用 const int timeout = 3; _proxy = new Proxy(kernel, null, tcpObj, timeout,upperProxy); }
//メールの送信 count=本文の行数(-1の場合は全部) public bool Send(SockTcp sockTcp, int count) { string fileName = string.Format("{0}\\MF_{1}", _dir, _fname); var mail = new Mail(); mail.Read(fileName); if (!mail.Send(sockTcp, count)){ // Logger.Set(LogKind.Error, null, 9000058, ex.Message); //mail.GetLastEror()を未処理 return false; } return true; }
string _timeFmt = "ddd M dd hh:mm:ss yyyy"; //(SSI用)日付書式文字列 #endregion Fields #region Constructors public Ssi(Kernel kernel, Logger logger, Conf conf, SockTcp tcpObj, Request request, Header recvHeader) { _kernel = kernel; _logger = logger; //_oneOption = oneOption; _conf = conf; //子プロセスでCGIを実行する場合に使用する _sockTcp = tcpObj; _request = request; _recvHeader = recvHeader; }
readonly SockTcp _sockObj; //Logger�o�͗p #endregion Fields #region Constructors public Request(Logger logger,SockTcp sockTcp) { //Logger�o�͗p(void Log()�̒��ł̂ݎg�p�����) _logger = logger; _sockObj = sockTcp; Method = HttpMethod.Unknown; Uri = ""; Param = ""; Ver = ""; LogStr = ""; }
public Proxy(Kernel kernel,Logger logger,SockTcp clientSocket,int optionTimeout,UpperProxy upperProxy) { _kernel = kernel; Logger = logger; OptionTimeout = optionTimeout; UpperProxy = upperProxy; _sock[CS.Client] = clientSocket; _sock[CS.Server] = null; //sock[CS.CLIENT].SendTimeout = optionTimeout; //Ver5.0.2 送信タイムアウトは設定しない ProxyProtocol = ProxyProtocol.Unknown; }
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(); }
void TcpTunnel(SockTcp tcpObj) { var client = tcpObj; SockTcp server = null; //*************************************************************** // �T�[�o�Ƃ̐ڑ� //*************************************************************** { 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,null,4,string.Format("{0}:{1}",_targetServer,_targetPort)); // goto end; // } //} var ipList = Kernel.GetIpList(_targetServer); if (ipList.Count == 0){ Logger.Set(LogKind.Normal, null, 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,server,5,string.Format("{0}:{1}",_targetServer,_targetPort)); goto end; } } Logger.Set(LogKind.Normal,server,6,string.Format("TCP {0}:{1} - {2}:{3}",client.RemoteHostname,client.RemoteAddress.Port,_targetServer,_targetPort)); //*************************************************************** // �p�C�v //*************************************************************** 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(); }
public BrowseDlg(Kernel kernel,SockTcp sockTcp,CtrlType ctrlType) { InitializeComponent(); _kernel = kernel; _sockTcp = sockTcp; _ctrlType = ctrlType; _browseData = new BrowseData(textBox,buttonOk,_ctrlType); listView.Columns.Add(kernel.IsJp() ? "名前" : "name"); listView.Columns.Add(kernel.IsJp() ? "サイズ" : "size"); listView.Columns[1].TextAlign = HorizontalAlignment.Right; listView.Columns.Add(kernel.IsJp() ? "種類" : "type"); listView.Columns.Add(kernel.IsJp() ? "更新日" : "date"); kernel.WindowSize.Read(this);//ウインドサイズの復元 kernel.WindowSize.Read(listView);//カラム幅の復元 }
public void OptionTest(string key, string val) { var request = new Request(null,null); var conf = new Conf(option); conf.Set("documentRoot", val); var header = new Header(); var tcpObj = new SockTcp(new Kernel(), new Ip("0.0.0.0"), 88, 1, null); const string fileName = ""; var env = new Env(_kernel,conf, request, header,tcpObj, fileName); foreach (var e in env) { if (e.Key == key) { Assert.AreEqual(e.Val, val); return; } } Assert.AreEqual(key, ""); }
public Document(Kernel kernel, Logger logger, Conf conf, SockTcp tcpObj, ContentType contentType) { //this.kernel = kernel; _logger = logger; //_oneOption = oneOption; _conf = conf; _sockTcp = tcpObj; _contentType = contentType; SetRangeTo = false; //���M�w�b�_������ _sendHeader = new Header(); _sendHeader.Replace("Server", Util.SwapStr("$v", kernel.Ver.Version(), (string)_conf.Get("serverHeader"))); _sendHeader.Replace("MIME-Version","1.0"); _sendHeader.Replace("Date",Util.UtcTime2Str(DateTime.UtcNow)); _body = new Body(); }
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(); }
public bool Send(SockTcp sockTcp) { if (sockTcp != null) { //1.REMOTE_DATA_KINDの送信(トレースなし) var b = new[] { (byte)Kind }; sockTcp.SendNoTrace(b); //データのバイナリ化 var data = Encoding.GetEncoding(932).GetBytes(Str); //2.データサイズの送信(トレースなし) b = BitConverter.GetBytes(data.Length == 0 ? 0 : data.Length); sockTcp.SendNoTrace(b); //3.データ本体の送信(トレースなし) if (data.Length != 0) sockTcp.SendNoTrace(data); return true; } return false; }
//受信(無効な場合 return null) public static OneRemoteData Recv(SockTcp sockTcp, ILife iLife) { if (sockTcp != null) { //Ver5.8.6 var sec = 10;//最初はタイムアウト値を最小に設定する var b = sockTcp.Recv(1, sec, iLife);//REMOTE_DATA_KINDの受信 if (b != null && b.Length == 1) { var kind = (RemoteDataKind)b[0]; //これ以降は、データが到着しているはずなので、タイムアウト値を上げて待機する //timeout = 3000; //Ver5.8.6 sec = 10; Thread.Sleep(1); b = sockTcp.Recv(4, sec, iLife);//データサイズの受信 if (b != null && b.Length == 4) { var len = BitConverter.ToInt32(b, 0); if (len == 0) { return new OneRemoteData(kind, "");//データ本体はサイズ0 } //Ver5.8.6 b = new byte[0]; while (iLife.IsLife()) { Thread.Sleep(1); var buf = sockTcp.Recv(len, sec, iLife);//データ本体の受信 if (buf == null) { return null; } b = Bytes.Create(b, buf); if (b.Length == len){ return new OneRemoteData(kind, Encoding.GetEncoding(932).GetString(b)); } } } } } return null; }
//接続 public bool Connect() { if (Status != PopClientStatus.Idle){ SetLastError("Connect() Status != Idle"); return false; } if (_ip.InetKind == InetKind.V4){ _sockTcp = Inet.Connect(_kernel, _ip, _port, _sec+3, null); } else{ _sockTcp = Inet.Connect(_kernel, _ip, _port, _sec+3, null); } if (_sockTcp.SockState == SockState.Connect){ //+OK受信 if (!RecvStatus()) { return false; } Status = PopClientStatus.Authorization; return true; } SetLastError("Faild in PopClient Connect()"); return false; }
protected ToolDlg(Kernel kernel,string nameTag,Object obj,string caption) { InitializeComponent(); Kernel = kernel; NameTag = nameTag; _caption = caption; if (kernel.RunMode == RunMode.Remote) { sockTcp = (SockTcp)obj; } else { Server = (OneServer)obj; } Text = caption; //ウインドウサイズの復元 kernel.WindowSize.Read(this); //MainMenuFile.Text = (kernel.IsJp()) ? "ファイル(&F)" : "&File"; //MainMenuClose.Text = (kernel.IsJp()) ? "閉じる(&C)" : "&Close"; }
public void HeaderTest(string key, string val) { var request = new Request(null,null); var header = new Header(); header.Append("Connection", Encoding.ASCII.GetBytes("keep-alive")); header.Append("User-Agent", Encoding.ASCII.GetBytes("Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)")); header.Append("Accept", Encoding.ASCII.GetBytes("text/html,application/xhtml")); header.Append("Accept-Encoding", Encoding.ASCII.GetBytes("gzip,deflate,sdch")); header.Append("Accept-Language", Encoding.ASCII.GetBytes("ja,en-US;q=0.8,en;q=0.6")); header.Append("Accept-Charset", Encoding.ASCII.GetBytes("Shift_JIS,utf-8;q=0.7,*;q=0.3")); header.Append("Cache-Control", Encoding.ASCII.GetBytes("max-age=0")); var tcpObj = new SockTcp(new Kernel(), new Ip("0.0.0.0"), 88, 3,null); const string fileName = ""; var env = new Env(_kernel,new Conf(option), request, header, tcpObj, fileName); foreach (var e in env) { if (e.Key == key) { Assert.AreEqual(e.Val, val); return; } } Assert.AreEqual(key, ""); }
protected override string ConnectJob(SockTcp client, SockTcp server,List<byte[]> clientBuf) { //最初のグリーティングメッセージ取得 var buf = server.LineRecv(Timeout, this); if (buf == null) return null;//タイムアウト //EHLO送信 server.LineSend(clientBuf[0]); clientBuf.RemoveAt(0); //「250 OK」が返るまで読み飛ばす while (IsLife()) { buf = server.LineRecv(Timeout, this); if (buf == null) return null;//タイムアウト var str = Inet.TrimCrlf(Encoding.ASCII.GetString(buf)); if (str.ToUpper().IndexOf("250 ") == 0) { return str; } } return null; }
//送信 bool Send(SockTcp sock,byte[] sendBuf) { var c = sock.SendUseEncode(sendBuf); if(c == sendBuf.Length) { sendBuf = new byte[0]; } else { return false; } return true; }
bool Login(SockTcp sockTcp,ref Pop3LoginState mode,ref MessageList messageList,string user,Ip addr) { //var folder = Kernel.MailBox.Login(user, addr); if(!Kernel.MailBox.Login(user, addr)){ Logger.Set(LogKind.Secure,sockTcp,1,string.Format("user={0}",user)); sockTcp.AsciiSend("-ERR Double login"); return false; } var folder = string.Format("{0}\\{1}", Kernel.MailBox.Dir, user); messageList = new MessageList(folder);//������ //if (kernel.MailBox.Login(user, addr)) {//POP before SMTP�̂��߂ɁA�Ō�̃��O�C���A�h���X��ۑ����� mode = Pop3LoginState.Login; Logger.Set(LogKind.Normal,sockTcp,2,string.Format("User {0} from {1}[{2}]",user,sockTcp.RemoteHostname,sockTcp.RemoteAddress.Address)); // LOGIN //dfList = kernel.MailBox.GetDfList(user); sockTcp.AsciiSend(string.Format("+OK {0} has {1} message ({2} octets).",user,messageList.Count,messageList.Size)); return true; }
void AuthError(SockTcp sockTcp,string user,string pass) { Logger.Set(LogKind.Secure,sockTcp,3,string.Format("user={0} pass={1}",user,pass)); // �F�̃G���[�͂����ɕԓ���Ԃ��Ȃ� var authTimeout = (int)Conf.Get("authTimeout"); for (int i = 0; i < (authTimeout * 10) && IsLife(); i++) { Thread.Sleep(100); } sockTcp.AsciiSend(string.Format("-ERR Password supplied for {0} is incorrect.",user)); }
public bool Quit() { //トランザクションでない場合エラー if (Status == SmtpClientStatus.Idle) { SetLastError("Quit() Status == Idle"); return false; } //QUIT送信 if (!SendCmd("QUIT")) { return false; } //221受信 if (!RecvStatus(221)) { return false; } //切断 _sockTcp.Close(); _sockTcp = null; Status = SmtpClientStatus.Idle; return true; }
public void Dispose() { if (_sockTcp != null) { _sockTcp.Close(); _sockTcp = null; } }
//接続 public bool Connect() { if (Status != SmtpClientStatus.Idle) { SetLastError("Connect() Status != Idle"); return false; } if (_ip.InetKind == InetKind.V4) { _sockTcp = Inet.Connect(new Kernel(), _ip, _port, _sec + 3, null); } else { _sockTcp = Inet.Connect(new Kernel(), _ip, _port, _sec + 3, null); } if (_sockTcp.SockState == SockState.Connect) { //220受信 if (!RecvStatus(220)) { return false; } Status = SmtpClientStatus.Helo; return true; } SetLastError("Faild in SmtpClient Connect()"); return false; }
static public List<String> RecvLines(SockTcp cl,int sec,ILife iLife){ var lines = new List<string>(); while (true) { var buf = cl.LineRecv(sec, iLife); if (buf == null) break; if (buf.Length==0) break; var s = Encoding.ASCII.GetString(TrimCrlf(buf)); //if (s == "") // break; lines.Add(s); } return lines; }
//通常はこれを使用する public bool Recv(SockTcp sockTcp, int sec,Logger logger, ILife iLife) { var dtLast = DateTime.Now; //受信が20秒無かった場合は、処理を中断する while (iLife.IsLife()){ if (dtLast.AddSeconds(sec) < DateTime.Now){ return false; //タイムアウト } var len = sockTcp.Length(); if (len == 0){ continue; } var buf = sockTcp.Recv(len, sec, iLife); if (buf == null){ return false; //切断された } dtLast = DateTime.Now; var recvStatus = Append(buf); if (recvStatus == RecvStatus.Limit){ //サイズ制限 if (logger != null){ logger.Set(LogKind.Secure, sockTcp, 7, string.Format("Limit:{0}KByte", _sizeLimit)); } sockTcp.AsciiSend("552 Requested mail action aborted: exceeded storage allocation"); return false; } if (recvStatus == RecvStatus.Finish){ return true; } } return false; }