fix broken logger during early initialization stage

This commit is contained in:
F. Duncanh
2024-03-28 00:47:02 -04:00
parent 088f6be0fb
commit 196507e23e
4 changed files with 67 additions and 54 deletions

View File

@@ -410,10 +410,10 @@ httpd_start(httpd_t *httpd, unsigned short *port)
return -1;
}
httpd->server_fd6 = netutils_init_socket(port, 1, 0);
if (httpd->server_fd6 == -1) {
logger_log(httpd->logger, LOGGER_WARNING, "Error initialising IPv6 socket %d", SOCKET_GET_ERROR());
logger_log(httpd->logger, LOGGER_WARNING, "Continuing without IPv6 support");
}
if (httpd->server_fd6 == -1) {
logger_log(httpd->logger, LOGGER_WARNING, "Error initialising IPv6 socket %d", SOCKET_GET_ERROR());
logger_log(httpd->logger, LOGGER_WARNING, "Continuing without IPv6 support");
}
if (httpd->server_fd4 != -1 && listen(httpd->server_fd4, backlog) == -1) {
logger_log(httpd->logger, LOGGER_ERR, "Error listening to IPv4 socket");

View File

@@ -342,15 +342,10 @@ conn_destroy(void *ptr) {
}
raop_t *
raop_init(int max_clients, raop_callbacks_t *callbacks, const char *device_id, const char *keyfile) {
raop_init(raop_callbacks_t *callbacks) {
raop_t *raop;
pairing_t *pairing;
httpd_t *httpd;
httpd_callbacks_t httpd_cbs;
assert(callbacks);
assert(max_clients > 0);
assert(max_clients < 100);
/* Initialize the network */
if (netutils_init() < 0) {
@@ -372,46 +367,8 @@ raop_init(int max_clients, raop_callbacks_t *callbacks, const char *device_id, c
/* Initialize the logger */
raop->logger = logger_init();
/* create a new public key for pairing */
int new_key;
pairing = pairing_init_generate(device_id, keyfile, &new_key);
if (!pairing) {
free(raop);
return NULL;
}
/* store PK as a string in raop->pk_str */
memset(raop->pk_str, 0, sizeof(raop->pk_str));
#ifdef PK
strncpy(raop->pk_str, PK, 2*ED25519_KEY_SIZE);
#else
unsigned char public_key[ED25519_KEY_SIZE];
pairing_get_public_key(pairing, public_key);
char *pk_str = utils_pk_to_string(public_key, ED25519_KEY_SIZE);
strncpy(raop->pk_str, (const char *) pk_str, 2*ED25519_KEY_SIZE);
free(pk_str);
#endif
if (new_key) {
printf("*** A new Public Key has been created and stored in %s\n", keyfile);
}
/* Set HTTP callbacks to our handlers */
memset(&httpd_cbs, 0, sizeof(httpd_cbs));
httpd_cbs.opaque = raop;
httpd_cbs.conn_init = &conn_init;
httpd_cbs.conn_request = &conn_request;
httpd_cbs.conn_destroy = &conn_destroy;
/* Initialize the http daemon */
httpd = httpd_init(raop->logger, &httpd_cbs, max_clients);
if (!httpd) {
pairing_destroy(pairing);
free(raop);
return NULL;
}
/* Copy callbacks structure */
memcpy(&raop->callbacks, callbacks, sizeof(raop_callbacks_t));
raop->pairing = pairing;
raop->httpd = httpd;
/* initialize network port list */
raop->port = 0;
@@ -440,6 +397,57 @@ raop_init(int max_clients, raop_callbacks_t *callbacks, const char *device_id, c
return raop;
}
int
raop_init2(raop_t *raop, int max_clients, const char *device_id, const char *keyfile) {
pairing_t *pairing;
httpd_t *httpd;
httpd_callbacks_t httpd_cbs;
assert(max_clients > 0);
assert(max_clients < 100);
/* create a new public key for pairing */
int new_key;
pairing = pairing_init_generate(device_id, keyfile, &new_key);
if (!pairing) {
logger_log(raop->logger, LOGGER_ERR, "failed to create new public key for pairing");
return -1;
}
/* store PK as a string in raop->pk_str */
memset(raop->pk_str, 0, sizeof(raop->pk_str));
#ifdef PK
strncpy(raop->pk_str, PK, 2*ED25519_KEY_SIZE);
#else
unsigned char public_key[ED25519_KEY_SIZE];
pairing_get_public_key(pairing, public_key);
char *pk_str = utils_pk_to_string(public_key, ED25519_KEY_SIZE);
strncpy(raop->pk_str, (const char *) pk_str, 2*ED25519_KEY_SIZE);
free(pk_str);
#endif
if (new_key) {
logger_log(raop->logger, LOGGER_INFO,"*** A new Public Key has been created and stored in %s", keyfile);
}
/* Set HTTP callbacks to our handlers */
memset(&httpd_cbs, 0, sizeof(httpd_cbs));
httpd_cbs.opaque = raop;
httpd_cbs.conn_init = &conn_init;
httpd_cbs.conn_request = &conn_request;
httpd_cbs.conn_destroy = &conn_destroy;
/* Initialize the http daemon */
httpd = httpd_init(raop->logger, &httpd_cbs, max_clients);
if (!httpd) {
logger_log(raop->logger, LOGGER_ERR, "failed to initialize http daemon");
pairing_destroy(pairing);
return -1;
}
raop->pairing = pairing;
raop->httpd = httpd;
return 0;
}
void
raop_destroy(raop_t *raop) {
if (raop) {

View File

@@ -68,7 +68,8 @@ typedef struct raop_callbacks_s raop_callbacks_t;
raop_ntp_t *raop_ntp_init(logger_t *logger, raop_callbacks_t *callbacks, const char *remote, int remote_addr_len,
unsigned short timing_rport, timing_protocol_t *time_protocol);
RAOP_API raop_t *raop_init(int max_clients, raop_callbacks_t *callbacks, const char *device_id, const char *keyfile);
RAOP_API raop_t *raop_init(raop_callbacks_t *callbacks);
RAOP_API int raop_init2(raop_t *raop, int max_clients, const char *device_id, const char *keyfile);
RAOP_API void raop_set_log_level(raop_t *raop, int level);
RAOP_API void raop_set_log_callback(raop_t *raop, raop_log_callback_t callback, void *cls);
RAOP_API int raop_set_plist(raop_t *raop, const char *plist_item, const int value);

View File

@@ -1723,12 +1723,19 @@ static int start_raop_server (unsigned short display[5], unsigned short tcp[3],
raop_cbs.check_register = check_register;
raop_cbs.export_dacp = export_dacp;
/* set max number of connections = 2 to protect against capture by new client */
raop = raop_init(max_connections, &raop_cbs, mac_address.c_str(), keyfile.c_str());
raop = raop_init(&raop_cbs);
if (raop == NULL) {
LOGE("Error initializing raop!");
return -1;
}
raop_set_log_callback(raop, log_callback, NULL);
raop_set_log_level(raop, log_level);
/* set max number of connections = 2 to protect against capture by new client */
if (raop_init2(raop, max_connections, mac_address.c_str(), keyfile.c_str())){
LOGE("Error initializing raop (2)!");
free (raop);
return -1;
}
/* write desired display pixel width, pixel height, refresh_rate, max_fps, overscanned. */
/* use 0 for default values 1920,1080,60,30,0; these are sent to the Airplay client */
@@ -1747,9 +1754,6 @@ static int start_raop_server (unsigned short display[5], unsigned short tcp[3],
/* network port selection (ports listed as "0" will be dynamically assigned) */
raop_set_tcp_ports(raop, tcp);
raop_set_udp_ports(raop, udp);
raop_set_log_callback(raop, log_callback, NULL);
raop_set_log_level(raop, log_level);
raop_port = raop_get_port(raop);
raop_start(raop, &raop_port);