public void connect(string host, int port) { IPHostEntry iphost; IPAddress[] addr; EndPoint ep; //tag = 10; removed root = 9; afid = -1; cwd = 7; fid = 6; ffid = 5; mmsgsz = (uint)proto.MAXPKTSIZE; mdatasz = mmsgsz - (uint)proto.IOHDRSIZE; uname = "andrey"; aname = ""; fT = new Fcall(); pktT = new Byte[mmsgsz]; iphost = Dns.GetHostEntry(host); addr = iphost.AddressList; ep = new IPEndPoint(addr[0], port); sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { sock.Connect(ep); Console.WriteLine("Connected"); } catch (Exception ex) { throw new ninepexception(ex.ToString()); } }
public TcpListener serve(int port) { //IPHostEntry iphost; //IPAddress[] addr; //EndPoint ep; TcpListener server; // tag = 10; removed root = 9; afid = -1; cwd = 7; fid = 6; ffid = 5; mmsgsz = (uint)proto.MAXPKTSIZE; mdatasz = mmsgsz - (uint)proto.IOHDRSIZE; uname = "andrey"; aname = ""; fT = new Fcall(); pktR = new Byte[mmsgsz]; //iphost = Dns.GetHostEntry(host); //addr = iphost.AddressList; server = new TcpListener(IPAddress.Any, port); return(server); // sock = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); // try { // sock.Connect(ep); // Console.WriteLine("Connected"); // } catch(Exception ex) { // throw new ninepexception(ex.ToString()); // } }
public void do9pT() { convS2M(fT, pktT); send9pmsg(pktT); pktR = read9pmsg(); fR = convM2S(pktR); }
public void do9p() { convS2M(fin, pktin); send9pmsg(pktin); pktout = read9pmsg(); fout = convM2S(pktout); }
public void recieve(TcpClient client) { ///convS2M(fT, pktT); //send9pmsg(pktT); pktT = read9pmsg(); //printPacket (pktT, "recieveT"); fT = convM2S(pktT); }
public uint convS2M(Fcall f, Byte[] pkt) { uint size, i, pp; size = sizeS2M(f); if (size == 0) { return(0); } if (size > pkt.Length) { return(0); } pp = 0; putuint(pkt, pp, size); pp += (uint)proto.BIT32SZ; pkt[pp] = f.type; pp += (uint)proto.BIT8SZ; putushort(pkt, pp, f.tag); pp += (uint)proto.BIT16SZ; switch (f.type) { default: return(0); case (byte)proto.Tversion: putuint(pkt, pp, f.msize); pp += (uint)proto.BIT32SZ; putstring(pkt, pp, f.version); pp += (uint)proto.BIT16SZ + (uint)f.version.Length; break; case (byte)proto.Tflush: putushort(pkt, pp, f.oldtag); pp += (uint)proto.BIT16SZ; break; case (byte)proto.Tauth: putuint(pkt, pp, (uint)f.afid); pp += (uint)proto.BIT32SZ; putstring(pkt, pp, f.uname); pp += (uint)proto.BIT16SZ + (uint)f.uname.Length; putstring(pkt, pp, f.aname); pp += (uint)proto.BIT16SZ + (uint)f.aname.Length; break; case (byte)proto.Tattach: putuint(pkt, pp, (uint)f.fid); pp += (uint)proto.BIT32SZ; putuint(pkt, pp, (uint)f.afid); pp += (uint)proto.BIT32SZ; putstring(pkt, pp, f.uname); pp += (uint)proto.BIT16SZ + (uint)f.uname.Length; putstring(pkt, pp, f.aname); pp += (uint)proto.BIT16SZ + (uint)f.aname.Length; break; case (byte)proto.Twalk: putuint(pkt, pp, (uint)f.fid); pp += (uint)proto.BIT32SZ; putuint(pkt, pp, (uint)f.newfid); pp += (uint)proto.BIT32SZ; putushort(pkt, pp, f.nwname); pp += (uint)proto.BIT16SZ; if (f.nwname > (uint)proto.MAXWELEM) { return(0); } for (i = 0; i < f.nwname; i++) { putstring(pkt, pp, f.wname[i]); pp += (uint)proto.BIT16SZ + (uint)f.wname[i].Length; } break; case (byte)proto.Topen: putuint(pkt, pp, (uint)f.fid); pp += (uint)proto.BIT32SZ; pkt[pp] = f.mode; pp += (uint)proto.BIT8SZ; break; case (byte)proto.Tcreate: putuint(pkt, pp, (uint)f.fid); pp += (uint)proto.BIT32SZ; putstring(pkt, pp, f.name); pp += (uint)proto.BIT16SZ + (uint)f.name.Length; putuint(pkt, pp, f.perm); pp += (uint)proto.BIT32SZ; pkt[pp] = f.mode; pp += (uint)proto.BIT8SZ; break; case (byte)proto.Tread: putuint(pkt, pp, (uint)f.fid); pp += (uint)proto.BIT32SZ; putulong(pkt, pp, f.offset); pp += (uint)proto.BIT64SZ; putuint(pkt, pp, f.count); pp += (uint)proto.BIT32SZ; break; case (byte)proto.Twrite: putuint(pkt, pp, (uint)f.fid); pp += (uint)proto.BIT32SZ; putulong(pkt, pp, f.offset); pp += (uint)proto.BIT64SZ; putuint(pkt, pp, f.count); pp += (uint)proto.BIT32SZ; Array.Copy(f.data, 0, pkt, pp, f.count); pp += f.count; break; case (byte)proto.Tclunk: case (byte)proto.Tremove: case (byte)proto.Tstat: putuint(pkt, pp, (uint)f.fid); pp += (uint)proto.BIT32SZ; break; case (byte)proto.Twstat: putuint(pkt, pp, (uint)f.fid); pp += (uint)proto.BIT32SZ; ///putushort(pkt, pp, f.nstat); // this is included in f.stat Array.Copy(f.stat, 0, pkt, pp, f.stat.Length); pp += (uint)f.stat.Length; break; case (byte)proto.Rversion: putuint(pkt, pp, f.msize); pp += (uint)proto.BIT32SZ; putstring(pkt, pp, f.version); pp += (uint)proto.BIT16SZ + (uint)f.version.Length; break; case (byte)proto.Rerror: putstring(pkt, pp, f.ename); pp += (uint)proto.BIT16SZ + (uint)f.ename.Length; break; case (byte)proto.Rflush: case (byte)proto.Rclunk: case (byte)proto.Rremove: case (byte)proto.Rwstat: break; case (byte)proto.Rauth: putqid(pkt, pp, f.aqid); pp += (uint)proto.QIDSZ; break; case (byte)proto.Rattach: putqid(pkt, pp, f.qid); pp += (uint)proto.QIDSZ; break; case (byte)proto.Rwalk: putushort(pkt, pp, f.nwqid); pp += (uint)proto.BIT16SZ; if (f.nwqid > (uint)proto.MAXWELEM) { return(0); } for (i = 0; i < f.nwqid; i++) { putqid(pkt, pp, f.wqid[i]); pp += (uint)proto.QIDSZ; } break; case (byte)proto.Ropen: case (byte)proto.Rcreate: putqid(pkt, pp, f.qid); pp += (uint)proto.QIDSZ; putuint(pkt, pp, f.iounit); pp += (uint)proto.BIT32SZ; break; case (byte)proto.Rread: putuint(pkt, pp, f.count); pp += (uint)proto.BIT32SZ; Array.Copy(f.data, 0, pkt, pp, f.count); pp += f.count; break; case (byte)proto.Rwrite: putuint(pkt, pp, f.count); pp += (uint)proto.BIT32SZ; break; case (byte)proto.Rstat: //putushort(pkt, pp, f.nstat); //pp += (uint)proto.BIT16SZ; Array.Copy(f.stat, 0, pkt, pp, f.stat.Length); //Console.WriteLine (BitConverter.ToString (f.stat, 0)); printPacket(pkt, "presentRstat"); pp += (uint)f.stat.Length; break; } if (size != pp) { return(0); } return(size); }
public uint sizeS2M(Fcall f) { uint n, i; n = (uint)proto.BIT32SZ + (uint)proto.BIT8SZ + (uint)proto.BIT16SZ; switch (f.type) { default: return(0); case (byte)proto.Tversion: case (byte)proto.Rversion: n += (uint)proto.BIT32SZ + (uint)proto.BIT16SZ + (uint)f.version.Length; break; case (byte)proto.Tflush: n += (uint)proto.BIT16SZ; break; case (byte)proto.Tauth: n += (uint)proto.BIT32SZ + 2 * (uint)proto.BIT16SZ + (uint)f.uname.Length + (uint)f.aname.Length; break; case (byte)proto.Tattach: n += 2 * (uint)proto.BIT32SZ + 2 * (uint)proto.BIT16SZ + (uint)f.uname.Length + (uint)f.aname.Length; break; case (byte)proto.Twalk: n += 2 * (uint)proto.BIT32SZ + (uint)proto.BIT16SZ; for (i = 0; i < f.nwname; i++) { n += (uint)f.wname[i].Length + (uint)proto.BIT16SZ; } break; case (byte)proto.Topen: n += (uint)proto.BIT32SZ + (uint)proto.BIT8SZ; break; case (byte)proto.Tcreate: n += 2 * (uint)proto.BIT32SZ + (uint)proto.BIT8SZ + (uint)proto.BIT16SZ + (uint)f.name.Length; break; case (byte)proto.Twrite: n += 2 * (uint)proto.BIT32SZ + (uint)proto.BIT64SZ + f.count; break; case (byte)proto.Tread: n += 2 * (uint)proto.BIT32SZ + (uint)proto.BIT64SZ; break; case (byte)proto.Tclunk: case (byte)proto.Tremove: case (byte)proto.Tstat: case (byte)proto.Rwrite: n += (uint)proto.BIT32SZ; break; case (byte)proto.Twstat: //n += (uint)proto.BIT32SZ + 2 * (uint)proto.BIT16SZ + f.nstat; n += (uint)proto.BIT32SZ + (uint)f.stat.Length; break; case (byte)proto.Rerror: n += (uint)proto.BIT16SZ + (uint)f.ename.Length; break; case (byte)proto.Rflush: case (byte)proto.Rclunk: case (byte)proto.Rremove: case (byte)proto.Rwstat: break; case (byte)proto.Rauth: case (byte)proto.Rattach: n += (uint)proto.QIDSZ; break; case (byte)proto.Rwalk: n += (uint)proto.BIT16SZ + f.nwqid * (uint)proto.QIDSZ; break; case (byte)proto.Ropen: case (byte)proto.Rcreate: n += (uint)proto.QIDSZ + (uint)proto.BIT32SZ; break; case (byte)proto.Rread: n += (uint)proto.BIT32SZ + f.count; break; case (byte)proto.Rstat: n += (uint)f.stat.Length; //(uint)proto.BIT16SZ + f.nstat; break; } return(n); }
public Fcall convM2S(Byte[] pkt) { Byte[] buf; uint len, pp, i; Fcall f; f = new Fcall(); buf = new Byte[(int)proto.BIT32SZ]; pp = 0; if (pkt.Length < (int)proto.BIT32SZ + (int)proto.BIT8SZ + (int)proto.BIT16SZ) { return(f); } len = getuint(pkt, 0); if (len < (int)proto.BIT32SZ + (int)proto.BIT8SZ + (int)proto.BIT16SZ) { return(f); } pp += (uint)proto.BIT32SZ; f.type = pkt[pp]; pp += (uint)proto.BIT8SZ; Array.Copy(pkt, pp, buf, (uint)proto.BIT32SZ - (uint)proto.BIT16SZ, (uint)proto.BIT16SZ); f.tag = getushort(pkt, pp); pp += (uint)proto.BIT16SZ; switch (f.type) { default: return(f); case (byte)proto.Tversion: if (pp + (uint)proto.BIT32SZ > len) { return(f); } Array.Copy(pkt, pp, buf, (uint)proto.BIT32SZ - (uint)proto.BIT16SZ, (uint)proto.BIT16SZ); f.msize = getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.version = getstring(pkt, pp); pp += (uint)f.version.Length; break; case (byte)proto.Tflush: if (pp + (uint)proto.BIT16SZ > len) { return(f); } f.oldtag = getushort(pkt, pp); pp += (uint)proto.BIT16SZ; break; case (byte)proto.Tauth: if (pp + (uint)proto.BIT32SZ > len) { return(f); } f.afid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.uname = getstring(pkt, pp); pp += (uint)proto.BIT16SZ + (uint)f.uname.Length; f.aname = getstring(pkt, pp); pp += (uint)proto.BIT16SZ + (uint)f.aname.Length; break; case (byte)proto.Tattach: if (pp + (uint)proto.BIT32SZ + (uint)proto.BIT32SZ > len) { return(f); } f.fid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.afid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.uname = getstring(pkt, pp); pp += (uint)proto.BIT16SZ + (uint)f.uname.Length; f.aname = getstring(pkt, pp); pp += (uint)proto.BIT16SZ + (uint)f.aname.Length; break; case (byte)proto.Twalk: if (pp + (uint)proto.BIT32SZ + (uint)proto.BIT32SZ + (uint)proto.BIT16SZ > len) { return(f); } f.fid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.newfid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.nwname = getushort(pkt, pp); pp += (uint)proto.BIT16SZ; if (f.nwname > (int)proto.MAXWELEM) { return(f); } f.wname = new string[f.nwname]; for (i = 0; i < f.nwname; i++) { f.wname[i] = getstring(pkt, pp); pp += (uint)f.wname[i].Length + (uint)proto.BIT16SZ; } break; case (byte)proto.Topen: if (pp + (uint)proto.BIT32SZ + (uint)proto.BIT8SZ > len) { return(f); } f.fid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.mode = pkt[pp]; pp += (uint)proto.BIT8SZ; break; case (byte)proto.Tcreate: if (pp + (uint)proto.BIT32SZ > len) { return(f); } f.fid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.name = getstring(pkt, pp); pp += (uint)proto.BIT16SZ + (uint)f.name.Length; if (pp + (uint)proto.BIT32SZ + (uint)proto.BIT8SZ > len) { return(f); } f.perm = getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.mode = pkt[pp]; pp += (uint)proto.BIT8SZ; break; case (byte)proto.Tread: if (pp + (uint)proto.BIT32SZ + (uint)proto.BIT64SZ + (uint)proto.BIT32SZ > len) { return(f); } f.fid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.offset = getulong(pkt, pp); pp += (uint)proto.BIT64SZ; f.count = getuint(pkt, pp); pp += (uint)proto.BIT32SZ; break; case (byte)proto.Twrite: if (pp + (uint)proto.BIT32SZ + (uint)proto.BIT64SZ + (uint)proto.BIT32SZ > len) { return(f); } f.fid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.offset = getulong(pkt, pp); pp += (uint)proto.BIT64SZ; f.count = getuint(pkt, pp); pp += (uint)proto.BIT32SZ; if (pp + f.count > len) { return(f); } f.data = new Byte[f.count]; Array.Copy(pkt, pp, f.data, 0, f.count); pp += f.count; break; case (byte)proto.Tclunk: case (byte)proto.Tremove: case (byte)proto.Tstat: if (pp + (uint)proto.BIT32SZ > len) { return(f); } f.fid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; break; case (byte)proto.Twstat: if (pp + (uint)proto.BIT32SZ + (uint)proto.BIT16SZ > len) { return(f); } f.fid = (int)getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.nstat = getushort(pkt, pp); //pp += (uint)proto.BIT16SZ; convm2d assumes size is still in byte[] if (pp + f.nstat + (uint)proto.BIT16SZ > len) { return(f); } f.stat = new Byte[f.nstat + (uint)proto.BIT16SZ]; Array.Copy(pkt, pp, f.stat, 0, f.nstat); //Console.WriteLine ("f.nstat={0}, f.stat={1}", f.nstat, BitConverter.ToString(f.stat)); pp += (uint)f.stat.Length; break; case (byte)proto.Rversion: if (pp + (uint)proto.BIT32SZ > len) { return(f); } f.msize = getuint(pkt, pp); pp += (uint)proto.BIT32SZ; f.version = getstring(pkt, pp); pp += (uint)f.version.Length; break; case (byte)proto.Rerror: f.ename = getstring(pkt, pp); pp += (uint)f.ename.Length; break; case (byte)proto.Rauth: f.aqid = getqid(pkt, pp); pp += (uint)proto.QIDSZ; break; case (byte)proto.Rattach: f.qid = getqid(pkt, pp); pp += (uint)proto.QIDSZ; break; case (byte)proto.Rwalk: if (pp + (uint)proto.BIT16SZ > len) { return(f); } f.nwqid = getushort(pkt, pp); pp += (uint)proto.BIT16SZ; if (f.nwqid > (int)proto.MAXWELEM) { return(f); } f.wqid = new Qid[f.nwqid]; for (i = 0; i < f.nwqid; i++) { f.wqid[i] = getqid(pkt, pp); pp += (uint)proto.QIDSZ; } break; case (byte)proto.Ropen: case (byte)proto.Rcreate: f.qid = getqid(pkt, pp); pp += (uint)proto.QIDSZ; if (pp + (uint)proto.BIT32SZ > len) { return(f); } f.iounit = getuint(pkt, pp); pp += (uint)proto.BIT32SZ; break; case (byte)proto.Rread: if (pp + (uint)proto.BIT32SZ > len) { return(f); } f.count = getuint(pkt, pp); pp += (uint)proto.BIT32SZ; if (pp + f.count > len) { return(f); } f.data = new Byte[f.count]; Array.Copy(pkt, pp, f.data, 0, f.count); pp += f.count; break; case (byte)proto.Rwrite: if (pp + (uint)proto.BIT32SZ > len) { return(f); } f.count = getuint(pkt, pp); pp += (uint)proto.BIT32SZ; break; case (byte)proto.Rremove: case (byte)proto.Rwstat: break; case (byte)proto.Rclunk: case (byte)proto.Rflush: if (pp + (uint)proto.BIT16SZ > len) { return(f); } f.tag = getushort(pkt, pp); pp += (uint)proto.BIT16SZ; break; case (byte)proto.Rstat: if (pp + (uint)proto.BIT16SZ > len) { return(f); } f.nstat = getushort(pkt, pp); //pp += (uint)proto.BIT16SZ; convM2D assumes length is still in the message if (pp + f.nstat > len) { return(f); } f.stat = new Byte[f.nstat + (uint)proto.BIT16SZ]; Array.Copy(pkt, pp, f.stat, 0, f.nstat + (uint)proto.BIT16SZ); pp += (uint)f.nstat + (uint)proto.BIT16SZ; break; } if (pp <= len) { f.status = 1; } return(f); }