28 #include <netinet/in.h>
29 #include <netinet/tcp.h>
30 #include <sys/socket.h>
73 uint8_t *buf = calloc(netInfo->
maxBytes,
sizeof(uint8_t));
75 time_t lastRead = time(NULL);
77 time_t now = time(NULL);
78 if ((lastRead + netInfo->
timeout) < now) {
88 args->
tag, strerror(errno));
96 if (net_hw < netInfo->maxBytes - 1) {
98 ti = read(netInfo->
handle, &(buf[net_hw]), netInfo->
maxBytes - net_hw);
107 if (errno != EAGAIN) {
110 "[Network:%s] Unexpected error while reading from network (%s)",
111 args->
tag, strerror(errno));
118 if (net_hw < netInfo->minBytes) {
151 if (netInfo->
handle >= 0) {
158 netInfo->
addr = NULL;
180 if (netInfo->
addr == NULL || netInfo->
port <= 0) {
186 if (netInfo->
handle >= 0) {
193 struct sockaddr_in targetSA;
197 netInfo->
handle = socket(PF_INET, SOCK_STREAM, 0);
199 struct hostent *hostinfo;
200 targetSA.sin_family = AF_INET;
201 targetSA.sin_port = htons(netInfo->
port);
202 hostinfo = gethostbyname(netInfo->
addr);
204 if (hostinfo == NULL) {
205 perror(
"net_connect(hostinfo)");
208 targetSA.sin_addr = *(
struct in_addr *)hostinfo->h_addr;
211 int rs = connect(netInfo->
handle, (
struct sockaddr *)&targetSA,
sizeof(targetSA));
212 if ((rs < 0) && (errno != EINPROGRESS)) {
213 perror(
"net_connect(connect)");
218 if (fcntl(netInfo->
handle, F_SETFL, fcntl(netInfo->
handle, F_GETFL, NULL) | O_NONBLOCK) < 0) {
219 perror(
"net_connect(fcntl)");
224 if (setsockopt(netInfo->
handle, IPPROTO_TCP, TCP_NODELAY, (
void *)&enable,
226 perror(
"net_connect(NODELAY)");
271 log_error(args->
pstate,
"[Network:%s] Error pushing channel name to queue",
287 log_error(args->
pstate,
"[Network:%s] Error pushing channel map to queue",
315 "[Network:%s] Unable to allocate memory for device parameters",
330 lta->
tag, strerror(errno));
347 int sn = strtol(t->
value, NULL, 0);
350 lta->
tag, strerror(errno));
367 "[Network:%s] Unexpected Source ID number (0x%02x)- this may cause analysis problems",
378 "[Network:%s] Error parsing minimum message size: %s", lta->
tag,
386 "[Network:%s] Invalid minimum packet size specified (%d is not greater than zero)",
399 "[Network:%s] Error parsing maximum message size: %s", lta->
tag,
408 "[Network:%s] Invalid maximum packet size specified (%d is not greater than zero)",
417 "[Network:%s] Invalid maximum packet size specified (%d is smaller than specified minimum packet size)",
438 "[Network:%s] Invalid timeout value (%d is not greater than zero)",
char * config_qstrdup(const char *c)
Duplicate string, stripping optional leading/trailing quote marks.
config_kv * config_get_key(const config_section *cs, const char *kn)
Find configugration key within specific section, by name.
void signalHandlersBlock(void)
Block signals that we have handlers for.
bool shutdown
Set true to start clean shutdown.
msg_t * msg_new_string(const uint8_t source, const uint8_t type, const size_t len, const char *str)
Create a new message with a single string embedded.
void msg_destroy(msg_t *msg)
Destroy a message.
msg_t * msg_new_string_array(const uint8_t source, const uint8_t type, const strarray *array)
Create a new message containing an array of strings.
msg_t * msg_new_bytes(const uint8_t source, const uint8_t type, const size_t len, const uint8_t *bytes)
Create a new message containing raw binary data.
#define SLCHAN_TSTAMP
Source timestamp (milliseconds, arbitrary epoch)
#define SLCHAN_RAW
Raw device data (Not mandatory)
#define SLCHAN_MAP
Channel name map (excludes log channels)
#define SLCHAN_NAME
Name of source device.
bool net_parseConfig(log_thread_args_t *lta, config_section *s)
Take a configuration section and parse parameters.
void * net_logging(void *ptargs)
Network source main logging loop.
device_callbacks net_getCallbacks()
Fill out device callback functions for logging.
void * net_channels(void *ptargs)
Channel map.
void * net_shutdown(void *ptargs)
Network source shutdown.
net_params net_getParams()
Fill out default MP source parameters.
void * net_setup(void *ptargs)
Device thread setup.
bool net_connect(void *ptargs)
Network connection helper function.
#define SLSOURCE_EXT
External data sources recorded but not interpreted by the logger.
atomic_bool shutdownFlag
Trigger clean software shutdown.
void log_info(const program_state *s, const int level, const char *format,...)
Output formatted information message at a given level.
void log_warning(const program_state *s, const char *format,...)
Output formatted warning message.
void log_error(const program_state *s, const char *format,...)
Output formatted error message.
bool queue_push(msgqueue *queue, msg_t *msg)
Add a message to the tail of the queue.
void sa_destroy(strarray *sa)
Destroy array and contents.
strarray * sa_new(int entries)
Allocate storage for a new array.
bool sa_create_entry(strarray *array, const int index, const size_t len, const char *src)
Create an string in a given position from a character array and length.
Represent a key=value pair.
char * value
Configuration item value.
Configuration file section.
Device specific function information.
device_fn startup
Called serially at startup, opens devices etc.
Logging thread information.
msgqueue * logQ
Main message queue. Pushed to by threads, consumed by main()
char * tag
Tag/source name for messages etc.
void * dParams
Device/Thread specific data.
program_state * pstate
Current program state, used for logging.
int returnCode
Thread return code (output)
Network device specific parameters.
uint8_t sourceNum
Source ID for messages.
int minBytes
Minimum number of bytes to group into a message.
int handle
Handle for currently opened device.
char * sourceName
User defined name for this source.
int maxBytes
Maximum number of bytes to group into a message.
int port
Target port number.
int timeout
Reconnect if no data received after this interval [s].