Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 1 | /* GSMTAP network logging support code */ |
| 2 | |
| 3 | /* (C) 2016 by Harald Welte <laforge@gnumonks.org> |
| 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 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | /*! \addtogroup logging |
| 23 | * @{ |
| 24 | */ |
| 25 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 26 | /*! \file logging_gsmtap.c |
| 27 | * \brief libosmocore log output encapsulated in GSMTAP |
| 28 | * |
| 29 | * Encapsulating the log output inside GSMTAP frames allows us to |
| 30 | * observer protocol traces (of Um, Abis, A or any other interface in |
| 31 | * the Osmocom world) with synchronous interspersed log messages. |
| 32 | */ |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 33 | |
| 34 | #include "../config.h" |
| 35 | |
| 36 | #include <stdarg.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <stdio.h> |
| 39 | #include <string.h> |
| 40 | #include <stdbool.h> |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 41 | |
| 42 | #ifdef HAVE_STRINGS_H |
| 43 | #include <strings.h> |
| 44 | #endif |
| 45 | |
| 46 | #include <osmocom/core/talloc.h> |
| 47 | #include <osmocom/core/utils.h> |
| 48 | #include <osmocom/core/gsmtap.h> |
| 49 | #include <osmocom/core/gsmtap_util.h> |
| 50 | #include <osmocom/core/logging.h> |
| 51 | #include <osmocom/core/timer.h> |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 52 | #include <osmocom/core/byteswap.h> |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 53 | |
| 54 | #define GSMTAP_LOG_MAX_SIZE 4096 |
| 55 | |
| 56 | static void _gsmtap_raw_output(struct log_target *target, int subsys, |
| 57 | unsigned int level, const char *file, |
| 58 | int line, int cont, const char *format, |
| 59 | va_list ap) |
| 60 | { |
| 61 | struct msgb *msg; |
| 62 | struct gsmtap_hdr *gh; |
| 63 | struct gsmtap_osmocore_log_hdr *golh; |
| 64 | const char *subsys_name = log_category_name(subsys); |
| 65 | struct timeval tv; |
| 66 | int rc; |
| 67 | |
| 68 | /* get timestamp ASAP */ |
| 69 | osmo_gettimeofday(&tv, NULL); |
| 70 | |
| 71 | msg = msgb_alloc(sizeof(*gh)+sizeof(*golh)+GSMTAP_LOG_MAX_SIZE, |
| 72 | "GSMTAP logging"); |
| 73 | |
| 74 | /* GSMTAP header */ |
| 75 | gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh)); |
| 76 | memset(gh, 0, sizeof(*gh)); |
| 77 | gh->version = GSMTAP_VERSION; |
| 78 | gh->hdr_len = sizeof(*gh)/4; |
| 79 | gh->type = GSMTAP_TYPE_OSMOCORE_LOG; |
| 80 | |
| 81 | /* Logging header */ |
| 82 | golh = (struct gsmtap_osmocore_log_hdr *) msgb_put(msg, sizeof(*golh)); |
| 83 | osmo_strlcpy(golh->proc_name, target->tgt_gsmtap.ident, |
| 84 | sizeof(golh->proc_name)); |
| 85 | if (subsys_name) |
| 86 | osmo_strlcpy(golh->subsys, subsys_name+1, sizeof(golh->subsys)); |
| 87 | else |
| 88 | golh->subsys[0] = '\0'; |
| 89 | osmo_strlcpy(golh->src_file.name, file, sizeof(golh->src_file.name)); |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 90 | golh->src_file.line_nr = osmo_htonl(line); |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 91 | golh->level = level; |
| 92 | /* we always store the timestamp in the message, irrespective |
| 93 | * of hat prrint_[ext_]timestamp say */ |
Harald Welte | 95871da | 2017-05-15 12:11:36 +0200 | [diff] [blame] | 94 | golh->ts.sec = osmo_htonl(tv.tv_sec); |
| 95 | golh->ts.usec = osmo_htonl(tv.tv_usec); |
Harald Welte | aa00f99 | 2016-12-02 15:30:02 +0100 | [diff] [blame] | 96 | |
| 97 | rc = vsnprintf((char *) msg->tail, msgb_tailroom(msg), format, ap); |
| 98 | if (rc < 0) |
| 99 | return; |
| 100 | msgb_put(msg, rc); |
| 101 | |
| 102 | gsmtap_sendmsg(target->tgt_gsmtap.gsmtap_inst, msg); |
| 103 | } |
| 104 | |
| 105 | /*! \brief Create a new logging target for GSMTAP logging |
| 106 | * \param[in] ident string identifier |
| 107 | * \returns Log target in case of success, NULL in case of error |
| 108 | */ |
| 109 | struct log_target *log_target_create_gsmtap(const char *host, uint16_t port, |
| 110 | const char *ident, |
| 111 | bool ofd_wq_mode, |
| 112 | bool add_sink) |
| 113 | { |
| 114 | struct log_target *target; |
| 115 | struct gsmtap_inst *gti; |
| 116 | |
| 117 | target = log_target_create(); |
| 118 | if (!target) |
| 119 | return NULL; |
| 120 | |
| 121 | gti = gsmtap_source_init(host, port, ofd_wq_mode); |
| 122 | if (!gti) { |
| 123 | log_target_destroy(target); |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | if (add_sink) |
| 128 | gsmtap_source_add_sink(gti); |
| 129 | |
| 130 | target->tgt_gsmtap.gsmtap_inst = gti; |
| 131 | target->tgt_gsmtap.ident = talloc_strdup(target, ident); |
| 132 | target->tgt_gsmtap.hostname = talloc_strdup(target, host); |
| 133 | |
| 134 | target->type = LOG_TGT_TYPE_GSMTAP; |
| 135 | target->raw_output = _gsmtap_raw_output; |
| 136 | |
| 137 | return target; |
| 138 | } |
| 139 | |
| 140 | /* @} */ |