Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 1 | /* GSMTAP support code in libmsomcore */ |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 2 | /* |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 3 | * (C) 2010-2011 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "../config.h" |
| 24 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 25 | #include <osmocom/core/gsmtap_util.h> |
| 26 | #include <osmocom/core/logging.h> |
| 27 | #include <osmocom/core/gsmtap.h> |
| 28 | #include <osmocom/core/msgb.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 29 | #include <osmocom/core/talloc.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 30 | #include <osmocom/core/select.h> |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 31 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 32 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 33 | #include <osmocom/gsm/rsl.h> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 34 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 35 | #include <sys/types.h> |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 36 | |
| 37 | #include <arpa/inet.h> |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 38 | |
| 39 | #include <stdio.h> |
| 40 | #include <unistd.h> |
| 41 | #include <stdint.h> |
| 42 | #include <string.h> |
| 43 | #include <errno.h> |
| 44 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 45 | /*! \addtogroup gsmtap |
| 46 | * @{ |
| 47 | */ |
| 48 | /*! \file gsmtap_util.c */ |
| 49 | |
| 50 | |
| 51 | /*! \brief convert RSL channel number to GSMTAP channel type |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 52 | * \param[in] rsl_chantype RSL channel type |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 53 | * \param[in] link_id RSL link identifier |
| 54 | * \returns GSMTAP channel type |
| 55 | */ |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 56 | uint8_t chantype_rsl2gsmtap(uint8_t rsl_chantype, uint8_t link_id) |
| 57 | { |
| 58 | uint8_t ret = GSMTAP_CHANNEL_UNKNOWN; |
| 59 | |
| 60 | switch (rsl_chantype) { |
| 61 | case RSL_CHAN_Bm_ACCHs: |
| 62 | ret = GSMTAP_CHANNEL_TCH_F; |
| 63 | break; |
| 64 | case RSL_CHAN_Lm_ACCHs: |
| 65 | ret = GSMTAP_CHANNEL_TCH_H; |
| 66 | break; |
| 67 | case RSL_CHAN_SDCCH4_ACCH: |
| 68 | ret = GSMTAP_CHANNEL_SDCCH4; |
| 69 | break; |
| 70 | case RSL_CHAN_SDCCH8_ACCH: |
| 71 | ret = GSMTAP_CHANNEL_SDCCH8; |
| 72 | break; |
| 73 | case RSL_CHAN_BCCH: |
| 74 | ret = GSMTAP_CHANNEL_BCCH; |
| 75 | break; |
| 76 | case RSL_CHAN_RACH: |
| 77 | ret = GSMTAP_CHANNEL_RACH; |
| 78 | break; |
| 79 | case RSL_CHAN_PCH_AGCH: |
| 80 | /* it could also be AGCH... */ |
| 81 | ret = GSMTAP_CHANNEL_PCH; |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | if (link_id & 0x40) |
| 86 | ret |= GSMTAP_CHANNEL_ACCH; |
| 87 | |
| 88 | return ret; |
| 89 | } |
| 90 | |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 91 | /*! \brief create an arbitrary type GSMTAP message |
| 92 | * \param[in] type The GSMTAP_TYPE_xxx constant of the message to create |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 93 | * \param[in] arfcn GSM ARFCN (Channel Number) |
| 94 | * \param[in] ts GSM time slot |
| 95 | * \param[in] chan_type Channel Type |
| 96 | * \param[in] ss Sub-slot |
| 97 | * \param[in] fn GSM Frame Number |
| 98 | * \param[in] signal_dbm Signal Strength (dBm) |
| 99 | * \param[in] snr Signal/Noise Ratio (SNR) |
| 100 | * \param[in] data Pointer to data buffer |
| 101 | * \param[in] len Length of \ref data |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 102 | * \return dynamically allocated message buffer containing data |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 103 | * |
| 104 | * This function will allocate a new msgb and fill it with a GSMTAP |
| 105 | * header containing the information |
| 106 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 107 | struct msgb *gsmtap_makemsg_ex(uint8_t type, uint16_t arfcn, uint8_t ts, uint8_t chan_type, |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 108 | uint8_t ss, uint32_t fn, int8_t signal_dbm, |
| 109 | uint8_t snr, const uint8_t *data, unsigned int len) |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 110 | { |
| 111 | struct msgb *msg; |
| 112 | struct gsmtap_hdr *gh; |
| 113 | uint8_t *dst; |
| 114 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 115 | msg = msgb_alloc(sizeof(*gh) + len, "gsmtap_tx"); |
| 116 | if (!msg) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 117 | return NULL; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 118 | |
| 119 | gh = (struct gsmtap_hdr *) msgb_put(msg, sizeof(*gh)); |
| 120 | |
| 121 | gh->version = GSMTAP_VERSION; |
| 122 | gh->hdr_len = sizeof(*gh)/4; |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 123 | gh->type = type; |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 124 | gh->timeslot = ts; |
| 125 | gh->sub_slot = ss; |
| 126 | gh->arfcn = htons(arfcn); |
| 127 | gh->snr_db = snr; |
| 128 | gh->signal_dbm = signal_dbm; |
| 129 | gh->frame_number = htonl(fn); |
| 130 | gh->sub_type = chan_type; |
| 131 | gh->antenna_nr = 0; |
| 132 | |
| 133 | dst = msgb_put(msg, len); |
| 134 | memcpy(dst, data, len); |
| 135 | |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 136 | return msg; |
| 137 | } |
| 138 | |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 139 | /*! \brief create L1/L2 data and put it into GSMTAP |
| 140 | * \param[in] arfcn GSM ARFCN (Channel Number) |
| 141 | * \param[in] ts GSM time slot |
| 142 | * \param[in] chan_type Channel Type |
| 143 | * \param[in] ss Sub-slot |
| 144 | * \param[in] fn GSM Frame Number |
| 145 | * \param[in] signal_dbm Signal Strength (dBm) |
| 146 | * \param[in] snr Signal/Noise Ratio (SNR) |
| 147 | * \param[in] data Pointer to data buffer |
| 148 | * \param[in] len Length of \ref data |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 149 | * \return message buffer or NULL in case of error |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 150 | * |
| 151 | * This function will allocate a new msgb and fill it with a GSMTAP |
| 152 | * header containing the information |
| 153 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 154 | struct msgb *gsmtap_makemsg(uint16_t arfcn, uint8_t ts, uint8_t chan_type, |
| 155 | uint8_t ss, uint32_t fn, int8_t signal_dbm, |
| 156 | uint8_t snr, const uint8_t *data, unsigned int len) |
| 157 | { |
| 158 | return gsmtap_makemsg_ex(GSMTAP_TYPE_UM, arfcn, ts, chan_type, |
| 159 | ss, fn, signal_dbm, snr, data, len); |
| 160 | } |
| 161 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 162 | #ifdef HAVE_SYS_SOCKET_H |
| 163 | |
| 164 | #include <sys/socket.h> |
| 165 | #include <netinet/in.h> |
| 166 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 167 | /*! \brief Create a new (sending) GSMTAP source socket |
| 168 | * \param[in] host host name or IP address in string format |
| 169 | * \param[in] port UDP port number in host byte order |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 170 | * \return file descriptor of the new socket |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 171 | * |
| 172 | * Opens a GSMTAP source (sending) socket, conncet it to host/port and |
| 173 | * return resulting fd. If \a host is NULL, the destination address |
| 174 | * will be localhost. If \a port is 0, the default \ref |
| 175 | * GSMTAP_UDP_PORT will be used. |
| 176 | * */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 177 | int gsmtap_source_init_fd(const char *host, uint16_t port) |
| 178 | { |
| 179 | if (port == 0) |
| 180 | port = GSMTAP_UDP_PORT; |
| 181 | if (host == NULL) |
| 182 | host = "localhost"; |
| 183 | |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 184 | return osmo_sock_init(AF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, host, port, |
| 185 | OSMO_SOCK_F_CONNECT); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 186 | } |
| 187 | |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 188 | /*! \brief Add a local sink to an existing GSMTAP source and return fd |
| 189 | * \param[in] gsmtap_fd file descriptor of the gsmtap socket |
| 190 | * \returns file descriptor of locally bound receive socket |
| 191 | * |
| 192 | * In case the GSMTAP socket is connected to a local destination |
| 193 | * IP/port, this function creates a corresponding receiving socket |
| 194 | * bound to that destination IP + port. |
| 195 | * |
| 196 | * In case the gsmtap socket is not connected to a local IP/port, or |
| 197 | * creation of the receiving socket fails, a negative error code is |
| 198 | * returned. |
| 199 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 200 | int gsmtap_source_add_sink_fd(int gsmtap_fd) |
| 201 | { |
| 202 | struct sockaddr_storage ss; |
| 203 | socklen_t ss_len = sizeof(ss); |
| 204 | int rc; |
| 205 | |
| 206 | rc = getpeername(gsmtap_fd, (struct sockaddr *)&ss, &ss_len); |
| 207 | if (rc < 0) |
| 208 | return rc; |
| 209 | |
| 210 | if (osmo_sockaddr_is_local((struct sockaddr *)&ss, ss_len) == 1) { |
Pablo Neira Ayuso | 0849c9a | 2011-06-09 15:04:30 +0200 | [diff] [blame] | 211 | rc = osmo_sock_init_sa((struct sockaddr *)&ss, SOCK_DGRAM, |
| 212 | IPPROTO_UDP, OSMO_SOCK_F_BIND); |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 213 | if (rc >= 0) |
| 214 | return rc; |
| 215 | } |
| 216 | |
| 217 | return -ENODEV; |
| 218 | } |
| 219 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 220 | /*! \brief Send a \ref msgb through a GSMTAP source |
| 221 | * \param[in] gti GSMTAP instance |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 222 | * \param[in] msg message buffer |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 223 | * \return 0 in case of success; negative in case of error |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 224 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 225 | int gsmtap_sendmsg(struct gsmtap_inst *gti, struct msgb *msg) |
| 226 | { |
Harald Welte | 13692a6 | 2011-05-22 20:06:11 +0200 | [diff] [blame] | 227 | if (!gti) |
| 228 | return -ENODEV; |
| 229 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 230 | if (gti->ofd_wq_mode) |
| 231 | return osmo_wqueue_enqueue(>i->wq, msg); |
| 232 | else { |
| 233 | /* try immediate send and return error if any */ |
| 234 | int rc; |
| 235 | |
| 236 | rc = write(gsmtap_inst_fd(gti), msg->data, msg->len); |
| 237 | if (rc <= 0) { |
| 238 | return rc; |
| 239 | } else if (rc >= msg->len) { |
| 240 | msgb_free(msg); |
| 241 | return 0; |
| 242 | } else { |
| 243 | /* short write */ |
| 244 | return -EIO; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 249 | /*! \brief send an arbitrary type through GSMTAP. |
| 250 | * See \ref gsmtap_makemsg_ex for arguments |
| 251 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 252 | int gsmtap_send_ex(struct gsmtap_inst *gti, uint8_t type, uint16_t arfcn, uint8_t ts, |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 253 | uint8_t chan_type, uint8_t ss, uint32_t fn, |
| 254 | int8_t signal_dbm, uint8_t snr, const uint8_t *data, |
| 255 | unsigned int len) |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 256 | { |
| 257 | struct msgb *msg; |
| 258 | |
Harald Welte | 13692a6 | 2011-05-22 20:06:11 +0200 | [diff] [blame] | 259 | if (!gti) |
| 260 | return -ENODEV; |
| 261 | |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 262 | msg = gsmtap_makemsg_ex(type, arfcn, ts, chan_type, ss, fn, signal_dbm, |
Harald Welte | e34a940 | 2010-06-29 22:31:21 +0200 | [diff] [blame] | 263 | snr, data, len); |
| 264 | if (!msg) |
| 265 | return -ENOMEM; |
| 266 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 267 | return gsmtap_sendmsg(gti, msg); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 268 | } |
| 269 | |
Sylvain Munaut | abf66e7 | 2011-09-26 13:23:19 +0200 | [diff] [blame] | 270 | /*! \brief send a message from L1/L2 through GSMTAP. |
| 271 | * See \ref gsmtap_makemsg for arguments |
| 272 | */ |
Sylvain Munaut | 15ae715 | 2011-09-26 13:05:07 +0200 | [diff] [blame] | 273 | int gsmtap_send(struct gsmtap_inst *gti, uint16_t arfcn, uint8_t ts, |
| 274 | uint8_t chan_type, uint8_t ss, uint32_t fn, |
| 275 | int8_t signal_dbm, uint8_t snr, const uint8_t *data, |
| 276 | unsigned int len) |
| 277 | { |
| 278 | return gsmtap_send_ex(gti, GSMTAP_TYPE_UM, arfcn, ts, chan_type, ss, fn, |
| 279 | signal_dbm, snr, data, len); |
| 280 | } |
| 281 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 282 | /* Callback from select layer if we can write to the socket */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 283 | static int gsmtap_wq_w_cb(struct osmo_fd *ofd, struct msgb *msg) |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 284 | { |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 285 | int rc; |
| 286 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 287 | rc = write(ofd->fd, msg->data, msg->len); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 288 | if (rc < 0) { |
| 289 | perror("writing msgb to gsmtap fd"); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 290 | return rc; |
| 291 | } |
| 292 | if (rc != msg->len) { |
| 293 | perror("short write to gsmtap fd"); |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 294 | return -EIO; |
| 295 | } |
| 296 | |
Harald Welte | e779c36 | 2010-06-29 20:51:13 +0200 | [diff] [blame] | 297 | return 0; |
| 298 | } |
| 299 | |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 300 | /* Callback from select layer if we can read from the sink socket */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 301 | static int gsmtap_sink_fd_cb(struct osmo_fd *fd, unsigned int flags) |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 302 | { |
| 303 | int rc; |
| 304 | uint8_t buf[4096]; |
| 305 | |
| 306 | if (!(flags & BSC_FD_READ)) |
| 307 | return 0; |
| 308 | |
| 309 | rc = read(fd->fd, buf, sizeof(buf)); |
| 310 | if (rc < 0) { |
| 311 | perror("reading from gsmtap sink fd"); |
| 312 | return rc; |
| 313 | } |
| 314 | /* simply discard any data arriving on the socket */ |
| 315 | |
| 316 | return 0; |
| 317 | } |
| 318 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 319 | /*! \brief Add a local sink to an existing GSMTAP source and return fd |
| 320 | * \param[in] gsmtap_fd file descriptor of the gsmtap socket |
| 321 | * \returns file descriptor of locally bound receive socket |
| 322 | * |
| 323 | * In case the GSMTAP socket is connected to a local destination |
| 324 | * IP/port, this function creates a corresponding receiving socket |
| 325 | * bound to that destination IP + port. |
| 326 | * |
| 327 | * In case the gsmtap socket is not connected to a local IP/port, or |
| 328 | * creation of the receiving socket fails, a negative error code is |
| 329 | * returned. |
| 330 | * |
| 331 | * The file descriptor of the receiving socket is automatically added |
| 332 | * to the libosmocore select() handling. |
| 333 | */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 334 | int gsmtap_source_add_sink(struct gsmtap_inst *gti) |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 335 | { |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 336 | int fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 337 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 338 | fd = gsmtap_source_add_sink_fd(gsmtap_inst_fd(gti)); |
| 339 | if (fd < 0) |
| 340 | return fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 341 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 342 | if (gti->ofd_wq_mode) { |
| 343 | struct osmo_fd *sink_ofd; |
| 344 | |
| 345 | sink_ofd = >i->sink_ofd; |
| 346 | sink_ofd->fd = fd; |
| 347 | sink_ofd->when = BSC_FD_READ; |
| 348 | sink_ofd->cb = gsmtap_sink_fd_cb; |
| 349 | |
| 350 | osmo_fd_register(sink_ofd); |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 353 | return fd; |
| 354 | } |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 355 | |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 356 | |
| 357 | /*! \brief Open GSMTAP source socket, connect and register osmo_fd |
| 358 | * \param[in] host host name or IP address in string format |
| 359 | * \param[in] port UDP port number in host byte order |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 360 | * \param[in] ofd_wq_mode Register \ref osmo_wqueue (1) or not (0) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 361 | * \return callee-allocated \ref gsmtap_inst |
Harald Welte | 47379ca | 2011-08-17 16:35:24 +0200 | [diff] [blame] | 362 | * |
| 363 | * Open GSMTAP source (sending) socket, connect it to host/port, |
| 364 | * allocate 'struct gsmtap_inst' and optionally osmo_fd/osmo_wqueue |
| 365 | * registration. This means it is like \ref gsmtap_init2 but integrated |
| 366 | * with libosmocore \ref select */ |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 367 | struct gsmtap_inst *gsmtap_source_init(const char *host, uint16_t port, |
| 368 | int ofd_wq_mode) |
| 369 | { |
| 370 | struct gsmtap_inst *gti; |
| 371 | int fd; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 372 | |
Harald Welte | 33cb71a | 2011-05-21 18:54:32 +0200 | [diff] [blame] | 373 | fd = gsmtap_source_init_fd(host, port); |
| 374 | if (fd < 0) |
| 375 | return NULL; |
| 376 | |
| 377 | gti = talloc_zero(NULL, struct gsmtap_inst); |
| 378 | gti->ofd_wq_mode = ofd_wq_mode; |
| 379 | gti->wq.bfd.fd = fd; |
| 380 | gti->sink_ofd.fd = -1; |
| 381 | |
| 382 | if (ofd_wq_mode) { |
| 383 | osmo_wqueue_init(>i->wq, 64); |
| 384 | gti->wq.write_cb = &gsmtap_wq_w_cb; |
| 385 | |
| 386 | osmo_fd_register(>i->wq.bfd); |
| 387 | } |
| 388 | |
| 389 | return gti; |
Harald Welte | d58ba46 | 2011-04-27 10:57:49 +0200 | [diff] [blame] | 390 | } |
| 391 | |
Harald Welte | e476442 | 2011-05-22 12:25:57 +0200 | [diff] [blame] | 392 | #endif /* HAVE_SYS_SOCKET_H */ |
Harald Welte | de6e498 | 2012-12-06 21:25:27 +0100 | [diff] [blame] | 393 | |
| 394 | /*! @} */ |