Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: RFC5424 origin structure added #479

Open
wants to merge 1 commit into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions include/stumpless/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,22 @@ vstumplog_trace( int priority,
} /* extern "C" */
# endif

/*
* Returns RFC 5424 origin structured data element that provides information about the originator of the log message.
* see: https://datatracker.ietf.org/doc/html/rfc5424#section-7.2
*
* It used by stumpless' syslog and vsyslogs replacement - stumplog, and vstumplog.
*
* @return stumpless_element with origin structure
* ip - Origin IP address (for now just first address used ipv4 or ipv6)
* enterpriseId - Enterprise ID (skipped for now)
* software - Software name (stumpless)
* swVersion Software version (current version of stumpless)
*
* @since 2.1.1
*/
STUMPLESS_PUBLIC_FUNCTION
struct stumpless_element*
get_origin_struct_instance( void );

#endif /* __STUMPLESS_LOG_H */
169 changes: 169 additions & 0 deletions src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,26 @@
*/

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

#include <stumpless/log.h>
#include "private/memory.h"
#include <stumpless/target.h>
#include <stumpless/version.h>

#include "private/target.h"

#ifdef _WIN32
#include <winsock2.h>
#include <iphlpapi.h>
#include <ws2tcpip.h>
#else
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#endif

int
stump( const char *message, ... ) {
int result;
Expand Down Expand Up @@ -199,3 +215,156 @@

vstumpless_trace_log( target, priority, file, line, func, message, subs );
}


char** get_ip_addresses() {
char **ip_addresses = NULL;
int ip_addresses_counter = 0;

Check warning on line 222 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L220-L222

Added lines #L220 - L222 were not covered by tests
char ip[INET6_ADDRSTRLEN];

#ifdef _WIN32
WSADATA wsaData;
if ( WSAStartup( MAKEWORD(2, 2), &wsaData ) != 0 ) {
perror( "WSAStartup failed" );
return NULL;
}

// Get the list of network interfaces
ULONG size = 0;
GetAdaptersAddresses( AF_UNSPEC, 0, NULL, NULL, &size );
IP_ADAPTER_ADDRESSES *adapterAddresses = (IP_ADAPTER_ADDRESSES *)malloc( size );
if ( !adapterAddresses ) {
WSACleanup(); // Cleanup before returning
return NULL;
}
if ( GetAdaptersAddresses( AF_UNSPEC, 0, NULL, adapterAddresses, &size ) == NO_ERROR ) {
IP_ADAPTER_ADDRESSES *adapter = adapterAddresses;
while ( adapter ) {
IP_ADAPTER_UNICAST_ADDRESS *unicast = adapter->FirstUnicastAddress;
while ( unicast ) {
if ( unicast->Address.lpSockaddr->sa_family == AF_INET ) {
struct sockaddr_in *sockaddr_in = (struct sockaddr_in *)unicast->Address.lpSockaddr;
inet_ntop(AF_INET, &sockaddr_in->sin_addr, ip, sizeof(ip));

char **temp = realloc( ip_addresses, ( ip_addresses_counter + 1 ) * sizeof(char*) );
if (!temp) {
// Reallocation failed, cleanup
free( adapterAddresses );
WSACleanup();
for ( int i = 0; i < ip_addresses_counter; i++ ) {
free(ip_addresses[i]);
}
free(ip_addresses);
return NULL;
}
ip_addresses = temp;
ip_addresses[ip_addresses_counter++] = strdup( ip );

} else if ( unicast->Address.lpSockaddr->sa_family == AF_INET6 ) {
struct sockaddr_in6 *sockaddr_in6 = (struct sockaddr_in6 *)unicast->Address.lpSockaddr;
inet_ntop( AF_INET6, &sockaddr_in6->sin6_addr, ip, sizeof( ip ) );

char **temp = realloc( ip_addresses, ( ip_addresses_counter + 1 ) * sizeof(char*) );
if ( !temp ) {
// Reallocation failed, cleanup
free( adapterAddresses );
WSACleanup();
for ( int i = 0; i < ip_addresses_counter; i++ ) {
free( ip_addresses[i] );
}
free( ip_addresses );
return NULL;
}
ip_addresses = temp;
ip_addresses[ip_addresses_counter++] = strdup(ip);
}
unicast = unicast->Next;
}
adapter = adapter->Next;
}
}
free( adapterAddresses );
WSACleanup();
#else
struct ifaddrs *ifaddr, *ifa;

// Get the list of network interfaces
if ( getifaddrs( &ifaddr ) == -1 ) {
perror( "getifaddrs" );
return NULL;

Check warning on line 294 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L293-L294

Added lines #L293 - L294 were not covered by tests
}

// Loop through all the interfaces
for ( ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next ) {
if ( ifa->ifa_addr == NULL )
continue;

Check warning on line 300 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L300

Added line #L300 was not covered by tests

if ( ifa->ifa_addr->sa_family == AF_INET ) { // IPv4
struct sockaddr_in *addr = (struct sockaddr_in *)ifa->ifa_addr;
inet_ntop( AF_INET, &addr->sin_addr, ip, sizeof( ip ) );
char **temp = realloc( ip_addresses, ( ip_addresses_counter + 1 ) * sizeof(char*) );

Check warning on line 305 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L303-L305

Added lines #L303 - L305 were not covered by tests
if (!temp) {
perror("realloc failed");
freeifaddrs(ifaddr);

Check warning on line 308 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L307-L308

Added lines #L307 - L308 were not covered by tests
for ( int i = 0; i < ip_addresses_counter; i++ ) {
free( ip_addresses[i] );

Check warning on line 310 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L310

Added line #L310 was not covered by tests
}
free( ip_addresses );
return NULL;

Check warning on line 313 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L312-L313

Added lines #L312 - L313 were not covered by tests
}
ip_addresses = temp;
ip_addresses[ip_addresses_counter++] = strdup( ip );

Check warning on line 316 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L315-L316

Added lines #L315 - L316 were not covered by tests

} else if ( ifa->ifa_addr->sa_family == AF_INET6 ) { // IPv6
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)ifa->ifa_addr;
inet_ntop( AF_INET6, &addr->sin6_addr, ip, sizeof( ip ) );
char **temp = realloc( ip_addresses, ( ip_addresses_counter + 1 ) * sizeof( char* ) );

Check warning on line 321 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L319-L321

Added lines #L319 - L321 were not covered by tests
if ( !temp ) {
perror( "realloc failed" );
freeifaddrs( ifaddr );

Check warning on line 324 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L323-L324

Added lines #L323 - L324 were not covered by tests
for ( int i = 0; i < ip_addresses_counter; i++ ) {
free( ip_addresses[i] );

Check warning on line 326 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L326

Added line #L326 was not covered by tests
}
free( ip_addresses );
return NULL;

Check warning on line 329 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L328-L329

Added lines #L328 - L329 were not covered by tests
}
ip_addresses = temp;
ip_addresses[ip_addresses_counter++] = strdup( ip );

Check warning on line 332 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L331-L332

Added lines #L331 - L332 were not covered by tests
}
}

