4 #include <sys/select.h>
6 #include <libubox/blob.h>
7 #include <libubox/blobmsg.h>
10 #include <arpa/inet.h>
12 #include <netinet/in.h>
13 #include <sys/ioctl.h>
14 #include <sys/socket.h>
18 #include "interfaces.h"
25 #define MAX(a, b) ((a) > (b) ? (a) : (b))
26 #define MIN(a, b) ((a) < (b) ? (a) : (b))
28 // Shared state maintained throughout calls to handle ubus messages.
29 static struct ubus_context
*shared_ctx
;
31 enum { INTERFACE_IFNAME
, __INTERFACE_MAX
};
33 static const struct blobmsg_policy interface_policy
[__INTERFACE_MAX
] = {
34 [INTERFACE_IFNAME
] = {"ifname", BLOBMSG_TYPE_STRING
},
37 static int olsrd_ubus_add_interface(struct ubus_context
*ctx_local
,
38 struct ubus_object
*obj
,
39 struct ubus_request_data
*req
,
40 const char *method
, struct blob_attr
*msg
) {
41 struct blob_attr
*tb
[__INTERFACE_MAX
];
42 struct blob_buf b
= {0};
46 blobmsg_parse(interface_policy
, __INTERFACE_MAX
, tb
, blob_data(msg
),
49 if (!tb
[INTERFACE_IFNAME
])
50 return UBUS_STATUS_INVALID_ARGUMENT
;
52 ifname
= blobmsg_get_string(tb
[INTERFACE_IFNAME
]);
54 struct interface_olsr
*tmp
= if_ifwithname(ifname
);
56 return UBUS_STATUS_PERMISSION_DENIED
;
60 for (temp
= olsr_cnf
->interfaces
; temp
!= NULL
; temp
= temp
->next
) {
61 if (strcmp(temp
->name
, ifname
) == 0)
62 return UBUS_STATUS_PERMISSION_DENIED
;
65 struct olsr_if
*tmp_ifs
= olsr_create_olsrif(ifname
, false);
66 tmp_ifs
->cnf
= olsr_malloc(sizeof(struct if_config_options
),"Set default config");
67 *tmp_ifs
->cnf
= *olsr_cnf
->interface_defaults
;
70 blobmsg_add_string(&b
, "adding", ifname
);
72 ret
= ubus_send_reply(ctx_local
, req
, b
.head
);
74 olsr_syslog(OLSR_LOG_ERR
, "Failed to send reply: %s\n",
82 static int olsrd_ubus_del_interface(struct ubus_context
*ctx_local
,
83 struct ubus_object
*obj
,
84 struct ubus_request_data
*req
,
85 const char *method
, struct blob_attr
*msg
) {
86 struct blob_attr
*tb
[__INTERFACE_MAX
];
87 struct blob_buf b
= {0};
90 struct olsr_if
*tmp_if
, *del_if
;
92 blobmsg_parse(interface_policy
, __INTERFACE_MAX
, tb
, blob_data(msg
),
95 if (!tb
[INTERFACE_IFNAME
])
96 return UBUS_STATUS_INVALID_ARGUMENT
;
98 ifname
= blobmsg_get_string(tb
[INTERFACE_IFNAME
]);
100 struct interface_olsr
*tmp
= if_ifwithname(ifname
);
104 struct olsr_if
*temp
= olsr_cnf
->interfaces
, *prev
;
105 if (temp
!= NULL
&& (strcmp(temp
->name
, ifname
) == 0)) {
106 olsr_cnf
->interfaces
= temp
->next
;
107 olsr_remove_interface(temp
);
111 while (temp
!= NULL
&& (strcmp(temp
->name
, ifname
) != 0)) {
120 prev
->next
= temp
->next
;
121 olsr_remove_interface(temp
);
123 return UBUS_STATUS_PERMISSION_DENIED
;
128 blob_buf_init(&b
, 0);
129 blobmsg_add_string(&b
, "deleting", ifname
);
131 ret
= ubus_send_reply(ctx_local
, req
, b
.head
);
133 olsr_syslog(OLSR_LOG_ERR
, "Failed to send reply: %s\n",
141 // List of functions we expose via the ubus bus.
142 static const struct ubus_method olsrd_methods
[] = {
143 UBUS_METHOD("add_interface", olsrd_ubus_add_interface
, interface_policy
),
144 UBUS_METHOD("del_interface", olsrd_ubus_del_interface
, interface_policy
),
147 // Definition of the ubus object type.
148 static struct ubus_object_type olsrd_object_type
=
149 UBUS_OBJECT_TYPE("olsrd", olsrd_methods
);
151 // Object we announce via the ubus bus.
152 static struct ubus_object olsrd_object
= {
154 .type
= &olsrd_object_type
,
155 .methods
= olsrd_methods
,
156 .n_methods
= ARRAY_SIZE(olsrd_methods
),
159 // Registers handlers for olsrd methods in the global ubus context.
160 static bool ubus_init_object() {
163 ret
= ubus_add_object(shared_ctx
, &olsrd_object
);
165 olsr_syslog(OLSR_LOG_ERR
, "Failed to add object: %s\n",
173 // Initializes the global ubus context, connecting to the bus to be able to
174 // receive and send messages.
175 static bool olsrd_ubus_init(void) {
179 shared_ctx
= ubus_connect(NULL
);
186 void olsrd_ubus_receive(fd_set
*readfds
) {
189 if (FD_ISSET(shared_ctx
->sock
.fd
, readfds
))
190 ubus_handle_event(shared_ctx
);
193 int olsrd_ubus_add_read_sock(fd_set
*readfds
, int maxfd
) {
197 FD_SET(shared_ctx
->sock
.fd
, readfds
);
198 return MAX(maxfd
, shared_ctx
->sock
.fd
+ 1);
201 bool olsrd_add_ubus() {
202 if (!olsrd_ubus_init()) {
203 olsr_syslog(OLSR_LOG_ERR
, "Failed to initialize ubus!\n");
206 if (!ubus_init_object()) {
207 olsr_syslog(OLSR_LOG_ERR
, "Failed to add objects to ubus!\n");