public IEndPoint AddEndPoint(string endpointName, EndPoint address, ICoapConfig config, Type[] layers)
        {
            MockChannel  channel  = new MockChannel(address, this);
            CoAPEndPoint endpoint = new CoAPEndPoint(channel, config);

            if (layers != null)
            {
                CoapStack stack = endpoint.Stack;

                foreach (IEntry <ILayer, INextLayer> e in stack.GetAll().ToArray())
                {
                    if (e.Name == "head" || e.Name == "tail")
                    {
                        continue;
                    }
                    if (!layers.Contains(e.Filter.GetType()))
                    {
                        stack.Remove(e.Filter);
                    }
                }
            }

            ChannelsByEndpoint.Add(address, new List <MockChannel>()
            {
                channel
            });

            return(endpoint);
        }
예제 #2
0
        private void CreateServer()
        {
            CoAPEndPoint endpoint = new CoAPEndPoint(0);

            _server = new CoapServer();

            //            _resource = new StorageResource(TARGET, CONTENT_1);
            //           _server.Add(_resource);

            Resource r2 = new EchoLocation("abc");

            _server.Add(r2);

            r2.Add(new EchoLocation("def"));

            _server.AddEndPoint(endpoint);
            _server.Start();
            _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port;
            Console.WriteLine($"Server port = {_serverPort}");

            SecurityContextSet oscoapContexts = new SecurityContextSet();

            _server.SecurityContexts.Add(SecurityContext.DeriveContext(secret, null, serverId, clientId));
            _server.SecurityContexts.OscoreEvents += ServerEventHandler;
        }
예제 #3
0
        private void CreateServer()
        {
            _server = new CoapServer();
            CoAPEndPoint endpoint = new CoAPEndPoint(_serverPort, _config);

            _server.AddEndPoint(endpoint);
            _server.MessageDeliverer = new MessageDeliverer(this);
            _server.Start();
        }
예제 #4
0
        private void CreateServer()
        {
            _server = new CoapServer();
            CoAPEndPoint endpoint = new CoAPEndPoint(_serverPort, _config);

            _server.AddEndPoint(endpoint);
            _server.MessageDeliverer = new MessageDeliverer(this);
            _server.SecurityContexts.Add(SecurityContext.DeriveContext(secret, null, serverId, clientId));
            _server.Start();
            _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port;
        }
예제 #5
0
        public void SetupServer()
        {
            Log.LogManager.Level = Log.LogLevel.Fatal;
            _config = new CoapConfig();
            _server = new CoapServer();
            CoAPEndPoint endpoint = new CoAPEndPoint(_serverPort, _config);

            _server.AddEndPoint(endpoint);
            _server.Add(new TestResource(TARGET));
            _server.Start();
        }
예제 #6
0
        private void CreateServer()
        {
            CoAPEndPoint endpoint = new CoAPEndPoint(0);

            _resource = new StorageResource(TARGET, CONTENT_1);
            _server   = new CoapServer();
            _server.Add(_resource);

            _server.AddEndPoint(endpoint);
            _server.Start();
            _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port;
        }
예제 #7
0
        public void SetupServer()
        {
            Log.LogManager.Level = Log.LogLevel.Fatal;
            CoAPEndPoint endpoint = new CoAPEndPoint();

            _server = new CoapServer();
            _server.Add(new AccResource());
            _server.Add(new NoAccResource());
            _server.AddEndPoint(endpoint);
            _server.Start();
            _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port;
        }
예제 #8
0
        public void SetupServer()
        {
            _server = new CoapServer();
            CoAPEndPoint ep = new CoAPEndPoint(0);

            _server.AddEndPoint(ep);

            Resource r1 = new PubSubResource("ps", true);

            _server.Add(r1);
            _server.Start();

            _port = ((System.Net.IPEndPoint)ep.LocalEndPoint).Port;
        }
예제 #9
0
        private void CreateServer()
        {
            CoAPEndPoint endpoint = new CoAPEndPoint(0);

            _resource = new TestResource(NAME_1, PAYLOAD);
            _server   = new CoapServer();
            _server
            .Add(new Resource(RES_A)
                 .Add(new Resource(RES_AA)
                      .Add(_resource
                           .Add(new TestResource(CHILD, CHILD_PAYLOAD)))));

            _server.AddEndPoint(endpoint);
            _server.Start();
            _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port;
        }
