aboutsummaryrefslogtreecommitdiffstats
path: root/utils/gsmtap-logsend.c
blob: 97a838b50bad0fd0add714f1c1c67562f50ff3e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Small program to read an input file / stdin and send each line via GSMTAP logging */
/* (C) 2023 by Harald Welte <laforge@osmocom.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdio.h>
#include <errno.h>
#include <sys/uio.h>
#include <getopt.h>

#include <osmocom/core/byteswap.h>
#include <osmocom/core/logging.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/gsmtap.h>
#include <osmocom/core/gsmtap_util.h>

static char *proc_name = "gsmtap-logsend";
static char *subsys_name = "unknown";
static char *dest_host = "localhost";
static int dest_port = GSMTAP_UDP_PORT;

static void help(void)
{
	printf("osmo-gsmtap-logsend Usage:\n"
	       "\t[ -r DESTADDR ] [ -p PORTNR ] [ -n PROC_NAME ] [ -s SUBSYS ] [ INFILE ]\n"
	       "\n"
	       " -a  --remote-address HOSTNAME     Destination IP destination address (default: localhost)\n"
	       " -p  --remote-port PORTNR          Destination UDP Port number (default: 4729)\n"
	       " -n  --process-name PROC_NAME      Process name to include in GSMTAP LOG header\n"
	       " -s  --subsys-name SUBSYS          Subsystem name to include in GSMTAP LOG header\n"
	       " -h  --help                        This help message\n"
	      );
}

int main(int argc, char **argv)
{
	char buf[1024];
	int gsmtap_fd;
	FILE *infile;
	char *line;
	int rc;

	while (1) {
		static const struct option long_options[] = {
			{ "remote-address", 1, 0, 'a' },
			{ "remote-port", 1, 0, 'p' },
			{ "process-name", 1, 0, 'n' },
			{ "subsys-name", 1, 0, 's' },
			{ "help", 0, 0, 'h' },
			{ 0, 0, 0, 0 }
		};
		int c, option_index;

		c = getopt_long(argc, argv, "a:p:n:s:h", long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
		case 'a':
			dest_host = optarg;
			break;
		case 'p':
			dest_port = atoi(optarg);
			break;
		case 'n':
			proc_name = optarg;
			break;
		case 's':
			subsys_name = optarg;
			break;
		case 'h':
			help();
			exit(0);
			break;
		default:
			help();
			exit(1);
		}
	}

	if (argc <= optind) {
		infile = stdin;
	} else {
		infile = fopen(argv[optind], "r");
		if (!infile) {
			fprintf(stderr, "Unable to open %s: %s\n", argv[optind], strerror(errno));
			exit(2);
		}
	}

	gsmtap_fd = gsmtap_source_init_fd(dest_host, dest_port);
	if (gsmtap_fd < 0) {
		fprintf(stderr, "Unable to create GSMTAP soicket: %s\n", strerror(errno));
		exit(2);
	}

	/* prepare all the data structures that don't change for each line */
	struct {
		struct gsmtap_hdr gsmtap;
		struct gsmtap_osmocore_log_hdr log;
	} __attribute__ ((packed)) hdr;
	struct timeval tv;

	memset(&hdr, 0, sizeof(hdr));
	hdr.gsmtap.version = GSMTAP_VERSION;
	hdr.gsmtap.hdr_len = sizeof(hdr.gsmtap)/4;
	hdr.gsmtap.type = GSMTAP_TYPE_OSMOCORE_LOG;

	OSMO_STRLCPY_ARRAY(hdr.log.proc_name, proc_name);
	OSMO_STRLCPY_ARRAY(hdr.log.subsys, subsys_name);
	hdr.log.level = LOGL_INFO;

	while ((line = fgets(buf, sizeof(buf), infile))) {
		struct iovec iov[2] = {
			{ .iov_base = &hdr, .iov_len = sizeof(hdr) },
			{ .iov_base = buf, .iov_len = strlen(line) + 1 },
		};
		osmo_gettimeofday(&tv, NULL);
		hdr.log.ts.sec = osmo_htonl(tv.tv_sec);
		hdr.log.ts.usec = osmo_htonl(tv.tv_usec);

		rc = writev(gsmtap_fd, iov, ARRAY_SIZE(iov));
		if (rc <= 0) {
			fprintf(stderr, "Short write on GSMTAP socket: %d (%s)\n", rc, strerror(errno));
			exit(1);
		}
	}
}