obex.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2004-2011 Marcel Holtmann <marcel@holtmann.org>
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "parser.h"
  20. static char *opcode2str(uint8_t opcode)
  21. {
  22. switch (opcode & 0x7f) {
  23. case 0x00:
  24. return "Connect";
  25. case 0x01:
  26. return "Disconnect";
  27. case 0x02:
  28. return "Put";
  29. case 0x03:
  30. return "Get";
  31. case 0x04:
  32. return "Reserved";
  33. case 0x05:
  34. return "SetPath";
  35. case 0x06:
  36. return "Action";
  37. case 0x07:
  38. return "Session";
  39. case 0x7f:
  40. return "Abort";
  41. case 0x10:
  42. return "Continue";
  43. case 0x20:
  44. return "Success";
  45. case 0x21:
  46. return "Created";
  47. case 0x22:
  48. return "Accepted";
  49. case 0x23:
  50. return "Non-authoritative information";
  51. case 0x24:
  52. return "No content";
  53. case 0x25:
  54. return "Reset content";
  55. case 0x26:
  56. return "Partial content";
  57. case 0x30:
  58. return "Multiple choices";
  59. case 0x31:
  60. return "Moved permanently";
  61. case 0x32:
  62. return "Moved temporarily";
  63. case 0x33:
  64. return "See other";
  65. case 0x34:
  66. return "Not modified";
  67. case 0x35:
  68. return "Use Proxy";
  69. case 0x40:
  70. return "Bad request";
  71. case 0x41:
  72. return "Unauthorized";
  73. case 0x42:
  74. return "Payment required";
  75. case 0x43:
  76. return "Forbidden";
  77. case 0x44:
  78. return "Not found";
  79. case 0x45:
  80. return "Method not allowed";
  81. case 0x46:
  82. return "Not acceptable";
  83. case 0x47:
  84. return "Proxy authentication required";
  85. case 0x48:
  86. return "Request timeout";
  87. case 0x49:
  88. return "Conflict";
  89. case 0x4a:
  90. return "Gone";
  91. case 0x4b:
  92. return "Length required";
  93. case 0x4c:
  94. return "Precondition failed";
  95. case 0x4d:
  96. return "Requested entity too large";
  97. case 0x4e:
  98. return "Requested URL too large";
  99. case 0x4f:
  100. return "Unsupported media type";
  101. case 0x50:
  102. return "Internal server error";
  103. case 0x51:
  104. return "Not implemented";
  105. case 0x52:
  106. return "Bad gateway";
  107. case 0x53:
  108. return "Service unavailable";
  109. case 0x54:
  110. return "Gateway timeout";
  111. case 0x55:
  112. return "HTTP version not supported";
  113. case 0x60:
  114. return "Database full";
  115. case 0x61:
  116. return "Database locked";
  117. default:
  118. return "Unknown";
  119. }
  120. }
  121. static char *hi2str(uint8_t hi)
  122. {
  123. switch (hi & 0x3f) {
  124. case 0x00:
  125. return "Count";
  126. case 0x01:
  127. return "Name";
  128. case 0x02:
  129. return "Type";
  130. case 0x03:
  131. return "Length";
  132. case 0x04:
  133. return "Time";
  134. case 0x05:
  135. return "Description";
  136. case 0x06:
  137. return "Target";
  138. case 0x07:
  139. return "HTTP";
  140. case 0x08:
  141. return "Body";
  142. case 0x09:
  143. return "End of Body";
  144. case 0x0a:
  145. return "Who";
  146. case 0x0b:
  147. return "Connection ID";
  148. case 0x0c:
  149. return "App. Parameters";
  150. case 0x0d:
  151. return "Auth. Challenge";
  152. case 0x0e:
  153. return "Auth. Response";
  154. case 0x0f:
  155. return "Creator ID";
  156. case 0x10:
  157. return "WAN UUID";
  158. case 0x11:
  159. return "Object Class";
  160. case 0x12:
  161. return "Session Parameters";
  162. case 0x13:
  163. return "Session Sequence Number";
  164. case 0x14:
  165. return "Action ID";
  166. case 0x15:
  167. return "DestName";
  168. case 0x16:
  169. return "Permission";
  170. case 0x17:
  171. return "Single Response Mode";
  172. case 0x18:
  173. return "Single Response Mode Parameters";
  174. default:
  175. return "Unknown";
  176. }
  177. }
  178. static void parse_headers(int level, struct frame *frm)
  179. {
  180. uint8_t hi, hv8;
  181. uint16_t len;
  182. uint32_t hv32;
  183. while (frm->len > 0) {
  184. hi = p_get_u8(frm);
  185. p_indent(level, frm);
  186. printf("%s (0x%02x)", hi2str(hi), hi);
  187. switch (hi & 0xc0) {
  188. case 0x00: /* Unicode */
  189. if (frm->len < 2) {
  190. printf("\n");
  191. return;
  192. }
  193. len = p_get_u16(frm) - 3;
  194. printf(" = Unicode length %d\n", len);
  195. if (frm->len < len)
  196. return;
  197. raw_ndump(level, frm, len);
  198. frm->ptr += len;
  199. frm->len -= len;
  200. break;
  201. case 0x40: /* Byte sequence */
  202. if (frm->len < 2) {
  203. printf("\n");
  204. return;
  205. }
  206. len = p_get_u16(frm) - 3;
  207. printf(" = Sequence length %d\n", len);
  208. if (frm->len < len)
  209. return;
  210. raw_ndump(level, frm, len);
  211. frm->ptr += len;
  212. frm->len -= len;
  213. break;
  214. case 0x80: /* One byte */
  215. if (frm->len < 1) {
  216. printf("\n");
  217. return;
  218. }
  219. hv8 = p_get_u8(frm);
  220. printf(" = %d\n", hv8);
  221. break;
  222. case 0xc0: /* Four bytes */
  223. if (frm->len < 4) {
  224. printf("\n");
  225. return;
  226. }
  227. hv32 = p_get_u32(frm);
  228. printf(" = %u\n", hv32);
  229. break;
  230. }
  231. }
  232. }
  233. void obex_dump(int level, struct frame *frm)
  234. {
  235. uint8_t last_opcode, opcode, status;
  236. uint8_t version, flags, constants;
  237. uint16_t length, pktlen;
  238. frm = add_frame(frm);
  239. while (frm->len > 2) {
  240. opcode = p_get_u8(frm);
  241. length = p_get_u16(frm);
  242. status = opcode & 0x7f;
  243. if ((int) frm->len < length - 3) {
  244. frm->ptr -= 3;
  245. frm->len += 3;
  246. return;
  247. }
  248. p_indent(level, frm);
  249. last_opcode = get_opcode(frm->handle, frm->dlci);
  250. if (!(opcode & 0x70)) {
  251. printf("OBEX: %s cmd(%c): len %d",
  252. opcode2str(opcode),
  253. opcode & 0x80 ? 'f' : 'c', length);
  254. set_opcode(frm->handle, frm->dlci, opcode);
  255. } else {
  256. printf("OBEX: %s rsp(%c): status %x%02d len %d",
  257. opcode2str(last_opcode),
  258. opcode & 0x80 ? 'f' : 'c',
  259. status >> 4, status & 0xf, length);
  260. opcode = last_opcode;
  261. }
  262. if (get_status(frm->handle, frm->dlci) == 0x10)
  263. printf(" (continue)");
  264. set_status(frm->handle, frm->dlci, status);
  265. if (frm->len == 0) {
  266. printf("\n");
  267. break;
  268. }
  269. switch (opcode & 0x7f) {
  270. case 0x00: /* Connect */
  271. if (frm->len < 4) {
  272. printf("\n");
  273. return;
  274. }
  275. version = p_get_u8(frm);
  276. flags = p_get_u8(frm);
  277. pktlen = p_get_u16(frm);
  278. printf(" version %d.%d flags %d mtu %d\n",
  279. version >> 4, version & 0xf, flags, pktlen);
  280. break;
  281. case 0x05: /* SetPath */
  282. if (frm->len < 2) {
  283. printf("\n");
  284. return;
  285. }
  286. flags = p_get_u8(frm);
  287. constants = p_get_u8(frm);
  288. printf(" flags %d constants %d\n", flags, constants);
  289. break;
  290. default:
  291. printf("\n");
  292. break;
  293. }
  294. if ((status & 0x70) && (parser.flags & DUMP_VERBOSE)) {
  295. p_indent(level, frm);
  296. printf("Status %x%02d = %s\n",
  297. status >> 4, status & 0xf,
  298. opcode2str(status));
  299. }
  300. parse_headers(level, frm);
  301. }
  302. }