obex-server-tool.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. *
  4. * OBEX library with GLib integration
  5. *
  6. * Copyright (C) 2011 Intel Corporation. All rights reserved.
  7. *
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. #include <config.h>
  11. #endif
  12. #define _GNU_SOURCE
  13. #include <sys/types.h>
  14. #include <sys/socket.h>
  15. #include <fcntl.h>
  16. #include <sys/un.h>
  17. #include <unistd.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include "gobex/gobex.h"
  23. #include "btio/btio.h"
  24. static GMainLoop *main_loop = NULL;
  25. static GSList *clients = NULL;
  26. static gboolean option_packet = FALSE;
  27. static gboolean option_bluetooth = FALSE;
  28. static int option_channel = -1;
  29. static int option_imtu = -1;
  30. static int option_omtu = -1;
  31. static char *option_root = NULL;
  32. static void sig_term(int sig)
  33. {
  34. g_print("Terminating due to signal %d\n", sig);
  35. g_main_loop_quit(main_loop);
  36. }
  37. static GOptionEntry options[] = {
  38. { "unix", 'u', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,
  39. &option_bluetooth, "Use a UNIX socket" },
  40. { "bluetooth", 'b', 0, G_OPTION_ARG_NONE,
  41. &option_bluetooth, "Use Bluetooth" },
  42. { "channel", 'c', 0, G_OPTION_ARG_INT,
  43. &option_channel, "Transport channel", "CHANNEL" },
  44. { "packet", 'p', 0, G_OPTION_ARG_NONE,
  45. &option_packet, "Packet based transport" },
  46. { "stream", 's', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE,
  47. &option_packet, "Stream based transport" },
  48. { "root", 'r', 0, G_OPTION_ARG_STRING,
  49. &option_root, "Root dir", "/..." },
  50. { "input-mtu", 'i', 0, G_OPTION_ARG_INT,
  51. &option_imtu, "Transport input MTU", "MTU" },
  52. { "output-mtu", 'o', 0, G_OPTION_ARG_INT,
  53. &option_omtu, "Transport output MTU", "MTU" },
  54. { NULL },
  55. };
  56. static void disconn_func(GObex *obex, GError *err, gpointer user_data)
  57. {
  58. g_print("Client disconnected: %s\n", err ? err->message : "<no err>");
  59. clients = g_slist_remove(clients, obex);
  60. g_obex_unref(obex);
  61. }
  62. struct transfer_data {
  63. int fd;
  64. };
  65. static void transfer_complete(GObex *obex, GError *err, gpointer user_data)
  66. {
  67. struct transfer_data *data = user_data;
  68. if (err != NULL)
  69. g_printerr("transfer failed: %s\n", err->message);
  70. else
  71. g_print("transfer succeeded\n");
  72. close(data->fd);
  73. g_free(data);
  74. }
  75. static gboolean recv_data(const void *buf, gsize len, gpointer user_data)
  76. {
  77. struct transfer_data *data = user_data;
  78. g_print("received %zu bytes of data\n", len);
  79. if (write(data->fd, buf, len) < 0) {
  80. g_printerr("write: %s\n", strerror(errno));
  81. return FALSE;
  82. }
  83. return TRUE;
  84. }
  85. static void handle_put(GObex *obex, GObexPacket *req, gpointer user_data)
  86. {
  87. GError *err = NULL;
  88. GObexHeader *hdr;
  89. const char *type, *name;
  90. struct transfer_data *data;
  91. gsize type_len;
  92. hdr = g_obex_packet_get_header(req, G_OBEX_HDR_TYPE);
  93. if (hdr != NULL) {
  94. g_obex_header_get_bytes(hdr, (const guint8 **) &type,
  95. &type_len);
  96. if (type[type_len - 1] != '\0') {
  97. g_printerr("non-nul terminated type header\n");
  98. type = NULL;
  99. }
  100. } else
  101. type = NULL;
  102. hdr = g_obex_packet_get_header(req, G_OBEX_HDR_NAME);
  103. if (hdr != NULL)
  104. g_obex_header_get_unicode(hdr, &name);
  105. else
  106. name = NULL;
  107. g_print("put type \"%s\" name \"%s\"\n", type ? type : "",
  108. name ? name : "");
  109. data = g_new0(struct transfer_data, 1);
  110. data->fd = open(name, O_WRONLY | O_CREAT | O_NOCTTY, 0600);
  111. if (data->fd < 0) {
  112. g_printerr("open(%s): %s\n", name, strerror(errno));
  113. g_free(data);
  114. g_obex_send_rsp(obex, G_OBEX_RSP_FORBIDDEN, NULL,
  115. G_OBEX_HDR_INVALID);
  116. return;
  117. }
  118. g_obex_put_rsp(obex, req, recv_data, transfer_complete, data, &err,
  119. G_OBEX_HDR_INVALID);
  120. if (err != NULL) {
  121. g_printerr("Unable to send response: %s\n", err->message);
  122. g_error_free(err);
  123. g_free(data);
  124. }
  125. }
  126. static gssize send_data(void *buf, gsize len, gpointer user_data)
  127. {
  128. struct transfer_data *data = user_data;
  129. gssize ret;
  130. ret = read(data->fd, buf, len);
  131. g_print("sending %zu bytes of data\n", ret);
  132. return ret;
  133. }
  134. static void handle_get(GObex *obex, GObexPacket *req, gpointer user_data)
  135. {
  136. GError *err = NULL;
  137. struct transfer_data *data;
  138. const char *type, *name;
  139. GObexHeader *hdr;
  140. gsize type_len;
  141. hdr = g_obex_packet_get_header(req, G_OBEX_HDR_TYPE);
  142. if (hdr != NULL) {
  143. g_obex_header_get_bytes(hdr, (const guint8 **) &type,
  144. &type_len);
  145. if (type[type_len - 1] != '\0') {
  146. g_printerr("non-nul terminated type header\n");
  147. type = NULL;
  148. }
  149. } else
  150. type = NULL;
  151. hdr = g_obex_packet_get_header(req, G_OBEX_HDR_NAME);
  152. if (hdr != NULL)
  153. g_obex_header_get_unicode(hdr, &name);
  154. else
  155. name = NULL;
  156. g_print("get type \"%s\" name \"%s\"\n", type ? type : "",
  157. name ? name : "");
  158. data = g_new0(struct transfer_data, 1);
  159. data->fd = open(name, O_RDONLY | O_NOCTTY, 0);
  160. if (data->fd < 0) {
  161. g_printerr("open(%s): %s\n", name, strerror(errno));
  162. g_free(data);
  163. g_obex_send_rsp(obex, G_OBEX_RSP_FORBIDDEN, NULL,
  164. G_OBEX_HDR_INVALID);
  165. return;
  166. }
  167. g_obex_get_rsp(obex, send_data, transfer_complete, data, &err,
  168. G_OBEX_HDR_INVALID);
  169. if (err != NULL) {
  170. g_printerr("Unable to send response: %s\n", err->message);
  171. g_error_free(err);
  172. g_free(data);
  173. }
  174. }
  175. static void handle_connect(GObex *obex, GObexPacket *req, gpointer user_data)
  176. {
  177. GObexPacket *rsp;
  178. g_print("connect\n");
  179. rsp = g_obex_packet_new(G_OBEX_RSP_SUCCESS, TRUE, G_OBEX_HDR_INVALID);
  180. g_obex_send(obex, rsp, NULL);
  181. }
  182. static void transport_accept(GIOChannel *io)
  183. {
  184. GObex *obex;
  185. GObexTransportType transport;
  186. g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
  187. g_io_channel_set_close_on_unref(io, TRUE);
  188. if (option_packet)
  189. transport = G_OBEX_TRANSPORT_PACKET;
  190. else
  191. transport = G_OBEX_TRANSPORT_STREAM;
  192. obex = g_obex_new(io, transport, option_imtu, option_omtu);
  193. g_obex_set_disconnect_function(obex, disconn_func, NULL);
  194. g_obex_add_request_function(obex, G_OBEX_OP_PUT, handle_put, NULL);
  195. g_obex_add_request_function(obex, G_OBEX_OP_GET, handle_get, NULL);
  196. g_obex_add_request_function(obex, G_OBEX_OP_CONNECT, handle_connect,
  197. NULL);
  198. clients = g_slist_append(clients, obex);
  199. }
  200. static gboolean unix_accept(GIOChannel *chan, GIOCondition cond, gpointer data)
  201. {
  202. struct sockaddr_un addr;
  203. socklen_t addrlen;
  204. int sk, cli_sk;
  205. GIOChannel *io;
  206. if (cond & G_IO_NVAL)
  207. return FALSE;
  208. if (cond & (G_IO_HUP | G_IO_ERR)) {
  209. g_io_channel_shutdown(chan, TRUE, NULL);
  210. return FALSE;
  211. }
  212. sk = g_io_channel_unix_get_fd(chan);
  213. memset(&addr, 0, sizeof(addr));
  214. addrlen = sizeof(addr);
  215. cli_sk = accept(sk, (struct sockaddr *) &addr, &addrlen);
  216. if (cli_sk < 0) {
  217. g_printerr("accept: %s (%d)\n", strerror(errno), errno);
  218. return TRUE;
  219. }
  220. g_print("Accepted new client connection on unix socket (fd=%d)\n",
  221. cli_sk);
  222. io = g_io_channel_unix_new(cli_sk);
  223. transport_accept(io);
  224. g_io_channel_unref(io);
  225. return TRUE;
  226. }
  227. static void bluetooth_accept(GIOChannel *io, GError *err, gpointer data)
  228. {
  229. if (err) {
  230. g_printerr("accept: %s\n", err->message);
  231. return;
  232. }
  233. g_print("Accepted new client connection on bluetooth socket\n");
  234. transport_accept(io);
  235. }
  236. static gboolean bluetooth_watch(GIOChannel *chan, GIOCondition cond, gpointer data)
  237. {
  238. if (cond & G_IO_NVAL)
  239. return FALSE;
  240. g_io_channel_shutdown(chan, TRUE, NULL);
  241. return FALSE;
  242. }
  243. static GIOChannel *l2cap_listen(GError **err)
  244. {
  245. return bt_io_listen(bluetooth_accept, NULL, NULL,
  246. NULL, err,
  247. BT_IO_OPT_PSM, option_channel,
  248. BT_IO_OPT_MODE, BT_IO_MODE_ERTM,
  249. BT_IO_OPT_OMTU, option_omtu,
  250. BT_IO_OPT_IMTU, option_imtu,
  251. BT_IO_OPT_INVALID);
  252. }
  253. static GIOChannel *rfcomm_listen(GError **err)
  254. {
  255. return bt_io_listen(bluetooth_accept, NULL, NULL,
  256. NULL, err,
  257. BT_IO_OPT_CHANNEL, option_channel,
  258. BT_IO_OPT_INVALID);
  259. }
  260. static guint bluetooth_listen(void)
  261. {
  262. GIOChannel *io;
  263. guint id;
  264. GError *err = NULL;
  265. if (option_channel == -1) {
  266. g_printerr("Bluetooth channel not set\n");
  267. return 0;
  268. }
  269. if (option_packet || option_channel > 31)
  270. io = l2cap_listen(&err);
  271. else
  272. io = rfcomm_listen(&err);
  273. if (io == NULL) {
  274. g_printerr("%s\n", err->message);
  275. g_error_free(err);
  276. return 0;
  277. }
  278. g_print("Bluetooth socket created\n");
  279. id = g_io_add_watch(io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  280. bluetooth_watch, NULL);
  281. g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
  282. g_io_channel_set_close_on_unref(io, TRUE);
  283. g_io_channel_unref(io);
  284. return id;
  285. }
  286. static guint unix_listen(void)
  287. {
  288. GIOChannel *io;
  289. struct sockaddr_un addr = {
  290. AF_UNIX, "\0/gobex/server"
  291. };
  292. int sk, err, sock_type;
  293. guint id;
  294. if (option_packet)
  295. sock_type = SOCK_SEQPACKET;
  296. else
  297. sock_type = SOCK_STREAM;
  298. sk = socket(PF_LOCAL, sock_type, 0);
  299. if (sk < 0) {
  300. err = errno;
  301. g_printerr("Can't create unix socket: %s (%d)\n",
  302. strerror(err), err);
  303. return 0;
  304. }
  305. if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  306. g_printerr("Can't bind unix socket: %s (%d)\n",
  307. strerror(errno), errno);
  308. close(sk);
  309. return 0;
  310. }
  311. if (listen(sk, 1) < 0) {
  312. g_printerr("Can't listen on unix socket: %s (%d)\n",
  313. strerror(errno), errno);
  314. close(sk);
  315. return 0;
  316. }
  317. g_print("Unix socket created: %d\n", sk);
  318. io = g_io_channel_unix_new(sk);
  319. id = g_io_add_watch(io, G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  320. unix_accept, NULL);
  321. g_io_channel_set_flags(io, G_IO_FLAG_NONBLOCK, NULL);
  322. g_io_channel_set_close_on_unref(io, TRUE);
  323. g_io_channel_unref(io);
  324. return id;
  325. }
  326. int main(int argc, char *argv[])
  327. {
  328. GOptionContext *context;
  329. GError *err = NULL;
  330. struct sigaction sa;
  331. guint server_id;
  332. context = g_option_context_new(NULL);
  333. g_option_context_add_main_entries(context, options, NULL);
  334. g_option_context_parse(context, &argc, &argv, &err);
  335. if (err != NULL) {
  336. g_printerr("%s\n", err->message);
  337. g_error_free(err);
  338. exit(EXIT_FAILURE);
  339. }
  340. if (option_root && chdir(option_root) < 0) {
  341. perror("chdir:");
  342. exit(EXIT_FAILURE);
  343. }
  344. if (option_bluetooth)
  345. server_id = bluetooth_listen();
  346. else
  347. server_id = unix_listen();
  348. if (server_id == 0)
  349. exit(EXIT_FAILURE);
  350. memset(&sa, 0, sizeof(sa));
  351. sa.sa_handler = sig_term;
  352. sigaction(SIGINT, &sa, NULL);
  353. sigaction(SIGTERM, &sa, NULL);
  354. main_loop = g_main_loop_new(NULL, FALSE);
  355. g_main_loop_run(main_loop);
  356. g_source_remove(server_id);
  357. g_slist_free_full(clients, (GDestroyNotify) g_obex_unref);
  358. g_option_context_free(context);
  359. g_main_loop_unref(main_loop);
  360. exit(EXIT_SUCCESS);
  361. }