blob: c25920f8d50182b0a4dbf97ad02af5f2226c8c40 [file] [log] [blame]
Harald Welte095ac6c2016-03-19 13:39:33 +01001/* simtrace - main program for the host PC
2 *
3 * (C) 2010-2016 by Harald Welte <hwelte@hmw-consulting.de>
4 *
Kévin Redonefbcf382018-07-07 17:35:15 +02005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
Harald Welte095ac6c2016-03-19 13:39:33 +01009 *
Kévin Redonefbcf382018-07-07 17:35:15 +020010 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Harald Welte095ac6c2016-03-19 13:39:33 +010014 *
Kévin Redonefbcf382018-07-07 17:35:15 +020015 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Harald Welte095ac6c2016-03-19 13:39:33 +010018 */
Harald Welte095ac6c2016-03-19 13:39:33 +010019#include <errno.h>
20#include <unistd.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <stdint.h>
25#include <time.h>
26#define _GNU_SOURCE
27#include <getopt.h>
28#include <poll.h>
29
30#include <sys/time.h>
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35
36#include <libusb.h>
37
38#include "simtrace.h"
Harald Welte25a9a802017-05-08 13:30:09 +020039#include "simtrace_prot.h"
Harald Welte095ac6c2016-03-19 13:39:33 +010040#include "apdu_dispatch.h"
Harald Welte236caf62016-03-19 21:28:09 +010041#include "simtrace2-discovery.h"
Harald Welte095ac6c2016-03-19 13:39:33 +010042
43#include <osmocom/core/utils.h>
44#include <osmocom/core/socket.h>
45#include <osmocom/core/select.h>
46
47struct libusb_device_handle *g_devh;
48static struct sockaddr_in g_sa_remote;
49static struct osmo_fd g_udp_ofd;
50
51static void print_welcome(void)
52{
53 printf("usb2udp - UDP/IP forwarding of SIMtrace card emulation\n"
54 "(C) 2016 by Harald Welte <laforge@gnumonks.org>\n\n");
55}
56
57static void print_help(void)
58{
59 printf( "\t-h\t--help\n"
Harald Welte236caf62016-03-19 21:28:09 +010060 "\t-i\t--interface <0-255>\n"
Harald Welte095ac6c2016-03-19 13:39:33 +010061 "\n"
62 );
63}
64
65struct ep_buf {
66 uint8_t ep;
67 uint8_t buf[1024];
68 struct libusb_transfer *xfer;
69};
70static struct ep_buf g_buf_in;
71static struct ep_buf g_buf_out;
72
73static void usb_in_xfer_cb(struct libusb_transfer *xfer)
74{
75 int rc;
76
77 printf("xfer_cb(ep=%02x): status=%d, flags=0x%x, type=%u, len=%u, act_len=%u\n",
78 xfer->endpoint, xfer->status, xfer->flags, xfer->type, xfer->length, xfer->actual_length);
79 switch (xfer->status) {
80 case LIBUSB_TRANSFER_COMPLETED:
Harald Welte236caf62016-03-19 21:28:09 +010081 if (xfer->endpoint == g_buf_in.ep) {
Harald Welte095ac6c2016-03-19 13:39:33 +010082 /* process the data */
83 printf("read %d bytes from SIMTRACE, forwarding to UDP\n", xfer->actual_length);
84 rc = sendto(g_udp_ofd.fd, xfer->buffer, xfer->actual_length, 0, (struct sockaddr *)&g_sa_remote, sizeof(g_sa_remote));
85 if (rc <= 0) {
86 fprintf(stderr, "error writing to UDP\n");
87 }
88 /* and re-submit the URB */
89 libusb_submit_transfer(xfer);
Harald Welte236caf62016-03-19 21:28:09 +010090 } else if (xfer->endpoint == g_buf_out.ep) {
Harald Welte095ac6c2016-03-19 13:39:33 +010091 /* re-enable reading from the UDP side */
92 g_udp_ofd.when |= BSC_FD_READ;
Harald Welte095ac6c2016-03-19 13:39:33 +010093 }
94 break;
95 default:
96 fprintf(stderr, "xfer_cb(ERROR '%s')\n", osmo_hexdump_nospc(xfer->buffer, xfer->actual_length));
97 break;
98 }
99}
100
Harald Welte236caf62016-03-19 21:28:09 +0100101static void init_ep_buf(struct ep_buf *epb)
Harald Welte095ac6c2016-03-19 13:39:33 +0100102{
Harald Welte095ac6c2016-03-19 13:39:33 +0100103 if (!epb->xfer)
104 epb->xfer = libusb_alloc_transfer(0);
105
106 epb->xfer->flags = 0;
107
108 libusb_fill_bulk_transfer(epb->xfer, g_devh, epb->ep, epb->buf, sizeof(epb->buf), usb_in_xfer_cb, NULL, 0);
109}
110
111/***********************************************************************
112 * libosmocore main loop integration of libusb async I/O
113 ***********************************************************************/
114
115static int g_libusb_pending = 0;
116
117static int ofd_libusb_cb(struct osmo_fd *ofd, unsigned int what)
118{
119 /* FIXME */
120 g_libusb_pending = 1;
121
122 return 0;
123}
124
125/* call-back when libusb adds a FD */
126static void libusb_fd_added_cb(int fd, short events, void *user_data)
127{
128 struct osmo_fd *ofd = talloc_zero(NULL, struct osmo_fd);
129
130 printf("%s(%u, %x)\n", __func__, fd, events);
131
132 ofd->fd = fd;
133 ofd->cb = &ofd_libusb_cb;
134 if (events & POLLIN)
135 ofd->when |= BSC_FD_READ;
136 if (events & POLLOUT)
137 ofd->when |= BSC_FD_WRITE;
138
139 osmo_fd_register(ofd);
140}
141
142/* call-back when libusb removes a FD */
143static void libusb_fd_removed_cb(int fd, void *user_data)
144{
Harald Welte095ac6c2016-03-19 13:39:33 +0100145
146 printf("%s(%u)\n", __func__, fd);
147#if 0
Harald Welte9f38ddd2017-05-10 22:49:02 +0200148 struct osmo_fd *ofd;
Harald Welte095ac6c2016-03-19 13:39:33 +0100149 /* FIXME: This needs new export in libosmocore! */
150 ofd = osmo_fd_get_by_fd(fd);
151
152 if (ofd) {
153 osmo_fd_unregister(ofd);
154 talloc_free(ofd);
155 }
156#endif
157}
158
159/* call-back when the UDP socket is readable */
160static int ofd_udp_cb(struct osmo_fd *ofd, unsigned int what)
161{
162 int rc;
Harald Welte9f38ddd2017-05-10 22:49:02 +0200163 socklen_t addrlen = sizeof(g_sa_remote);
Harald Welte095ac6c2016-03-19 13:39:33 +0100164
165 rc = recvfrom(ofd->fd, g_buf_out.buf, sizeof(g_buf_out.buf), 0,
166 (struct sockaddr *)&g_sa_remote, &addrlen);
167 if (rc <= 0) {
168 fprintf(stderr, "error reading from UDP\n");
169 return 0;
170 }
171 printf("read %d bytes from UDP, forwarding to SIMTRACE\n", rc);
172 g_buf_out.xfer->length = rc;
173
174 /* disable further READ interest for the UDP socket */
175 ofd->when &= ~BSC_FD_READ;
176
177 /* submit the URB on the OUT end point */
178 libusb_submit_transfer(g_buf_out.xfer);
179
180 return 0;
181}
182
183static void run_mainloop(void)
184{
185 int rc;
186
187 printf("Entering main loop\n");
188
189 while (1) {
190 osmo_select_main(0);
191 if (g_libusb_pending) {
192 struct timeval tv;
193 memset(&tv, 0, sizeof(tv));
194 rc = libusb_handle_events_timeout_completed(NULL, &tv, NULL);
195 if (rc != 0) {
196 fprintf(stderr, "handle_events_timeout_completed == %d\n", rc);
197 break;
198 }
199 }
200 }
201}
202
203int main(int argc, char **argv)
204{
205 int rc;
206 int c, ret = 1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100207 int local_udp_port = 52342;
Harald Welte236caf62016-03-19 21:28:09 +0100208 unsigned int if_num = 0;
Harald Welte095ac6c2016-03-19 13:39:33 +0100209
210 print_welcome();
211
212 while (1) {
213 int option_index = 0;
214 static const struct option opts[] = {
215 { "udp-port", 1, 0, 'u' },
Harald Welte236caf62016-03-19 21:28:09 +0100216 { "interface", 1, 0, 'I' },
Harald Welte095ac6c2016-03-19 13:39:33 +0100217 { "help", 0, 0, 'h' },
218 { NULL, 0, 0, 0 }
219 };
220
Harald Welte236caf62016-03-19 21:28:09 +0100221 c = getopt_long(argc, argv, "u:I:h", opts, &option_index);
Harald Welte095ac6c2016-03-19 13:39:33 +0100222 if (c == -1)
223 break;
224 switch (c) {
225 case 'u':
226 local_udp_port = atoi(optarg);
227 break;
Harald Welte236caf62016-03-19 21:28:09 +0100228 case 'I':
229 if_num = atoi(optarg);
230 break;
Harald Welte095ac6c2016-03-19 13:39:33 +0100231 case 'h':
232 print_help();
233 exit(0);
234 break;
235 }
236 }
237
238 rc = libusb_init(NULL);
239 if (rc < 0) {
240 fprintf(stderr, "libusb initialization failed\n");
241 goto close_exit;
242 }
243
244 libusb_set_pollfd_notifiers(NULL, &libusb_fd_added_cb, &libusb_fd_removed_cb, NULL);
245
246 g_devh = libusb_open_device_with_vid_pid(NULL, SIMTRACE_USB_VENDOR, SIMTRACE_USB_PRODUCT);
247 if (!g_devh) {
248 fprintf(stderr, "can't open USB device\n");
249 goto close_exit;
250 }
251
Harald Welte236caf62016-03-19 21:28:09 +0100252 rc = libusb_claim_interface(g_devh, if_num);
Harald Welte095ac6c2016-03-19 13:39:33 +0100253 if (rc < 0) {
Harald Welte236caf62016-03-19 21:28:09 +0100254 fprintf(stderr, "can't claim interface %u; rc=%d\n", if_num, rc);
Harald Welte095ac6c2016-03-19 13:39:33 +0100255 goto close_exit;
256 }
257
258 /* open UDP socket, register with select handling and mark it
259 * readable */
260 g_udp_ofd.cb = ofd_udp_cb;
Harald Welte236caf62016-03-19 21:28:09 +0100261 osmo_sock_init_ofd(&g_udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, local_udp_port + if_num, OSMO_SOCK_F_BIND);
Harald Welte095ac6c2016-03-19 13:39:33 +0100262
Harald Welte236caf62016-03-19 21:28:09 +0100263 rc = get_usb_ep_addrs(g_devh, if_num, &g_buf_out.ep, &g_buf_in.ep, NULL);
264 if (rc < 0) {
265 fprintf(stderr, "couldn't find enpdoint addresses; rc=%d\n", rc);
266 goto close_exit;
267 }
Harald Welte095ac6c2016-03-19 13:39:33 +0100268 /* initialize USB buffers / transfers */
Harald Welte236caf62016-03-19 21:28:09 +0100269 init_ep_buf(&g_buf_out);
270 init_ep_buf(&g_buf_in);
Harald Welte095ac6c2016-03-19 13:39:33 +0100271
272 /* submit the first transfer for the IN endpoint */
273 libusb_submit_transfer(g_buf_in.xfer);
274
275 run_mainloop();
276
277 ret = 0;
278
279 libusb_release_interface(g_devh, 0);
280close_exit:
281 if (g_devh)
282 libusb_close(g_devh);
283
Harald Welte095ac6c2016-03-19 13:39:33 +0100284 libusb_exit(NULL);
285 return ret;
286}