Repository navigation
Expand file tree
/
Copy pathplugin_entry.cpp
More file actions
615 lines (553 loc) · 23.2 KB
/
Copy pathplugin_entry.cpp
File metadata and controls
615 lines (553 loc) · 23.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// SPDX-License-Identifier: GPL-2.0-only WITH GoodNet-linking-exception
/// @file plugins/links/ice/plugin_entry.cpp
/// @brief Plugin entry surface for `link-ice`.
///
/// Hand-rolled instead of using `GN_LINK_PLUGIN` because ICE registers
/// two extensions: the standard `gn.link.ice` (stats / send / close)
/// the kernel snapshots from every link, plus `gn.link.ice.signal`
/// for peer-to-peer offer / answer delivery.
#include "link_ice.hpp"
#include <sdk/abi.h>
#include <sdk/convenience.h>
#include <sdk/cpp/log.hpp>
#include <sdk/extensions/link.h>
#include <sdk/host_api.h>
#include <sdk/link.h>
#include <sdk/plugin.h>
#include <sdk/security.h>
#include <sdk/topology.h>
#include <sdk/trust.h>
#include <sdk/types.h>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <memory>
#include <new>
#include <span>
#include <vector>
namespace {
using ::gn::link::ice::IceLink;
using ::gn::link::ice::gn_link_ice_signal_api_t;
using ::gn::link::ice::gn_link_ice_path_mtu_api_t;
using ::gn::link::ice::gn_link_ice_metrics_api_t;
using ::gn::link::ice::gn_ice_nomination_t;
using ::gn::link::ice::gn_ice_candidate_counts_t;
using ::gn::link::ice::kIceSignalVersion;
using ::gn::link::ice::kIcePathMtuVersion;
using ::gn::link::ice::kIceMetricsVersion;
constexpr const char* kIceScheme = "ice";
constexpr const char* kIceLinkExtensionName = "gn.link.ice";
constexpr const char* kIceSignalExtensionName = "gn.link.ice.signal";
constexpr const char* kIcePathMtuExtensionName = "gn.link.ice.path_mtu";
constexpr const char* kIceMetricsExtensionName = "gn.link.ice.metrics";
constexpr const char* kPluginName = "goodnet_link_ice";
struct IcePlugin {
const host_api_t* api = nullptr;
void* host_ctx = nullptr;
std::shared_ptr<IceLink> link;
gn_link_id_t link_id = GN_INVALID_ID;
gn_link_caps_t caps = {};
gn_link_vtable_t link_vtable = {};
gn_link_api_t link_extension_vtable = {};
gn_link_ice_signal_api_t signal_extension_vtable = {};
gn_link_ice_path_mtu_api_t path_mtu_extension_vtable = {};
gn_link_ice_metrics_api_t metrics_extension_vtable = {};
bool link_extension_registered = false;
bool signal_extension_registered = false;
bool path_mtu_extension_registered = false;
bool metrics_extension_registered = false;
};
// ── kernel-facing gn_link_vtable_t thunks ───────────────────────────────────
const char* link_scheme(void* /*self*/) {
return kIceScheme;
}
gn_result_t link_listen(void* self, const char* uri) noexcept {
if (!self || !uri) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(self);
return p->link->listen(uri);
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_connect(void* self, const char* uri) noexcept {
if (!self || !uri) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(self);
return p->link->connect(uri);
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_send(void* self, gn_conn_id_t conn,
const std::uint8_t* bytes, std::size_t size) noexcept {
if (!self) return GN_ERR_NULL_ARG;
if (!bytes && size > 0) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(self);
return p->link->send(conn, std::span<const std::uint8_t>(bytes, size));
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_send_batch(void* self, gn_conn_id_t conn,
const gn_byte_span_t* batch,
std::size_t count) noexcept {
if (!self) return GN_ERR_NULL_ARG;
if (count > 0 && !batch) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(self);
std::vector<std::span<const std::uint8_t>> frames;
frames.reserve(count);
for (std::size_t i = 0; i < count; ++i) {
frames.emplace_back(batch[i].bytes, batch[i].size);
}
return p->link->send_batch(conn,
std::span<const std::span<const std::uint8_t>>(frames));
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_disconnect(void* self, gn_conn_id_t conn) noexcept {
if (!self) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(self);
return p->link->disconnect(conn);
} catch (...) { return GN_ERR_NULL_ARG; }
}
const char* link_extension_name(void* /*self*/) noexcept {
return kIceLinkExtensionName;
}
const void* link_extension_vtable(void* self) noexcept {
if (!self) return nullptr;
return &static_cast<IcePlugin*>(self)->link_extension_vtable;
}
void link_destroy(void* /*self*/) noexcept {
/// Lifetime is owned by `gn_plugin_shutdown`; this slot is the
/// link-vtable destructor path, not the plugin destructor.
}
// ── gn.link.ice (stats / capabilities / send / close) thunks ───────────────
gn_result_t link_ext_get_stats(void* ctx, gn_link_stats_t* out) noexcept {
if (!ctx || !out) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
const auto s = p->link->stats();
std::memset(out, 0, sizeof(*out));
out->bytes_in = s.bytes_in;
out->bytes_out = s.bytes_out;
out->frames_in = s.frames_in;
out->frames_out = s.frames_out;
out->active_connections = s.active_connections;
return GN_OK;
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_ext_get_caps(void* ctx, gn_link_caps_t* out) noexcept {
if (!ctx || !out) return GN_ERR_NULL_ARG;
*out = static_cast<IcePlugin*>(ctx)->caps;
return GN_OK;
}
gn_result_t link_ext_send(void* ctx, gn_conn_id_t conn,
const std::uint8_t* bytes,
std::size_t size) noexcept {
if (!ctx) return GN_ERR_NULL_ARG;
if (!bytes && size > 0) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->send(conn, std::span<const std::uint8_t>(bytes, size));
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_ext_send_batch(void* ctx, gn_conn_id_t conn,
const gn_byte_span_t* batch,
std::size_t count) noexcept {
return link_send_batch(ctx, conn, batch, count);
}
gn_result_t link_ext_close(void* ctx, gn_conn_id_t conn, int /*hard*/) noexcept {
if (!ctx) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->disconnect(conn);
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_ext_composer_listen(void* ctx, const char* uri) noexcept {
if (!ctx || !uri) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->composer_listen(uri);
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_ext_composer_connect(void* ctx, const char* uri,
gn_conn_id_t* out_conn) noexcept {
if (!ctx || !uri || !out_conn) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->connect(uri, out_conn);
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_ext_composer_subscribe_data(
void* ctx, gn_conn_id_t conn,
gn_link_data_cb_t cb, void* user_data) noexcept {
if (!ctx) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->composer_subscribe_data(conn, cb, user_data);
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_ext_composer_unsubscribe_data(
void* ctx, gn_conn_id_t conn) noexcept {
if (!ctx) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->composer_unsubscribe_data(conn);
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_ext_composer_subscribe_accept(
void* ctx, gn_link_accept_cb_t cb, void* user_data,
gn_subscription_id_t* out_token) noexcept {
if (!ctx) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->composer_subscribe_accept(cb, user_data, out_token);
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_ext_composer_unsubscribe_accept(
void* ctx, gn_subscription_id_t token) noexcept {
if (!ctx) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->composer_unsubscribe_accept(token);
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t link_ext_composer_listen_port(void* ctx,
std::uint16_t* out_port) noexcept {
if (!ctx || !out_port) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->composer_listen_port(out_port);
} catch (...) { return GN_ERR_NULL_ARG; }
}
// ── gn.link.ice.signal thunks ─────────────────────────────────────────────
gn_result_t signal_ext_offer(void* ctx,
const std::uint8_t peer_pk[GN_PUBLIC_KEY_BYTES],
const std::uint8_t* blob,
std::size_t size) {
if (!ctx || !peer_pk) return GN_ERR_NULL_ARG;
if (!blob && size > 0) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->deliver_signal(
peer_pk, ::gn::link::ice::ICE_SIGNAL_OFFER,
std::span<const std::uint8_t>(blob, size));
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t signal_ext_answer(void* ctx,
const std::uint8_t peer_pk[GN_PUBLIC_KEY_BYTES],
const std::uint8_t* blob,
std::size_t size) {
if (!ctx || !peer_pk) return GN_ERR_NULL_ARG;
if (!blob && size > 0) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->deliver_signal(
peer_pk, ::gn::link::ice::ICE_SIGNAL_ANSWER,
std::span<const std::uint8_t>(blob, size));
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t signal_ext_offer_eoc(void* ctx,
const std::uint8_t peer_pk[GN_PUBLIC_KEY_BYTES],
const std::uint8_t* blob,
std::size_t size) {
if (!ctx || !peer_pk) return GN_ERR_NULL_ARG;
if (!blob && size > 0) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->deliver_signal(
peer_pk, ::gn::link::ice::ICE_SIGNAL_OFFER_EOC,
std::span<const std::uint8_t>(blob, size));
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t signal_ext_answer_eoc(void* ctx,
const std::uint8_t peer_pk[GN_PUBLIC_KEY_BYTES],
const std::uint8_t* blob,
std::size_t size) {
if (!ctx || !peer_pk) return GN_ERR_NULL_ARG;
if (!blob && size > 0) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->deliver_signal(
peer_pk, ::gn::link::ice::ICE_SIGNAL_ANSWER_EOC,
std::span<const std::uint8_t>(blob, size));
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t signal_ext_poll_local(void* ctx,
const std::uint8_t peer_pk[GN_PUBLIC_KEY_BYTES],
std::uint32_t* kind_out,
std::uint8_t* blob_out,
std::size_t blob_cap,
std::size_t* blob_len_out) {
if (!ctx || !peer_pk || !kind_out || !blob_len_out) return GN_ERR_NULL_ARG;
if (blob_cap > 0 && !blob_out) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
return p->link->poll_local_signal(peer_pk, kind_out,
blob_out, blob_cap, blob_len_out);
} catch (...) { return GN_ERR_NULL_ARG; }
}
void install_link_extension(IcePlugin* p) {
auto& v = p->link_extension_vtable;
v = gn_link_api_t{};
v.api_size = sizeof(gn_link_api_t);
v.get_stats = &link_ext_get_stats;
v.get_capabilities = &link_ext_get_caps;
v.send = &link_ext_send;
v.send_batch = &link_ext_send_batch;
v.close = &link_ext_close;
/// Composer surface — L2 plugins can `composer_connect`
/// over this ICE link to ride a NAT-traversed UDP pair as a
/// reliable / encrypted stream carrier (QUIC, DTLS).
v.listen = &link_ext_composer_listen;
v.connect = &link_ext_composer_connect;
v.subscribe_data = &link_ext_composer_subscribe_data;
v.unsubscribe_data = &link_ext_composer_unsubscribe_data;
v.subscribe_accept = &link_ext_composer_subscribe_accept;
v.unsubscribe_accept = &link_ext_composer_unsubscribe_accept;
v.composer_listen_port = &link_ext_composer_listen_port;
v.ctx = p;
}
gn_result_t path_mtu_ext_get(void* ctx, gn_conn_id_t conn,
std::uint32_t* out_mtu) noexcept {
if (!ctx || !out_mtu) return GN_ERR_NULL_ARG;
try {
auto* p = static_cast<IcePlugin*>(ctx);
*out_mtu = p->link->effective_path_mtu(conn);
return GN_OK;
} catch (...) { return GN_ERR_NULL_ARG; }
}
void install_path_mtu_extension(IcePlugin* p) {
auto& v = p->path_mtu_extension_vtable;
v = gn_link_ice_path_mtu_api_t{};
v.api_size = sizeof(gn_link_ice_path_mtu_api_t);
v.get = &path_mtu_ext_get;
v.ctx = p;
}
gn_result_t metrics_ext_get_nomination(void* ctx, gn_conn_id_t conn,
gn_ice_nomination_t* out) noexcept {
if (!ctx || !out) return GN_ERR_NULL_ARG;
if (out->api_size < sizeof(gn_ice_nomination_t)) {
return GN_ERR_OUT_OF_RANGE;
}
try {
auto* p = static_cast<IcePlugin*>(ctx);
const auto m = p->link->nomination_metrics(conn);
out->nominated = m.nominated ? 1 : 0;
out->uses_relay = m.uses_relay ? 1 : 0;
out->local_type = static_cast<std::uint8_t>(m.local_type);
out->remote_type = static_cast<std::uint8_t>(m.remote_type);
out->transport = static_cast<std::uint8_t>(m.transport);
out->rtt_us = m.rtt_us;
return GN_OK;
} catch (...) { return GN_ERR_NULL_ARG; }
}
gn_result_t metrics_ext_get_candidate_counts(
void* ctx, gn_conn_id_t conn,
gn_ice_candidate_counts_t* out) noexcept {
if (!ctx || !out) return GN_ERR_NULL_ARG;
if (out->api_size < sizeof(gn_ice_candidate_counts_t)) {
return GN_ERR_OUT_OF_RANGE;
}
try {
auto* p = static_cast<IcePlugin*>(ctx);
const auto c = p->link->candidate_counts(conn);
out->local_host = c.local_host;
out->local_srflx = c.local_srflx;
out->local_relay = c.local_relay;
out->local_prflx = c.local_prflx;
out->remote_host = c.remote_host;
out->remote_srflx = c.remote_srflx;
out->remote_relay = c.remote_relay;
out->remote_prflx = c.remote_prflx;
return GN_OK;
} catch (...) { return GN_ERR_NULL_ARG; }
}
void install_metrics_extension(IcePlugin* p) {
auto& v = p->metrics_extension_vtable;
v = gn_link_ice_metrics_api_t{};
v.api_size = sizeof(gn_link_ice_metrics_api_t);
v.get_nomination = &metrics_ext_get_nomination;
v.get_candidate_counts = &metrics_ext_get_candidate_counts;
v.ctx = p;
}
void install_signal_extension(IcePlugin* p) {
auto& v = p->signal_extension_vtable;
v = gn_link_ice_signal_api_t{};
v.api_size = sizeof(gn_link_ice_signal_api_t);
v.offer = &signal_ext_offer;
v.answer = &signal_ext_answer;
v.offer_eoc = &signal_ext_offer_eoc;
v.answer_eoc = &signal_ext_answer_eoc;
v.poll_local = &signal_ext_poll_local;
v.ctx = p;
}
void link_on_topology_sealed(void* self,
const struct gn_topology_s* topo) noexcept {
if (!self || !topo) return;
auto* p = static_cast<IcePlugin*>(self);
const bool contour_open =
(topo->contour_gaps & ((1u << GN_TRUST_UNTRUSTED) |
(1u << GN_TRUST_PEER))) != 0;
p->link->set_prefer_turn_tcp(contour_open);
char fp[65] = {};
for (int i = 0; i < 32; ++i)
std::snprintf(fp + i * 2, 3, "%02x",
static_cast<unsigned>(topo->fingerprint[i]));
bool e2e_present = false;
for (std::uint32_t i = 0; i < topo->security_count; ++i) {
const auto& s = topo->security[i];
gn::log::info(p->api,
"ice: topology security provider={} trust_mask={:#x} "
"provides={:#x} e2e={}",
s.provider_id ? s.provider_id : "(null)",
s.allowed_trust_mask,
s.provides_flags,
(s.provides_flags & GN_SEC_PROVIDES_E2E_ENCRYPTION) ? 1 : 0);
if (s.provides_flags & GN_SEC_PROVIDES_E2E_ENCRYPTION)
e2e_present = true;
}
// 16 bytes AEAD tag + 4 bytes seqno header = 20 bytes total overhead
p->link->set_security_overhead(e2e_present ? 20 : 0);
p->link->set_ordered_delivery(e2e_present);
gn::log::info(p->api,
"ice: topology sealed fingerprint={} contour_gaps={:#x} "
"prefer_turn_tcp={} e2e_present={}",
std::string_view{fp}, topo->contour_gaps,
contour_open ? 1 : 0, e2e_present ? 1 : 0);
}
void install_link_vtable(IcePlugin* p) {
auto& v = p->link_vtable;
v = gn_link_vtable_t{};
v.api_size = sizeof(gn_link_vtable_t);
v.scheme = &link_scheme;
v.listen = &link_listen;
v.connect = &link_connect;
v.send = &link_send;
v.send_batch = &link_send_batch;
v.disconnect = &link_disconnect;
v.extension_name = &link_extension_name;
v.extension_vtable = &link_extension_vtable;
v.destroy = &link_destroy;
v.on_topology_sealed = &link_on_topology_sealed;
}
const char* const kProvidesList[] = {
kIceLinkExtensionName,
kIceSignalExtensionName,
kIcePathMtuExtensionName,
kIceMetricsExtensionName,
nullptr,
};
const gn_plugin_descriptor_t kDescriptor = {
/* name */ kPluginName,
/* version */ "0.1.0",
/* hot_reload_safe */ 0,
/* ext_requires */ nullptr,
/* ext_provides */ kProvidesList,
/* kind */ GN_PLUGIN_KIND_LINK,
/* inject_targets */ nullptr,
/* _reserved */ {nullptr, nullptr, nullptr, nullptr},
};
} // namespace
extern "C" {
GN_PLUGIN_EXPORT void GN_PLUGIN_SDK_VERSION_NAME(std::uint32_t* major,
std::uint32_t* minor,
std::uint32_t* patch) {
if (major) *major = GN_SDK_VERSION_MAJOR;
if (minor) *minor = GN_SDK_VERSION_MINOR;
if (patch) *patch = GN_SDK_VERSION_PATCH;
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_INIT_NAME(const host_api_t* api,
void** out_self) {
if (!api || !out_self) return GN_ERR_NULL_ARG;
auto* p = new (std::nothrow) IcePlugin{};
if (!p) return GN_ERR_OUT_OF_MEMORY;
try {
p->api = api;
p->host_ctx = api->host_ctx;
p->link = std::make_shared<IceLink>();
p->link->set_host_api(api);
p->caps = IceLink::capabilities();
install_link_extension(p);
install_signal_extension(p);
install_path_mtu_extension(p);
install_metrics_extension(p);
install_link_vtable(p);
*out_self = p;
return GN_OK;
} catch (...) {
delete p;
return GN_ERR_OUT_OF_MEMORY;
}
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_REGISTER_NAME(void* self) {
if (!self) return GN_ERR_NULL_ARG;
auto* p = static_cast<IcePlugin*>(self);
if (!p->api || !p->api->register_vtable) return GN_ERR_NOT_IMPLEMENTED;
gn_register_meta_t meta{};
meta.api_size = sizeof(gn_register_meta_t);
meta.name = kIceScheme;
if (auto rc = p->api->register_vtable(
p->host_ctx, GN_REGISTER_LINK, &meta,
&p->link_vtable, p, &p->link_id);
rc != GN_OK) {
return rc;
}
if (p->api->register_extension) {
if (p->api->register_extension(
p->host_ctx, kIceLinkExtensionName,
GN_EXT_LINK_VERSION, &p->link_extension_vtable) == GN_OK) {
p->link_extension_registered = true;
}
if (p->api->register_extension(
p->host_ctx, kIceSignalExtensionName,
kIceSignalVersion, &p->signal_extension_vtable) == GN_OK) {
p->signal_extension_registered = true;
}
if (p->api->register_extension(
p->host_ctx, kIcePathMtuExtensionName,
kIcePathMtuVersion, &p->path_mtu_extension_vtable) == GN_OK) {
p->path_mtu_extension_registered = true;
}
if (p->api->register_extension(
p->host_ctx, kIceMetricsExtensionName,
kIceMetricsVersion, &p->metrics_extension_vtable) == GN_OK) {
p->metrics_extension_registered = true;
}
}
return GN_OK;
}
GN_PLUGIN_EXPORT gn_result_t GN_PLUGIN_UNREGISTER_NAME(void* self) {
if (!self) return GN_ERR_NULL_ARG;
auto* p = static_cast<IcePlugin*>(self);
if (p->metrics_extension_registered &&
p->api && p->api->unregister_extension) {
(void)p->api->unregister_extension(p->host_ctx, kIceMetricsExtensionName);
p->metrics_extension_registered = false;
}
if (p->path_mtu_extension_registered &&
p->api && p->api->unregister_extension) {
(void)p->api->unregister_extension(p->host_ctx, kIcePathMtuExtensionName);
p->path_mtu_extension_registered = false;
}
if (p->signal_extension_registered &&
p->api && p->api->unregister_extension) {
(void)p->api->unregister_extension(p->host_ctx, kIceSignalExtensionName);
p->signal_extension_registered = false;
}
if (p->link_extension_registered &&
p->api && p->api->unregister_extension) {
(void)p->api->unregister_extension(p->host_ctx, kIceLinkExtensionName);
p->link_extension_registered = false;
}
if (p->api && p->api->unregister_vtable && p->link_id != GN_INVALID_ID) {
(void)p->api->unregister_vtable(p->host_ctx, p->link_id);
p->link_id = GN_INVALID_ID;
}
if (p->link) p->link->shutdown();
return GN_OK;
}
GN_PLUGIN_EXPORT void GN_PLUGIN_SHUTDOWN_NAME(void* self) {
delete static_cast<IcePlugin*>(self);
}
GN_PLUGIN_EXPORT const gn_plugin_descriptor_t* GN_PLUGIN_DESCRIPTOR_NAME(void) {
return &kDescriptor;
}
} // extern "C"