diff --git a/src/pEpTransportUDP.c b/src/pEpTransportUDP.c index e6d6bfd..71c90c4 100644 --- a/src/pEpTransportUDP.c +++ b/src/pEpTransportUDP.c @@ -18,6 +18,8 @@ static int polling_pid = 0; //static bool cb_sendto_result_registered; static bool cb_incoming_message_registered = false; +PEP_SESSION logsession; // Only so I can use pEp_log + PEP_transport_t udp = { 0x05, "udp://", &udp_configure, @@ -33,6 +35,7 @@ PEP_transport_t udp = { 0x05, 0 }; void _run_udp_poll() { + PEP_SESSION session = logsession; polling_pid = fork(); if (polling_pid < 0) { perror("fork"); @@ -46,7 +49,7 @@ void _run_udp_poll() { // don't care about writefds and exceptfds, never timeout: select(socki_r + 1, &readfds, NULL, NULL, NULL); if (FD_ISSET(socki_r, &readfds)) { - fprintf(stderr, "Ready to read from socket %i\n", socki_r); + LOG_EVENT("Ready to read from socket %i\n", socki_r); (*cb_incoming_message)(udp.id, PEP_tsc_ready); usleep(100000); } @@ -59,11 +62,12 @@ PEP_STATUS udp_configure( PEP_transport_t *transport, transport_config_t *config, PEP_transport_status_code *tsc) { + PEP_SESSION session = logsession; // if (allCallbacksRegistered() || execmode != PEP_cbe_async) { // return PEP_STATUS_OK; // } else { // // error - // fprintf(stderr, "Callback for signal_incoming_message needs to be registered first\n"); + // LOG_ERROR("Callback for signal_incoming_message needs to be registered first\n"); // *tsc = PEP_tsc_config_incomplete_or_wrong; // return PEP_TRANSPORT_CANNOT_INIT; // } @@ -71,6 +75,7 @@ PEP_STATUS udp_configure( } PEP_STATUS _udp_startup_sender(PEP_transport_status_code *tsc) { + PEP_SESSION session = logsession; int broadcast = 1; // aux variable to make socket broadcastable int udp_status; // Two different status variables for PEP_STATUS and other status values. PEP_STATUS pEp_status = PEP_STATUS_OK; @@ -103,8 +108,7 @@ PEP_STATUS _udp_startup_sender(PEP_transport_status_code *tsc) { sin_s.sin_port = htons(atoi(SERVERPORT)); size = sizeof(sin_s); inet_ntop(AF_INET, &(sin_s.sin_addr), ip4, INET_ADDRSTRLEN); - fprintf( - stderr, + LOG_EVENT( "Using network interface %s with IP %s %s AF_INET for sending.\n", ifa->ifa_name, ip4, @@ -136,6 +140,7 @@ end: } PEP_STATUS _udp_startup_listener(PEP_transport_status_code *tsc) { + PEP_SESSION session = logsession; int yes = 1; int udp_status; // Two different status variables for PEP_STATUS and other status values. PEP_STATUS pEp_status = PEP_STATUS_OK; @@ -154,7 +159,7 @@ PEP_STATUS _udp_startup_listener(PEP_transport_status_code *tsc) { rv = getaddrinfo(NULL, SERVERPORT, &hints, &servinfo); if (rv != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); + LOG_ERROR("getaddrinfo: %s\n", gai_strerror(rv)); *tsc = PEP_tsc_udp_no_suitable_network_for_listening; pEp_status = PEP_TRANSPORT_CANNOT_INIT_RECV; goto end; @@ -164,7 +169,8 @@ PEP_STATUS _udp_startup_listener(PEP_transport_status_code *tsc) { // maybe this should respect the sender socket more? (FIXME) for (p = servinfo; p != NULL; p = p->ai_next) { if ((socki_r = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { - perror("pEpTransportUDP: Reading socket"); + //perror("pEpTransportUDP: Reading socket"); + LOG_ERROR("pEpTransportUDP: Reading socket: %s", sys_errlist[errno]); continue; } @@ -188,7 +194,7 @@ PEP_STATUS _udp_startup_listener(PEP_transport_status_code *tsc) { } if (p == NULL) { - fprintf(stderr, "pEpTransportUDP: Reading socket: failed to bind socket\n"); + LOG_ERROR("pEpTransportUDP: Reading socket: failed to bind socket\n"); *tsc = PEP_tsc_udp_socket_error_for_listening; pEp_status = PEP_TRANSPORT_CANNOT_INIT_RECV; goto end; @@ -198,8 +204,7 @@ PEP_STATUS _udp_startup_listener(PEP_transport_status_code *tsc) { // sin_r = (struct sockaddr_in) (p->ai_addr); memset(&ip4, 0, sizeof ip4); inet_ntop(AF_INET, &(sin_r.sin_addr), ip4, INET_ADDRSTRLEN); - fprintf( - stderr, + LOG_EVENT( "pEpTransportUPD: Reading socket bound to %s, port %s, using %s protocol.\n", ip4, SERVERPORT, @@ -221,6 +226,17 @@ PEP_STATUS udp_startup(PEP_transport_t *transport, PEP_transport_status_code *ts PEP_STATUS receive_status = PEP_STATUS_OK; PEP_transport_status_code send_tsc = PEP_tsc_ready; PEP_transport_status_code receive_tsc = PEP_tsc_ready; + + // prepare a session just for logging. This won't be used otherwise. + messageToSend_t messageToSend; + inject_sync_event_t inject_sync_event; + ensure_passphrase_t ensure_passphrase; + messageToSend = NULL; //&msgts; + inject_sync_event = NULL; + ensure_passphrase = NULL; + status = init(&logsession, messageToSend, inject_sync_event, ensure_passphrase); + + send_status = _udp_startup_sender(&send_tsc); receive_status = _udp_startup_listener(&receive_tsc); *tsc = send_tsc | receive_tsc; @@ -230,6 +246,7 @@ PEP_STATUS udp_startup(PEP_transport_t *transport, PEP_transport_status_code *ts } PEP_STATUS udp_shutdown(PEP_transport_t *transport, PEP_transport_status_code *tsc) { + PEP_SESSION session = logsession; if (execmode == PEP_cbe_async && polling_pid != 0) kill(polling_pid, SIGKILL); close(socki_s); @@ -332,7 +349,7 @@ PEP_STATUS udp_sendto(PEP_SESSION session, message *msg, PEP_transport_status_co // send asn1text as an experiment udp_status = sendto(socki_s, ctext, csize, 0, (struct sockaddr *)&to_addr, size); - fprintf(stderr, "pEpTransportUDP: Bytes sent for asn.1.msg: %i of %lu\n", udp_status, csize); + LOG_EVENT("pEpTransportUDP: Bytes sent for asn.1.msg: %i of %lu\n", udp_status, csize); if (udp_status == -1) { perror("pEpTransportUDP: udp_sendto"); if (csize >= MAXBUFLEN) @@ -379,13 +396,10 @@ PEP_STATUS udp_recvnext(PEP_SESSION session, message **msg, PEP_transport_status // for future logs inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), from_ip, sizeof from_ip); - fprintf(stderr, "pEpTransportUDP: recvnext: got packet from %s\n", from_ip); - fprintf(stderr, "pEpTransportUDP: recvnext: packet is %d bytes long\n", numbytes); + LOG_EVENT("pEpTransportUDP: recvnext: got packet from %s\n", from_ip); + LOG_EVENT("pEpTransportUDP: recvnext: packet is %d bytes long\n", numbytes); buf[numbytes] = '\0'; - fprintf( - stderr, - "pEpTransportUDP: packet contains: \n------------------\n%s\n==================\n", - buf); + LOG_TRACE("pEpTransportUDP: packet contains: \n------------------\n%s\n==================\n", buf); message *_msg = NULL; ASN1Message_t *asn1msg; @@ -396,7 +410,7 @@ PEP_STATUS udp_recvnext(PEP_SESSION session, message **msg, PEP_transport_status if (numbytes > 0) { // first decrypt pep_status = decrypt_and_verify(session, buf, numbytes, NULL, 0, &asn1text, &asn1size, &keylist, NULL); - fprintf(stderr, "pEpTransportUDP: Decryption status %s\n", pEp_status_to_string(pep_status)); + LOG_TRACE("pEpTransportUDP: Decryption status %s\n", pEp_status_to_string(pep_status)); // then decode pep_status = decode_ASN1Message_message(asn1text, asn1size, &asn1msg); _msg = ASN1Message_to_message(asn1msg, NULL, 1, 0); @@ -447,6 +461,7 @@ PEP_STATUS udp_notify( signal_sendto_result_t sendto_result, signal_incoming_message_t incoming, callback_execution cbe) { + PEP_SESSION session = logsession; cb_incoming_message = incoming; if (cb_incoming_message) cb_incoming_message_registered = true; //keep it false if the callback is NULL diff --git a/src/pEpTransportUDP.h b/src/pEpTransportUDP.h index 3240b49..1c65dbf 100644 --- a/src/pEpTransportUDP.h +++ b/src/pEpTransportUDP.h @@ -23,39 +23,55 @@ extern "C" { #include #include +#include #include #include #include #include -PEP_STATUS udp_configure( - PEP_transport_t *transport, - transport_config_t *config, - PEP_transport_status_code *tsc); -PEP_STATUS udp_startup(PEP_transport_t *transport, PEP_transport_status_code *tsc); -PEP_STATUS udp_shutdown(PEP_transport_t *transport, PEP_transport_status_code *tsc); -PEP_STATUS udp_sendto(PEP_SESSION session, message *msg, PEP_transport_status_code *tsc); -PEP_STATUS udp_recvnext(PEP_SESSION session, message **msg, PEP_transport_status_code *tsc); -PEP_STATUS udp_notify( - signal_statuschange_t status_change, - signal_sendto_result_t sendto_result, - signal_incoming_message_t incoming, - callback_execution cbe); - -//PEP_transport_id get_id(); -//bool shortmsg_supported(); -//bool longmsg_supported(); -//bool longmsg_formatted_supported(); -//PEP_text_format native_text_format(); - -callback_execution execmode; - -// The registered callback functions -//signal_status_change_t cb_statuschange; -//signal_sendto_result_t cb_sendto_result; -signal_incoming_message_t cb_incoming_message; - -bool allCallbacksRegistered(); + PEP_STATUS udp_configure( + PEP_transport_t *transport, + transport_config_t *config, + PEP_transport_status_code *tsc); + PEP_STATUS udp_startup(PEP_transport_t *transport, PEP_transport_status_code *tsc); + PEP_STATUS udp_shutdown(PEP_transport_t *transport, PEP_transport_status_code *tsc); + PEP_STATUS udp_sendto(PEP_SESSION session, message *msg, PEP_transport_status_code *tsc); + PEP_STATUS udp_recvnext(PEP_SESSION session, message **msg, PEP_transport_status_code *tsc); + PEP_STATUS udp_notify( + signal_statuschange_t status_change, + signal_sendto_result_t sendto_result, + signal_incoming_message_t incoming, + callback_execution cbe); + + //PEP_transport_id get_id(); + //bool shortmsg_supported(); + //bool longmsg_supported(); + //bool longmsg_formatted_supported(); + //PEP_text_format native_text_format(); + + callback_execution execmode; + + // The registered callback functions + //signal_status_change_t cb_statuschange; + //signal_sendto_result_t cb_sendto_result; + signal_incoming_message_t cb_incoming_message; + + bool allCallbacksRegistered(); + +/* Define convenient logging macros. These macros take either zero arguments or a format + string followed by the arguments specified in the format. */ +#define LOG_CRITICAL(...) PEP_LOG_CRITICAL("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_ERROR(...) PEP_LOG_ERROR("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_WARNING(...) PEP_LOG_WARNING("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_API(...) PEP_LOG_API("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_EVENT(...) PEP_LOG_EVENT("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_FUNCTION(...) PEP_LOG_FUNCTION("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_NONOK(...) PEP_LOG_NONOK("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_NOTOK(...) PEP_LOG_NOTOK("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_TRACE(...) PEP_LOG_TRACE("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_PRODUCTION(...) PEP_LOG_PRODUCTION("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_BASIC(...) PEP_LOG_BASIC("p≡p", "Engine", "" __VA_ARGS__) +#define LOG_SERVICE(...) PEP_LOG_SERVICE("p≡p", "Engine", "" __VA_ARGS__) #ifdef __cplusplus }