Repository navigation
resolve: provide service resolve over varlink - #23750
Conversation
f858866 to
2f5fae7
Compare
|
Which systemd component uses the varlink method? |
Sorry, I cannot follow. This provides a verlink method, but no one use it... Unlike DBus methods, we do not expose varlink methods, and only they are used internally, at least currently. |
|
Our basic goal is to enable mDNS service discovery and resolution like in avahi. |
I'm not sure why it can't use DBus for that. If it should be integrated with |
| if (r < 0) | ||
| return r; | ||
| } | ||
| } |
There was a problem hiding this comment.
this loop exists in almost the exact same way in vl_method_resolve_hostname_complete() already. please unify it in a common helper.
There was a problem hiding this comment.
Updated in the latest commit
|
So I think this makes sense to add. But we really need some test case for this, that checks that this even remotely does what is intended. For the address/hostname resolution we pretty quickly notice if things go wrong, since it's used all the time by nss-resolve. But this here has no user in our own codebase, hence relaly needs some proper testcase. |
51a95f6 to
b838245
Compare
Testing is being discussed in #22532 (at least for mDNS) |
b838245 to
0ec4cc6
Compare
poettering
left a comment
There was a problem hiding this comment.
this really needs some form of testing, i.e some simple addition to TEST-75-RESOLVED.
| static const JsonDispatch dispatch_table[] = { | ||
| { "name", JSON_VARIANT_STRING, json_dispatch_string, offsetof(LookupParametersResolveService, name), JSON_MANDATORY }, | ||
| { "type", JSON_VARIANT_STRING, json_dispatch_string, offsetof(LookupParametersResolveService, type), JSON_MANDATORY }, | ||
| { "domain", JSON_VARIANT_STRING, json_dispatch_string, offsetof(LookupParametersResolveService, domain), JSON_MANDATORY }, |
There was a problem hiding this comment.
please use json_dispatch_const_string(). That way the strings don't have to be copied. Given you don't need them after this function finished that sounds like a good idea.
| DnsQuestion *question, | ||
| DnsQuery *q, | ||
| DnsResourceRecord **canonical, | ||
| bool search_domain_flag) { |
There was a problem hiding this comment.
hmmm, why not make this parameter a const char* search_domain? i.e. already call DNS_SEARCH_DOMAIN_NAME() in the caller where appropriate and pass NULL here if not appropriate. I think that would be more readable.
| r = json_variant_append_array(array, entry); | ||
| if (r < 0) | ||
| goto finish; | ||
| return r; |
There was a problem hiding this comment.
i'd really update the array first, since this can fail, and if it does then you already updated *canonical... if you switch this, then the function will either fail or succeed, but never return half-initialized data...
| if (i->length <= 0) | ||
| continue; | ||
|
|
||
| r = json_build(&entry, JSON_BUILD_BYTE_ARRAY(i->data, i->length)); |
There was a problem hiding this comment.
please encode this as base63, not as byte array.
also, use json_build() only if you build complex json objects. for simple stuff just call the indiviudal constructors directly, i.e.:
r = json_variant_new_base64(&entry, i->data, i->length);| r = json_variant_append_array(_txt, entry); | ||
| if (r < 0) | ||
| return r; | ||
| } |
There was a problem hiding this comment.
this leaks memory, as you do not free the entry object here. i.e. move the declaration of entry into the loop
| if (r < 0) | ||
| return r; | ||
|
|
||
| return 1; |
There was a problem hiding this comment.
so we generally have this rule that parameters that are purely output should be filled in only on success, and not touched on failure. here, you will touch _srv early on, even if later stuff can still fail. Hence, please always build your stuff in temporary vairables, and only if you know nothing can fail anymore, move it to the return parameters. (usually TAKE_PTR() is a good way to do so).
also, please name return variables ret_xyz as indicated above
|
|
||
| static void resolve_service_all_complete(DnsQuery *query) { | ||
| _cleanup_(dns_query_freep) DnsQuery *q = query; | ||
| _cleanup_(json_variant_unrefp) JsonVariant *_srv = NULL, *_addr = NULL, *_txt = NULL, *_norm = NULL, *_canonical = NULL; |
| JSON_BUILD_PAIR("type", JSON_BUILD_STRING(type)), | ||
| JSON_BUILD_PAIR("domain", JSON_BUILD_STRING(domain)))); | ||
| if (r < 0) | ||
| goto finish; |
There was a problem hiding this comment.
why build this object separately? you can just build this as part of varlink_replyb(), simply by inserting JSON_BUILD_OBJECT() where you currently use JSON_BUILD_VARIANT() for the canonical field
| typedef struct LookupParametersResolveService { | ||
| char *name; | ||
| char *type; | ||
| char *domain; |
There was a problem hiding this comment.
as mentioned further down, please use const char* here, and fill in the fields via json_dispatch_const_string() as then you don't need to copy them, and also not free them
|
and sorry here too for the slow slow reviews. mea culpa! |
0ec4cc6 to
222c2c9
Compare
|
An -rc1 tag has been created and a release is being prepared, so please note that PRs introducing new features and APIs will be held back until the new version has been released. |
222c2c9 to
a5aa61e
Compare
|
@poettering addressed your comments. |
a5aa61e to
86594db
Compare
86594db to
066dcb0
Compare
|
@poettering added test for resolve over varlink in TEST-75-RESOLVED |
066dcb0 to
4a6b708
Compare
|
Rebased and resolved conflicts. |
| assert(rr->key); | ||
|
|
||
| if (rr->key->type != DNS_TYPE_TXT) | ||
| return 0; |
There was a problem hiding this comment.
you return from the function here successfully (i.e. with an error code >= 0).
according to our coding style functions should initialize all return paramaters on success (and leave them untouched on failure). THus, you need to add *ret_txt = NULL here, if you return 0.
| if (r < 0) | ||
| return r; | ||
|
|
||
| r = json_variant_append_array(ret_txt, entry); |
There was a problem hiding this comment.
hmm, so now i am confused. our coding style says that you should name return parameters ret_xyz. A return parameter is a purely output param. But here you extend what you get. Hence either the code is wrong or the name (i.e. drop the "ret_" prefix from the parmaete name)
my comment regarding return parameter initialization above is rooted in the misunderstanding that I thought this was a purely output param because of the name, which it appaerntly isn't after all
There was a problem hiding this comment.
dropped the ret_ prefix.
|
|
||
| static void resolve_service_all_complete(DnsQuery *query) { | ||
| _cleanup_(dns_query_freep) DnsQuery *q = query; | ||
| _cleanup_(json_variant_unrefp) JsonVariant *ret_srv = NULL, *ret_addr = NULL, *ret_txt = NULL, *ret_norm = NULL; |
There was a problem hiding this comment.
please don't name local variables ret_xyz. As per our coding style that's for output-only (i.e. "return" params), hence never for local variables. just drop the ret_ prefix
| grep -qF '"srv":{"priority":10,"weight":5,"port":1234,"hostname":"myservice.signed.test"}' "$VL_RESOLVE_LOGS" | ||
| grep -qF '{"ifindex":4,"family":10,"address":[253,0,222,173,190,239,202,254,0,0,0,0,0,0,0,23]}' "$VL_RESOLVE_LOGS" | ||
| grep -qF '{"ifindex":4,"family":2,"address":[10,0,0,20]}' "$VL_RESOLVE_LOGS" | ||
| grep -qF '"normalized":{"normalized":"myservice.signed.test"}' "$VL_RESOLVE_LOGS" |
There was a problem hiding this comment.
hmm a field which contains an object whith a field also called like the object#s field? that doesn't look right
There was a problem hiding this comment.
removed the wrapper field
| grep -qF '"normalized":{"normalized":"myservice.signed.test"}' "$VL_RESOLVE_LOGS" | ||
| grep -qF '"canonical":{"name":null,"type":"_mysvc._tcp","domain":"signed.test"}' "$VL_RESOLVE_LOGS" | ||
| TXT_OUT=$(grep -a -o -P '(?<=\"txt\"\:\[\").*(?=\"\])' "$VL_RESOLVE_LOGS" | base64 --decode) | ||
| assert_in "This is TXT for myservice" "$TXT_OUT" |
There was a problem hiding this comment.
please add a test with the varlinkctl tool
There was a problem hiding this comment.
added test with varlinkctl
|
CI failed badly. Please add varlink introspection support, i.e. add the method call, all errors and the structures you added to src/shared/varlink-io.systemd.Resolve.c |
|
And sorry for taking so terribly long to review this. |
4a6b708 to
5ca187d
Compare
5ca187d to
cf32e82
Compare
ported the d-bus implementation of service resolve to varlink extended TEST-75-RESOLVED to cover this use-case
cf32e82 to
e2252a5
Compare
|
@poettering fixed the CI failures due to TEST-75 additions. A couple of mkosi checks seem to be failing, but it looks unrelated to the code changes. |
| VARLINK_DEFINE_INPUT(flags, VARLINK_INT, VARLINK_NULLABLE), | ||
| VARLINK_DEFINE_OUTPUT_BY_TYPE(srv, ResolvedService, 0), | ||
| VARLINK_DEFINE_OUTPUT_BY_TYPE(addr, ResolvedAddress, VARLINK_ARRAY), | ||
| VARLINK_DEFINE_OUTPUT(txt, VARLINK_STRING, VARLINK_ARRAY), |
There was a problem hiding this comment.
Looks like this is triggered when services comes with no key/value pairs:
Varlink-IDL: Mandatory field 'txt' is null or missing on object, refusing.
varlink-26: Return parameters for method reply ResolveService() didn't pass validation on field 'txt', ignoring: No anode
varlink-26: Sending message: {"parameters":{"srv":{"priority":0,"weight":0,"port":123,"hostname":"host-5.local"},"addr":[{"ifindex":3,"family":2,"address":[192,168,56,3]},{"ifindex":3,"family":10,"address":[254,128,0,0,0,0,0,0,0,224,25,56,10,209,68,157]}],"txt":null,"normalized":"host-5.local","canonical":{"name":"X","type":"_ipp._tcp","domain":"local"}}}
it should probably emit empty arrays instead of null when it receives TXT RRs with a single empty string
Name: X._ipp._tcp.local
Type: TXT (Text strings) (16)
.000 0000 0000 0001 = Class: IN (0x0001)
1... .... .... .... = Cache flush: True
Time to live: 4500 (1 hour, 15 minutes)
Data length: 1
TXT Length: 0
TXT:
or zero-length RDATA.
| return varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("domain")); | ||
|
|
||
| if (!validate_and_mangle_flags(p.name, &p.in_flags, 0)) | ||
| return varlink_error_invalid_parameter(link, JSON_VARIANT_STRING_CONST("flags")); |
There was a problem hiding this comment.
I'm not sure if it's intentional but it seems it isn't possible to pass SD_RESOLVED_NO_TXT or SD_RESOLVED_NO_ADDRESS (corresponding to --service-txt=no and --service-txt=no). Currently resolved rejects those flags with
varlink-26: Received message: {"method":"io.systemd.Resolve.ResolveService","parameters":{"name":"Y","type":"_ipp._tcp","domain":"local","flags":192}}
varlink-26: Changing state idle-server → processing-method
varlink-26: Sending message: {"error":"org.varlink.service.InvalidParameter","parameters":{"parameter":"flags"}}
varlink-26: Changing state processing-method → processed-method
varlink-26: Changing state processed-method → idle-server
|
There is also a memory leak #31371 but other than that it seems that unlike the D-Bus method (which returns all the SRV and TXT RRs) the varlink method picks the first ones: resolvectl service X._qotd._tcp.local
X._qotd._tcp.local: host-5.local:124 [priority=0, weight=0]
192.168.56.3 -- link: eth1
192.168.56.3 -- link: eth1
host-5.local:123 [priority=0, weight=0]
192.168.56.3 -- link: eth1
192.168.56.3 -- link: eth1
x=1
x=2
a=1
b=2
(X/_qotd._tcp/local) |
ported the d-bus implementation of service resolve to varlink