// Free the memory allocated by getifaddrs
freeifaddrs(ifaddr);

Check warning on line 337 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L337

Added line #L337 was not covered by tests
#endif

// Add a NULL terminator to mark the end of the array
ip_addresses = realloc( ip_addresses, ( ip_addresses_counter + 1 ) * sizeof( char* ) );

Check warning on line 341 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L341

Added line #L341 was not covered by tests
if ( ip_addresses ) {
ip_addresses[ip_addresses_counter] = NULL;

Check warning on line 343 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L343

Added line #L343 was not covered by tests
}

return ip_addresses;

Check warning on line 346 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L346

Added line #L346 was not covered by tests
}


struct stumpless_element* get_origin_struct_instance( void ) {
char** ip_addressess = get_ip_addresses();
int ip_addresses_counter = 0;

Check warning on line 352 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L350-L352

Added lines #L350 - L352 were not covered by tests

struct stumpless_version* version = stumpless_get_version();

Check warning on line 354 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L354

Added line #L354 was not covered by tests

struct stumpless_element *origin_element = alloc_mem( sizeof( *origin_element ) );
stumpless_set_element_name( origin_element, "origin structure" );
stumpless_add_new_param( origin_element, "software", "stumpless" );
stumpless_add_new_param( origin_element, "swVersion", stumpless_version_to_string( version ) ) ;

Check warning on line 359 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L356-L359

Added lines #L356 - L359 were not covered by tests

if ( ip_addressess[ ip_addresses_counter ] != NULL ) {
stumpless_add_new_param( origin_element, "ip", ip_addressess[ ip_addresses_counter ] );

Check warning on line 362 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L362

Added line #L362 was not covered by tests

while ( ip_addressess[ ip_addresses_counter ] != NULL ) {
free( ip_addressess[ ip_addresses_counter ] );
ip_addresses_counter++;

Check warning on line 366 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L365-L366

Added lines #L365 - L366 were not covered by tests
}
free( ip_addressess );

Check warning on line 368 in src/log.c

View check run for this annotation

Codecov / codecov/patch

src/log.c#L368

Added line #L368 was not covered by tests
}
}
Loading