3 #include "curve25519.h"
5 static struct ubus_auto_conn conn
;
6 static struct blob_buf b
;
9 udht_ubus_network_add(struct blob_attr
*data
, int seq
)
11 static const struct blobmsg_policy net_policy
=
12 { "config", BLOBMSG_TYPE_TABLE
};
18 static const struct blobmsg_policy policy
[__CONFIG_ATTR_MAX
] = {
19 [CONFIG_ATTR_AUTH_KEY
] = { "auth_key", BLOBMSG_TYPE_STRING
},
20 [CONFIG_ATTR_DHT
] = { "dht", BLOBMSG_TYPE_BOOL
},
22 struct blob_attr
*tb
[__CONFIG_ATTR_MAX
];
23 struct blob_attr
*config
, *cur
;
24 uint8_t auth_key
[CURVE25519_KEY_SIZE
];
26 blobmsg_parse(&net_policy
, 1, &config
, blobmsg_data(data
), blobmsg_len(data
));
31 blobmsg_parse(policy
, __CONFIG_ATTR_MAX
, tb
, blobmsg_data(config
), blobmsg_len(config
));
32 if ((cur
= tb
[CONFIG_ATTR_DHT
]) == NULL
|| !blobmsg_get_u8(cur
))
35 if ((cur
= tb
[CONFIG_ATTR_AUTH_KEY
]) == NULL
||
36 (b64_decode(blobmsg_get_string(cur
), auth_key
, CURVE25519_KEY_SIZE
) !=
40 udht_network_add(auth_key
, seq
);
45 udht_ubus_network_cb(struct ubus_request
*req
, int type
,
46 struct blob_attr
*msg
)
48 static const struct blobmsg_policy policy
=
49 { "networks", BLOBMSG_TYPE_TABLE
};
50 struct blob_attr
*networks
, *cur
;
54 blobmsg_parse(&policy
, 1, &networks
, blobmsg_data(msg
), blobmsg_len(msg
));
59 blobmsg_for_each_attr(cur
, networks
, rem
)
60 udht_ubus_network_add(cur
, *seq
);
64 udht_ubus_update_networks(struct ubus_context
*ctx
)
71 if (ubus_lookup_id(ctx
, "unetd", &id
) == 0)
72 ubus_invoke(ctx
, id
, "network_get", b
.head
, udht_ubus_network_cb
, &seq
, 5000);
74 udht_network_flush(seq
);
78 udht_ubus_unetd_cb(struct ubus_context
*ctx
, struct ubus_object
*obj
,
79 struct ubus_request_data
*req
, const char *method
,
80 struct blob_attr
*msg
)
82 udht_ubus_update_networks(ctx
);
88 udht_subscribe_unetd(struct ubus_context
*ctx
)
90 static struct ubus_subscriber sub
= {
91 .cb
= udht_ubus_unetd_cb
95 if (!sub
.obj
.id
&& ubus_register_subscriber(ctx
, &sub
))
98 if (ubus_lookup_id(ctx
, "unetd", &id
))
101 ubus_subscribe(ctx
, &sub
, id
);
103 /* ensure that unetd's socket is ready by testing if it's reachable over ubus */
104 if (ubus_invoke(ctx
, id
, "network_get", b
.head
, NULL
, NULL
, 10000))
108 udht_ubus_update_networks(ctx
);
112 udht_ubus_event_cb(struct ubus_context
*ctx
, struct ubus_event_handler
*ev
,
113 const char *type
, struct blob_attr
*msg
)
115 static const struct blobmsg_policy policy
=
116 { "path", BLOBMSG_TYPE_STRING
};
117 struct blob_attr
*attr
;
119 blobmsg_parse(&policy
, 1, &attr
, blobmsg_data(msg
), blobmsg_len(msg
));
123 if (!strcmp(blobmsg_get_string(attr
), "unetd"))
124 udht_subscribe_unetd(ctx
);
128 ubus_connect_handler(struct ubus_context
*ctx
)
130 static struct ubus_event_handler ev
= {
131 .cb
= udht_ubus_event_cb
,
134 ubus_register_event_handler(ctx
, &ev
, "ubus.object.add");
135 udht_subscribe_unetd(ctx
);
138 void udht_ubus_init(void)
140 blob_buf_init(&b
, 0);
141 conn
.cb
= ubus_connect_handler
;
142 ubus_auto_connect(&conn
);