public void send(ControlPacket packet)
        {
            //ControlPacket packet = new ControlPacket(ControlInterface.CALL_REQUEST, 0, resourceIdentifier);
            string data = JMessage.Serialize(JMessage.FromValue(packet));

            writer.Write(data);
        }
Example #2
0
        public void SendMessage(Socket handler, string rec, Header type, RSAParameters key)
        {
            switch (type)
            {
            case Header.Login:
                JMessage jmess = JMessage.Deserialize(rec);
                KeyValuePair <string, string> kv = jmess.ToValue <KeyValuePair <string, string> >();

                string answ = ResolveLogin(kv.Key, kv.Value);

                Send(handler, answ, key);

                break;

            case Header.Message:
                jmess = JMessage.Deserialize(rec);
                //Console.WriteLine(Convert.FromBase64String(rec));
                Message message = jmess.ToValue <Message>();
                ServerApp.Messages.AddMessage(message);
                Send(handler, JMessage.Serialize(JMessage.FromValue <IList <CommonApp.Model.Message> >(ServerApp.Messages.GetMessagesOfUser(message.UserNameFrom), Header.Message)), key);
                break;

            case Header.Messages:
                string user = JMessage.Deserialize(rec).ToValue <string>();
                Send(handler, JMessage.Serialize(JMessage.FromValue <IList <CommonApp.Model.Message> >(ServerApp.Messages.GetMessagesOfUser(user), Header.Messages)), key);
                break;

            case Header.Users:
                jmess = JMessage.FromValue <Dictionary <string, string> >(ServerApp.Users.UsersDict, Header.Users);
                Send(handler, JMessage.Serialize(jmess), key);
                break;
            }
        }
        public byte[] EncryptMessage(string msg, Header type)
        {
            KeyValuePair <string, string> messageAnduser = new KeyValuePair <string, string>(Username, msg);

            JMessage jmess     = JMessage.FromValue <KeyValuePair <string, string> >(messageAnduser, type);
            string   serialize = JMessage.Serialize(jmess);

            byte[] msgbytes = Encoding.Unicode.GetBytes(serialize);
            return(CommonApp.RSAClass.RSAEncrypt(msgbytes, ServerPublicKey, false));
        }
Example #4
0
        public void SendKey(Socket handler)
        {
            //Console.WriteLine("Key {0} ", Convert.ToBase64String(rijndael.Key));


            string json = JMessage.Serialize(JMessage.FromValue <RSAParameters>(_RSA.ExportParameters(false), Header.ExchangePKs));

            Console.WriteLine(json);

            handler.Send(Encoding.ASCII.GetBytes(json));
        }
Example #5
0
        public void SendUnencrypted(Socket handler, string rec)
        {
            JMessage des = JMessage.Deserialize(rec);
            KeyValuePair <string, RSAParameters> kv2 = des.ToValue <KeyValuePair <string, RSAParameters> >();

            ServerApp.Users.SetUserKey(kv2.Key, kv2.Value);
            JMessage jmess;

            jmess = JMessage.FromValue <RSAParameters>(_RSA.ExportParameters(false), Header.ExchangePKs);
            handler.Send(Encoding.ASCII.GetBytes(JMessage.Serialize(jmess)));
        }
        public void SendKeyAndArray(Socket handler)
        {
            Console.WriteLine("Key {0} ", Convert.ToBase64String(_rijndael.Key));
            Console.WriteLine("IV {0} ", Convert.ToBase64String(_rijndael.IV));
            KeyValuePair <byte[], byte[]> msg = new KeyValuePair <byte[], byte[]>(_rijndael.Key, _rijndael.IV);
            string json = JMessage.Serialize(JMessage.FromValue <KeyValuePair <byte[], byte[]> >(msg, Header.Handshake));

            Console.WriteLine(json);

            handler.Send(Encoding.ASCII.GetBytes(json));
        }
        internal override byte[] EncryptMessage(string serialized, Header type)
        {
            //Add a new layer of information: the username for each message so that the server can identify the sender
            KeyValuePair <string, string> messageAnduser = new KeyValuePair <string, string>(_username, serialized);

            JMessage jmess     = JMessage.FromValue <KeyValuePair <string, string> >(messageAnduser, type);
            string   serialize = JMessage.Serialize(jmess);

            byte[] msgbytes = Encoding.Unicode.GetBytes(serialize);
            return(CommonApp.RSAClass.RSAEncrypt(msgbytes, ServerPublicKey, false));
        }
