blob: 97a838b50bad0fd0add714f1c1c67562f50ff3e4 [file] [log] [blame]
Harald Welteb394d6f2023-03-14 20:33:04 +01001/* Small program to read an input file / stdin and send each line via GSMTAP logging */
2/* (C) 2023 by Harald Welte <laforge@osmocom.org>
3 *
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <stdio.h>
18#include <errno.h>
19#include <sys/uio.h>
20#include <getopt.h>
21
22#include <osmocom/core/byteswap.h>
23#include <osmocom/core/logging.h>
24#include <osmocom/core/timer.h>
25#include <osmocom/core/gsmtap.h>
26#include <osmocom/core/gsmtap_util.h>
27
28static char *proc_name = "gsmtap-logsend";
29static char *subsys_name = "unknown";
30static char *dest_host = "localhost";
31static int dest_port = GSMTAP_UDP_PORT;
32
33static void help(void)
34{
35 printf("osmo-gsmtap-logsend Usage:\n"
36 "\t[ -r DESTADDR ] [ -p PORTNR ] [ -n PROC_NAME ] [ -s SUBSYS ] [ INFILE ]\n"
37 "\n"
38 " -a --remote-address HOSTNAME Destination IP destination address (default: localhost)\n"
39 " -p --remote-port PORTNR Destination UDP Port number (default: 4729)\n"
40 " -n --process-name PROC_NAME Process name to include in GSMTAP LOG header\n"
41 " -s --subsys-name SUBSYS Subsystem name to include in GSMTAP LOG header\n"
42 " -h --help This help message\n"
43 );
44}
45
46int main(int argc, char **argv)
47{
48 char buf[1024];
49 int gsmtap_fd;
50 FILE *infile;
51 char *line;
52 int rc;
53
54 while (1) {
55 static const struct option long_options[] = {
56 { "remote-address", 1, 0, 'a' },
57 { "remote-port", 1, 0, 'p' },
58 { "process-name", 1, 0, 'n' },
59 { "subsys-name", 1, 0, 's' },
60 { "help", 0, 0, 'h' },
61 { 0, 0, 0, 0 }
62 };
63 int c, option_index;
64
65 c = getopt_long(argc, argv, "a:p:n:s:h", long_options, &option_index);
66 if (c == -1)
67 break;
68
69 switch (c) {
70 case 'a':
71 dest_host = optarg;
72 break;
73 case 'p':
74 dest_port = atoi(optarg);
75 break;
76 case 'n':
77 proc_name = optarg;
78 break;
79 case 's':
80 subsys_name = optarg;
81 break;
82 case 'h':
83 help();
84 exit(0);
85 break;
86 default:
87 help();
88 exit(1);
89 }
90 }
91
92 if (argc <= optind) {
93 infile = stdin;
94 } else {
95 infile = fopen(argv[optind], "r");
96 if (!infile) {
97 fprintf(stderr, "Unable to open %s: %s\n", argv[optind], strerror(errno));
98 exit(2);
99 }
100 }
101
102 gsmtap_fd = gsmtap_source_init_fd(dest_host, dest_port);
103 if (gsmtap_fd < 0) {
104 fprintf(stderr, "Unable to create GSMTAP soicket: %s\n", strerror(errno));
105 exit(2);
106 }
107
108 /* prepare all the data structures that don't change for each line */
109 struct {
110 struct gsmtap_hdr gsmtap;
111 struct gsmtap_osmocore_log_hdr log;
112 } __attribute__ ((packed)) hdr;
113 struct timeval tv;
114
115 memset(&hdr, 0, sizeof(hdr));
116 hdr.gsmtap.version = GSMTAP_VERSION;
117 hdr.gsmtap.hdr_len = sizeof(hdr.gsmtap)/4;
118 hdr.gsmtap.type = GSMTAP_TYPE_OSMOCORE_LOG;
119
120 OSMO_STRLCPY_ARRAY(hdr.log.proc_name, proc_name);
121 OSMO_STRLCPY_ARRAY(hdr.log.subsys, subsys_name);
122 hdr.log.level = LOGL_INFO;
123
124 while ((line = fgets(buf, sizeof(buf), infile))) {
125 struct iovec iov[2] = {
126 { .iov_base = &hdr, .iov_len = sizeof(hdr) },
127 { .iov_base = buf, .iov_len = strlen(line) + 1 },
128 };
129 osmo_gettimeofday(&tv, NULL);
130 hdr.log.ts.sec = osmo_htonl(tv.tv_sec);
131 hdr.log.ts.usec = osmo_htonl(tv.tv_usec);
132
133 rc = writev(gsmtap_fd, iov, ARRAY_SIZE(iov));
134 if (rc <= 0) {
135 fprintf(stderr, "Short write on GSMTAP socket: %d (%s)\n", rc, strerror(errno));
136 exit(1);
137 }
138 }
139}