mirror of
https://github.com/morgan9e/UxPlay
synced 2026-04-14 00:04:13 +09:00
cleanup unitialized variables
This commit is contained in:
61
lib/crypto.c
61
lib/crypto.c
@@ -196,13 +196,11 @@ struct x25519_key_s {
|
||||
};
|
||||
|
||||
x25519_key_t *x25519_key_generate(void) {
|
||||
x25519_key_t *key;
|
||||
EVP_PKEY_CTX *pctx;
|
||||
|
||||
key = calloc(1, sizeof(x25519_key_t));
|
||||
x25519_key_t *key = (x25519_key_t *) calloc(1, sizeof(x25519_key_t));
|
||||
assert(key);
|
||||
|
||||
pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_X25519, NULL);
|
||||
EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_X25519, NULL);
|
||||
if (!pctx) {
|
||||
handle_error(__func__);
|
||||
}
|
||||
@@ -218,9 +216,8 @@ x25519_key_t *x25519_key_generate(void) {
|
||||
}
|
||||
|
||||
x25519_key_t *x25519_key_from_raw(const unsigned char data[X25519_KEY_SIZE]) {
|
||||
x25519_key_t *key;
|
||||
|
||||
key = malloc(sizeof(x25519_key_t));
|
||||
x25519_key_t *key = (x25519_key_t *) calloc(1, sizeof(x25519_key_t));
|
||||
assert(key);
|
||||
|
||||
key->pkey = EVP_PKEY_new_raw_public_key(EVP_PKEY_X25519, NULL, data, X25519_KEY_SIZE);
|
||||
@@ -272,11 +269,11 @@ void x25519_derive_secret(unsigned char secret[X25519_KEY_SIZE], const x25519_ke
|
||||
int gcm_encrypt(const unsigned char *plaintext, int plaintext_len, unsigned char *ciphertext,
|
||||
unsigned char *key, unsigned char *iv, unsigned char *tag)
|
||||
{
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
EVP_CIPHER_CTX *ctx = NULL;
|
||||
|
||||
int len;
|
||||
int len = 0;
|
||||
|
||||
int ciphertext_len;
|
||||
int ciphertext_len = 0;
|
||||
|
||||
if(!(ctx = EVP_CIPHER_CTX_new()))
|
||||
handle_error(__func__);
|
||||
@@ -309,11 +306,8 @@ int gcm_encrypt(const unsigned char *plaintext, int plaintext_len, unsigned char
|
||||
int gcm_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *plaintext,
|
||||
unsigned char *key, unsigned char *iv, unsigned char *tag)
|
||||
{
|
||||
EVP_CIPHER_CTX *ctx;
|
||||
int len;
|
||||
int plaintext_len;
|
||||
int ret;
|
||||
|
||||
EVP_CIPHER_CTX *ctx = NULL;
|
||||
|
||||
if(!(ctx = EVP_CIPHER_CTX_new()))
|
||||
handle_error(__func__);
|
||||
|
||||
@@ -326,14 +320,15 @@ int gcm_decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *pl
|
||||
if(!EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv))
|
||||
handle_error(__func__);
|
||||
|
||||
int len = 0;
|
||||
if(!EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len))
|
||||
handle_error(__func__);
|
||||
plaintext_len = len;
|
||||
int plaintext_len = len;
|
||||
|
||||
if(!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, 16, tag))
|
||||
handle_error(__func__);
|
||||
|
||||
ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
|
||||
int ret = EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
@@ -354,16 +349,15 @@ struct ed25519_key_s {
|
||||
};
|
||||
|
||||
ed25519_key_t *ed25519_key_generate(const char *device_id, const char *keyfile, int *result) {
|
||||
ed25519_key_t *key;
|
||||
EVP_PKEY_CTX *pctx;
|
||||
BIO *bp;
|
||||
FILE *file;
|
||||
EVP_PKEY_CTX *pctx = NULL;
|
||||
BIO *bp = NULL;
|
||||
FILE *file = NULL;
|
||||
bool new_pk = false;
|
||||
bool use_keyfile = strlen(keyfile);
|
||||
|
||||
*result = 0;
|
||||
|
||||
key = calloc(1, sizeof(ed25519_key_t));
|
||||
ed25519_key_t *key = (ed25519_key_t *) calloc(1, sizeof(ed25519_key_t));
|
||||
assert(key);
|
||||
|
||||
if (use_keyfile) {
|
||||
@@ -381,7 +375,7 @@ ed25519_key_t *ed25519_key_generate(const char *device_id, const char *keyfile,
|
||||
}
|
||||
} else {
|
||||
/* generate (insecure) persistent keypair using device_id */
|
||||
unsigned char hash[SHA512_DIGEST_LENGTH];
|
||||
unsigned char hash[SHA512_DIGEST_LENGTH] = {0};
|
||||
char salt[] = SALT_PK;
|
||||
sha_ctx_t *ctx = sha_init();
|
||||
sha_update(ctx, (const unsigned char *) salt, (unsigned int) strlen(salt));
|
||||
@@ -418,9 +412,8 @@ ed25519_key_t *ed25519_key_generate(const char *device_id, const char *keyfile,
|
||||
}
|
||||
|
||||
ed25519_key_t *ed25519_key_from_raw(const unsigned char data[ED25519_KEY_SIZE]) {
|
||||
ed25519_key_t *key;
|
||||
|
||||
key = malloc(sizeof(ed25519_key_t));
|
||||
ed25519_key_t *key = (ed25519_key_t *) calloc(1, sizeof(ed25519_key_t));
|
||||
assert(key);
|
||||
|
||||
key->pkey = EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, data, ED25519_KEY_SIZE);
|
||||
@@ -439,11 +432,9 @@ void ed25519_key_get_raw(unsigned char data[ED25519_KEY_SIZE], const ed25519_key
|
||||
}
|
||||
|
||||
ed25519_key_t *ed25519_key_copy(const ed25519_key_t *key) {
|
||||
ed25519_key_t *new_key;
|
||||
|
||||
assert(key);
|
||||
|
||||
new_key = malloc(sizeof(ed25519_key_t));
|
||||
ed25519_key_t *new_key = (ed25519_key_t *) calloc(1, sizeof(ed25519_key_t));
|
||||
assert(new_key);
|
||||
|
||||
new_key->pkey = key->pkey;
|
||||
@@ -458,9 +449,7 @@ void ed25519_sign(unsigned char *signature, size_t signature_len,
|
||||
const unsigned char *data, size_t data_len,
|
||||
const ed25519_key_t *key)
|
||||
{
|
||||
EVP_MD_CTX *mctx;
|
||||
|
||||
mctx = EVP_MD_CTX_new();
|
||||
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
|
||||
if (!mctx) {
|
||||
handle_error(__func__);
|
||||
}
|
||||
@@ -479,9 +468,7 @@ int ed25519_verify(const unsigned char *signature, size_t signature_len,
|
||||
const unsigned char *data, size_t data_len,
|
||||
const ed25519_key_t *key)
|
||||
{
|
||||
EVP_MD_CTX *mctx;
|
||||
|
||||
mctx = EVP_MD_CTX_new();
|
||||
EVP_MD_CTX *mctx = EVP_MD_CTX_new();
|
||||
if (!mctx) {
|
||||
handle_error(__func__);
|
||||
}
|
||||
@@ -516,7 +503,7 @@ struct sha_ctx_s {
|
||||
};
|
||||
|
||||
sha_ctx_t *sha_init() {
|
||||
sha_ctx_t *ctx = malloc(sizeof(sha_ctx_t));
|
||||
sha_ctx_t *ctx = (sha_ctx_t *) calloc(1, sizeof(sha_ctx_t));
|
||||
assert(ctx != NULL);
|
||||
ctx->digest_ctx = EVP_MD_CTX_new();
|
||||
assert(ctx->digest_ctx != NULL);
|
||||
@@ -559,7 +546,7 @@ struct md5_ctx_s {
|
||||
};
|
||||
|
||||
md5_ctx_t *md5_init() {
|
||||
md5_ctx_t *ctx = malloc(sizeof(md5_ctx_t));
|
||||
md5_ctx_t *ctx = (md5_ctx_t *) calloc(1, sizeof(md5_ctx_t));
|
||||
assert(ctx != NULL);
|
||||
ctx->digest_ctx = EVP_MD_CTX_new();
|
||||
assert(ctx->digest_ctx != NULL);
|
||||
@@ -599,7 +586,7 @@ void md5_destroy(md5_ctx_t *ctx) {
|
||||
|
||||
#define MD5_DIGEST_LENGTH 16
|
||||
char *get_md5(char *string) {
|
||||
unsigned char hash[MD5_DIGEST_LENGTH];
|
||||
unsigned char hash[MD5_DIGEST_LENGTH] = {0};
|
||||
md5_ctx_t *ctx = NULL;
|
||||
ctx = md5_init();
|
||||
md5_update(ctx, (const unsigned char *) string, strlen(string));
|
||||
@@ -622,7 +609,7 @@ void pk_to_base64(const unsigned char *pk, int pk_len, char *pk_base64, int len)
|
||||
|
||||
BIO *b64 = BIO_new(BIO_f_base64());
|
||||
BIO *bio = BIO_new(BIO_s_mem());
|
||||
BUF_MEM *bufferPtr;
|
||||
BUF_MEM *bufferPtr = NULL;
|
||||
|
||||
|
||||
bio = BIO_push(b64, bio);
|
||||
|
||||
49
lib/dnssd.c
49
lib/dnssd.c
@@ -159,9 +159,6 @@ struct dnssd_s {
|
||||
dnssd_t *
|
||||
dnssd_init(const char* name, int name_len, const char* hw_addr, int hw_addr_len, int *error, unsigned char pin_pw)
|
||||
{
|
||||
dnssd_t *dnssd;
|
||||
char *end;
|
||||
unsigned long features;
|
||||
/* pin_pw = 0: no pin or password
|
||||
1: use onscreen pin for client access control
|
||||
2 or 3: require password for client access control
|
||||
@@ -169,15 +166,16 @@ dnssd_init(const char* name, int name_len, const char* hw_addr, int hw_addr_len,
|
||||
|
||||
if (error) *error = DNSSD_ERROR_NOERROR;
|
||||
|
||||
dnssd = calloc(1, sizeof(dnssd_t));
|
||||
dnssd_t *dnssd = (dnssd_t *) calloc(1, sizeof(dnssd_t));
|
||||
if (!dnssd) {
|
||||
if (error) *error = DNSSD_ERROR_OUTOFMEM;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
dnssd->pin_pw = pin_pw;
|
||||
|
||||
features = strtoul(FEATURES_1, &end, 16);
|
||||
|
||||
char *end = NULL;
|
||||
unsigned long features = strtoul(FEATURES_1, &end, 16);
|
||||
if (!end || (features & 0xFFFFFFFF) != features) {
|
||||
free (dnssd);
|
||||
if (error) *error = DNSSD_ERROR_BADFEATURES;
|
||||
@@ -289,8 +287,8 @@ int
|
||||
dnssd_register_raop(dnssd_t *dnssd, unsigned short port)
|
||||
{
|
||||
char servname[MAX_SERVNAME];
|
||||
DNSServiceErrorType retval;
|
||||
char features[22];
|
||||
|
||||
char features[22] = {0};
|
||||
|
||||
assert(dnssd);
|
||||
|
||||
@@ -347,13 +345,13 @@ dnssd_register_raop(dnssd_t *dnssd, unsigned short port)
|
||||
strncat(servname, dnssd->name, sizeof(servname)-strlen(servname)-1);
|
||||
|
||||
/* Register the service */
|
||||
retval = dnssd->DNSServiceRegister(&dnssd->raop_service, 0, 0,
|
||||
servname, "_raop._tcp",
|
||||
NULL, NULL,
|
||||
htons(port),
|
||||
dnssd->TXTRecordGetLength(&dnssd->raop_record),
|
||||
dnssd->TXTRecordGetBytesPtr(&dnssd->raop_record),
|
||||
NULL, NULL);
|
||||
DNSServiceErrorType retval = dnssd->DNSServiceRegister(&dnssd->raop_service, 0, 0,
|
||||
servname, "_raop._tcp",
|
||||
NULL, NULL,
|
||||
htons(port),
|
||||
dnssd->TXTRecordGetLength(&dnssd->raop_record),
|
||||
dnssd->TXTRecordGetBytesPtr(&dnssd->raop_record),
|
||||
NULL, NULL);
|
||||
|
||||
return (int) retval; /* error codes are listed in Apple's dns_sd.h */
|
||||
}
|
||||
@@ -362,8 +360,7 @@ int
|
||||
dnssd_register_airplay(dnssd_t *dnssd, unsigned short port)
|
||||
{
|
||||
char device_id[3 * MAX_HWADDR_LEN];
|
||||
DNSServiceErrorType retval;
|
||||
char features[22];
|
||||
char features[22] = {0};
|
||||
|
||||
assert(dnssd);
|
||||
|
||||
@@ -401,13 +398,13 @@ dnssd_register_airplay(dnssd_t *dnssd, unsigned short port)
|
||||
dnssd->TXTRecordSetValue(&dnssd->airplay_record, "vv", strlen(AIRPLAY_VV), AIRPLAY_VV);
|
||||
|
||||
/* Register the service */
|
||||
retval = dnssd->DNSServiceRegister(&dnssd->airplay_service, 0, 0,
|
||||
dnssd->name, "_airplay._tcp",
|
||||
NULL, NULL,
|
||||
htons(port),
|
||||
dnssd->TXTRecordGetLength(&dnssd->airplay_record),
|
||||
dnssd->TXTRecordGetBytesPtr(&dnssd->airplay_record),
|
||||
NULL, NULL);
|
||||
DNSServiceErrorType retval = dnssd->DNSServiceRegister(&dnssd->airplay_service, 0, 0,
|
||||
dnssd->name, "_airplay._tcp",
|
||||
NULL, NULL,
|
||||
htons(port),
|
||||
dnssd->TXTRecordGetLength(&dnssd->airplay_record),
|
||||
dnssd->TXTRecordGetBytesPtr(&dnssd->airplay_record),
|
||||
NULL, NULL);
|
||||
|
||||
return (int) retval; /* error codes are listed in Apple's dns_sd.h */
|
||||
}
|
||||
@@ -493,8 +490,8 @@ void dnssd_set_pk(dnssd_t *dnssd, char * pk_str) {
|
||||
}
|
||||
|
||||
void dnssd_set_airplay_features(dnssd_t *dnssd, int bit, int val) {
|
||||
uint32_t mask;
|
||||
uint32_t *features;
|
||||
uint32_t mask = 0;
|
||||
uint32_t *features = 0;
|
||||
if (bit < 0 || bit > 63) return;
|
||||
if (val < 0 || val > 1) return;
|
||||
if (bit >= 32) {
|
||||
|
||||
@@ -36,9 +36,7 @@ struct fairplay_s {
|
||||
fairplay_t *
|
||||
fairplay_init(logger_t *logger)
|
||||
{
|
||||
fairplay_t *fp;
|
||||
|
||||
fp = calloc(1, sizeof(fairplay_t));
|
||||
fairplay_t *fp = calloc(1, sizeof(fairplay_t));
|
||||
if (!fp) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -50,8 +48,6 @@ fairplay_init(logger_t *logger)
|
||||
int
|
||||
fairplay_setup(fairplay_t *fp, const unsigned char req[16], unsigned char res[142])
|
||||
{
|
||||
int mode;
|
||||
|
||||
assert(fp);
|
||||
|
||||
if (req[4] != 0x03) {
|
||||
@@ -59,7 +55,7 @@ fairplay_setup(fairplay_t *fp, const unsigned char req[16], unsigned char res[14
|
||||
return -1;
|
||||
}
|
||||
|
||||
mode = req[14];
|
||||
int mode = req[14];
|
||||
memcpy(res, reply_message[mode], 142);
|
||||
fp->keymsglen = 0;
|
||||
return 0;
|
||||
|
||||
@@ -145,9 +145,7 @@ on_message_complete(llhttp_t *parser)
|
||||
http_request_t *
|
||||
http_request_init(void)
|
||||
{
|
||||
http_request_t *request;
|
||||
|
||||
request = calloc(1, sizeof(http_request_t));
|
||||
http_request_t *request = calloc(1, sizeof(http_request_t));
|
||||
if (!request) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -168,11 +166,9 @@ http_request_init(void)
|
||||
void
|
||||
http_request_destroy(http_request_t *request)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (request) {
|
||||
free(request->url);
|
||||
for (i = 0; i < request->headers_size; i++) {
|
||||
for (int i = 0; i < request->headers_size; i++) {
|
||||
free(request->headers[i]);
|
||||
}
|
||||
free(request->headers);
|
||||
@@ -184,11 +180,9 @@ http_request_destroy(http_request_t *request)
|
||||
int
|
||||
http_request_add_data(http_request_t *request, const char *data, int datalen)
|
||||
{
|
||||
int ret;
|
||||
|
||||
assert(request);
|
||||
|
||||
ret = llhttp_execute(&request->parser, data, datalen);
|
||||
int ret = llhttp_execute(&request->parser, data, datalen);
|
||||
|
||||
/* support for "Upgrade" to reverse http ("PTTH/1.0") protocol */
|
||||
llhttp_resume_after_upgrade(&request->parser);
|
||||
@@ -266,14 +260,12 @@ http_request_get_protocol(http_request_t *request)
|
||||
const char *
|
||||
http_request_get_header(http_request_t *request, const char *name)
|
||||
{
|
||||
int i;
|
||||
|
||||
assert(request);
|
||||
if (request->is_reverse) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
for (i = 0; i < request->headers_size; i += 2) {
|
||||
for (int i = 0; i < request->headers_size; i += 2) {
|
||||
if (!strcmp(request->headers[i], name)) {
|
||||
return request->headers[i+1];
|
||||
}
|
||||
@@ -311,7 +303,7 @@ http_request_get_header_string(http_request_t *request, char **header_str)
|
||||
len++;
|
||||
}
|
||||
}
|
||||
char *str = calloc(len+1, sizeof(char));
|
||||
char *str = (char *) calloc(len+1, sizeof(char));
|
||||
assert(str);
|
||||
*header_str = str;
|
||||
char *p = str;
|
||||
|
||||
@@ -33,13 +33,11 @@ struct http_response_s {
|
||||
static void
|
||||
http_response_add_data(http_response_t *response, const char *data, int datalen)
|
||||
{
|
||||
int newdatasize;
|
||||
|
||||
assert(response);
|
||||
assert(data);
|
||||
assert(datalen > 0);
|
||||
|
||||
newdatasize = response->data_size;
|
||||
int newdatasize = response->data_size;
|
||||
while (response->data_size+datalen > newdatasize) {
|
||||
newdatasize *= 2;
|
||||
}
|
||||
@@ -74,7 +72,7 @@ http_response_init(http_response_t *response, const char *protocol, int code, co
|
||||
{
|
||||
assert(response);
|
||||
response->data_length = 0; /* can be used to reinitialize a previously-initialized response */
|
||||
char codestr[4];
|
||||
char codestr[4] = {0};
|
||||
|
||||
assert(code >= 100 && code < 1000);
|
||||
|
||||
@@ -136,7 +134,7 @@ http_response_finish(http_response_t *response, const char *data, int datalen)
|
||||
|
||||
if (data && datalen > 0) {
|
||||
const char *hdrname = "Content-Length";
|
||||
char hdrvalue[16];
|
||||
char hdrvalue[16] = {0};
|
||||
|
||||
memset(hdrvalue, 0, sizeof(hdrvalue));
|
||||
snprintf(hdrvalue, sizeof(hdrvalue)-1, "%d", datalen);
|
||||
|
||||
149
lib/logger.c
149
lib/logger.c
@@ -24,132 +24,127 @@
|
||||
#include "compat.h"
|
||||
|
||||
struct logger_s {
|
||||
mutex_handle_t lvl_mutex;
|
||||
mutex_handle_t cb_mutex;
|
||||
|
||||
int level;
|
||||
void *cls;
|
||||
logger_callback_t callback;
|
||||
mutex_handle_t lvl_mutex;
|
||||
mutex_handle_t cb_mutex;
|
||||
|
||||
int level;
|
||||
void *cls;
|
||||
logger_callback_t callback;
|
||||
};
|
||||
|
||||
logger_t *
|
||||
logger_init()
|
||||
{
|
||||
logger_t *logger = calloc(1, sizeof(logger_t));
|
||||
assert(logger);
|
||||
logger_t *logger = calloc(1, sizeof(logger_t));
|
||||
assert(logger);
|
||||
|
||||
MUTEX_CREATE(logger->lvl_mutex);
|
||||
MUTEX_CREATE(logger->cb_mutex);
|
||||
MUTEX_CREATE(logger->lvl_mutex);
|
||||
MUTEX_CREATE(logger->cb_mutex);
|
||||
|
||||
logger->level = LOGGER_WARNING;
|
||||
logger->callback = NULL;
|
||||
return logger;
|
||||
logger->level = LOGGER_WARNING;
|
||||
logger->callback = NULL;
|
||||
return logger;
|
||||
}
|
||||
|
||||
void
|
||||
logger_destroy(logger_t *logger)
|
||||
{
|
||||
MUTEX_DESTROY(logger->lvl_mutex);
|
||||
MUTEX_DESTROY(logger->cb_mutex);
|
||||
free(logger);
|
||||
MUTEX_DESTROY(logger->lvl_mutex);
|
||||
MUTEX_DESTROY(logger->cb_mutex);
|
||||
free(logger);
|
||||
}
|
||||
|
||||
void
|
||||
logger_set_level(logger_t *logger, int level)
|
||||
{
|
||||
assert(logger);
|
||||
assert(logger);
|
||||
|
||||
MUTEX_LOCK(logger->lvl_mutex);
|
||||
logger->level = level;
|
||||
MUTEX_UNLOCK(logger->lvl_mutex);
|
||||
MUTEX_LOCK(logger->lvl_mutex);
|
||||
logger->level = level;
|
||||
MUTEX_UNLOCK(logger->lvl_mutex);
|
||||
}
|
||||
|
||||
int
|
||||
logger_get_level(logger_t *logger)
|
||||
{
|
||||
int level;
|
||||
assert(logger);
|
||||
{
|
||||
assert(logger);
|
||||
|
||||
MUTEX_LOCK(logger->lvl_mutex);
|
||||
level = logger->level;
|
||||
MUTEX_UNLOCK(logger->lvl_mutex);
|
||||
MUTEX_LOCK(logger->lvl_mutex);
|
||||
int level = logger->level;
|
||||
MUTEX_UNLOCK(logger->lvl_mutex);
|
||||
|
||||
return level;
|
||||
return level;
|
||||
}
|
||||
|
||||
void
|
||||
logger_set_callback(logger_t *logger, logger_callback_t callback, void *cls)
|
||||
{
|
||||
assert(logger);
|
||||
assert(logger);
|
||||
|
||||
MUTEX_LOCK(logger->cb_mutex);
|
||||
logger->cls = cls;
|
||||
logger->callback = callback;
|
||||
MUTEX_UNLOCK(logger->cb_mutex);
|
||||
MUTEX_LOCK(logger->cb_mutex);
|
||||
logger->cls = cls;
|
||||
logger->callback = callback;
|
||||
MUTEX_UNLOCK(logger->cb_mutex);
|
||||
}
|
||||
|
||||
static char *
|
||||
logger_utf8_to_local(const char *str)
|
||||
{
|
||||
char *ret = NULL;
|
||||
char *ret = NULL;
|
||||
|
||||
/* FIXME: This is only implemented on Windows for now */
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
int wclen, mblen;
|
||||
WCHAR *wcstr;
|
||||
BOOL failed;
|
||||
BOOL failed;
|
||||
|
||||
wclen = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
|
||||
wcstr = malloc(sizeof(WCHAR) * wclen);
|
||||
MultiByteToWideChar(CP_UTF8, 0, str, -1, wcstr, wclen);
|
||||
int wclen = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
|
||||
WCHAR wcstr = malloc(sizeof(WCHAR) * wclen);
|
||||
MultiByteToWideChar(CP_UTF8, 0, str, -1, wcstr, wclen);
|
||||
|
||||
mblen = WideCharToMultiByte(CP_ACP, 0, wcstr, wclen, NULL, 0, NULL, &failed);
|
||||
if (failed) {
|
||||
/* Invalid characters in input, conversion failed */
|
||||
free(wcstr);
|
||||
return NULL;
|
||||
}
|
||||
int mblen = WideCharToMultiByte(CP_ACP, 0, wcstr, wclen, NULL, 0, NULL, &failed);
|
||||
if (failed) {
|
||||
/* Invalid characters in input, conversion failed */
|
||||
free(wcstr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = malloc(sizeof(CHAR) * mblen);
|
||||
WideCharToMultiByte(CP_ACP, 0, wcstr, wclen, ret, mblen, NULL, NULL);
|
||||
free(wcstr);
|
||||
ret = malloc(sizeof(CHAR) * mblen);
|
||||
WideCharToMultiByte(CP_ACP, 0, wcstr, wclen, ret, mblen, NULL, NULL);
|
||||
free(wcstr);
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
logger_log(logger_t *logger, int level, const char *fmt, ...)
|
||||
{
|
||||
char buffer[4096];
|
||||
va_list ap;
|
||||
char buffer[4096] = {0};
|
||||
va_list ap;
|
||||
|
||||
MUTEX_LOCK(logger->lvl_mutex);
|
||||
if (level > logger->level) {
|
||||
MUTEX_UNLOCK(logger->lvl_mutex);
|
||||
return;
|
||||
}
|
||||
MUTEX_UNLOCK(logger->lvl_mutex);
|
||||
MUTEX_LOCK(logger->lvl_mutex);
|
||||
if (level > logger->level) {
|
||||
MUTEX_UNLOCK(logger->lvl_mutex);
|
||||
return;
|
||||
}
|
||||
MUTEX_UNLOCK(logger->lvl_mutex);
|
||||
|
||||
buffer[sizeof(buffer)-1] = '\0';
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buffer, sizeof(buffer)-1, fmt, ap);
|
||||
va_end(ap);
|
||||
buffer[sizeof(buffer)-1] = '\0';
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(buffer, sizeof(buffer)-1, fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
MUTEX_LOCK(logger->cb_mutex);
|
||||
if (logger->callback) {
|
||||
logger->callback(logger->cls, level, buffer);
|
||||
MUTEX_UNLOCK(logger->cb_mutex);
|
||||
} else {
|
||||
char *local;
|
||||
MUTEX_UNLOCK(logger->cb_mutex);
|
||||
local = logger_utf8_to_local(buffer);
|
||||
if (local) {
|
||||
fprintf(stderr, "%s\n", local);
|
||||
free(local);
|
||||
} else {
|
||||
fprintf(stderr, "%s\n", buffer);
|
||||
}
|
||||
}
|
||||
MUTEX_LOCK(logger->cb_mutex);
|
||||
if (logger->callback) {
|
||||
logger->callback(logger->cls, level, buffer);
|
||||
MUTEX_UNLOCK(logger->cb_mutex);
|
||||
} else {
|
||||
MUTEX_UNLOCK(logger->cb_mutex);
|
||||
char *local = logger_utf8_to_local(buffer);
|
||||
if (local) {
|
||||
fprintf(stderr, "%s\n", local);
|
||||
free(local);
|
||||
} else {
|
||||
fprintf(stderr, "%s\n", buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -40,8 +40,8 @@ struct mirror_buffer_s {
|
||||
void
|
||||
mirror_buffer_init_aes(mirror_buffer_t *mirror_buffer, const uint64_t *streamConnectionID)
|
||||
{
|
||||
unsigned char aeskey_video[64];
|
||||
unsigned char aesiv_video[64];
|
||||
unsigned char aeskey_video[64] = {0};
|
||||
unsigned char aesiv_video[64] = {0};
|
||||
|
||||
assert(mirror_buffer);
|
||||
assert(streamConnectionID);
|
||||
@@ -70,9 +70,8 @@ mirror_buffer_init_aes(mirror_buffer_t *mirror_buffer, const uint64_t *streamCon
|
||||
mirror_buffer_t *
|
||||
mirror_buffer_init(logger_t *logger, const unsigned char *aeskey)
|
||||
{
|
||||
mirror_buffer_t *mirror_buffer;
|
||||
assert(aeskey);
|
||||
mirror_buffer = calloc(1, sizeof(mirror_buffer_t));
|
||||
mirror_buffer_t *mirror_buffer = (mirror_buffer_t *) calloc(1, sizeof(mirror_buffer_t));
|
||||
if (!mirror_buffer) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -100,9 +100,7 @@ netutils_init_socket(unsigned short *port, int use_ipv6, int use_udp)
|
||||
int proto = use_udp ? IPPROTO_UDP : IPPROTO_TCP;
|
||||
|
||||
struct sockaddr_storage saddr;
|
||||
socklen_t socklen;
|
||||
int server_fd;
|
||||
int ret;
|
||||
socklen_t socklen = 0;
|
||||
#ifndef _WIN32
|
||||
int reuseaddr = 1;
|
||||
#else
|
||||
@@ -111,12 +109,12 @@ netutils_init_socket(unsigned short *port, int use_ipv6, int use_udp)
|
||||
|
||||
assert(port);
|
||||
|
||||
server_fd = socket(family, type, proto);
|
||||
int server_fd = socket(family, type, proto);
|
||||
if (server_fd == -1) {
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ret = setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof (reuseaddr));
|
||||
int ret = setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof (reuseaddr));
|
||||
if (ret == -1) {
|
||||
goto cleanup;
|
||||
}
|
||||
@@ -186,8 +184,6 @@ netutils_parse_address(int family, const char *src, void *dst, int dstlen)
|
||||
struct addrinfo *result;
|
||||
struct addrinfo *ptr;
|
||||
struct addrinfo hints;
|
||||
int length;
|
||||
int ret;
|
||||
|
||||
if (family != AF_INET && family != AF_INET6) {
|
||||
return -1;
|
||||
@@ -200,13 +196,13 @@ netutils_parse_address(int family, const char *src, void *dst, int dstlen)
|
||||
hints.ai_family = family;
|
||||
hints.ai_flags = AI_PASSIVE | AI_NUMERICHOST;
|
||||
|
||||
ret = getaddrinfo(src, NULL, &hints, &result);
|
||||
int ret = getaddrinfo(src, NULL, &hints, &result);
|
||||
if (ret != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
length = -1;
|
||||
for (ptr=result; ptr!=NULL; ptr=ptr->ai_next) {
|
||||
int length = -1;
|
||||
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
|
||||
if (family == ptr->ai_family && (unsigned int)dstlen >= ptr->ai_addrlen) {
|
||||
memcpy(dst, ptr->ai_addr, ptr->ai_addrlen);
|
||||
length = ptr->ai_addrlen;
|
||||
|
||||
@@ -68,7 +68,7 @@ struct pairing_session_s {
|
||||
static int
|
||||
derive_key_internal(pairing_session_t *session, const unsigned char *salt, unsigned int saltlen, unsigned char *key, unsigned int keylen)
|
||||
{
|
||||
unsigned char hash[SHA512_DIGEST_LENGTH];
|
||||
unsigned char hash[SHA512_DIGEST_LENGTH] = {0};
|
||||
|
||||
if (keylen > sizeof(hash)) {
|
||||
return -1;
|
||||
@@ -87,9 +87,8 @@ derive_key_internal(pairing_session_t *session, const unsigned char *salt, unsig
|
||||
pairing_t *
|
||||
pairing_init_generate(const char *device_id, const char *keyfile, int *result)
|
||||
{
|
||||
pairing_t *pairing;
|
||||
*result = 0;
|
||||
pairing = calloc(1, sizeof(pairing_t));
|
||||
pairing_t *pairing = (pairing_t *) calloc(1, sizeof(pairing_t));
|
||||
if (!pairing) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -122,13 +121,11 @@ pairing_get_ecdh_secret_key(pairing_session_t *session, unsigned char ecdh_secre
|
||||
pairing_session_t *
|
||||
pairing_session_init(pairing_t *pairing)
|
||||
{
|
||||
pairing_session_t *session;
|
||||
|
||||
if (!pairing) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
session = calloc(1, sizeof(pairing_session_t));
|
||||
pairing_session_t *session = (pairing_session_t *) calloc(1, sizeof(pairing_session_t));
|
||||
if (!session) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -262,8 +259,8 @@ pairing_digest_verify(const char *method, const char * authorization, const char
|
||||
char *cursor = sentence;
|
||||
const char *pwd = password;
|
||||
const char *mthd = method;
|
||||
char *raw;
|
||||
int len;
|
||||
int len = 0;
|
||||
char *raw = NULL;
|
||||
bool authenticated;
|
||||
|
||||
#ifdef test_digest
|
||||
@@ -472,7 +469,7 @@ int
|
||||
random_pin() {
|
||||
unsigned char random_bytes[2] = { 0 };
|
||||
unsigned short random_short = 0;
|
||||
int ret;
|
||||
int ret = 0;
|
||||
/* create a random unsigned short in range 1-9999 */
|
||||
while (!random_short) {
|
||||
if ((ret = get_random_bytes(random_bytes, sizeof(random_bytes)) < 1)) {
|
||||
@@ -505,13 +502,13 @@ srp_new_user(pairing_session_t *session, pairing_t *pairing, const char *device_
|
||||
get_random_bytes(session->srp->private_key, SRP_PRIVATE_KEY_SIZE);
|
||||
|
||||
const unsigned char *srp_b = session->srp->private_key;
|
||||
unsigned char * srp_B;
|
||||
unsigned char * srp_s;
|
||||
unsigned char * srp_v;
|
||||
unsigned char * srp_B = NULL;
|
||||
unsigned char * srp_s = NULL;
|
||||
unsigned char * srp_v = NULL;
|
||||
int len_b = SRP_PRIVATE_KEY_SIZE;
|
||||
int len_B;
|
||||
int len_s;
|
||||
int len_v;
|
||||
int len_B = 0;
|
||||
int len_s = 0;
|
||||
int len_v = 0;
|
||||
srp_create_salted_verification_key(SRP_SHA, SRP_NG, device_id,
|
||||
(const unsigned char *) pin, strlen (pin),
|
||||
(const unsigned char **) &srp_s, &len_s,
|
||||
@@ -579,10 +576,9 @@ srp_validate_proof(pairing_session_t *session, pairing_t *pairing, const unsigne
|
||||
int
|
||||
srp_confirm_pair_setup(pairing_session_t *session, pairing_t *pairing,
|
||||
unsigned char *epk, unsigned char *auth_tag) {
|
||||
unsigned char aesKey[16], aesIV[16];
|
||||
unsigned char hash[SHA512_DIGEST_LENGTH];
|
||||
unsigned char pk[ED25519_KEY_SIZE];
|
||||
int pk_len_client, epk_len;
|
||||
unsigned char aesKey[16] = {0}, aesIV[16] = {0};
|
||||
unsigned char hash[SHA512_DIGEST_LENGTH] = {0};
|
||||
unsigned char pk[ED25519_KEY_SIZE] = {0};
|
||||
/* decrypt client epk to get client pk, authenticate with auth_tag*/
|
||||
|
||||
const char *salt = "Pair-Setup-AES-Key";
|
||||
@@ -607,7 +603,7 @@ srp_confirm_pair_setup(pairing_session_t *session, pairing_t *pairing,
|
||||
session->srp = NULL;
|
||||
|
||||
/* decrypt client epk to authenticate client using auth_tag */
|
||||
pk_len_client = gcm_decrypt(epk, ED25519_KEY_SIZE, pk, aesKey, aesIV, auth_tag);
|
||||
int pk_len_client = gcm_decrypt(epk, ED25519_KEY_SIZE, pk, aesKey, aesIV, auth_tag);
|
||||
if (pk_len_client <= 0) {
|
||||
/* authentication failed */
|
||||
return pk_len_client;
|
||||
@@ -622,7 +618,7 @@ srp_confirm_pair_setup(pairing_session_t *session, pairing_t *pairing,
|
||||
|
||||
/* encryption needs this previously undocumented additional "nonce" */
|
||||
aesIV[15]++;
|
||||
epk_len = gcm_encrypt(pk, ED25519_KEY_SIZE, epk, aesKey, aesIV, auth_tag);
|
||||
int epk_len = gcm_encrypt(pk, ED25519_KEY_SIZE, epk, aesKey, aesIV, auth_tag);
|
||||
return epk_len;
|
||||
}
|
||||
|
||||
@@ -630,7 +626,7 @@ void get_pairing_session_client_data(pairing_session_t *session, char **username
|
||||
int len64 = 4 * (1 + (ED25519_KEY_SIZE / 3)) + 1;
|
||||
*username = session->username;
|
||||
if (session->pair_setup) {
|
||||
*client_pk64 = (char *) malloc(len64);
|
||||
*client_pk64 = (char *) calloc(len64, sizeof(char));
|
||||
pk_to_base64(session->client_pk, ED25519_KEY_SIZE, *client_pk64, len64);
|
||||
} else {
|
||||
*client_pk64 = NULL;
|
||||
@@ -639,6 +635,6 @@ void get_pairing_session_client_data(pairing_session_t *session, char **username
|
||||
|
||||
void ed25519_pk_to_base64(const unsigned char *pk, char **pk64) {
|
||||
int len64 = 4 * (1 + (ED25519_KEY_SIZE / 3)) + 1;
|
||||
*pk64 = (char *) malloc(len64);
|
||||
*pk64 = (char *) calloc(len64, sizeof(char));
|
||||
pk_to_base64(pk, ED25519_KEY_SIZE, *pk64, len64);
|
||||
}
|
||||
|
||||
10
lib/raop.c
10
lib/raop.c
@@ -329,7 +329,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) {
|
||||
logger_log(conn->raop->logger, LOGGER_DEBUG, "%s", header_str);
|
||||
bool data_is_plist = (strstr(header_str,"apple-binary-plist") != NULL);
|
||||
bool data_is_text = (strstr(header_str,"text/") != NULL);
|
||||
int request_datalen;
|
||||
int request_datalen = 0;
|
||||
const char *request_data = http_request_get_data(request, &request_datalen);
|
||||
if (request_data) {
|
||||
if (request_datalen > 0) {
|
||||
@@ -339,7 +339,7 @@ conn_request(void *ptr, http_request_t *request, http_response_t **response) {
|
||||
plist_from_bin(request_data, request_datalen, &req_root_node);
|
||||
char * plist_xml = NULL;
|
||||
char * stripped_xml = NULL;
|
||||
uint32_t plist_len;
|
||||
uint32_t plist_len = 0;
|
||||
plist_to_xml(req_root_node, &plist_xml, &plist_len);
|
||||
stripped_xml = utils_strip_data_from_plist_xml(plist_xml);
|
||||
logger_log(conn->raop->logger, LOGGER_DEBUG, "%s", (stripped_xml ? stripped_xml : plist_xml));
|
||||
@@ -552,8 +552,6 @@ conn_destroy(void *ptr) {
|
||||
|
||||
raop_t *
|
||||
raop_init(raop_callbacks_t *callbacks) {
|
||||
raop_t *raop;
|
||||
|
||||
assert(callbacks);
|
||||
|
||||
/* Initialize the network */
|
||||
@@ -568,7 +566,7 @@ raop_init(raop_callbacks_t *callbacks) {
|
||||
}
|
||||
|
||||
/* Allocate the raop_t structure */
|
||||
raop = calloc(1, sizeof(raop_t));
|
||||
raop_t *raop = (raop_t *) calloc(1, sizeof(raop_t));
|
||||
if (!raop) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -623,7 +621,7 @@ raop_init2(raop_t *raop, int nohold, const char *device_id, const char *keyfile)
|
||||
httpd_t *httpd = NULL;
|
||||
|
||||
/* create a new public key for pairing */
|
||||
int new_key;
|
||||
int new_key = 0;
|
||||
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");
|
||||
|
||||
@@ -69,10 +69,9 @@ raop_buffer_init(logger_t *logger,
|
||||
const unsigned char *aeskey,
|
||||
const unsigned char *aesiv)
|
||||
{
|
||||
raop_buffer_t *raop_buffer;
|
||||
assert(aeskey);
|
||||
assert(aesiv);
|
||||
raop_buffer = calloc(1, sizeof(raop_buffer_t));
|
||||
raop_buffer_t *raop_buffer = (raop_buffer_t *) calloc(1, sizeof(raop_buffer_t));
|
||||
if (!raop_buffer) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -118,7 +117,6 @@ int
|
||||
raop_buffer_decrypt(raop_buffer_t *raop_buffer, unsigned char *data, unsigned char* output, unsigned int payload_size, unsigned int *outputlen)
|
||||
{
|
||||
assert(raop_buffer);
|
||||
int encryptedlen;
|
||||
if (DECRYPTION_TEST) {
|
||||
char *str = utils_data_to_string(data,12,12);
|
||||
logger_log(raop_buffer->logger, LOGGER_INFO, "encrypted 12 byte header %s", str);
|
||||
@@ -129,7 +127,7 @@ raop_buffer_decrypt(raop_buffer_t *raop_buffer, unsigned char *data, unsigned ch
|
||||
free(str);
|
||||
}
|
||||
}
|
||||
encryptedlen = payload_size / 16*16;
|
||||
int encryptedlen = payload_size / 16*16;
|
||||
memset(output, 0, payload_size);
|
||||
|
||||
aes_cbc_decrypt(raop_buffer->aes_ctx, &data[12], output, encryptedlen);
|
||||
@@ -181,7 +179,7 @@ raop_buffer_enqueue(raop_buffer_t *raop_buffer, unsigned char *data, unsigned sh
|
||||
int payload_size = datalen - 12;
|
||||
|
||||
/* Get correct seqnum for the packet */
|
||||
unsigned short seqnum;
|
||||
unsigned short seqnum = 0;
|
||||
if (use_seqnum) {
|
||||
seqnum = byteutils_get_short_be(data, 2);
|
||||
} else {
|
||||
|
||||
@@ -126,8 +126,7 @@ raop_ntp_compare(const void* av, const void* bv)
|
||||
static int
|
||||
raop_ntp_parse_remote(raop_ntp_t *raop_ntp, const char *remote, int remote_addr_len)
|
||||
{
|
||||
int family;
|
||||
int ret;
|
||||
int family = AF_UNSPEC;
|
||||
assert(raop_ntp);
|
||||
if (remote_addr_len == 4) {
|
||||
family = AF_INET;
|
||||
@@ -137,7 +136,7 @@ raop_ntp_parse_remote(raop_ntp_t *raop_ntp, const char *remote, int remote_addr_
|
||||
return -1;
|
||||
}
|
||||
logger_log(raop_ntp->logger, LOGGER_DEBUG, "raop_ntp parse remote ip = %s", remote);
|
||||
ret = netutils_parse_address(family, remote,
|
||||
int ret = netutils_parse_address(family, remote,
|
||||
&raop_ntp->remote_saddr,
|
||||
sizeof(raop_ntp->remote_saddr));
|
||||
if (ret < 0) {
|
||||
@@ -149,12 +148,10 @@ raop_ntp_parse_remote(raop_ntp_t *raop_ntp, const char *remote, int remote_addr_
|
||||
|
||||
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_ntp_t *raop_ntp;
|
||||
|
||||
assert(logger);
|
||||
assert(callbacks);
|
||||
|
||||
raop_ntp = calloc(1, sizeof(raop_ntp_t));
|
||||
raop_ntp_t *raop_ntp = calloc(1, sizeof(raop_ntp_t));
|
||||
if (!raop_ntp) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -279,8 +276,8 @@ raop_ntp_thread(void *arg)
|
||||
{
|
||||
raop_ntp_t *raop_ntp = arg;
|
||||
assert(raop_ntp);
|
||||
unsigned char response[128];
|
||||
int response_len;
|
||||
unsigned char response[128] = {0};
|
||||
int response_len = 0;
|
||||
unsigned char request[32] = {0x80, 0xd2, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
@@ -125,8 +125,8 @@ struct raop_rtp_s {
|
||||
static int
|
||||
raop_rtp_parse_remote(raop_rtp_t *raop_rtp, const char *remote, int remotelen)
|
||||
{
|
||||
int family;
|
||||
int ret;
|
||||
int family = AF_UNSPEC;
|
||||
int ret = 0;
|
||||
assert(raop_rtp);
|
||||
if (remotelen == 4) {
|
||||
family = AF_INET;
|
||||
@@ -150,7 +150,7 @@ raop_rtp_t *
|
||||
raop_rtp_init(logger_t *logger, raop_callbacks_t *callbacks, raop_ntp_t *ntp, const char *remote,
|
||||
int remotelen, const unsigned char *aeskey, const unsigned char *aesiv)
|
||||
{
|
||||
raop_rtp_t *raop_rtp;
|
||||
raop_rtp_t *raop_rtp = NULL;
|
||||
|
||||
assert(logger);
|
||||
assert(callbacks);
|
||||
@@ -214,11 +214,11 @@ static int
|
||||
raop_rtp_resend_callback(void *opaque, unsigned short seqnum, unsigned short count)
|
||||
{
|
||||
raop_rtp_t *raop_rtp = opaque;
|
||||
unsigned char packet[8];
|
||||
unsigned short ourseqnum;
|
||||
struct sockaddr *addr;
|
||||
socklen_t addrlen;
|
||||
int ret;
|
||||
unsigned char packet[8] = {0};
|
||||
unsigned short ourseqnum = 0;
|
||||
struct sockaddr *addr = NULL;
|
||||
socklen_t addrlen = 0;
|
||||
int ret = 0;
|
||||
|
||||
addr = (struct sockaddr *)&raop_rtp->control_saddr;
|
||||
addrlen = raop_rtp->control_saddr_len;
|
||||
@@ -278,20 +278,6 @@ raop_rtp_init_sockets(raop_rtp_t *raop_rtp, int use_ipv6)
|
||||
static int
|
||||
raop_rtp_process_events(raop_rtp_t *raop_rtp, void *cb_data)
|
||||
{
|
||||
int flush;
|
||||
float volume;
|
||||
int volume_changed;
|
||||
unsigned char *metadata;
|
||||
int metadata_len;
|
||||
unsigned char *coverart;
|
||||
int coverart_len;
|
||||
char *dacp_id;
|
||||
char *active_remote_header;
|
||||
uint32_t progress_start;
|
||||
uint32_t progress_curr;
|
||||
uint32_t progress_end;
|
||||
int progress_changed;
|
||||
|
||||
assert(raop_rtp);
|
||||
|
||||
MUTEX_LOCK(raop_rtp->run_mutex);
|
||||
@@ -301,37 +287,37 @@ raop_rtp_process_events(raop_rtp_t *raop_rtp, void *cb_data)
|
||||
}
|
||||
|
||||
/* Read the volume level */
|
||||
volume = raop_rtp->volume;
|
||||
volume_changed = raop_rtp->volume_changed;
|
||||
float volume = raop_rtp->volume;
|
||||
int volume_changed = raop_rtp->volume_changed;
|
||||
raop_rtp->volume_changed = 0;
|
||||
|
||||
/* Read the flush value */
|
||||
flush = raop_rtp->flush;
|
||||
int flush = raop_rtp->flush;
|
||||
raop_rtp->flush = NO_FLUSH;
|
||||
|
||||
/* Read the metadata */
|
||||
metadata = raop_rtp->metadata;
|
||||
metadata_len = raop_rtp->metadata_len;
|
||||
unsigned char *metadata = raop_rtp->metadata;
|
||||
int metadata_len = raop_rtp->metadata_len;
|
||||
raop_rtp->metadata = NULL;
|
||||
raop_rtp->metadata_len = 0;
|
||||
|
||||
/* Read the coverart */
|
||||
coverart = raop_rtp->coverart;
|
||||
coverart_len = raop_rtp->coverart_len;
|
||||
unsigned char *coverart = raop_rtp->coverart;
|
||||
int coverart_len = raop_rtp->coverart_len;
|
||||
raop_rtp->coverart = NULL;
|
||||
raop_rtp->coverart_len = 0;
|
||||
|
||||
/* Read DACP remote control data */
|
||||
dacp_id = raop_rtp->dacp_id;
|
||||
active_remote_header = raop_rtp->active_remote_header;
|
||||
char *dacp_id = raop_rtp->dacp_id;
|
||||
char *active_remote_header = raop_rtp->active_remote_header;
|
||||
raop_rtp->dacp_id = NULL;
|
||||
raop_rtp->active_remote_header = NULL;
|
||||
|
||||
/* Read the progress values */
|
||||
progress_start = raop_rtp->progress_start;
|
||||
progress_curr = raop_rtp->progress_curr;
|
||||
progress_end = raop_rtp->progress_end;
|
||||
progress_changed = raop_rtp->progress_changed;
|
||||
uint32_t progress_start = raop_rtp->progress_start;
|
||||
uint32_t progress_curr = raop_rtp->progress_curr;
|
||||
uint32_t progress_end = raop_rtp->progress_end;
|
||||
int progress_changed = raop_rtp->progress_changed;
|
||||
raop_rtp->progress_changed = 0;
|
||||
|
||||
MUTEX_UNLOCK(raop_rtp->run_mutex);
|
||||
@@ -409,9 +395,9 @@ raop_rtp_thread_udp(void *arg)
|
||||
{
|
||||
raop_rtp_t *raop_rtp = arg;
|
||||
unsigned char packet[RAOP_PACKET_LEN];
|
||||
unsigned int packetlen;
|
||||
unsigned int packetlen = 0;
|
||||
struct sockaddr_storage saddr;
|
||||
socklen_t saddrlen;
|
||||
socklen_t saddrlen = 0;
|
||||
bool got_remote_control_saddr = false;
|
||||
uint64_t video_arrival_offset = 0;
|
||||
|
||||
@@ -445,8 +431,7 @@ raop_rtp_thread_udp(void *arg)
|
||||
while(1) {
|
||||
fd_set rfds;
|
||||
struct timeval tv;
|
||||
int nfds, ret;
|
||||
/* Check if we are still running and process callbacks */
|
||||
/* Check if we are still running and process callbacks */
|
||||
if (raop_rtp_process_events(raop_rtp, NULL)) {
|
||||
break;
|
||||
}
|
||||
@@ -456,7 +441,7 @@ raop_rtp_thread_udp(void *arg)
|
||||
tv.tv_usec = 5000;
|
||||
|
||||
/* Get the correct nfds value */
|
||||
nfds = raop_rtp->csock+1;
|
||||
int nfds = raop_rtp->csock+1;
|
||||
if (raop_rtp->dsock >= nfds)
|
||||
nfds = raop_rtp->dsock+1;
|
||||
|
||||
@@ -465,7 +450,7 @@ raop_rtp_thread_udp(void *arg)
|
||||
FD_SET(raop_rtp->csock, &rfds);
|
||||
FD_SET(raop_rtp->dsock, &rfds);
|
||||
|
||||
ret = select(nfds, &rfds, NULL, NULL, &tv);
|
||||
int ret = select(nfds, &rfds, NULL, NULL, &tv);
|
||||
if (ret == 0) {
|
||||
/* Timeout happened */
|
||||
continue;
|
||||
@@ -635,9 +620,9 @@ raop_rtp_thread_udp(void *arg)
|
||||
} else {
|
||||
// Render continuous buffer entries
|
||||
void *payload = NULL;
|
||||
unsigned int payload_size;
|
||||
unsigned short seqnum;
|
||||
uint32_t rtp_timestamp;
|
||||
unsigned int payload_size = 0;
|
||||
unsigned short seqnum = 0;
|
||||
uint32_t rtp_timestamp = 0;
|
||||
|
||||
while ((payload = raop_buffer_dequeue(raop_rtp->buffer, &payload_size, &rtp_timestamp, &seqnum, no_resend))) {
|
||||
audio_decode_struct audio_data;
|
||||
@@ -743,14 +728,12 @@ raop_rtp_set_volume(raop_rtp_t *raop_rtp, float volume)
|
||||
void
|
||||
raop_rtp_set_metadata(raop_rtp_t *raop_rtp, const char *data, int datalen)
|
||||
{
|
||||
unsigned char *metadata;
|
||||
|
||||
assert(raop_rtp);
|
||||
|
||||
if (datalen <= 0) {
|
||||
return;
|
||||
}
|
||||
metadata = malloc(datalen);
|
||||
unsigned char *metadata = (unsigned char *) malloc(datalen);
|
||||
assert(metadata);
|
||||
memcpy(metadata, data, datalen);
|
||||
|
||||
@@ -767,14 +750,12 @@ raop_rtp_set_metadata(raop_rtp_t *raop_rtp, const char *data, int datalen)
|
||||
void
|
||||
raop_rtp_set_coverart(raop_rtp_t *raop_rtp, const char *data, int datalen)
|
||||
{
|
||||
unsigned char *coverart;
|
||||
|
||||
assert(raop_rtp);
|
||||
|
||||
if (datalen <= 0) {
|
||||
return;
|
||||
}
|
||||
coverart = malloc(datalen);
|
||||
unsigned char *coverart = (unsigned char *) malloc(datalen);
|
||||
assert(coverart);
|
||||
memcpy(coverart, data, datalen);
|
||||
|
||||
|
||||
@@ -114,9 +114,8 @@ struct raop_rtp_mirror_s {
|
||||
static int
|
||||
raop_rtp_mirror_parse_remote(raop_rtp_mirror_t *raop_rtp_mirror, const char *remote, int remotelen)
|
||||
{
|
||||
int family;
|
||||
int ret;
|
||||
assert(raop_rtp_mirror);
|
||||
int family = AF_UNSPEC;
|
||||
if (remotelen == 4) {
|
||||
family = AF_INET;
|
||||
} else if (remotelen == 16) {
|
||||
@@ -125,7 +124,7 @@ raop_rtp_mirror_parse_remote(raop_rtp_mirror_t *raop_rtp_mirror, const char *rem
|
||||
return -1;
|
||||
}
|
||||
logger_log(raop_rtp_mirror->logger, LOGGER_DEBUG, "raop_rtp_mirror parse remote ip = %s", remote);
|
||||
ret = netutils_parse_address(family, remote,
|
||||
int ret = netutils_parse_address(family, remote,
|
||||
&raop_rtp_mirror->remote_saddr,
|
||||
sizeof(raop_rtp_mirror->remote_saddr));
|
||||
if (ret < 0) {
|
||||
@@ -139,12 +138,10 @@ raop_rtp_mirror_parse_remote(raop_rtp_mirror_t *raop_rtp_mirror, const char *rem
|
||||
raop_rtp_mirror_t *raop_rtp_mirror_init(logger_t *logger, raop_callbacks_t *callbacks, raop_ntp_t *ntp,
|
||||
const char *remote, int remotelen, const unsigned char *aeskey)
|
||||
{
|
||||
raop_rtp_mirror_t *raop_rtp_mirror;
|
||||
|
||||
assert(logger);
|
||||
assert(callbacks);
|
||||
|
||||
raop_rtp_mirror = calloc(1, sizeof(raop_rtp_mirror_t));
|
||||
raop_rtp_mirror_t *raop_rtp_mirror = calloc(1, sizeof(raop_rtp_mirror_t));
|
||||
if (!raop_rtp_mirror) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -186,8 +183,7 @@ raop_rtp_mirror_thread(void *arg)
|
||||
assert(raop_rtp_mirror);
|
||||
|
||||
int stream_fd = -1;
|
||||
unsigned char packet[128];
|
||||
memset(packet, 0 , 128);
|
||||
unsigned char packet[128] = {0};
|
||||
unsigned char* sps_pps = NULL;
|
||||
bool prepend_sps_pps = false;
|
||||
int sps_pps_len = 0;
|
||||
@@ -212,7 +208,6 @@ raop_rtp_mirror_thread(void *arg)
|
||||
while (1) {
|
||||
fd_set rfds;
|
||||
struct timeval tv;
|
||||
int nfds, ret;
|
||||
MUTEX_LOCK(raop_rtp_mirror->run_mutex);
|
||||
if (!raop_rtp_mirror->running) {
|
||||
MUTEX_UNLOCK(raop_rtp_mirror->run_mutex);
|
||||
@@ -227,6 +222,7 @@ raop_rtp_mirror_thread(void *arg)
|
||||
|
||||
/* Get the correct nfds value and set rfds */
|
||||
FD_ZERO(&rfds);
|
||||
int nfds = 0;
|
||||
if (stream_fd == -1) {
|
||||
FD_SET(raop_rtp_mirror->mirror_data_sock, &rfds);
|
||||
nfds = raop_rtp_mirror->mirror_data_sock+1;
|
||||
@@ -234,7 +230,7 @@ raop_rtp_mirror_thread(void *arg)
|
||||
FD_SET(stream_fd, &rfds);
|
||||
nfds = stream_fd+1;
|
||||
}
|
||||
ret = select(nfds, &rfds, NULL, NULL, &tv);
|
||||
int ret = select(nfds, &rfds, NULL, NULL, &tv);
|
||||
if (ret == 0) {
|
||||
/* Timeout happened */
|
||||
continue;
|
||||
@@ -249,7 +245,7 @@ raop_rtp_mirror_thread(void *arg)
|
||||
(raop_rtp_mirror && raop_rtp_mirror->mirror_data_sock >= 0) &&
|
||||
FD_ISSET(raop_rtp_mirror->mirror_data_sock, &rfds)) {
|
||||
struct sockaddr_storage saddr;
|
||||
socklen_t saddrlen;
|
||||
socklen_t saddrlen = 0;
|
||||
logger_log(raop_rtp_mirror->logger, LOGGER_DEBUG, "raop_rtp_mirror accepting client");
|
||||
saddrlen = sizeof(saddr);
|
||||
stream_fd = accept(raop_rtp_mirror->mirror_data_sock, (struct sockaddr *)&saddr, &saddrlen);
|
||||
@@ -271,8 +267,7 @@ raop_rtp_mirror_thread(void *arg)
|
||||
break;
|
||||
}
|
||||
|
||||
int option;
|
||||
option = 1;
|
||||
int option = 1;
|
||||
if (setsockopt(stream_fd, SOL_SOCKET, SO_KEEPALIVE, CAST &option, sizeof(option)) < 0) {
|
||||
int sock_err = SOCKET_GET_ERROR();
|
||||
logger_log(raop_rtp_mirror->logger, LOGGER_WARNING,
|
||||
@@ -474,7 +469,7 @@ raop_rtp_mirror_thread(void *arg)
|
||||
valid_data = false;
|
||||
break;
|
||||
}
|
||||
int nalu_type;
|
||||
int nalu_type = 0;
|
||||
if (h265_video) {
|
||||
nalu_type = payload_decrypted[nalu_size] & 0x7e >> 1;;
|
||||
//logger_log(raop_rtp_mirror->logger, LOGGER_DEBUG," h265 video, NALU type %d, size %d", nalu_type, nc_len);
|
||||
@@ -648,12 +643,6 @@ raop_rtp_mirror_thread(void *arg)
|
||||
unsigned char vps_start_code[] = { 0xa0, 0x00, 0x01, 0x00 };
|
||||
unsigned char sps_start_code[] = { 0xa1, 0x00, 0x01, 0x00 };
|
||||
unsigned char pps_start_code[] = { 0xa2, 0x00, 0x01, 0x00 };
|
||||
unsigned char *vps;
|
||||
short vps_size;
|
||||
unsigned char *sps;
|
||||
short sps_size;
|
||||
unsigned char *pps;
|
||||
short pps_size;
|
||||
|
||||
unsigned char * ptr = payload + 0x75;
|
||||
|
||||
@@ -662,9 +651,9 @@ raop_rtp_mirror_thread(void *arg)
|
||||
raop_rtp_mirror->callbacks.video_pause(raop_rtp_mirror->callbacks.cls);
|
||||
break;
|
||||
}
|
||||
vps_size = byteutils_get_short_be(ptr, 3);
|
||||
short vps_size = byteutils_get_short_be(ptr, 3);
|
||||
ptr += 5;
|
||||
vps = ptr;
|
||||
unsigned char *vps = ptr;
|
||||
if (logger_debug) {
|
||||
char *str = utils_data_to_string(vps, vps_size, 16);
|
||||
logger_log(raop_rtp_mirror->logger, LOGGER_INFO, "h265 vps size %d\n%s",vps_size, str);
|
||||
@@ -676,9 +665,9 @@ raop_rtp_mirror_thread(void *arg)
|
||||
raop_rtp_mirror->callbacks.video_pause(raop_rtp_mirror->callbacks.cls);
|
||||
break;
|
||||
}
|
||||
sps_size = byteutils_get_short_be(ptr, 3);
|
||||
short sps_size = byteutils_get_short_be(ptr, 3);
|
||||
ptr += 5;
|
||||
sps = ptr;
|
||||
unsigned char *sps = ptr;
|
||||
if (logger_debug) {
|
||||
char *str = utils_data_to_string(sps, sps_size, 16);
|
||||
logger_log(raop_rtp_mirror->logger, LOGGER_INFO, "h265 sps size %d\n%s",vps_size, str);
|
||||
@@ -690,9 +679,9 @@ raop_rtp_mirror_thread(void *arg)
|
||||
raop_rtp_mirror->callbacks.video_pause(raop_rtp_mirror->callbacks.cls);
|
||||
break;
|
||||
}
|
||||
pps_size = byteutils_get_short_be(ptr, 3);
|
||||
short pps_size = byteutils_get_short_be(ptr, 3);
|
||||
ptr += 5;
|
||||
pps = ptr;
|
||||
unsigned char *pps = ptr;
|
||||
if (logger_debug) {
|
||||
char *str = utils_data_to_string(pps, pps_size, 16);
|
||||
logger_log(raop_rtp_mirror->logger, LOGGER_INFO, "h265 pps size %d\n%s",pps_size, str);
|
||||
@@ -812,8 +801,8 @@ raop_rtp_mirror_thread(void *arg)
|
||||
}
|
||||
}
|
||||
if (plist_size) {
|
||||
char *plist_xml;
|
||||
uint32_t plist_len;
|
||||
char *plist_xml = NULL;
|
||||
uint32_t plist_len = 0;
|
||||
plist_t root_node = NULL;
|
||||
plist_from_bin((char *) payload, plist_size, &root_node);
|
||||
if (raop_rtp_mirror->callbacks.mirror_video_activity) {
|
||||
|
||||
62
lib/utils.c
62
lib/utils.c
@@ -26,15 +26,12 @@
|
||||
char *
|
||||
utils_strsep(char **stringp, const char *delim)
|
||||
{
|
||||
char *original;
|
||||
char *strptr;
|
||||
|
||||
if (*stringp == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
original = *stringp;
|
||||
strptr = strstr(*stringp, delim);
|
||||
char *original = *stringp;
|
||||
char *strptr = strstr(*stringp, delim);
|
||||
if (strptr == NULL) {
|
||||
*stringp = NULL;
|
||||
return original;
|
||||
@@ -47,31 +44,26 @@ utils_strsep(char **stringp, const char *delim)
|
||||
int
|
||||
utils_read_file(char **dst, const char *filename)
|
||||
{
|
||||
FILE *stream;
|
||||
int filesize;
|
||||
char *buffer;
|
||||
int read_bytes;
|
||||
|
||||
/* Open stream for reading */
|
||||
stream = fopen(filename, "rb");
|
||||
FILE *stream = fopen(filename, "rb");
|
||||
if (!stream) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Find out file size */
|
||||
fseek(stream, 0, SEEK_END);
|
||||
filesize = ftell(stream);
|
||||
int filesize = ftell(stream);
|
||||
fseek(stream, 0, SEEK_SET);
|
||||
|
||||
/* Allocate one extra byte for zero */
|
||||
buffer = malloc(filesize+1);
|
||||
char *buffer = malloc(filesize+1);
|
||||
if (!buffer) {
|
||||
fclose(stream);
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* Read data in a loop to buffer */
|
||||
read_bytes = 0;
|
||||
int read_bytes = 0;
|
||||
do {
|
||||
int ret = fread(buffer+read_bytes, 1,
|
||||
filesize-read_bytes, stream);
|
||||
@@ -99,14 +91,13 @@ utils_read_file(char **dst, const char *filename)
|
||||
int
|
||||
utils_hwaddr_raop(char *str, int strlen, const char *hwaddr, int hwaddrlen)
|
||||
{
|
||||
int i,j;
|
||||
|
||||
/* Check that our string is long enough */
|
||||
if (strlen == 0 || strlen < 2*hwaddrlen+1)
|
||||
return -1;
|
||||
|
||||
/* Convert hardware address to hex string */
|
||||
for (i=0,j=0; i<hwaddrlen; i++) {
|
||||
int j = 0;
|
||||
for (int i = 0; i < hwaddrlen; i++) {
|
||||
int hi = (hwaddr[i]>>4) & 0x0f;
|
||||
int lo = hwaddr[i] & 0x0f;
|
||||
|
||||
@@ -124,14 +115,13 @@ utils_hwaddr_raop(char *str, int strlen, const char *hwaddr, int hwaddrlen)
|
||||
int
|
||||
utils_hwaddr_airplay(char *str, int strlen, const char *hwaddr, int hwaddrlen)
|
||||
{
|
||||
int i,j;
|
||||
|
||||
/* Check that our string is long enough */
|
||||
if (strlen == 0 || strlen < 2*hwaddrlen+hwaddrlen)
|
||||
return -1;
|
||||
|
||||
/* Convert hardware address to hex string */
|
||||
for (i=0,j=0; i<hwaddrlen; i++) {
|
||||
int j = 0;
|
||||
for (int i = 0; i < hwaddrlen; i++) {
|
||||
int hi = (hwaddr[i]>>4) & 0x0f;
|
||||
int lo = hwaddr[i] & 0x0f;
|
||||
|
||||
@@ -290,15 +280,9 @@ char *utils_strip_data_from_plist_xml(char *plist_xml) {
|
||||
assert(plist_xml);
|
||||
int len = (int) strlen(plist_xml);
|
||||
char *last = plist_xml + len * sizeof(char); // position of null termination of plist_xml
|
||||
|
||||
char *eol;
|
||||
char *eol_data;
|
||||
char *xml = NULL;
|
||||
int nchars;
|
||||
char line[81];
|
||||
int count;
|
||||
char line[81] = { '\0' };
|
||||
char *begin = strstr(plist_xml, "<data>");
|
||||
char *end;
|
||||
if (!begin) {
|
||||
/* there are no data lines, nothing to do */
|
||||
return NULL;
|
||||
@@ -308,14 +292,15 @@ char *utils_strip_data_from_plist_xml(char *plist_xml) {
|
||||
char *ptr1 = plist_xml;
|
||||
char *ptr2 = xml;
|
||||
do {
|
||||
eol = strchr(begin,'\n');
|
||||
nchars = eol + 1 - ptr1;
|
||||
char *eol = strchr(begin,'\n');
|
||||
int nchars = eol + 1 - ptr1;
|
||||
memcpy(ptr2, ptr1, nchars);
|
||||
ptr2 += nchars;
|
||||
ptr1 += nchars;
|
||||
end = strstr(ptr1, "</data>");
|
||||
char *end = strstr(ptr1, "</data>");
|
||||
assert(end);
|
||||
count = 0;
|
||||
int count = 0;
|
||||
char *eol_data = NULL;
|
||||
do {
|
||||
eol_data = eol;
|
||||
eol = strchr(eol + 1, '\n');
|
||||
@@ -345,12 +330,13 @@ char *utils_strip_data_from_plist_xml(char *plist_xml) {
|
||||
}
|
||||
|
||||
const char *gmt_time_string() {
|
||||
static char date_buf[64];
|
||||
memset(date_buf, 0, 64);
|
||||
static char date_buf[64] = { '\0' };
|
||||
memset(date_buf, 0, 64);
|
||||
|
||||
time_t now = time(0);
|
||||
if (strftime(date_buf, 63, "%c GMT", gmtime(&now)))
|
||||
return date_buf;
|
||||
else
|
||||
return "";
|
||||
time_t now = time(0);
|
||||
if (strftime(date_buf, 63, "%c GMT", gmtime(&now))) {
|
||||
return date_buf;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user