Example #8
0
        public static void sendTopologyInit(string ip)
        {
            NetNode.log(DateTime.Now.ToLongTimeString() + " [LRM -> RC] inittopology: " + ip, ConsoleColor.Yellow);

            RCtoLRMSignallingMessage protocol = new RCtoLRMSignallingMessage();

            protocol.State    = RCtoLRMSignallingMessage.LRM_INIT;
            protocol.NodeName = ip;
            String send_object = JMessage.Serialize(JMessage.FromValue(protocol));

            writer.Write(send_object);
        }
Example #9
0
        public static void sendDeletedCC(string from, int port, string to)
        {
            string toSend = port.ToString() + " " + to;

            NetNode.log(DateTime.Now.ToLongTimeString() + " [node -> CC] info about deletion: " + toSend, ConsoleColor.Yellow);

            CCtoCCSignallingMessage protocol = new CCtoCCSignallingMessage();

            protocol.State = CCtoCCSignallingMessage.RE_ROUTE_QUERY;
            String send_object = JMessage.Serialize(JMessage.FromValue(protocol));

            writer.Write(send_object);
        }
Example #10
0
        public static void sendDeleted(string from, int port, string to)
        {
            string toSend = port.ToString() + " " + to;

            NetNode.log(DateTime.Now.ToLongTimeString() + " [LRM -> RC] info about deletion: " + toSend, ConsoleColor.Yellow);

            RCtoLRMSignallingMessage protocol = new RCtoLRMSignallingMessage();

            protocol.State = RCtoLRMSignallingMessage.LRM_TOPOLOGY_DELETE;
            //protocol.ConnectedNodePort = port;
            protocol.ConnectedNode = to;
            String send_object = JMessage.Serialize(JMessage.FromValue(protocol));

            writer.Write(send_object);
        }
Example #11
0
        public static void sendTopologyDeallocated(int port, int no_vc3)
        {
            string to;

            LRM.connections.TryGetValue(port, out to);
            NetNode.log(DateTime.Now.ToLongTimeString() + " [LRM -> RC] topology deallocated: " + to + " " + no_vc3, ConsoleColor.Red);

            RCtoLRMSignallingMessage protocol = new RCtoLRMSignallingMessage();

            protocol.State         = RCtoLRMSignallingMessage.LRM_TOPOLOGY_DEALLOCATED;
            protocol.ConnectedNode = to;
            protocol.AllocatedSlot = no_vc3;
            String send_object = JMessage.Serialize(JMessage.FromValue(protocol));

            writer.Write(send_object);
        }
Example #12
0
        public static void sendConfirmation(int port, int no_vc3, bool flag)
        {
            CCtoCCSignallingMessage protocol = new CCtoCCSignallingMessage();

            if (flag == true)
            {
                protocol.State = CCtoCCSignallingMessage.CC_LOW_CONFIRM;
                NetNode.log(DateTime.Now.ToLongTimeString() + " [node -> CC] CONFIRM", ConsoleColor.Green);
            }
            else
            {
                protocol.State = CCtoCCSignallingMessage.CC_LOW_REJECT;
                NetNode.log(DateTime.Now.ToLongTimeString() + " [node -> CC] REJECT", ConsoleColor.Red);
            }
            String send_object = JMessage.Serialize(JMessage.FromValue(protocol));

            writer.Write(send_object);
        }
Example #13
0
        private void UpdateTween(object sender, EventArgs e)
        {
            Tween tween = new Tween();

            try
            {
                tween.Duration   = float.Parse(durationField.Text);
                tween.EasingType = (EasingType)Enum.Parse(typeof(EasingType), easeTypeField.Text);
                string tweenData = JMessage.Serialize(JMessage.FromValue(tween));

                byte[] msg = Encoding.ASCII.GetBytes(tweenData);
                ClientSocket.Socket.Send(msg);
            }
            catch
            {
                MessageBox.Show("Could not update tween. Make sure all fields are filled out.");
            }
        }
