obgm / libcoap

A CoAP (RFC 7252) implementation in C
Other
777 stars 421 forks source link

[Question] Proxy-scheme and proxy-uri example with coap-client and coap-server applications #1423

Closed fpic78 closed 1 month ago

fpic78 commented 1 month ago

I'm trying to understand how does it work proxy-scheme and proxy-uri using coap-client and coap-server application. Is it possible? Is there any example on how to configure them properly and verify that is working fine? Can you provide command lines to be used for that purpose? Thanks.

mrdeep1 commented 1 month ago

I'm trying to understand how does it work proxy-scheme and proxy-uri using coap-client and coap-server application. Is it possible?

Simple answer is Yes.

See coap-client(5) and coap-server(5) for a full explanation of the options available. The -P option in both cases describes how to use the proxy logic for the specific application. For the coap-client, you will need to use the -O option to define a proxy-scheme. So, for the client, you can use

$ examples/coap-client -v7 -O39,coap coap://localhost
   or (edit - text corrected)
$ examples/coap-client -v7 -P coap://upstream-proxy coap://localhost/.well-known/core

substituting as appropriate. For the server, you can do something like

$ examples/coap-server -v7 -P,127.0.0.2

which routes the proxy request on to the host defined by the client, unless the client specifies the host as 12.7.0.0.2, when the request is not sent on to the requested server, but treated as a local connection.

fpic78 commented 1 month ago

Many thanks for your quick reply.

I tried with $ examples/coap-client -v7 -O39,coap coap://localhost $ examples/coap-server -v7 -P,127.0.0.2

and it works fine it seems.

From logic point of view, with PROXY-SCHEME option I can set coap/coaps/coap+tcp/coaps+tcp configuration in order to forward it back and forth to an external server (that can be also http?). Is this right? Do I understand it well?

When I substitute client string with -P option I got following error

$ examples/coap-client -v7 -P coap://upstream-proxy/.well-known/core coap://localhost May 28 15:11:14.530 ERR invalid CoAP Proxy definition error specifying proxy address

What is wrong in this case?

With PROXY-URI option and adding URI into my CoAP request instead, I use a coap-proxy server to forward my request to another proxy device. Is that right?

Thanks.

mrdeep1 commented 1 month ago

From logic point of view, with PROXY-SCHEME option I can set coap/coaps/coap+tcp/coaps+tcp configuration in order to forward it back and forth to an external server (that can be also http?). Is this right? Do I understand it well?

Yes, you get the principle here. However, the examples coap-server does not support making an ongoing http/https connection - someone just needs to write that code.

$ examples/coap-client -v7 -P coap://upstream-proxy/.well-known/core coap://localhost

Oops - that should read

$ examples/coap-client -v7 -P coap://upstream-proxy coap://localhost/.well-known/core

where upstream-proxy is replaced with the server name / IP address to connect to.

With PROXY-URI option and adding URI into my CoAP request instead, I use a coap-proxy server to forward my request to another proxy device. Is that right?

There is nothing stopping you having a set of intermediate proxies that eventually connect to a real server.

fpic78 commented 1 month ago

I used following sequence (server) $examples/coap-server -v7 (proxy-server) $examples/coap-server -p 2020 -P,127.0.0.1:5683 -v9 (client) $examples/coap-client -v7 -P coap://127.0.0.1:2020 coap://localhost:5683/.well-known/core

and I can see that request is forwarded to server from proxy and return back to client.

In any case libcoap server and client do support only CoAP protocol. Proxy server should be always able to understand CoAP protocol and eventually transform these request into HTTP format. Is this right?

Thanks

mrdeep1 commented 1 month ago

A proxy server can map between CoAP and HTTP as per RFC 7252.

This support has not been added into the examples coap-server, but can be added if someone wants to work on the code.

fpic78 commented 1 month ago

Ok. Thanks for your support.