diff --git a/examples/c-bindings/main.c b/examples/c-bindings/main.c index 909da4d4..d1cb049e 100644 --- a/examples/c-bindings/main.c +++ b/examples/c-bindings/main.c @@ -86,6 +86,14 @@ int main(int argc, char *argv[]) if (isError(response)) return 1; + + /* + // To use dns discovery, and retrieve nodes from a enrtree url + response = waku_dns_discovery("enrtree://AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM@test.waku.nodes.status.im", "", 0); // Discover Nodes + if (isError(response)) + return 1; + printf("Discovered nodes: %s\n", response); + */ /* // To see a store query in action: diff --git a/library/README.md b/library/README.md index 1fb29ef4..8348712b 100644 --- a/library/README.md +++ b/library/README.md @@ -1018,6 +1018,38 @@ An `error` message otherwise. } ``` +## DNS Discovery + +### `extern char* waku_dns_discovery(char* url, char* nameserver, int timeoutMs)` +Returns a list of multiaddress given a url to a DNS discoverable ENR tree + +**Parameters** + +1. `char* url`: URL containing a discoverable ENR tree +2. `char* nameserver`: The nameserver to resolve the ENR tree url. + If `NULL` or empty, it will automatically use the default system dns. +3. `int timeoutMs`: Timeout value in milliseconds to execute the call. + If the function execution takes longer than this value, + the execution will be canceled and an error returned. + Use `0` for no timeout. + +**Returns** + +A [`JsonResponse`](#jsonresponse-type). +If the execution is successful, the `result` field contains an array with multiaddresses. +An `error` message otherwise. + +```json +{ + "result": [ + "/ip4/134.209.139.210/tcp/30303/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ", + "/ip4/104.154.239.128/tcp/30303/p2p/16Uiu2HAmJb2e28qLXxT5kZxVUUoJt72EMzNGXB47Rxx5hw3q4YjS", + "/ip4/47.242.210.73/tcp/30303/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm" + ] +} +``` + + ## Utils ### `extern char* waku_utils_base64_encode(char* data)` diff --git a/library/api_discovery.go b/library/api_discovery.go new file mode 100644 index 00000000..6d4afb17 --- /dev/null +++ b/library/api_discovery.go @@ -0,0 +1,19 @@ +package main + +import ( + "C" + + mobile "github.com/status-im/go-waku/mobile" +) + +// RetrieveNodes returns a list of multiaddress given a url to a DNS discoverable ENR tree +// The nameserver can optionally be specified to resolve the enrtree url. Otherwise NULL or +// empty to automatically use the default system dns. +// If ms is greater than 0, the subscription must happen before the timeout +// (in milliseconds) is reached, or an error will be returned +// +//export waku_dns_discovery +func waku_dns_discovery(url *C.char, nameserver *C.char, ms C.int) *C.char { + response := mobile.DnsDiscovery(C.GoString(url), C.GoString(nameserver), int(ms)) + return C.CString(response) +} diff --git a/mobile/api_discovery.go b/mobile/api_discovery.go new file mode 100644 index 00000000..575093d5 --- /dev/null +++ b/mobile/api_discovery.go @@ -0,0 +1,39 @@ +package gowaku + +import ( + "context" + "time" + + "github.com/status-im/go-waku/waku/v2/dnsdisc" +) + +func DnsDiscovery(url string, nameserver string, ms int) string { + var ctx context.Context + var cancel context.CancelFunc + + if ms > 0 { + ctx, cancel = context.WithTimeout(context.Background(), time.Duration(int(ms))*time.Millisecond) + defer cancel() + } else { + ctx = context.Background() + } + + var dnsDiscOpt []dnsdisc.DnsDiscoveryOption + if nameserver != "" { + dnsDiscOpt = append(dnsDiscOpt, dnsdisc.WithNameserver(nameserver)) + } + + nodes, err := dnsdisc.RetrieveNodes(ctx, url, dnsDiscOpt...) + if err != nil { + return MakeJSONResponse(err) + } + + var ma []string + for _, n := range nodes { + for _, addr := range n.Addresses { + ma = append(ma, addr.String()) + } + } + + return PrepareJSONResponse(ma, nil) +}