Exemplo n.º 1
0
        public void TestReadWriteTwstat()
        {
            var stream = new MemoryStream();
            var qid    = new Qid((byte)QidType.QtFile, 1, 0x111L);
            var stat   = new Stat(
                1, 2, qid, 4, 5, 6, 65535L, "foo", "root", "root", "root");
            var message = new Twstat(1, stat)
            {
                Tag = 1254
            };

            var p = new Protocol.Protocol(stream);

            p.Write(message);
            stream.Position = 0;

            var data   = new byte[message.Length];
            var length = stream.Read(data, 0, (int)message.Length);

            Assert.That(length, Is.EqualTo(message.Length));

            stream.Position = 0;
            var message2 = (Twstat)p.Read();

            Assert.That(message, Is.EqualTo(message2));
        }
Exemplo n.º 2
0
 public Ropen(Qid qid, uint iounit)
 {
     Type    = (byte)MessageType.Ropen;
     Qid     = qid;
     Iounit  = iounit;
     Length += Constants.Qidsz + Constants.Bit32Sz;
 }
Exemplo n.º 3
0
        public void TestReadWriteQid()
        {
            var qid   = new Qid((byte)QidType.QtFile, 1, 0x111L);
            var bytes = qid.ToBytes();
            var qid2  = new Qid(bytes);

            Assert.That(qid, Is.EqualTo(qid2));
        }