Example #14
0
        public string SendAndReceiveMessage <T>(T obj, Header type = Header.Unspecified)
        {
            byte[] bytes = new byte[1024];
            try
            {
                // Establish the remote endpoint for the socket.
                // This example uses port 11000 on the local computer.
                IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress   ipAddress  = ipHostInfo.AddressList[0];
                IPEndPoint  remoteEP   = new IPEndPoint(ipAddress, 11000);

                // Create a TCP/IP  socket.
                Socket sender = new Socket(ipAddress.AddressFamily,
                                           SocketType.Stream, ProtocolType.Tcp);

                // Connect the socket to the remote endpoint. Catch any errors.
                try
                {
                    sender.Connect(remoteEP);

                    Console.WriteLine("Socket connected to {0}",
                                      sender.RemoteEndPoint.ToString());

                    JMessage msg        = JMessage.FromValue <T>(obj, type);
                    string   serialized = JMessage.Serialize(msg);



                    //byte[] send = Encoding.ASCII.GetBytes(serialized);
                    // Send the data through the socket.
                    int    bytesSent, bytesRec;
                    string received;
                    if (type == Header.Handshake)
                    {
                        bytesSent = sender.Send(Encoding.ASCII.GetBytes(serialized));
                        bytesRec  = sender.Receive(bytes);
                        received  = Encoding.ASCII.GetString(bytes);
                        var cleaned = received.Replace("\0", string.Empty);
                        Console.WriteLine("Echoed = {0}", cleaned);
                        received = cleaned;
                    }
                    else
                    {
                        Console.WriteLine("Going to send {0}", Convert.ToBase64String(EncryptMessage(serialized)));
                        bytesSent = sender.Send(EncryptMessage(serialized));
                        Console.WriteLine("Sent data");
                        bytesRec = sender.Receive(bytes);
                        Console.WriteLine("Received data");
                        CommonApp.RijndaelClass.TruncateBytesArray(ref bytes);
                        Console.WriteLine("Received, truncated and got {0}", Convert.ToBase64String(bytes));
                        received = DecryptMessage(Convert.ToBase64String(bytes));
                        Console.WriteLine("Decrypted message and got {0}", received);

                        received = received.TrimEnd('\0');
                        Console.WriteLine("Trimmed {0}", received);
                    }

                    sender.Close();


                    return(received);
                }
                catch (ArgumentNullException ane)
                {
                    Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
                }
                catch (SocketException se)
                {
                    Console.WriteLine("SocketException : {0}", se.ToString());
                }
                catch (Exception e)
                {
                    Console.WriteLine("Unexpected exception : {0}", e.ToString());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return(null);
        }
Example #15
0
        private void Listen()
        {
            TcpClient    clienttmp = new TcpClient("127.0.0.1", this.port);
            BinaryReader reader    = new BinaryReader(clienttmp.GetStream());
            BinaryWriter writer    = new BinaryWriter(clienttmp.GetStream());

            try
            {
                while (true)
                {
                    string            received_data     = reader.ReadString();
                    JSON              received_object   = JSON.Deserialize(received_data);
                    ManagmentProtocol received_Protocol = received_object.Value.ToObject <ManagmentProtocol>();

                    if (received_Protocol.State == ManagmentProtocol.WHOIS)
                    {
                        NetNode.log(DateTime.Now.ToLongTimeString() + " [Management]" + " receiving: receivedWhoIs", ConsoleColor.Blue);
                        //send name to management
                        ManagmentProtocol protocol = new ManagmentProtocol();
                        protocol.Name = this.virtualIp;
                        String send_object = JMessage.Serialize(JMessage.FromValue(protocol));
                        writer.Write(send_object);
                        NetNode.log(DateTime.Now.ToLongTimeString() + " [Management]" + " sending: " + protocol.Name, ConsoleColor.Blue);
                    }
                    else if (received_Protocol.State == ManagmentProtocol.ROUTINGTABLES)
                    {
                        NetNode.log(DateTime.Now.ToLongTimeString() + " [Management]" + " receiving: receivedroutingtable", ConsoleColor.Blue);
                        //receiving fibs
                        if (received_Protocol.RoutingTable != null)
                        {
                            foreach (var fib in received_Protocol.RoutingTable)
                            {
                                SwitchingField.addToSwitch(fib);
                                //adding fib for two-way communication
                                SwitchingField.addToSwitch(new FIB(fib.oport, fib.out_cont, fib.iport, fib.in_cont));
                            }
                        }
                    }
                    else if (received_Protocol.State == ManagmentProtocol.ROUTINGENTRY)
                    {
                        NetNode.log(DateTime.Now.ToLongTimeString() + " [Management]" + " receiving: receivedroutingentry", ConsoleColor.Blue);
                        //receiving fibs
                        if (received_Protocol.RoutingEntry != null)
                        {
                            SwitchingField.addToSwitch(received_Protocol.RoutingEntry);
                            //adding fib for two-way communication
                            SwitchingField.addToSwitch(new FIB(received_Protocol.RoutingEntry.oport, received_Protocol.RoutingEntry.out_cont,
                                                               received_Protocol.RoutingEntry.iport, received_Protocol.RoutingEntry.in_cont));
                        }
                    }
                    else if (received_Protocol.State == ManagmentProtocol.INTERFACEINFORMATION)
                    {
                        NetNode.log(DateTime.Now.ToLongTimeString() + " [Management]" + " receiving: iterfaceinformation", ConsoleColor.Blue);
                        //send dictionary from LRM to management
                        ManagmentProtocol protocol = new ManagmentProtocol();
                        protocol.State      = ManagmentProtocol.INTERFACEINFORMATION;
                        protocol.Interfaces = LRM.getConn();
                        String send_object = JMessage.Serialize(JMessage.FromValue(protocol));
                        writer.Write(send_object);
                    }
                    else if (received_Protocol.State == ManagmentProtocol.GETTABLE)
                    {
                        NetNode.log(DateTime.Now.ToLongTimeString() + " [Management]" + " receiving: getTable", ConsoleColor.Blue);
                        //send dictionary from LRM to management
                        ManagmentProtocol protocol = new ManagmentProtocol();
                        protocol.State        = ManagmentProtocol.GETTABLE;
                        protocol.RoutingTable = SwitchingField.fib;
                        String send_object = JMessage.Serialize(JMessage.FromValue(protocol));
                        writer.Write(send_object);
                    }
                    else
                    {
                        NetNode.log("[Management] undefined protocol", ConsoleColor.Red);
                    }
                }
            }
            catch (Exception e)
            {
                NetNode.log("\nError sending signal: " + e.Message, ConsoleColor.Red);
                Thread.Sleep(2000);
                Environment.Exit(1);
            }
        }
        public void sendCCRequest(CCtoNCCSingallingMessage packet)
        {
            string data = JMessage.Serialize(JMessage.FromValue(packet));

            writer.Write(data);
        }
        private void commutation()
        {
            while (flag)
            {
                int opt = commOption();

                foreach (IPort iport in this.ports.iports)
                {
                    //check if there is frame in queue and try to process it
                    if (iport.input.Count > 0)
                    {
                        STM1 frame = iport.input.Dequeue();

                        //if (frame.vc4 != null)
                        if (opt != 1)
                        {
                            Console.WriteLine("vc4");
                            int out_pos           = -1;
                            VirtualContainer4 vc4 = frame.vc4;
                            out_pos = switchField.commutateContainer(vc4, iport.port);
                            if (out_pos != -1)
                            {
                                log("ok", ConsoleColor.Green);
                                this.ports.oports[out_pos].addToOutQueue(vc4);
                            }
                        }
                        //else if (frame.vc4.vc3List.Count > 0)
                        else
                        {
                            Console.WriteLine("vc3->vc4");
                            Console.WriteLine("unpacking container");
                            foreach (var vc in frame.vc4.vc3List)
                            {
                                VirtualContainer3 vc3 = vc.Value;
                                if (vc3 != null)
                                {
                                    int[] out_pos = { -1, -1 };
                                    out_pos = switchField.commutateContainer(vc3, iport.port, vc.Key);
                                    if (out_pos[0] != -1)
                                    {
                                        log("ok", ConsoleColor.Green);
                                        this.ports.oports[out_pos[0]].addToTempQueue(vc3, out_pos[1]);
                                    }
                                }
                            }
                        }
                        //else
                        //{
                        //Console.WriteLine("smth wrong with stm1");
                        //}
                    }
                }
                foreach (OPort oport in this.ports.oports)
                {
                    //packing STM from tempQueue to outqueue
                    oport.addToOutQueue();
                }

                foreach (OPort oport in this.ports.oports)
                {
                    //check if there is frame in queue and try to send it
                    if (oport.output.Count > 0)
                    {
                        STM1 frame = oport.output.Dequeue();
                        if (frame.vc4 != null || frame.vc4.vc3List.Count > 0)
                        {
                            try
                            {
                                pathList.Add(this.virtualIp);
                                Signal signal = new Signal(oport.port, frame, pathList);
                                consoleWriter("sending signal port: " + signal.port);
                                string data = JMessage.Serialize(JMessage.FromValue(signal));
                                Console.WriteLine(data);
                                writer.Write(data);
                            }
                            catch (Exception e)
                            {
                                log("\nError sending signal: " + e.Message, ConsoleColor.Red);
                                Thread.Sleep(2000);
                                Environment.Exit(1);
                            }
                        }
                    }
                }
                Thread.Sleep(125);
            }
        }