1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021 Felix Fietkau <nbd@nbd.name> */
3 #include <sys/socket.h>
4 #include <netinet/in.h>
9 #include <libubox/usock.h>
13 static LIST_HEAD(servers
);
14 static LIST_HEAD(pending
);
16 static struct uloop_timeout restart_timer
;
19 server_cb(struct uloop_fd
*fd
, unsigned int events
)
21 struct server
*s
= container_of(fd
, struct server
, fd
);
22 struct sockaddr_in6 addr
;
28 cfd
= accept(fd
->fd
, (struct sockaddr
*)&addr
, &sl
);
37 /* other error, restart */
40 list_move_tail(&s
->list
, &pending
);
41 uloop_timeout_set(&restart_timer
, 1000);
44 rcd_client_accept(cfd
);
48 static void server_start(struct server
*s
)
50 s
->fd
.fd
= usock(USOCK_SERVER
| USOCK_NONBLOCK
| USOCK_TCP
, s
->addr
, usock_port(RCD_PORT
));
53 fprintf(stderr
, "WARNING: Failed to open server port on %s\n", s
->addr
);
58 uloop_fd_add(&s
->fd
, ULOOP_READ
);
59 list_move_tail(&s
->list
, &servers
);
63 server_start_pending(struct uloop_timeout
*timeout
)
65 struct server
*s
, *tmp
;
67 list_for_each_entry_safe(s
, tmp
, &pending
, list
)
70 if (!list_empty(&pending
))
71 uloop_timeout_set(timeout
, 1000);
74 void rcd_server_add(const char *addr
)
78 s
= calloc(1, sizeof(*s
));
80 list_add_tail(&s
->list
, &pending
);
84 void rcd_server_init(void)
86 if (list_empty(&pending
))
87 rcd_server_add("127.0.0.1");
89 restart_timer
.cb
= server_start_pending
;
92 server_start_pending(&restart_timer
);