Exemplo n.º 4
0
 public override int GetHashCode()
 {
     unchecked
     {
         var hashCode = base.GetHashCode();
         hashCode = (hashCode * 397) ^ (Qid?.GetHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (int)Iounit;
         return(hashCode);
     }
 }
Exemplo n.º 5
0
        public Ropen(byte[] bytes) : base(bytes)
        {
            var offset = Constants.HeaderOffset;

            Qid     = Protocol.ReadQid(bytes, offset);
            offset += Constants.Qidsz;
            Iounit  = Protocol.ReadUInt(bytes, offset);
            offset += Constants.Bit32Sz;
            if (offset < Length)
            {
                throw new InsufficientDataException(Length, offset);
            }
        }
Exemplo n.º 6
0
 public Dir(ushort Type = default, uint Dev = default, Qid Qid = default, uint Mode = default, uint Atime = default, uint Mtime = default, long Length = default, @string Name = default, @string Uid = default, @string Gid = default, @string Muid = default)
 {
     this.Type   = Type;
     this.Dev    = Dev;
     this.Qid    = Qid;
     this.Mode   = Mode;
     this.Atime  = Atime;
     this.Mtime  = Mtime;
     this.Length = Length;
     this.Name   = Name;
     this.Uid    = Uid;
     this.Gid    = Gid;
     this.Muid   = Muid;
 }
Exemplo n.º 7
0
        public Rwalk(byte[] bytes) : base(bytes)
        {
            var offset = Constants.HeaderOffset;

            Nwqid   = Protocol.ReadUShort(bytes, offset);
            offset += Constants.Bit16Sz;
            Wqid    = new Qid[Nwqid];
            for (var i = 0; i < Nwqid; i++)
            {
                Wqid[i] = Protocol.ReadQid(bytes, offset);
                offset += Constants.Qidsz;
            }
            if (offset < Length)
            {
                throw new InsufficientDataException(Length, offset);
            }
        }
Exemplo n.º 8
0
        private Message createReturnMessage(Message m)
        {
            switch (m.Type)
            {
            case (byte)MessageType.Twalk:
                var qid = new Qid((byte)QidType.QtFile, 1, 0x111L);
                return(new Rwalk(1, new [] { qid })
                {
                    Tag = m.Tag
                });

            case (byte)MessageType.Tclunk:
                return(new Rclunk {
                    Tag = m.Tag
                });
            }
            throw new Exception("Unexpected Message");
        }
Exemplo n.º 9
0
        public void TestReadWriteRauth()
        {
            var stream  = new MemoryStream();
            var qid     = new Qid((byte)QidType.QtFile, 1, 0x111L);
            var message = new Rauth(qid)
            {
                Tag = 1235
            };

            var p = new Protocol.Protocol(stream);

            p.Write(message);
            stream.Position = 0;

            var data   = new byte[message.Length];
            var length = stream.Read(data, 0, (int)message.Length);

            Assert.That(length, Is.EqualTo(message.Length));

            stream.Position = 0;
            var message2 = (Rauth)p.Read();

            Assert.That(qid, Is.EqualTo(message2.Aqid));
        }
Exemplo n.º 10
0
        public static void Main(string[] args)
        {
            //SERVER CODE: if you want the client, comment until CLIENT CODE
            ninepc.ninep protocol;
            protocol = new ninepc.ninep();
            string clientname = "unset";
            //string cmd, server;
            TcpListener server = protocol.serve(9999);



            Inode root = createnode("/", 0x00000755, "jeb", "kerman", (byte)proto.QTDIR);

            root.data = new Byte[0];


            Inode testdir = createnode("testdir", 0x00000755, "jeb", "kerman", (byte)proto.QTDIR);

            testdir.data   = new Byte[0];
            testdir.parent = root;
            root.children.Add(testdir);


            Qid[] wqid = new Qid[10];              //max number of walks... increase if needed.


            UTF8Encoding utf8 = new UTF8Encoding();

            Byte[] strdata = utf8.GetBytes("This is the file contents");
            Inode  child   = createnode("test", 0x00000755, "valerie", "kerman", (byte)proto.QTFILE);

            child.data       = strdata;
            child.dir.length = (ulong)strdata.Length;
            child.dir.qid.vers++;
            testdir.children.Add(child);
            child.parent = testdir;


            server.Start();
            //ep = new IPEndPoint(addr[0], port);
            while (true)
            {
                TcpClient client = protocol.AcceptTcpClient(server);
                while (client.Connected)
                {
                    protocol.recieve(client);
                    printPacket(protocol.pktT, "T");
                    ushort tag = protocol.fT.tag;
                    int    fid = protocol.fT.fid;
                    switch (protocol.fT.type)
                    {
                    case (byte)proto.Tstat:
                        Inode statnode;
                        if (!fidlist.TryGetValue(fid, out statnode))
                        {
                            protocol.doRerror(tag, "unrecognized fid");
                        }
                        protocol.doRstat(tag, statnode.dir);
                        break;

                    case (byte)proto.Topen:
                        Inode opnode;
                        if (!fidlist.TryGetValue(fid, out opnode))
                        {
                            protocol.doRerror(tag, "unrecognized fid");
                        }
                        //permissions stuff goes here
                        opnode.mode = protocol.fT.mode;
                        protocol.doRopen(tag, opnode.dir.qid, protocol.mmsgsz);
                        break;

                    case (byte)proto.Tread:
                        Inode  rnode;
                        uint   count;
                        Byte[] data;
                        if (!fidlist.TryGetValue(fid, out rnode))
                        {
                            protocol.doRerror(tag, "unrecognized fid");
                        }
                        count = protocol.fT.count;
                        if (count + protocol.fT.offset > rnode.dir.length)
                        {
                            count = (uint)rnode.dir.length - (uint)protocol.fT.offset;
                        }
                        if (rnode.dir.qid.type == (byte)proto.QTDIR)
                        {
                            List <Byte[]> dirdata = new List <Byte[]> ();
                            int           len     = 0;
                            foreach (Inode i in rnode.children)
                            {
                                Byte[] temp = new Byte[protocol.sizeD2M(i.dir)];
                                protocol.convD2M(i.dir, temp);
                                //printPacket (temp, "D");
                                len += temp.Length;
                                dirdata.Add(temp);
                            }
                            data = new Byte[len];
                            int pp = 0;
                            foreach (Byte[] b in dirdata)
                            {
                                System.Buffer.BlockCopy(b, 0, data, pp, b.Length);
                                pp += b.Length;
                            }
                            protocol.doRread(tag, (uint)data.Length, data);
                        }
                        else
                        {
                            if (count > 0)
                            {
                                data = new Byte[count];
                                Array.Copy(rnode.data, (int)protocol.fT.offset, data, 0, count);
                                protocol.doRread(tag, count, data);
                            }
                            else
                            {
                                protocol.doRread(tag, 0, new Byte[0]);
                            }
                        }
                        break;

                    case (byte)proto.Tauth:
                        clientname = protocol.fT.uname;
                        protocol.doRerror(tag, "u9fs authnone: no authentication required");
                        break;

                    case (byte)proto.Tattach:

                        if (protocol.fT.aname.Equals(""))
                        {
                            if (fidlist.ContainsKey(fid) == false)
                            {
                                fidlist.Add(fid, root);
                                protocol.doRattach(tag, root.dir.qid);
                            }
                            else
                            {
                                protocol.doRerror(tag, "Fid currently in use");
                            }
                        }
                        printfidlist();
                        break;

                    case (byte)proto.Tclunk:
                        if (listclunk(fid))
                        {
                            Console.WriteLine("clunked fid:{0}", fid);
                            protocol.doRclunk(tag, fid);
                        }
                        else
                        {
                            protocol.doRerror(tag, "Unrecognized fid");
                        }
                        printfidlist();
                        break;

                    case (byte)proto.Twalk:
                        Inode  cfidnode;
                        Inode  wfidnode;
                        ushort nwqid = 0;
                        if (fidlist.TryGetValue(fid, out cfidnode) == false)
                        {
                            protocol.doRerror(tag, "Unrecognized fid");
                            break;
                        }
                        else if (fidlist.ContainsKey(protocol.fT.newfid))
                        {
                            //protocol.doRerror (tag, protocol.fT.tag, "New fid already in use");
                            listclunk(fid);
                            break;
                        }
                        if (protocol.fT.nwname > 0)
                        {
                            //walk throught the file tree, creating qid's
                            wfidnode = cfidnode;

                            for (nwqid = 0; nwqid < protocol.fT.nwname; nwqid++, cfidnode = wfidnode)
                            {
                                Console.WriteLine("nwname={0}, wname={1}", protocol.fT.nwname, protocol.fT.wname [nwqid]);
                                if (walkChild(cfidnode, protocol.fT.wname [nwqid], out wfidnode))
                                {
                                    //Console.WriteLine ("Walkchild succeeded");
                                    wqid [nwqid] = wfidnode.dir.qid;
                                }
                                else if (nwqid == 0)
                                {
                                    protocol.doRerror(tag, "first nwname walk failed");
                                    //Console.WriteLine ("first nwname walk failed");
                                    break;
                                }
                            }
                            // take the last successful walk and make that the new fid.
                            fidlist.Add(protocol.fT.newfid, wfidnode);
                        }
                        else                             // simply create a new fid for the current file
                        {
                            fidlist.Add(protocol.fT.newfid, cfidnode);
                            nwqid    = 1;
                            wqid [0] = cfidnode.dir.qid;
                        }
                        printfidlist();
                        protocol.doRwalk(tag, nwqid, wqid);

                        break;

                    case (byte)proto.Tremove:
                        Inode rfidnode;
                        if (fidlist.TryGetValue(fid, out rfidnode) == false)
                        {
                            protocol.doRerror(tag, "Unrecognized fid");
                            break;
                        }
                        if (delnode(rfidnode.parent, rfidnode.dir.name))
                        {
                            listclunk(fid);
                            protocol.doRremove(tag);
                        }
                        break;

                    case (byte)proto.Tversion:
                        if (protocol.fT.version.Equals("9P2000"))
                        {
                            protocol.doRversion(tag);
                        }
                        else
                        {
                            protocol.doRerror(tag, "Version :" + protocol.fT.version + " not supported.");
                        }

                        break;

                    case (byte)proto.Tflush:
                        //do nothing
                        protocol.doRflush(tag);
                        break;

                    case (byte)proto.Tcreate:
                        Inode dirnode;
                        if (fidlist.TryGetValue(fid, out dirnode) == false)
                        {
                            protocol.doRerror(tag, "Unrecognized fid");
                            break;
                        }
                        byte type = (byte)proto.QTFILE;
                        Console.WriteLine("perm = {0}", BitConverter.ToString((BitConverter.GetBytes(protocol.fT.perm))));
                        if ((protocol.fT.perm & (uint)proto.DMDIR) != 0)
                        {
                            type = (byte)proto.QTDIR;
                            Console.WriteLine("Created Directory");
                        }
                        Inode newfile = createnode(protocol.fT.name, protocol.fT.perm, clientname, "client", type);
                        dirnode.children.Add(newfile);
                        newfile.parent = dirnode;
                        newfile.mode   = protocol.fT.mode;
                        newfile.data   = new Byte[0];
                        protocol.doRcreate(tag, new Qid(), protocol.mmsgsz);
                        break;

                    case (byte)proto.Twstat:
                        Inode wnode;
                        Dir   tdir;
                        if (fidlist.TryGetValue(fid, out wnode) == false)
                        {
                            protocol.doRerror(tag, "Unrecognized fid");
                            break;
                        }
                        tdir      = protocol.convM2D(protocol.fT.stat, 0);
                        wnode.dir = tdir;                         // some permissions stuff should precede this.
                        protocol.doRwstat(tag);
                        break;

                    case (byte)proto.Twrite:
                        Inode wrnode;
                        if (fidlist.TryGetValue(fid, out wrnode) == false)
                        {
                            protocol.doRerror(tag, "Unrecognized fid");
                            break;
                        }
                        if (wrnode.mode.Equals((uint)proto.OREAD))
                        {
                            protocol.doRerror(tag, "File not opened for writing");
                            break;
                        }
                        int woffset = (int)protocol.fT.offset;
                        if ((wrnode.mode & (uint)proto.OAPPEND) > 0)
                        {
                            woffset = (int)wrnode.data.Length;
                        }
                        if (woffset > (int)wrnode.data.Length)
                        {
                            protocol.doRerror(tag, "offset out of bounds");
                            break;
                        }
                        Byte[] newdata = new Byte[woffset + (int)protocol.fT.count];
                        Array.Copy(wrnode.data, 0, newdata, 0, woffset);                          //copy existing data before offset
                        Array.Copy(protocol.fT.data, 0, newdata, woffset, protocol.fT.count);
                        wrnode.data          = newdata;
                        wrnode.dir.muid      = clientname;
                        wrnode.dir.mtime     = (uint)DateTime.Now.ToFileTime();
                        wrnode.dir.qid.vers += 1;
                        wrnode.dir.length    = (ulong)wrnode.data.Length;
                        protocol.doRwrite(tag, protocol.fT.count);
                        Console.WriteLine("new contents:{0}", BitConverter.ToString(wrnode.data, 0, wrnode.data.Length));
                        break;

                    default:
                        throw new ninepexception("unrecognized message type");
                    }
                    printPacket(protocol.pktR, "R");
                }
            }
        }
Exemplo n.º 11
0
 public Rauth(Qid aqid)
 {
     Type    = (byte)MessageType.Rauth;
     Aqid    = aqid;
     Length += Constants.Qidsz;
 }
Exemplo n.º 12
0
 public Rattach(Qid qid)
 {
     Type    = (byte)MessageType.Rattach;
     Qid     = qid;
     Length += Constants.Qidsz;
 }