예제 #10
0
        private void CreateServer()
        {
            _config            = new CoapConfig();
            _config.NonTimeout = 10; // 10 ms

            CoAPEndPoint endpoint = new CoAPEndPoint(0, _config);

            _resource  = new ObserveResource(target1);
            _resource2 = new ObserveResource(target2);
            _server    = new CoapServer(_config);
            _server.Add(_resource);
            _server.Add(_resource2);

            _server.AddEndPoint(endpoint);
            _server.Start();
            _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port;
        }
예제 #11
0
        private void CreateServer()
        {
            CoAPEndPoint endpoint = new CoAPEndPoint(0);

            _resource = new StorageResource(TARGET, CONTENT_1);
            _server   = new CoapServer();
            _server.Add(_resource);

            _server.AddEndPoint(endpoint);
            _server.Start();
            _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port;

            Resource r2 = new EchoLocation("abc");

            _server.Add(r2);

            r2.Add(new EchoLocation("def"));
        }
예제 #12
0
        private void CreateServer()
        {
            CoAPEndPoint endpoint = new CoAPEndPoint(0);


            KeySet ks = new KeySet();

            ks.AddKey(psk);
            ks.AddKey(_clientSignKey.PublicKey());

            _resource = new EdhocResource(ks, serverSignKey);
            _server   = new CoapServer();
            _server.Add(_resource);

            _server.Add(new Hello("hello"));

            _server.AddEndPoint(endpoint);
            _server.Start();
            _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port;
        }
예제 #13
0
        static void Main(string[] args)
        {
            CoapConfig _config = new CoapConfig();

            _config.DefaultBlockSize = 32;
            _config.MaxMessageSize   = 32;

            CoapServer   server   = new CoapServer(_config);
            CoAPEndPoint endpoint = new CoAPEndPoint(CoapConstants.DefaultPort, _config);

            server.AddEndPoint(endpoint);

            server.Add(new Temp("/sensors/temp"));
            server.Add(new Humidity("/sensors/humidity"));
            server.Add(new Vcc3("/sensors/vcc3"));
            server.Add(new Storage("/data/buffer"));
            server.Add(new Light("/sensors/light"));
            server.Add(new Hops("/info/hops"));

            try
            {
                server.Start();

                Console.Write("CoAP server [{0}] is listening on", server.Config.Version);

                foreach (var item in server.EndPoints)
                {
                    Console.Write(" ");
                    Console.Write(item.LocalEndPoint);
                }
                Console.WriteLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
예제 #14
0
        public Client(int port)
        {
            _MessageDeliverer            = _CoapServer.MessageDeliverer;
            _CoapServer.MessageDeliverer = this;
            //_EndPoint = new CoAPEndPoint(port, CoapConfig.Default);

            //_EndPoint = new CoAPEndPoint(new FlowClientSecureChannel(port), CoapConfig.Default);
            //_EndPoint = new CoAPEndPoint(new FlowChannel(port), CoapConfig.Default);

            _Channel = new FlowClientChannel(port);

            UseCertificateFile("Client.pem");

            _EndPoint = new CoAPEndPoint(_Channel, CoapConfig.Default);
            _CoapServer.AddEndPoint(_EndPoint);
            _CoapServer.Add(_SecurityResources);
            _CoapServer.Add(_ServerResources);
            _CoapServer.Add(_BootsrapComplete);
            _Root           = _BootsrapComplete.Parent;
            _Timer.Interval = 30000;
            _Timer.Elapsed += new System.Timers.ElapsedEventHandler(_Timer_Elapsed);
        }
예제 #15
0
        private void CreateServer()
        {
            CoAPEndPoint endpoint = new CoAPEndPoint(0);

            _server = new CoapServer();

            //            _resource = new StorageResource(TARGET, CONTENT_1);
            //           _server.Add(_resource);

            Resource r2 = new EchoLocation("abc");

            _server.Add(r2);

            r2.Add(new EchoLocation("def"));

            _server.AddEndPoint(endpoint);
            _server.Start();
            _serverPort = ((System.Net.IPEndPoint)endpoint.LocalEndPoint).Port;

            SecurityContextSet oscoapContexts = new SecurityContextSet();

            SecurityContextSet.AllContexts.Add(SecurityContext.DeriveContext(_Secret, null, _ServerId, _ClientId));
        }
예제 #16
0
        private void CreateServer()
        {
            CoapConfig config = new CoapConfig();

            CoAPEndPoint endpoint = new CoAPEndPoint(0, config);

            _resource = new MulticastResource(MulticastTarget, MulticastResponse);
            _server   = new CoapServer();
            _server.Add(_resource);

            _server.AddEndPoint(endpoint);
            _server.Start();
            _serverPort = ((IPEndPoint)endpoint.LocalEndPoint).Port;

            endpoint.AddMulticastAddress(new IPEndPoint(multicastAddress3, _serverPort));
            endpoint.AddMulticastAddress(new IPEndPoint(multicastAddress4, _serverPort));
            endpoint.AddMulticastAddress(new IPEndPoint(multicastAddress, _serverPort + PortJump));
            endpoint.AddMulticastAddress(new IPEndPoint(multicastAddress2, _serverPort + PortJump));

            Resource r2 = new UnicastResource(UnicastTarget, UnicastResponse);

            _server.Add(r2);
        }
예제 #17
0
        public static bool AddEndPoint(Request request)
        {
            Uri    url    = request.URI;
            string scheme = url.Scheme;
            string server;

            if (scheme == "coap" || scheme == "coaps")
            {
                scheme = scheme + "+" + Transport.ToLower();
            }

            int port = url.Port;

            if (port == 0)
            {
                port = defaultPort[scheme];
            }

            server = $"{scheme}://{url.Host}:{port}";

            if (!endpoints.ContainsKey(scheme))
            {
                CoAPEndPoint ep;


                switch (scheme)
                {
#if DO_TCP
                case "coap+tcp":
                    ep = new TCPClientEndPoint();
                    break;

                case "coaps+tcp":
                    if (currentTlsKey == null)
                    {
                        Console.WriteLine("No current TLS key specified");
                        return(false);
                    }
                    TLSClientEndPoint tep = new TLSClientEndPoint(_TlsKeys[currentTlsKey]);
                    // tep.TlsEventHandler += OnTlsEvent;
                    ep = tep;
                    break;
#endif

                case "coap+udp":
                    ep = new CoAPEndPoint();
                    break;

                case "coaps+udp":
                    if (currentTlsKey == null)
                    {
                        Console.WriteLine("No current TLS key specified");
                        return(false);
                    }
                    DTLSClientEndPoint dep = new DTLSClientEndPoint(_TlsKeys[currentTlsKey]);
                    dep.TlsEventHandler += OnTlsEvent;
                    ep = dep;
                    break;

                default:
                    Console.WriteLine("Unknown schema");
                    return(false);
                }

                endpoints[server] = ep;
                ep.Start();

                ep.ReceivingSignalMessage += (sender, args) =>
                {
                    Console.WriteLine("Signal message from {0}", "???");
                    Console.WriteLine(args.Message.ToString());
                };
            }

            request.EndPoint = endpoints[server];


            return(true);
        }
예제 #18
0
        public static void Main(String[] args)
        {
            String             method     = null;
            Uri                uri        = null;
            String             payload    = null;
            Boolean            loop       = false;
            Boolean            byEvent    = false;
            OneKey             authKey    = null;
            SecurityContext    oscoap     = null;
            SecurityContextSet contextSet = null;

            if (args.Length == 0)
            {
                PrintUsage();
            }

            Int32 index = 0;

            foreach (String arg in args)
            {
                if (arg[0] == '-')
                {
                    if (arg.Equals("-l"))
                    {
                        loop = true;
                    }
                    else if (arg.Equals("-e"))
                    {
                        byEvent = true;
                    }
                    else if (arg.StartsWith("-psk="))
                    {
                        if (authKey == null)
                        {
                            authKey = new OneKey();
                            authKey.Add(COSE.CoseKeyKeys.KeyType, COSE.GeneralValues.KeyType_Octet);
                        }
                        authKey.Add(CoseKeyParameterKeys.Octet_k, CBORObject.FromObject(Encoding.UTF8.GetBytes(arg.Substring(5))));
                    }
                    else if (arg.StartsWith("-psk-id="))
                    {
                        if (authKey == null)
                        {
                            authKey = new OneKey();
                            authKey.Add(COSE.CoseKeyKeys.KeyType, COSE.GeneralValues.KeyType_Octet);
                        }
                        authKey.Add(COSE.CoseKeyKeys.KeyIdentifier, CBORObject.FromObject(Encoding.UTF8.GetBytes(arg.Substring(8))));
                    }
                    else if (arg.StartsWith("-oscoap="))
                    {
                        if (contextSet == null)
                        {
                            Console.WriteLine("Must have -oscoap-data before -oscoap");
                            Environment.Exit(1);
                        }

                        byte[] id = Encoding.UTF8.GetBytes(arg.Substring(8));

                        oscoap = contextSet.FindByGroupId(id).First();
                    }
                    else if (arg.StartsWith("-oscoap-data="))
                    {
                        contextSet = LoadContextSet(arg.Substring(13));
                    }
                    else
                    {
                        Console.WriteLine("Unknown option: " + arg);
                    }
                }
                else
                {
                    switch (index)
                    {
                    case 0:
                        method = arg.ToUpper();
                        break;

                    case 1:
                        try
                        {
                            uri = new Uri(arg);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Failed parsing URI: " + ex.Message);
                            Environment.Exit(1);
                        }
                        break;

                    case 2:
                        payload = arg;
                        break;

                    default:
                        Console.WriteLine("Unexpected argument: " + arg);
                        break;
                    }
                    index++;
                }
            }

            if (method == null || uri == null)
            {
                PrintUsage();
            }

            Request request = NewRequest(method);

            if (request == null)
            {
                Console.WriteLine("Unknown method: " + method);
                Environment.Exit(1);
            }

            if ("OBSERVE".Equals(method))
            {
                request.MarkObserve();
                loop = true;
            }
            else if ("DISCOVER".Equals(method) &&
                     (String.IsNullOrEmpty(uri.AbsolutePath) || uri.AbsolutePath.Equals("/")))
            {
                uri = new Uri(uri, "/.well-known/core");
            }

            CoAPEndPoint ep = null;

            if (uri.Scheme == "coaps")
            {
                if (authKey == null)
                {
                    Console.WriteLine("Must use the -psk option to provide an authentication key");
                    return;
                }
                ep = new DTLSClientEndPoint(authKey);
                ep.Start();
                request.EndPoint = ep;
            }

            request.URI = uri;
            if (payload != null)
            {
                request.SetPayload(payload, MediaType.TextPlain);
            }
            if (oscoap != null)
            {
                request.OscoapContext = oscoap;
            }

            // uncomment the next line if you want to specify a draft to use
            // request.EndPoint = CoAP.Net.EndPointManager.Draft13;

            Console.WriteLine(Utils.ToString(request));

            try
            {
                if (byEvent)
                {
                    request.Respond += delegate(Object sender, ResponseEventArgs e)
                    {
                        Response response = e.Response;
                        if (response == null)
                        {
                            Console.WriteLine("Request timeout");
                        }
                        else
                        {
                            Console.WriteLine(Utils.ToString(response));
                            Console.WriteLine("Time (ms): " + response.RTT);
                        }
                        if (!loop)
                        {
                            if (ep != null)
                            {
                                ep.Stop();
                            }
                            Environment.Exit(0);
                        }
                    };
                    request.Send();
                    while (true)
                    {
                        Console.ReadKey();
                    }
                }
                else
                {
                    // uncomment the next line if you need retransmission disabled.
                    // request.AckTimeout = -1;

                    request.Send();

                    do
                    {
                        Console.WriteLine("Receiving response...");

                        Response response = null;
                        response = request.WaitForResponse();

                        if (response == null)
                        {
                            Console.WriteLine("Request timeout");
                            break;
                        }
                        else
                        {
                            Console.WriteLine(Utils.ToString(response));
                            Console.WriteLine("Time elapsed (ms): " + response.RTT);

                            if (response.ContentType == MediaType.ApplicationLinkFormat)
                            {
                                IEnumerable <WebLink> links = LinkFormat.Parse(response.PayloadString);
                                if (links == null)
                                {
                                    Console.WriteLine("Failed parsing link format");
                                    Environment.Exit(1);
                                }
                                else
                                {
                                    Console.WriteLine("Discovered resources:");
                                    foreach (var link in links)
                                    {
                                        Console.WriteLine(link);
                                    }
                                }
                            }
                        }
                    } while (loop);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Failed executing request: " + ex.Message);
                Console.WriteLine(ex);
                if (ep != null)
                {
                    ep.Stop();
                }
                Environment.Exit(1);
            }
        }