gnunet-helper-transport-bluetooth.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2010, 2011, 2012 GNUnet e.V.
  4. Copyright (c) 2007, 2008, Andy Green <andy@warmcat.com>
  5. Copyright Copyright (C) 2009 Thomas d'Otreppe
  6. GNUnet is free software: you can redistribute it and/or modify it
  7. under the terms of the GNU Affero General Public License as published
  8. by the Free Software Foundation, either version 3 of the License,
  9. or (at your option) any later version.
  10. GNUnet is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Affero General Public License for more details.
  14. You should have received a copy of the GNU Affero General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. SPDX-License-Identifier: AGPL3.0-or-later
  17. */
  18. #include "gnunet_config.h"
  19. #ifdef MINGW
  20. #include "platform.h"
  21. #include "gnunet_util_lib.h"
  22. #include <bthdef.h>
  23. #include <ws2bth.h>
  24. #else
  25. #define SOCKTYPE int
  26. #include <bluetooth/bluetooth.h>
  27. #include <bluetooth/hci.h>
  28. #include <bluetooth/hci_lib.h>
  29. #include <bluetooth/rfcomm.h>
  30. #include <bluetooth/sdp.h>
  31. #include <bluetooth/sdp_lib.h>
  32. #include <errno.h>
  33. #include <linux/if.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <sys/ioctl.h>
  37. #include <sys/param.h>
  38. #include <sys/socket.h>
  39. #include <sys/stat.h>
  40. #include <sys/types.h>
  41. #include <unistd.h>
  42. #endif
  43. #include "plugin_transport_wlan.h"
  44. #include "gnunet_protocols.h"
  45. /**
  46. * Maximum number of ports assignable for RFCOMMM protocol.
  47. */
  48. #define MAX_PORTS 30
  49. /**
  50. * Maximum size of a message allowed in either direction
  51. * (used for our receive and sent buffers).
  52. */
  53. #define MAXLINE 4096
  54. /**
  55. * Maximum number of loops without inquiring for new devices.
  56. */
  57. #define MAX_LOOPS 5
  58. #ifdef MINGW
  59. /* Maximum size of the interface's name */
  60. #define IFNAMSIZ 16
  61. #ifndef NS_BTH
  62. #define NS_BTH 16
  63. #endif
  64. /**
  65. * A copy of the MAC Address.
  66. */
  67. struct GNUNET_TRANSPORT_WLAN_MacAddress_Copy
  68. {
  69. UINT8 mac[MAC_ADDR_SIZE];
  70. };
  71. /**
  72. * The UUID used for the SDP service.
  73. * {31191E56-FA7E-4517-870E-71B86BBCC52F}
  74. */
  75. #define GNUNET_BLUETOOTH_SDP_UUID \
  76. { \
  77. 0x31, 0x19, 0x1E, 0x56, \
  78. 0xFA, 0x7E, \
  79. 0x45, 0x17, \
  80. 0x87, 0x0E, \
  81. 0x71, 0xB8, 0x6B, 0xBC, 0xC5, 0x2F \
  82. }
  83. #endif
  84. /**
  85. * In bluez library, the maximum name length of a device is 8
  86. */
  87. #define BLUEZ_DEVNAME_SIZE 8
  88. /**
  89. * struct for storing the information of the hardware. There is only
  90. * one of these.
  91. */
  92. struct HardwareInfos
  93. {
  94. /**
  95. * Name of the interface, not necessarily 0-terminated (!).
  96. */
  97. char iface[IFNAMSIZ];
  98. #ifdef MINGW
  99. /**
  100. * socket handle
  101. */
  102. struct GNUNET_NETWORK_Handle *handle;
  103. /**
  104. * MAC address of our own bluetooth interface.
  105. */
  106. struct GNUNET_TRANSPORT_WLAN_MacAddress_Copy pl_mac;
  107. #else
  108. /**
  109. * file descriptor for the rfcomm socket
  110. */
  111. int fd_rfcomm;
  112. /**
  113. * MAC address of our own bluetooth interface.
  114. */
  115. struct GNUNET_TRANSPORT_WLAN_MacAddress pl_mac;
  116. /**
  117. * SDP session
  118. */
  119. sdp_session_t *session ;
  120. #endif
  121. };
  122. /**
  123. * IO buffer used for buffering data in transit (to wireless or to stdout).
  124. */
  125. struct SendBuffer
  126. {
  127. /**
  128. * How many bytes of data are stored in 'buf' for transmission right now?
  129. * Data always starts at offset 0 and extends to 'size'.
  130. */
  131. size_t size;
  132. /**
  133. * How many bytes that were stored in 'buf' did we already write to the
  134. * destination? Always smaller than 'size'.
  135. */
  136. size_t pos;
  137. /**
  138. * Buffered data; twice the maximum allowed message size as we add some
  139. * headers.
  140. */
  141. char buf[MAXLINE * 2];
  142. };
  143. #ifdef LINUX
  144. /**
  145. * Devices buffer used to keep a list with all the discoverable devices in
  146. * order to send them HELLO messages one by one when it receive a broadcast message.
  147. */
  148. struct BroadcastMessages
  149. {
  150. /* List with the discoverable devices' addresses */
  151. bdaddr_t devices[MAX_PORTS];
  152. /* List with the open sockets */
  153. int fds[MAX_PORTS];
  154. /* The number of the devices */
  155. int size;
  156. /* The current position */
  157. int pos;
  158. /* The device id */
  159. int dev_id;
  160. };
  161. /**
  162. * Address used to identify the broadcast messages.
  163. */
  164. static struct GNUNET_TRANSPORT_WLAN_MacAddress broadcast_address = {{255, 255, 255, 255, 255, 255}};
  165. /**
  166. * Buffer with the discoverable devices.
  167. */
  168. static struct BroadcastMessages neighbours;
  169. static int searching_devices_count = 0;
  170. #endif
  171. /**
  172. * Buffer for data read from stdin to be transmitted to the bluetooth device
  173. */
  174. static struct SendBuffer write_pout;
  175. /**
  176. * Buffer for data read from the bluetooth device to be transmitted to stdout.
  177. */
  178. static struct SendBuffer write_std;
  179. /* ****** this are the same functions as the ones used in gnunet-helper-transport-wlan.c ****** */
  180. /**
  181. * To what multiple do we align messages? 8 byte should suffice for everyone
  182. * for now.
  183. */
  184. #define ALIGN_FACTOR 8
  185. /**
  186. * Smallest supported message.
  187. */
  188. #define MIN_BUFFER_SIZE sizeof (struct GNUNET_MessageHeader)
  189. /**
  190. * Functions with this signature are called whenever a
  191. * complete message is received by the tokenizer.
  192. *
  193. * @param cls closure
  194. * @param message the actual message
  195. */
  196. typedef void (*MessageTokenizerCallback) (void *cls,
  197. const struct
  198. GNUNET_MessageHeader *
  199. message);
  200. /**
  201. * Handle to a message stream tokenizer.
  202. */
  203. struct MessageStreamTokenizer
  204. {
  205. /**
  206. * Function to call on completed messages.
  207. */
  208. MessageTokenizerCallback cb;
  209. /**
  210. * Closure for cb.
  211. */
  212. void *cb_cls;
  213. /**
  214. * Size of the buffer (starting at 'hdr').
  215. */
  216. size_t curr_buf;
  217. /**
  218. * How many bytes in buffer have we already processed?
  219. */
  220. size_t off;
  221. /**
  222. * How many bytes in buffer are valid right now?
  223. */
  224. size_t pos;
  225. /**
  226. * Beginning of the buffer. Typed like this to force alignment.
  227. */
  228. struct GNUNET_MessageHeader *hdr;
  229. };
  230. /**
  231. * Create a message stream tokenizer.
  232. *
  233. * @param cb function to call on completed messages
  234. * @param cb_cls closure for cb
  235. * @return handle to tokenizer
  236. */
  237. static struct MessageStreamTokenizer *
  238. mst_create (MessageTokenizerCallback cb,
  239. void *cb_cls)
  240. {
  241. struct MessageStreamTokenizer *ret;
  242. ret = malloc (sizeof (struct MessageStreamTokenizer));
  243. if (NULL == ret)
  244. {
  245. fprintf (stderr, "Failed to allocate buffer for tokenizer\n");
  246. exit (1);
  247. }
  248. ret->hdr = malloc (MIN_BUFFER_SIZE);
  249. if (NULL == ret->hdr)
  250. {
  251. fprintf (stderr, "Failed to allocate buffer for alignment\n");
  252. exit (1);
  253. }
  254. ret->curr_buf = MIN_BUFFER_SIZE;
  255. ret->cb = cb;
  256. ret->cb_cls = cb_cls;
  257. ret->pos = 0;
  258. return ret;
  259. }
  260. /**
  261. * Add incoming data to the receive buffer and call the
  262. * callback for all complete messages.
  263. *
  264. * @param mst tokenizer to use
  265. * @param buf input data to add
  266. * @param size number of bytes in buf
  267. * @return GNUNET_OK if we are done processing (need more data)
  268. * GNUNET_SYSERR if the data stream is corrupt
  269. */
  270. static int
  271. mst_receive (struct MessageStreamTokenizer *mst,
  272. const char *buf, size_t size)
  273. {
  274. const struct GNUNET_MessageHeader *hdr;
  275. size_t delta;
  276. uint16_t want;
  277. char *ibuf;
  278. int need_align;
  279. unsigned long offset;
  280. int ret;
  281. ret = GNUNET_OK;
  282. ibuf = (char *) mst->hdr;
  283. while (mst->pos > 0)
  284. {
  285. do_align:
  286. if (mst->pos < mst->off)
  287. {
  288. //fprintf (stderr, "We processed too many bytes!\n");
  289. return GNUNET_SYSERR;
  290. }
  291. if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) ||
  292. (0 != (mst->off % ALIGN_FACTOR)))
  293. {
  294. /* need to align or need more space */
  295. mst->pos -= mst->off;
  296. memmove (ibuf, &ibuf[mst->off], mst->pos);
  297. mst->off = 0;
  298. }
  299. if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
  300. {
  301. delta =
  302. GNUNET_MIN (sizeof (struct GNUNET_MessageHeader) -
  303. (mst->pos - mst->off), size);
  304. GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
  305. mst->pos += delta;
  306. buf += delta;
  307. size -= delta;
  308. }
  309. if (mst->pos - mst->off < sizeof (struct GNUNET_MessageHeader))
  310. {
  311. //FIXME should I reset ??
  312. // mst->off = 0;
  313. // mst->pos = 0;
  314. return GNUNET_OK;
  315. }
  316. hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
  317. want = ntohs (hdr->size);
  318. if (want < sizeof (struct GNUNET_MessageHeader))
  319. {
  320. fprintf (stderr,
  321. "Received invalid message from stdin\n");
  322. return GNUNET_SYSERR;
  323. }
  324. if ((mst->curr_buf - mst->off < want) &&
  325. (mst->off > 0))
  326. {
  327. /* need more space */
  328. mst->pos -= mst->off;
  329. memmove (ibuf, &ibuf[mst->off], mst->pos);
  330. mst->off = 0;
  331. }
  332. if (want > mst->curr_buf)
  333. {
  334. if (mst->off != 0)
  335. {
  336. fprintf (stderr, "Error! We should proceeded 0 bytes\n");
  337. return GNUNET_SYSERR;
  338. }
  339. mst->hdr = realloc (mst->hdr, want);
  340. if (NULL == mst->hdr)
  341. {
  342. fprintf (stderr, "Failed to allocate buffer for alignment\n");
  343. exit (1);
  344. }
  345. ibuf = (char *) mst->hdr;
  346. mst->curr_buf = want;
  347. }
  348. hdr = (const struct GNUNET_MessageHeader *) &ibuf[mst->off];
  349. if (mst->pos - mst->off < want)
  350. {
  351. delta = GNUNET_MIN (want - (mst->pos - mst->off), size);
  352. if (mst->pos + delta > mst->curr_buf)
  353. {
  354. fprintf (stderr, "The size of the buffer will be exceeded!\n");
  355. return GNUNET_SYSERR;
  356. }
  357. GNUNET_memcpy (&ibuf[mst->pos], buf, delta);
  358. mst->pos += delta;
  359. buf += delta;
  360. size -= delta;
  361. }
  362. if (mst->pos - mst->off < want)
  363. {
  364. //FIXME should I use this?
  365. // mst->off = 0;
  366. // mst->pos = 0;
  367. return GNUNET_OK;
  368. }
  369. mst->cb (mst->cb_cls, hdr);
  370. mst->off += want;
  371. if (mst->off == mst->pos)
  372. {
  373. /* reset to beginning of buffer, it's free right now! */
  374. mst->off = 0;
  375. mst->pos = 0;
  376. }
  377. }
  378. if (0 != mst->pos)
  379. {
  380. fprintf (stderr, "There should some valid bytes in the buffer on this stage\n");
  381. return GNUNET_SYSERR;
  382. }
  383. while (size > 0)
  384. {
  385. if (size < sizeof (struct GNUNET_MessageHeader))
  386. break;
  387. offset = (unsigned long) buf;
  388. need_align = (0 != offset % ALIGN_FACTOR) ? GNUNET_YES : GNUNET_NO;
  389. if (GNUNET_NO == need_align)
  390. {
  391. /* can try to do zero-copy and process directly from original buffer */
  392. hdr = (const struct GNUNET_MessageHeader *) buf;
  393. want = ntohs (hdr->size);
  394. if (want < sizeof (struct GNUNET_MessageHeader))
  395. {
  396. fprintf (stderr,
  397. "Received invalid message from stdin\n");
  398. //exit (1);
  399. mst->off = 0;
  400. return GNUNET_SYSERR;
  401. }
  402. if (size < want)
  403. break; /* or not, buffer incomplete, so copy to private buffer... */
  404. mst->cb (mst->cb_cls, hdr);
  405. buf += want;
  406. size -= want;
  407. }
  408. else
  409. {
  410. /* need to copy to private buffer to align;
  411. * yes, we go a bit more spagetti than usual here */
  412. goto do_align;
  413. }
  414. }
  415. if (size > 0)
  416. {
  417. if (size + mst->pos > mst->curr_buf)
  418. {
  419. mst->hdr = realloc (mst->hdr, size + mst->pos);
  420. if (NULL == mst->hdr)
  421. {
  422. fprintf (stderr, "Failed to allocate buffer for alignment\n");
  423. exit (1);
  424. }
  425. ibuf = (char *) mst->hdr;
  426. mst->curr_buf = size + mst->pos;
  427. }
  428. if (mst->pos + size > mst->curr_buf)
  429. {
  430. fprintf (stderr,
  431. "Assertion failed\n");
  432. exit (1);
  433. }
  434. GNUNET_memcpy (&ibuf[mst->pos], buf, size);
  435. mst->pos += size;
  436. }
  437. return ret;
  438. }
  439. /**
  440. * Destroys a tokenizer.
  441. *
  442. * @param mst tokenizer to destroy
  443. */
  444. static void
  445. mst_destroy (struct MessageStreamTokenizer *mst)
  446. {
  447. free (mst->hdr);
  448. free (mst);
  449. }
  450. /**
  451. * Calculate crc32, the start of the calculation
  452. *
  453. * @param buf buffer to calc the crc
  454. * @param len len of the buffer
  455. * @return crc sum
  456. */
  457. static unsigned long
  458. calc_crc_osdep (const unsigned char *buf, size_t len)
  459. {
  460. static const unsigned long int crc_tbl_osdep[256] = {
  461. 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
  462. 0xE963A535, 0x9E6495A3,
  463. 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD,
  464. 0xE7B82D07, 0x90BF1D91,
  465. 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB,
  466. 0xF4D4B551, 0x83D385C7,
  467. 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
  468. 0xFA0F3D63, 0x8D080DF5,
  469. 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447,
  470. 0xD20D85FD, 0xA50AB56B,
  471. 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75,
  472. 0xDCD60DCF, 0xABD13D59,
  473. 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
  474. 0xCFBA9599, 0xB8BDA50F,
  475. 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11,
  476. 0xC1611DAB, 0xB6662D3D,
  477. 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F,
  478. 0x9FBFE4A5, 0xE8B8D433,
  479. 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
  480. 0x91646C97, 0xE6635C01,
  481. 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B,
  482. 0x8208F4C1, 0xF50FC457,
  483. 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49,
  484. 0x8CD37CF3, 0xFBD44C65,
  485. 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
  486. 0xA4D1C46D, 0xD3D6F4FB,
  487. 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5,
  488. 0xAA0A4C5F, 0xDD0D7CC9,
  489. 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3,
  490. 0xB966D409, 0xCE61E49F,
  491. 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
  492. 0xB7BD5C3B, 0xC0BA6CAD,
  493. 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF,
  494. 0x04DB2615, 0x73DC1683,
  495. 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D,
  496. 0x0A00AE27, 0x7D079EB1,
  497. 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
  498. 0x196C3671, 0x6E6B06E7,
  499. 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9,
  500. 0x17B7BE43, 0x60B08ED5,
  501. 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767,
  502. 0x3FB506DD, 0x48B2364B,
  503. 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
  504. 0x316E8EEF, 0x4669BE79,
  505. 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703,
  506. 0x220216B9, 0x5505262F,
  507. 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31,
  508. 0x2CD99E8B, 0x5BDEAE1D,
  509. 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
  510. 0x72076785, 0x05005713,
  511. 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D,
  512. 0x7CDCEFB7, 0x0BDBDF21,
  513. 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B,
  514. 0x6FB077E1, 0x18B74777,
  515. 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
  516. 0x616BFFD3, 0x166CCF45,
  517. 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7,
  518. 0x4969474D, 0x3E6E77DB,
  519. 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5,
  520. 0x47B2CF7F, 0x30B5FFE9,
  521. 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
  522. 0x54DE5729, 0x23D967BF,
  523. 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1,
  524. 0x5A05DF1B, 0x2D02EF8D
  525. };
  526. unsigned long crc = 0xFFFFFFFF;
  527. for (; len > 0; len--, buf++)
  528. crc = crc_tbl_osdep[(crc ^ *buf) & 0xFF] ^ (crc >> 8);
  529. return (~crc);
  530. }
  531. /**
  532. * Calculate and check crc of the bluetooth packet
  533. *
  534. * @param buf buffer of the packet, with len + 4 bytes of data,
  535. * the last 4 bytes being the checksum
  536. * @param len length of the payload in data
  537. * @return 0 on success (checksum matches), 1 on error
  538. */
  539. static int
  540. check_crc_buf_osdep (const unsigned char *buf, size_t len)
  541. {
  542. unsigned long crc;
  543. crc = calc_crc_osdep (buf, len);
  544. buf += len;
  545. if (((crc) & 0xFF) == buf[0] && ((crc >> 8) & 0xFF) == buf[1] &&
  546. ((crc >> 16) & 0xFF) == buf[2] && ((crc >> 24) & 0xFF) == buf[3])
  547. return 0;
  548. return 1;
  549. }
  550. /* ************** end of clone ***************** */
  551. #ifdef MINGW
  552. /**
  553. * Function used to get the code of last error and to print the type of error.
  554. */
  555. static void
  556. print_last_error()
  557. {
  558. LPVOID lpMsgBuf = NULL;
  559. if (FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  560. NULL, GetLastError(), 0, (LPTSTR) &lpMsgBuf, 0, NULL))
  561. fprintf (stderr, "%s\n", (char *)lpMsgBuf);
  562. else
  563. fprintf (stderr, "Failed to format the message for the last error! Error number : %d\n", GetLastError());
  564. }
  565. /**
  566. * Function used to initialize the Windows Sockets
  567. */
  568. static void
  569. initialize_windows_sockets()
  570. {
  571. WSADATA wsaData ;
  572. WORD wVersionRequested = MAKEWORD (2, 0);
  573. if (WSAStartup (wVersionRequested, &wsaData) != NO_ERROR)
  574. {
  575. fprintf (stderr , "Error initializing window sockets!\n");
  576. print_last_error();
  577. ExitProcess (2) ;
  578. }
  579. }
  580. /**
  581. * Function used to convert the GUID.
  582. * @param bytes the GUID represented as a char array
  583. * @param uuid pointer to the GUID
  584. */
  585. static void
  586. convert_guid(char *bytes, GUID * uuid)
  587. {
  588. int i;
  589. uuid->Data1 = ((bytes[0] << 24) & 0xff000000) | ((bytes[1] << 16) & 0x00ff0000) | ((bytes[2] << 8) & 0x0000ff00) | (bytes[3] & 0x000000ff);
  590. uuid->Data2 = ((bytes[4] << 8) & 0xff00) | (bytes[5] & 0x00ff);
  591. uuid->Data3 = ((bytes[6] << 8) & 0xff00) | (bytes[7] & 0x00ff);
  592. for (i = 0; i < 8; i++)
  593. {
  594. uuid->Data4[i] = bytes[i + 8];
  595. }
  596. }
  597. #endif
  598. #ifdef LINUX
  599. /**
  600. * Function for assigning a port number
  601. *
  602. * @param socket the socket used to bind
  603. * @param addr pointer to the rfcomm address
  604. * @return 0 on success
  605. */
  606. static int
  607. bind_socket (int socket, struct sockaddr_rc *addr)
  608. {
  609. int port, status;
  610. /* Bind every possible port (from 0 to 30) and stop when binding doesn't fail */
  611. //FIXME : it should start from port 1, but on my computer it doesn't work :)
  612. for (port = 3; port <= 30; port++)
  613. {
  614. addr->rc_channel = port;
  615. status = bind (socket, (struct sockaddr *) addr, sizeof (struct sockaddr_rc));
  616. if (status == 0)
  617. return 0;
  618. }
  619. return -1;
  620. }
  621. #endif
  622. #ifdef MINGW
  623. /**
  624. * Function used for creating the service record and registering it.
  625. *
  626. * @param dev pointer to the device struct
  627. * @return 0 on success
  628. */
  629. static int
  630. register_service (struct HardwareInfos *dev)
  631. {
  632. /* advertise the service */
  633. CSADDR_INFO addr_info;
  634. WSAQUERYSET wqs;
  635. GUID guid;
  636. unsigned char uuid[] = GNUNET_BLUETOOTH_SDP_UUID;
  637. SOCKADDR_BTH addr;
  638. int addr_len = sizeof (SOCKADDR_BTH);
  639. int fd;
  640. /* get the port on which we are listening on */
  641. memset (& addr, 0, sizeof (SOCKADDR_BTH));
  642. fd = GNUNET_NETWORK_get_fd (dev->handle);
  643. if (fd <= 0)
  644. {
  645. fprintf (stderr, "Failed to get the file descriptor\n");
  646. return -1;
  647. }
  648. if (SOCKET_ERROR == getsockname (fd, (SOCKADDR*)&addr, &addr_len))
  649. {
  650. fprintf (stderr, "Failed to get the port on which we are listening on: \n");
  651. print_last_error();
  652. return -1;
  653. }
  654. /* save the device address */
  655. GNUNET_memcpy (&dev->pl_mac, &addr.btAddr, sizeof (BTH_ADDR));
  656. /* set the address information */
  657. memset (&addr_info, 0, sizeof (CSADDR_INFO));
  658. addr_info.iProtocol = BTHPROTO_RFCOMM;
  659. addr_info.iSocketType = SOCK_STREAM;
  660. addr_info.LocalAddr.lpSockaddr = (LPSOCKADDR)&addr;
  661. addr_info.LocalAddr.iSockaddrLength = sizeof (addr);
  662. addr_info.RemoteAddr.lpSockaddr = (LPSOCKADDR)&addr;
  663. addr_info.RemoteAddr.iSockaddrLength = sizeof (addr);
  664. convert_guid((char *) uuid, &guid);
  665. /* register the service */
  666. memset (&wqs, 0, sizeof (WSAQUERYSET));
  667. wqs.dwSize = sizeof (WSAQUERYSET);
  668. wqs.dwNameSpace = NS_BTH;
  669. wqs.lpszServiceInstanceName = "GNUnet Bluetooth Service";
  670. wqs.lpszComment = "This is the service used by the GNUnnet plugin transport";
  671. wqs.lpServiceClassId = &guid;
  672. wqs.dwNumberOfCsAddrs = 1;
  673. wqs.lpcsaBuffer = &addr_info ;
  674. wqs.lpBlob = 0;
  675. if (SOCKET_ERROR == WSASetService (&wqs , RNRSERVICE_REGISTER, 0))
  676. {
  677. fprintf (stderr, "Failed to register the SDP service: ");
  678. print_last_error();
  679. return -1;
  680. }
  681. else
  682. {
  683. fprintf (stderr, "The SDP service was registered\n");
  684. }
  685. return 0;
  686. }
  687. #else
  688. /**
  689. * Function used for creating the service record and registering it.
  690. *
  691. * @param dev pointer to the device struct
  692. * @param rc_channel the rfcomm channel
  693. * @return 0 on success
  694. */
  695. static int
  696. register_service (struct HardwareInfos *dev, int rc_channel)
  697. {
  698. /**
  699. * 1. initializations
  700. * 2. set the service ID, class, profile information
  701. * 3. make the service record publicly browsable
  702. * 4. register the RFCOMM channel
  703. * 5. set the name, provider and description
  704. * 6. register the service record to the local SDP server
  705. * 7. cleanup
  706. */
  707. uint8_t svc_uuid_int[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  708. dev->pl_mac.mac[5], dev->pl_mac.mac[4], dev->pl_mac.mac[3],
  709. dev->pl_mac.mac[2], dev->pl_mac.mac[1], dev->pl_mac.mac[0]};
  710. const char *service_dsc = "Bluetooth plugin services";
  711. const char *service_prov = "GNUnet provider";
  712. uuid_t root_uuid, rfcomm_uuid, svc_uuid;
  713. sdp_list_t *root_list = 0, *rfcomm_list = 0, *proto_list = 0,
  714. *access_proto_list = 0, *svc_list = 0;
  715. sdp_record_t *record = 0;
  716. sdp_data_t *channel = 0;
  717. record = sdp_record_alloc();
  718. /* Set the general service ID */
  719. sdp_uuid128_create (&svc_uuid, &svc_uuid_int);
  720. svc_list = sdp_list_append (0, &svc_uuid);
  721. sdp_set_service_classes (record, svc_list);
  722. sdp_set_service_id (record, svc_uuid);
  723. /* Make the service record publicly browsable */
  724. sdp_uuid16_create (&root_uuid, PUBLIC_BROWSE_GROUP);
  725. root_list = sdp_list_append (0, &root_uuid);
  726. sdp_set_browse_groups (record, root_list);
  727. /* Register the RFCOMM channel */
  728. sdp_uuid16_create (&rfcomm_uuid, RFCOMM_UUID);
  729. channel = sdp_data_alloc (SDP_UINT8, &rc_channel);
  730. rfcomm_list = sdp_list_append (0, &rfcomm_uuid);
  731. sdp_list_append (rfcomm_list, channel);
  732. proto_list = sdp_list_append (0, rfcomm_list);
  733. /* Set protocol information */
  734. access_proto_list = sdp_list_append (0, proto_list);
  735. sdp_set_access_protos (record, access_proto_list);
  736. /* Set the name, provider, and description */
  737. sdp_set_info_attr (record, dev->iface, service_prov, service_dsc);
  738. /* Connect to the local SDP server */
  739. dev->session = sdp_connect (BDADDR_ANY, BDADDR_LOCAL, SDP_RETRY_IF_BUSY);
  740. if (!dev->session)
  741. {
  742. fprintf (stderr, "Failed to connect to the SDP server on interface `%.*s': %s\n",
  743. IFNAMSIZ, dev->iface, strerror (errno));
  744. //FIXME exit?
  745. return 1;
  746. }
  747. /* Register the service record */
  748. if (sdp_record_register (dev->session, record, 0) < 0)
  749. {
  750. fprintf (stderr, "Failed to register a service record on interface `%.*s': %s\n",
  751. IFNAMSIZ, dev->iface, strerror (errno));
  752. //FIXME exit?
  753. return 1;
  754. }
  755. /* Cleanup */
  756. sdp_data_free (channel);
  757. sdp_list_free (root_list, 0);
  758. sdp_list_free (rfcomm_list, 0);
  759. sdp_list_free (proto_list, 0);
  760. sdp_list_free (access_proto_list, 0);
  761. sdp_list_free (svc_list, 0);
  762. sdp_record_free (record);
  763. return 0;
  764. }
  765. #endif
  766. #ifdef MINGW
  767. /**
  768. * Function for searching and browsing for a service. This will return the
  769. * port number on which the service is running.
  770. *
  771. * @param dest target address
  772. * @return channel
  773. */
  774. static int
  775. get_channel(const char *dest)
  776. {
  777. HANDLE h;
  778. WSAQUERYSET *wqs;
  779. DWORD wqs_len = sizeof (WSAQUERYSET);
  780. int done = 0;
  781. int channel = -1;
  782. GUID guid;
  783. unsigned char uuid[] = GNUNET_BLUETOOTH_SDP_UUID;
  784. convert_guid ((char *) uuid, &guid);
  785. wqs = (WSAQUERYSET*)malloc (wqs_len);
  786. ZeroMemory (wqs, wqs_len);
  787. wqs->dwSize = sizeof (WSAQUERYSET) ;
  788. wqs->lpServiceClassId = &guid;
  789. wqs->dwNameSpace = NS_BTH;
  790. wqs->dwNumberOfCsAddrs = 0;
  791. wqs->lpszContext = (LPSTR)dest;
  792. if (SOCKET_ERROR == WSALookupServiceBegin (wqs, LUP_FLUSHCACHE | LUP_RETURN_ALL, &h))
  793. {
  794. if (GetLastError() == WSASERVICE_NOT_FOUND)
  795. {
  796. fprintf (stderr, "WARNING! The device with address %s wasn't found. Skipping the message!", dest);
  797. return -1;
  798. }
  799. else
  800. {
  801. fprintf (stderr, "Failed to find the port number: ");
  802. print_last_error();
  803. ExitProcess (2);
  804. return -1;
  805. }
  806. }
  807. /* search the sdp service */
  808. while (!done)
  809. {
  810. if (SOCKET_ERROR == WSALookupServiceNext (h, LUP_FLUSHCACHE | LUP_RETURN_ALL, &wqs_len, wqs))
  811. {
  812. int error = WSAGetLastError();
  813. switch (error)
  814. {
  815. case WSAEFAULT:
  816. free (wqs);
  817. wqs = (WSAQUERYSET*)malloc (wqs_len);
  818. break;
  819. case WSANO_DATA:
  820. fprintf (stderr, "Failed! The address was valid but there was no data record of requested type\n");
  821. done = 1;
  822. break;
  823. case WSA_E_NO_MORE:
  824. done = 1;
  825. break;
  826. default:
  827. fprintf (stderr, "Failed to look over the services: ");
  828. print_last_error();
  829. WSALookupServiceEnd (h);
  830. ExitProcess (2);
  831. }
  832. }
  833. else
  834. {
  835. channel = ((SOCKADDR_BTH*)wqs->lpcsaBuffer->RemoteAddr.lpSockaddr)->port;
  836. }
  837. }
  838. free (wqs) ;
  839. WSALookupServiceEnd (h);
  840. return channel;
  841. }
  842. #else
  843. /**
  844. * Function used for searching and browsing for a service. This will return the
  845. * port number on which the service is running.
  846. *
  847. * @param dev pointer to the device struct
  848. * @param dest target address
  849. * @return channel
  850. */
  851. static int
  852. get_channel(struct HardwareInfos *dev, bdaddr_t dest)
  853. {
  854. /**
  855. * 1. detect all nearby devices
  856. * 2. for each device:
  857. * 2.1. connect to the SDP server running
  858. * 2.2. get a list of service records with the specific UUID
  859. * 2.3. for each service record get a list of the protocol sequences and get
  860. * the port number
  861. */
  862. uint8_t svc_uuid_int[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  863. dest.b[5], dest.b[4], dest.b[3],
  864. dest.b[2], dest.b[1], dest.b[0]};
  865. sdp_session_t *session = 0;
  866. sdp_list_t *search_list = 0, *attrid_list = 0, *response_list = 0, *it = 0;
  867. uuid_t svc_uuid;
  868. uint32_t range = 0x0000ffff;
  869. int channel = -1;
  870. /* Connect to the local SDP server */
  871. session = sdp_connect (BDADDR_ANY, &dest, 0);
  872. if (!session)
  873. {
  874. fprintf (stderr, "Failed to connect to the SDP server on interface `%.*s': %s\n",
  875. IFNAMSIZ, dev->iface, strerror (errno));
  876. return -1;
  877. }
  878. sdp_uuid128_create (&svc_uuid, &svc_uuid_int);
  879. search_list = sdp_list_append (0, &svc_uuid);
  880. attrid_list = sdp_list_append (0, &range);
  881. if (sdp_service_search_attr_req (session, search_list,
  882. SDP_ATTR_REQ_RANGE, attrid_list, &response_list) == 0)
  883. {
  884. for (it = response_list; it; it = it->next)
  885. {
  886. sdp_record_t *record = (sdp_record_t*) it->data;
  887. sdp_list_t *proto_list = 0;
  888. if (sdp_get_access_protos (record, &proto_list) == 0)
  889. {
  890. channel = sdp_get_proto_port (proto_list, RFCOMM_UUID);
  891. sdp_list_free (proto_list, 0);
  892. }
  893. sdp_record_free (record);
  894. }
  895. }
  896. sdp_list_free (search_list, 0);
  897. sdp_list_free (attrid_list, 0);
  898. sdp_list_free (response_list, 0);
  899. sdp_close (session);
  900. if (-1 == channel)
  901. fprintf (stderr,
  902. "Failed to find the listening channel for interface `%.*s': %s\n",
  903. IFNAMSIZ,
  904. dev->iface,
  905. strerror (errno));
  906. return channel;
  907. }
  908. #endif
  909. /**
  910. * Read from the socket and put the result into the buffer for transmission to 'stdout'.
  911. *
  912. * @param sock file descriptor for reading
  913. * @param buf buffer to read to; first bytes will be the 'struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame',
  914. * followed by the actual payload
  915. * @param buf_size size of the buffer
  916. * @param ri where to write radiotap_rx info
  917. * @return number of bytes written to 'buf'
  918. */
  919. static ssize_t
  920. read_from_the_socket (void *sock,
  921. unsigned char *buf, size_t buf_size,
  922. struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *ri)
  923. {
  924. unsigned char tmpbuf[buf_size];
  925. ssize_t count;
  926. #ifdef MINGW
  927. count = GNUNET_NETWORK_socket_recv ((struct GNUNET_NETWORK_Handle *)sock, tmpbuf, buf_size);
  928. #else
  929. count = read (*((int *)sock), tmpbuf, buf_size);
  930. #endif
  931. if (0 > count)
  932. {
  933. if (EAGAIN == errno)
  934. return 0;
  935. #if MINGW
  936. print_last_error();
  937. #else
  938. fprintf (stderr, "Failed to read from the HCI socket: %s\n", strerror (errno));
  939. #endif
  940. return -1;
  941. }
  942. #ifdef LINUX
  943. /* Get the channel used */
  944. int len;
  945. struct sockaddr_rc rc_addr = { 0 };
  946. memset (&rc_addr, 0, sizeof (rc_addr));
  947. len = sizeof (rc_addr);
  948. if (0 > getsockname (*((int *)sock), (struct sockaddr *) &rc_addr, (socklen_t *) &len))
  949. {
  950. fprintf (stderr, "getsockname() call failed : %s\n", strerror (errno));
  951. return -1;
  952. }
  953. memset (ri, 0, sizeof (*ri));
  954. ri->ri_channel = rc_addr.rc_channel;
  955. #endif
  956. /* Detect CRC32 at the end */
  957. if (0 == check_crc_buf_osdep (tmpbuf, count - sizeof (uint32_t)))
  958. {
  959. count -= sizeof(uint32_t);
  960. }
  961. GNUNET_memcpy (buf, tmpbuf, count);
  962. return count;
  963. }
  964. /**
  965. * Open the bluetooth interface for reading/writing
  966. *
  967. * @param dev pointer to the device struct
  968. * @return 0 on success, non-zero on error
  969. */
  970. static int
  971. open_device (struct HardwareInfos *dev)
  972. {
  973. #ifdef MINGW
  974. SOCKADDR_BTH addr;
  975. /* bind the RFCOMM socket to the interface */
  976. addr.addressFamily = AF_BTH;
  977. addr.btAddr = 0;
  978. addr.port = BT_PORT_ANY;
  979. if (GNUNET_OK !=
  980. GNUNET_NETWORK_socket_bind (dev->handle, (const SOCKADDR*)&addr, sizeof (SOCKADDR_BTH)))
  981. {
  982. fprintf (stderr, "Failed to bind the socket: ");
  983. if (GetLastError() == WSAENETDOWN)
  984. {
  985. fprintf (stderr, "Please make sure that your Bluetooth device is ON!\n");
  986. ExitProcess (2);
  987. }
  988. print_last_error();
  989. return -1;
  990. }
  991. /* start listening on the socket */
  992. if (GNUNET_NETWORK_socket_listen (dev->handle, 4) != GNUNET_OK)
  993. {
  994. fprintf (stderr, "Failed to listen on the socket: ");
  995. print_last_error();
  996. return -1;
  997. }
  998. /* register the sdp service */
  999. if (register_service(dev) != 0)
  1000. {
  1001. fprintf (stderr, "Failed to register a service: ");
  1002. print_last_error();
  1003. return 1;
  1004. }
  1005. #else
  1006. int i, dev_id = -1, fd_hci;
  1007. struct
  1008. {
  1009. struct hci_dev_list_req list;
  1010. struct hci_dev_req dev[HCI_MAX_DEV];
  1011. } request; //used for detecting the local devices
  1012. struct sockaddr_rc rc_addr = { 0 }; //used for binding
  1013. /* Initialize the neighbour structure */
  1014. neighbours.dev_id = -1;
  1015. for (i = 0; i < MAX_PORTS; i++)
  1016. neighbours.fds[i] = -1;
  1017. /* Open a HCI socket */
  1018. fd_hci = socket (AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
  1019. if (fd_hci < 0)
  1020. {
  1021. fprintf (stderr,
  1022. "Failed to create HCI socket: %s\n",
  1023. strerror (errno));
  1024. return -1;
  1025. }
  1026. memset (&request, 0, sizeof(request));
  1027. request.list.dev_num = HCI_MAX_DEV;
  1028. if (ioctl (fd_hci, HCIGETDEVLIST, (void *) &request) < 0)
  1029. {
  1030. fprintf (stderr,
  1031. "ioctl(HCIGETDEVLIST) on interface `%.*s' failed: %s\n",
  1032. IFNAMSIZ,
  1033. dev->iface,
  1034. strerror (errno));
  1035. (void) close (fd_hci);
  1036. return 1;
  1037. }
  1038. /* Search for a device with dev->iface name */
  1039. for (i = 0; i < request.list.dev_num; i++)
  1040. {
  1041. struct hci_dev_info dev_info;
  1042. memset (&dev_info, 0, sizeof(struct hci_dev_info));
  1043. dev_info.dev_id = request.dev[i].dev_id;
  1044. strncpy (dev_info.name, dev->iface, BLUEZ_DEVNAME_SIZE);
  1045. if (ioctl (fd_hci, HCIGETDEVINFO, (void *) &dev_info))
  1046. {
  1047. fprintf (stderr,
  1048. "ioctl(HCIGETDEVINFO) on interface `%.*s' failed: %s\n",
  1049. IFNAMSIZ,
  1050. dev->iface,
  1051. strerror (errno));
  1052. (void) close (fd_hci);
  1053. return 1;
  1054. }
  1055. if (strncmp (dev_info.name, dev->iface, BLUEZ_DEVNAME_SIZE) == 0)
  1056. {
  1057. dev_id = dev_info.dev_id; //the device was found
  1058. /**
  1059. * Copy the MAC address to the device structure
  1060. */
  1061. GNUNET_memcpy (&dev->pl_mac, &dev_info.bdaddr, sizeof (bdaddr_t));
  1062. /* Check if the interface is up */
  1063. if (hci_test_bit (HCI_UP, (void *) &dev_info.flags) == 0)
  1064. {
  1065. /* Bring the interface up */
  1066. if (ioctl (fd_hci, HCIDEVUP, dev_info.dev_id))
  1067. {
  1068. fprintf (stderr,
  1069. "ioctl(HCIDEVUP) on interface `%.*s' failed: %s\n",
  1070. IFNAMSIZ,
  1071. dev->iface,
  1072. strerror (errno));
  1073. (void) close (fd_hci);
  1074. return 1;
  1075. }
  1076. }
  1077. /* Check if the device is discoverable */
  1078. if (hci_test_bit (HCI_PSCAN, (void *) &dev_info.flags) == 0 ||
  1079. hci_test_bit (HCI_ISCAN, (void *) &dev_info.flags) == 0)
  1080. {
  1081. /* Set interface Page Scan and Inqury Scan ON */
  1082. struct hci_dev_req dev_req;
  1083. memset (&dev_req, 0, sizeof (dev_req));
  1084. dev_req.dev_id = dev_info.dev_id;
  1085. dev_req.dev_opt = SCAN_PAGE | SCAN_INQUIRY;
  1086. if (ioctl (fd_hci, HCISETSCAN, (unsigned long) &dev_req))
  1087. {
  1088. fprintf (stderr,
  1089. "ioctl(HCISETSCAN) on interface `%.*s' failed: %s\n",
  1090. IFNAMSIZ,
  1091. dev->iface,
  1092. strerror (errno));
  1093. (void) close (fd_hci);
  1094. return 1;
  1095. }
  1096. }
  1097. break;
  1098. }
  1099. }
  1100. /* Check if the interface was not found */
  1101. if (-1 == dev_id)
  1102. {
  1103. fprintf (stderr,
  1104. "The interface %s was not found\n",
  1105. dev->iface);
  1106. (void) close (fd_hci);
  1107. return 1;
  1108. }
  1109. /* Close the hci socket */
  1110. (void) close(fd_hci);
  1111. /* Bind the rfcomm socket to the interface */
  1112. memset (&rc_addr, 0, sizeof (rc_addr));
  1113. rc_addr.rc_family = AF_BLUETOOTH;
  1114. rc_addr.rc_bdaddr = *BDADDR_ANY;
  1115. if (bind_socket (dev->fd_rfcomm, &rc_addr) != 0)
  1116. {
  1117. fprintf (stderr,
  1118. "Failed to bind interface `%.*s': %s\n",
  1119. IFNAMSIZ,
  1120. dev->iface,
  1121. strerror (errno));
  1122. return 1;
  1123. }
  1124. /* Register a SDP service */
  1125. if (register_service (dev, rc_addr.rc_channel) != 0)
  1126. {
  1127. fprintf (stderr,
  1128. "Failed to register a service on interface `%.*s': %s\n",
  1129. IFNAMSIZ,
  1130. dev->iface, strerror (errno));
  1131. return 1;
  1132. }
  1133. /* Switch socket in listening mode */
  1134. if (listen (dev->fd_rfcomm, 5) == -1) //FIXME: probably we need a bigger number
  1135. {
  1136. fprintf (stderr, "Failed to listen on socket for interface `%.*s': %s\n", IFNAMSIZ,
  1137. dev->iface, strerror (errno));
  1138. return 1;
  1139. }
  1140. #endif
  1141. return 0;
  1142. }
  1143. /**
  1144. * Set the header to sane values to make attacks more difficult
  1145. *
  1146. * @param taIeeeHeader pointer to the header of the packet
  1147. * @param dev pointer to the Hardware_Infos struct
  1148. *
  1149. **** copy from gnunet-helper-transport-wlan.c ****
  1150. */
  1151. static void
  1152. mac_set (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
  1153. const struct HardwareInfos *dev)
  1154. {
  1155. taIeeeHeader->frame_control = htons (IEEE80211_FC0_TYPE_DATA);
  1156. taIeeeHeader->addr3 = mac_bssid_gnunet;
  1157. #ifdef MINGW
  1158. GNUNET_memcpy (&taIeeeHeader->addr2, &dev->pl_mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
  1159. #else
  1160. taIeeeHeader->addr2 = dev->pl_mac;
  1161. #endif
  1162. }
  1163. #ifdef LINUX
  1164. /**
  1165. * Test if the given interface name really corresponds to a bluetooth
  1166. * device.
  1167. *
  1168. * @param iface name of the interface
  1169. * @return 0 on success, 1 on error
  1170. **** similar with the one from gnunet-helper-transport-wlan.c ****
  1171. */
  1172. static int
  1173. test_bluetooth_interface (const char *iface)
  1174. {
  1175. char strbuf[512];
  1176. struct stat sbuf;
  1177. int ret;
  1178. ret = snprintf (strbuf, sizeof (strbuf),
  1179. "/sys/class/bluetooth/%s/subsystem",
  1180. iface);
  1181. if ((ret < 0) || (ret >= sizeof (strbuf)) || (0 != stat (strbuf, &sbuf)))
  1182. {
  1183. fprintf (stderr,
  1184. "Did not find 802.15.1 interface `%s'. Exiting.\n",
  1185. iface);
  1186. exit (1);
  1187. }
  1188. return 0;
  1189. }
  1190. #endif
  1191. /**
  1192. * Test incoming packets mac for being our own.
  1193. *
  1194. * @param taIeeeHeader buffer of the packet
  1195. * @param dev the Hardware_Infos struct
  1196. * @return 0 if mac belongs to us, 1 if mac is for another target
  1197. *
  1198. **** same as the one from gnunet-helper-transport-wlan.c ****
  1199. */
  1200. static int
  1201. mac_test (const struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *taIeeeHeader,
  1202. const struct HardwareInfos *dev)
  1203. {
  1204. static struct GNUNET_TRANSPORT_WLAN_MacAddress all_zeros;
  1205. if ( (0 == memcmp (&taIeeeHeader->addr3, &all_zeros, MAC_ADDR_SIZE)) ||
  1206. (0 == memcmp (&taIeeeHeader->addr1, &all_zeros, MAC_ADDR_SIZE)) )
  1207. return 0; /* some drivers set no Macs, then assume it is all for us! */
  1208. if (0 != memcmp (&taIeeeHeader->addr3, &mac_bssid_gnunet, MAC_ADDR_SIZE))
  1209. return 1; /* not a GNUnet ad-hoc package */
  1210. if ( (0 == memcmp (&taIeeeHeader->addr1, &dev->pl_mac, MAC_ADDR_SIZE)) ||
  1211. (0 == memcmp (&taIeeeHeader->addr1, &bc_all_mac, MAC_ADDR_SIZE)) )
  1212. return 0; /* for us, or broadcast */
  1213. return 1; /* not for us */
  1214. }
  1215. /**
  1216. * Process data from the stdin. Takes the message, forces the sender MAC to be correct
  1217. * and puts it into our buffer for transmission to the receiver.
  1218. *
  1219. * @param cls pointer to the device struct ('struct HardwareInfos*')
  1220. * @param hdr pointer to the start of the packet
  1221. *
  1222. **** same as the one from gnunet-helper-transport-wlan.c ****
  1223. */
  1224. static void
  1225. stdin_send_hw (void *cls, const struct GNUNET_MessageHeader *hdr)
  1226. {
  1227. struct HardwareInfos *dev = cls;
  1228. const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *header;
  1229. struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *blueheader;
  1230. size_t sendsize;
  1231. sendsize = ntohs (hdr->size);
  1232. if ( (sendsize <
  1233. sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage)) ||
  1234. (GNUNET_MESSAGE_TYPE_WLAN_DATA_TO_HELPER != ntohs (hdr->type)) )
  1235. {
  1236. fprintf (stderr, "Received malformed message\n");
  1237. exit (1);
  1238. }
  1239. sendsize -= (sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage) -
  1240. sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame));
  1241. if (MAXLINE < sendsize)
  1242. {
  1243. fprintf (stderr, "Packet too big for buffer\n");
  1244. exit (1);
  1245. }
  1246. header = (const struct GNUNET_TRANSPORT_WLAN_RadiotapSendMessage *) hdr;
  1247. GNUNET_memcpy (&write_pout.buf, &header->frame, sendsize);
  1248. blueheader = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *) &write_pout.buf;
  1249. /* payload contains MAC address, but we don't trust it, so we'll
  1250. * overwrite it with OUR MAC address to prevent mischief */
  1251. mac_set (blueheader, dev);
  1252. GNUNET_memcpy (&blueheader->addr1, &header->frame.addr1,
  1253. sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
  1254. write_pout.size = sendsize;
  1255. }
  1256. #ifdef LINUX
  1257. /**
  1258. * Broadcast a HELLO message for peer discovery
  1259. *
  1260. * @param dev pointer to the device struct
  1261. * @param dev pointer to the socket which was added to the set
  1262. * @return 0 on success
  1263. */
  1264. static int
  1265. send_broadcast (struct HardwareInfos *dev, int *sendsocket)
  1266. {
  1267. int new_device = 0;
  1268. int loops = 0;
  1269. search_for_devices:
  1270. if ((neighbours.size == neighbours.pos && new_device == 1) || neighbours.size == 0)
  1271. {
  1272. inquiry_devices: //skip the conditions and force a inquiry for new devices
  1273. {
  1274. /**
  1275. * It means that I sent HELLO messages to all the devices from the list and I should search
  1276. * for new ones or that this is the first time when I do a search.
  1277. */
  1278. inquiry_info *devices = NULL;
  1279. int i, responses, max_responses = MAX_PORTS;
  1280. /* sanity checks */
  1281. if (neighbours.size >= MAX_PORTS)
  1282. {
  1283. fprintf (stderr, "%.*s reached the top limit for the discovarable devices\n", IFNAMSIZ, dev->iface);
  1284. return 2;
  1285. }
  1286. /* Get the device id */
  1287. if (neighbours.dev_id == -1)
  1288. {
  1289. char addr[19] = { 0 }; //the device MAC address
  1290. ba2str ((bdaddr_t *) &dev->pl_mac, addr);
  1291. neighbours.dev_id = hci_devid (addr);
  1292. if (neighbours.dev_id < 0)
  1293. {
  1294. fprintf (stderr, "Failed to get the device id for interface %.*s : %s\n", IFNAMSIZ,
  1295. dev->iface, strerror (errno));
  1296. return 1;
  1297. }
  1298. }
  1299. devices = malloc (max_responses * sizeof (inquiry_info));
  1300. if (devices == NULL)
  1301. {
  1302. fprintf (stderr, "Failed to allocate memory for inquiry info list on interface %.*s\n", IFNAMSIZ,
  1303. dev->iface);
  1304. return 1;
  1305. }
  1306. responses = hci_inquiry (neighbours.dev_id, 8, max_responses, NULL, &devices, IREQ_CACHE_FLUSH);
  1307. if (responses < 0)
  1308. {
  1309. fprintf (stderr, "Failed to inquiry on interface %.*s\n", IFNAMSIZ, dev->iface);
  1310. return 1;
  1311. }
  1312. fprintf (stderr, "LOG : Found %d devices\n", responses); //FIXME delete it after debugging stage
  1313. if (responses == 0)
  1314. {
  1315. fprintf (stderr, "LOG : No devices discoverable\n");
  1316. return 1;
  1317. }
  1318. for (i = 0; i < responses; i++)
  1319. {
  1320. int j;
  1321. int found = 0;
  1322. /* sanity check */
  1323. if (i >= MAX_PORTS)
  1324. {
  1325. fprintf (stderr, "%.*s reached the top limit for the discoverable devices (after inquiry)\n", IFNAMSIZ,
  1326. dev->iface);
  1327. return 2;
  1328. }
  1329. /* Search if the address already exists on the list */
  1330. for (j = 0; j < neighbours.size; j++)
  1331. {
  1332. if (memcmp (&(devices + i)->bdaddr, &(neighbours.devices[j]), sizeof (bdaddr_t)) == 0)
  1333. {
  1334. found = 1;
  1335. fprintf (stderr, "LOG : the device already exists on the list\n"); //FIXME debugging message
  1336. break;
  1337. }
  1338. }
  1339. if (found == 0)
  1340. {
  1341. char addr[19] = { 0 };
  1342. ba2str (&(devices +i)->bdaddr, addr);
  1343. fprintf (stderr, "LOG : %s was added to the list\n", addr); //FIXME debugging message
  1344. GNUNET_memcpy (&(neighbours.devices[neighbours.size++]), &(devices + i)->bdaddr, sizeof (bdaddr_t));
  1345. }
  1346. }
  1347. free (devices);
  1348. }
  1349. }
  1350. int connection_successful = 0;
  1351. struct sockaddr_rc addr_rc = { 0 };
  1352. int errno_copy = 0;
  1353. addr_rc.rc_family = AF_BLUETOOTH;
  1354. /* Try to connect to a new device from the list */
  1355. while (neighbours.pos < neighbours.size)
  1356. {
  1357. /* Check if we are already connected to this device */
  1358. if (neighbours.fds[neighbours.pos] == -1)
  1359. {
  1360. memset (&addr_rc.rc_bdaddr, 0, sizeof (addr_rc.rc_bdaddr));
  1361. GNUNET_memcpy (&addr_rc.rc_bdaddr, &(neighbours.devices[neighbours.pos]), sizeof (addr_rc.rc_bdaddr));
  1362. addr_rc.rc_channel = get_channel (dev, addr_rc.rc_bdaddr);
  1363. *sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
  1364. if ( (-1 < *sendsocket) &&
  1365. (0 == connect (*sendsocket,
  1366. (struct sockaddr *) &addr_rc,
  1367. sizeof (addr_rc))) )
  1368. {
  1369. neighbours.fds[neighbours.pos++] = *sendsocket;
  1370. connection_successful = 1;
  1371. char addr[19] = { 0 };
  1372. ba2str (&(neighbours.devices[neighbours.pos - 1]), addr);
  1373. fprintf (stderr, "LOG : Connected to %s\n", addr);
  1374. break;
  1375. }
  1376. else
  1377. {
  1378. char addr[19] = { 0 };
  1379. errno_copy = errno; //Save a copy for later
  1380. if (-1 != *sendsocket)
  1381. {
  1382. (void) close (*sendsocket);
  1383. *sendsocket = -1;
  1384. }
  1385. ba2str (&(neighbours.devices[neighbours.pos]), addr);
  1386. fprintf (stderr,
  1387. "LOG : Couldn't connect on device %s, error : %s\n",
  1388. addr,
  1389. strerror (errno));
  1390. if (errno != ECONNREFUSED) //FIXME be sure that this works
  1391. {
  1392. fprintf (stderr, "LOG : Removes %d device from the list\n", neighbours.pos);
  1393. /* Remove the device from the list */
  1394. GNUNET_memcpy (&neighbours.devices[neighbours.pos], &neighbours.devices[neighbours.size - 1], sizeof (bdaddr_t));
  1395. memset (&neighbours.devices[neighbours.size - 1], 0, sizeof (bdaddr_t));
  1396. neighbours.fds[neighbours.pos] = neighbours.fds[neighbours.size - 1];
  1397. neighbours.fds[neighbours.size - 1] = -1;
  1398. neighbours.size -= 1;
  1399. }
  1400. neighbours.pos += 1;
  1401. if (neighbours.pos >= neighbours.size)
  1402. neighbours.pos = 0;
  1403. loops += 1;
  1404. if (loops == MAX_LOOPS) //don't get stuck trying to connect to one device
  1405. return 1;
  1406. }
  1407. }
  1408. else
  1409. {
  1410. fprintf (stderr, "LOG : Search for a new device\n"); //FIXME debugging message
  1411. neighbours.pos += 1;
  1412. }
  1413. }
  1414. /* Cycle on the list */
  1415. if (neighbours.pos == neighbours.size)
  1416. {
  1417. neighbours.pos = 0;
  1418. searching_devices_count += 1;
  1419. if (searching_devices_count == MAX_LOOPS)
  1420. {
  1421. fprintf (stderr, "LOG : Force to inquiry for new devices\n");
  1422. searching_devices_count = 0;
  1423. goto inquiry_devices;
  1424. }
  1425. }
  1426. /* If a new device wasn't found, search an old one */
  1427. if (connection_successful == 0)
  1428. {
  1429. int loop_check = neighbours.pos;
  1430. while (neighbours.fds[neighbours.pos] == -1)
  1431. {
  1432. if (neighbours.pos == neighbours.size)
  1433. neighbours.pos = 0;
  1434. if (neighbours.pos == loop_check)
  1435. {
  1436. if (errno_copy == ECONNREFUSED)
  1437. {
  1438. fprintf (stderr, "LOG : No device found. Go back and search again\n"); //FIXME debugging message
  1439. new_device = 1;
  1440. loops += 1;
  1441. goto search_for_devices;
  1442. }
  1443. else
  1444. {
  1445. return 1; // Skip the broadcast message
  1446. }
  1447. }
  1448. neighbours.pos += 1;
  1449. }
  1450. *sendsocket = neighbours.fds[neighbours.pos++];
  1451. }
  1452. return 0;
  1453. }
  1454. #endif
  1455. /**
  1456. * Main function of the helper. This code accesses a bluetooth interface
  1457. * forwards traffic in both directions between the bluetooth interface and
  1458. * stdin/stdout of this process. Error messages are written to stderr.
  1459. *
  1460. * @param argc number of arguments, must be 2
  1461. * @param argv arguments only argument is the name of the interface (i.e. 'hci0')
  1462. * @return 0 on success (never happens, as we don't return unless aborted), 1 on error
  1463. *
  1464. **** similar to gnunet-helper-transport-wlan.c ****
  1465. */
  1466. int
  1467. main (int argc, char *argv[])
  1468. {
  1469. #ifdef LINUX
  1470. struct HardwareInfos dev;
  1471. char readbuf[MAXLINE];
  1472. int maxfd;
  1473. fd_set rfds;
  1474. fd_set wfds;
  1475. int stdin_open;
  1476. struct MessageStreamTokenizer *stdin_mst;
  1477. int raw_eno, i;
  1478. int crt_rfds = 0, rfds_list[MAX_PORTS];
  1479. int broadcast, sendsocket;
  1480. /* Assert privs so we can modify the firewall rules! */
  1481. {
  1482. #ifdef HAVE_SETRESUID
  1483. uid_t uid = getuid ();
  1484. if (0 != setresuid (uid, 0, 0))
  1485. {
  1486. fprintf (stderr,
  1487. "Failed to setresuid to root: %s\n",
  1488. strerror (errno));
  1489. return 254;
  1490. }
  1491. #else
  1492. if (0 != seteuid (0))
  1493. {
  1494. fprintf (stderr,
  1495. "Failed to seteuid back to root: %s\n", strerror (errno));
  1496. return 254;
  1497. }
  1498. #endif
  1499. }
  1500. /* Make use of SGID capabilities on POSIX */
  1501. memset (&dev, 0, sizeof (dev));
  1502. dev.fd_rfcomm = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
  1503. raw_eno = errno; /* remember for later */
  1504. /* Now that we've dropped root rights, we can do error checking */
  1505. if (2 != argc)
  1506. {
  1507. fprintf (stderr, "You must specify the name of the interface as the first \
  1508. and only argument to this program.\n");
  1509. if (-1 != dev.fd_rfcomm)
  1510. (void) close (dev.fd_rfcomm);
  1511. return 1;
  1512. }
  1513. if (-1 == dev.fd_rfcomm)
  1514. {
  1515. fprintf (stderr, "Failed to create a RFCOMM socket: %s\n", strerror (raw_eno));
  1516. return 1;
  1517. }
  1518. if (dev.fd_rfcomm >= FD_SETSIZE)
  1519. {
  1520. fprintf (stderr, "File descriptor too large for select (%d > %d)\n",
  1521. dev.fd_rfcomm, FD_SETSIZE);
  1522. (void) close (dev.fd_rfcomm);
  1523. return 1;
  1524. }
  1525. if (0 != test_bluetooth_interface (argv[1]))
  1526. {
  1527. (void) close (dev.fd_rfcomm);
  1528. return 1;
  1529. }
  1530. strncpy (dev.iface, argv[1], IFNAMSIZ);
  1531. if (0 != open_device (&dev))
  1532. {
  1533. (void) close (dev.fd_rfcomm);
  1534. return 1;
  1535. }
  1536. /* Drop privs */
  1537. {
  1538. uid_t uid = getuid ();
  1539. #ifdef HAVE_SETRESUID
  1540. if (0 != setresuid (uid, uid, uid))
  1541. {
  1542. fprintf (stderr, "Failed to setresuid: %s\n", strerror (errno));
  1543. if (-1 != dev.fd_rfcomm)
  1544. (void) close (dev.fd_rfcomm);
  1545. return 1;
  1546. }
  1547. #else
  1548. if (0 != (setuid (uid) | seteuid (uid)))
  1549. {
  1550. fprintf (stderr, "Failed to setuid: %s\n", strerror (errno));
  1551. if (-1 != dev.fd_rfcomm)
  1552. (void) close (dev.fd_rfcomm);
  1553. return 1;
  1554. }
  1555. #endif
  1556. }
  1557. /* Send MAC address of the bluetooth interface to STDOUT first */
  1558. {
  1559. struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
  1560. macmsg.hdr.size = htons (sizeof (macmsg));
  1561. macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
  1562. GNUNET_memcpy (&macmsg.mac, &dev.pl_mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress));
  1563. GNUNET_memcpy (write_std.buf, &macmsg, sizeof (macmsg));
  1564. write_std.size = sizeof (macmsg);
  1565. }
  1566. stdin_mst = mst_create (&stdin_send_hw, &dev);
  1567. stdin_open = 1;
  1568. /**
  1569. * TODO : I should make the time out of a mac endpoint smaller and check if the rate
  1570. * from get_wlan_header (plugin_transport_bluetooth.c) is correct.
  1571. */
  1572. while (1)
  1573. {
  1574. maxfd = -1;
  1575. broadcast = 0;
  1576. sendsocket = -1;
  1577. FD_ZERO (&rfds);
  1578. if ((0 == write_pout.size) && (1 == stdin_open))
  1579. {
  1580. FD_SET (STDIN_FILENO, &rfds);
  1581. maxfd = MAX (maxfd, STDIN_FILENO);
  1582. }
  1583. if (0 == write_std.size)
  1584. {
  1585. FD_SET (dev.fd_rfcomm, &rfds);
  1586. maxfd = MAX (maxfd, dev.fd_rfcomm);
  1587. }
  1588. for (i = 0; i < crt_rfds; i++) // it can receive messages from multiple devices
  1589. {
  1590. FD_SET (rfds_list[i], &rfds);
  1591. maxfd = MAX (maxfd, rfds_list[i]);
  1592. }
  1593. FD_ZERO (&wfds);
  1594. if (0 < write_std.size)
  1595. {
  1596. FD_SET (STDOUT_FILENO, &wfds);
  1597. maxfd = MAX (maxfd, STDOUT_FILENO);
  1598. }
  1599. if (0 < write_pout.size) //it can send messages only to one device per loop
  1600. {
  1601. struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *frame;
  1602. /* Get the destination address */
  1603. frame = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *) write_pout.buf;
  1604. if (memcmp (&frame->addr1, &dev.pl_mac,
  1605. sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
  1606. {
  1607. broadcast = 1;
  1608. memset (&write_pout, 0, sizeof (write_pout)); //clear the buffer
  1609. }
  1610. else if (memcmp (&frame->addr1, &broadcast_address,
  1611. sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0)
  1612. {
  1613. fprintf (stderr, "LOG : %s has a broadcast message (pos %d, size %d)\n", dev.iface, neighbours.pos, neighbours.size); //FIXME: debugging message
  1614. if (send_broadcast(&dev, &sendsocket) != 0) //if the searching wasn't successful don't get stuck on the select stage
  1615. {
  1616. broadcast = 1;
  1617. memset (&write_pout, 0, sizeof (write_pout)); //remove the message
  1618. fprintf (stderr, "LOG : Skipping the broadcast message (pos %d, size %d)\n", neighbours.pos, neighbours.size);
  1619. }
  1620. else
  1621. {
  1622. FD_SET (sendsocket, &wfds);
  1623. maxfd = MAX (maxfd, sendsocket);
  1624. }
  1625. }
  1626. else
  1627. {
  1628. int found = 0;
  1629. int pos = 0;
  1630. /* Search if the address already exists on the list */
  1631. for (i = 0; i < neighbours.size; i++)
  1632. {
  1633. if (memcmp (&frame->addr1, &(neighbours.devices[i]), sizeof (bdaddr_t)) == 0)
  1634. {
  1635. pos = i;
  1636. if (neighbours.fds[i] != -1)
  1637. {
  1638. found = 1; //save the position where it was found
  1639. FD_SET (neighbours.fds[i], &wfds);
  1640. maxfd = MAX (maxfd, neighbours.fds[i]);
  1641. sendsocket = neighbours.fds[i];
  1642. fprintf (stderr, "LOG: the address was found in the list\n");
  1643. break;
  1644. }
  1645. }
  1646. }
  1647. if (found == 0)
  1648. {
  1649. int status;
  1650. struct sockaddr_rc addr = { 0 };
  1651. fprintf (stderr, "LOG : %s has a new message for %.2X:%.2X:%.2X:%.2X:%.2X:%.2X which isn't on the broadcast list\n", dev.iface,
  1652. frame->addr1.mac[5], frame->addr1.mac[4], frame->addr1.mac[3],
  1653. frame->addr1.mac[2], frame->addr1.mac[1], frame->addr1.mac[0]); //FIXME: debugging message
  1654. sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
  1655. if (sendsocket < 0)
  1656. {
  1657. fprintf (stderr, "Failed to create a RFCOMM socket (sending stage): %s\n",
  1658. strerror (errno));
  1659. return -1;
  1660. }
  1661. GNUNET_memcpy (&addr.rc_bdaddr, &frame->addr1, sizeof (bdaddr_t));
  1662. addr.rc_family = AF_BLUETOOTH;
  1663. addr.rc_channel = get_channel (&dev, addr.rc_bdaddr);
  1664. int tries = 0;
  1665. connect_retry:
  1666. status = connect (sendsocket, (struct sockaddr *) &addr, sizeof (addr));
  1667. if (0 != status && errno != EAGAIN)
  1668. {
  1669. if (errno == ECONNREFUSED && tries < 2)
  1670. {
  1671. fprintf (stderr, "LOG : %.*s failed to connect. Trying again!\n", IFNAMSIZ, dev.iface);
  1672. tries++;
  1673. goto connect_retry;
  1674. }
  1675. else if (errno == EBADF)
  1676. {
  1677. fprintf (stderr, "LOG : %s failed to connect : %s. Skip it!\n", dev.iface, strerror (errno));
  1678. memset (&write_pout, 0, sizeof (write_pout));
  1679. broadcast = 1;
  1680. }
  1681. else
  1682. {
  1683. fprintf (stderr, "LOG : %s failed to connect : %s. Try again later!\n", dev.iface, strerror (errno));
  1684. memset (&write_pout, 0, sizeof (write_pout));
  1685. broadcast = 1;
  1686. }
  1687. }
  1688. else
  1689. {
  1690. FD_SET (sendsocket, &wfds);
  1691. maxfd = MAX (maxfd, sendsocket);
  1692. fprintf (stderr, "LOG : Connection successful\n");
  1693. if (pos != 0) // save the socket
  1694. {
  1695. neighbours.fds[pos] = sendsocket;
  1696. }
  1697. else
  1698. {
  1699. /* Add the new device to the discovered devices list */
  1700. if (neighbours.size < MAX_PORTS)
  1701. {
  1702. neighbours.fds[neighbours.size] = sendsocket;
  1703. GNUNET_memcpy (&(neighbours.devices[neighbours.size++]), &addr.rc_bdaddr, sizeof (bdaddr_t));
  1704. }
  1705. else
  1706. {
  1707. fprintf (stderr, "The top limit for the discovarable devices' list was reached\n");
  1708. }
  1709. }
  1710. }
  1711. }
  1712. }
  1713. }
  1714. if (broadcast == 0)
  1715. {
  1716. /* Select a fd which is ready for action :) */
  1717. {
  1718. int retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL);
  1719. if ((-1 == retval) && (EINTR == errno))
  1720. continue;
  1721. if (0 > retval && errno != EBADF) // we handle BADF errors later
  1722. {
  1723. fprintf (stderr, "select failed: %s\n", strerror (errno));
  1724. break;
  1725. }
  1726. }
  1727. if (FD_ISSET (STDOUT_FILENO , &wfds))
  1728. {
  1729. ssize_t ret =
  1730. write (STDOUT_FILENO, write_std.buf + write_std.pos,
  1731. write_std.size - write_std.pos);
  1732. if (0 > ret)
  1733. {
  1734. fprintf (stderr, "Failed to write to STDOUT: %s\n", strerror (errno));
  1735. break;
  1736. }
  1737. write_std.pos += ret;
  1738. if (write_std.pos == write_std.size)
  1739. {
  1740. write_std.pos = 0;
  1741. write_std.size = 0;
  1742. }
  1743. fprintf (stderr, "LOG : %s sends a message to STDOUT\n", dev.iface); //FIXME: debugging message
  1744. }
  1745. if (-1 != sendsocket)
  1746. {
  1747. if (FD_ISSET (sendsocket , &wfds))
  1748. {
  1749. ssize_t ret = write (sendsocket,
  1750. write_pout.buf + write_std.pos,
  1751. write_pout.size - write_pout.pos);
  1752. if (0 > ret) //FIXME should I first check the error type?
  1753. {
  1754. fprintf (stderr, "Failed to write to bluetooth device: %s. Closing the socket!\n",
  1755. strerror (errno));
  1756. for (i = 0; i < neighbours.size; i++)
  1757. {
  1758. if (neighbours.fds[i] == sendsocket)
  1759. {
  1760. (void) close(sendsocket);
  1761. neighbours.fds[i] = -1;
  1762. break;
  1763. }
  1764. }
  1765. /* Remove the message */
  1766. memset (&write_pout.buf + write_std.pos, 0, (write_pout.size - write_pout.pos));
  1767. write_pout.pos = 0 ;
  1768. write_pout.size = 0;
  1769. }
  1770. else
  1771. {
  1772. write_pout.pos += ret;
  1773. if ((write_pout.pos != write_pout.size) && (0 != ret))
  1774. {
  1775. /* We should not get partial sends with packet-oriented devices... */
  1776. fprintf (stderr, "Write error, partial send: %u/%u\n",
  1777. (unsigned int) write_pout.pos,
  1778. (unsigned int) write_pout.size);
  1779. break;
  1780. }
  1781. if (write_pout.pos == write_pout.size)
  1782. {
  1783. write_pout.pos = 0;
  1784. write_pout.size = 0;
  1785. }
  1786. fprintf (stderr, "LOG : %s sends a message to a DEVICE\n", dev.iface); //FIXME: debugging message
  1787. }
  1788. }
  1789. }
  1790. for (i = 0; i <= maxfd; i++)
  1791. {
  1792. if (FD_ISSET (i, &rfds))
  1793. {
  1794. if (i == STDIN_FILENO)
  1795. {
  1796. ssize_t ret =
  1797. read (i, readbuf, sizeof (readbuf));
  1798. if (0 > ret)
  1799. {
  1800. fprintf (stderr,
  1801. "Read error from STDIN: %s\n",
  1802. strerror (errno));
  1803. break;
  1804. }
  1805. if (0 == ret)
  1806. {
  1807. /* stop reading... */
  1808. stdin_open = 0;
  1809. }
  1810. else
  1811. {
  1812. mst_receive (stdin_mst, readbuf, ret);
  1813. fprintf (stderr, "LOG : %s receives a message from STDIN\n", dev.iface); //FIXME: debugging message
  1814. }
  1815. }
  1816. else if (i == dev.fd_rfcomm)
  1817. {
  1818. int readsocket;
  1819. struct sockaddr_rc addr = { 0 };
  1820. unsigned int opt = sizeof (addr);
  1821. readsocket = accept (dev.fd_rfcomm, (struct sockaddr *) &addr, &opt);
  1822. fprintf(stderr, "LOG : %s accepts a message\n", dev.iface); //FIXME: debugging message
  1823. if (readsocket == -1)
  1824. {
  1825. fprintf (stderr, "Failed to accept a connection on interface: %.*s\n", IFNAMSIZ,
  1826. strerror (errno));
  1827. break;
  1828. }
  1829. else
  1830. {
  1831. FD_SET (readsocket, &rfds);
  1832. maxfd = MAX (maxfd, readsocket);
  1833. if (crt_rfds < MAX_PORTS)
  1834. rfds_list[crt_rfds++] = readsocket;
  1835. else
  1836. {
  1837. fprintf (stderr, "The limit for the read file descriptors list was \
  1838. reached\n");
  1839. break;
  1840. }
  1841. }
  1842. }
  1843. else
  1844. {
  1845. struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm;
  1846. ssize_t ret;
  1847. fprintf (stderr, "LOG : %s reads something from the socket\n", dev.iface);//FIXME : debugging message
  1848. rrm = (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) write_std.buf;
  1849. ret =
  1850. read_from_the_socket ((void *)&i, (unsigned char *) &rrm->frame,
  1851. sizeof (write_std.buf)
  1852. - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
  1853. + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame),
  1854. rrm);
  1855. if (0 >= ret)
  1856. {
  1857. int j;
  1858. FD_CLR (i, &rfds);
  1859. close (i);
  1860. /* Remove the socket from the list */
  1861. for (j = 0; j < crt_rfds; j++)
  1862. {
  1863. if (rfds_list[j] == i)
  1864. {
  1865. rfds_list[j] ^= rfds_list[crt_rfds - 1];
  1866. rfds_list[crt_rfds - 1] ^= rfds_list[j];
  1867. rfds_list[j] ^= rfds_list[crt_rfds - 1];
  1868. crt_rfds -= 1;
  1869. break;
  1870. }
  1871. }
  1872. fprintf (stderr, "Read error from raw socket: %s\n", strerror (errno));
  1873. break;
  1874. }
  1875. if ((0 < ret) && (0 == mac_test (&rrm->frame, &dev)))
  1876. {
  1877. write_std.size = ret
  1878. + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
  1879. - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame);
  1880. rrm->header.size = htons (write_std.size);
  1881. rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
  1882. }
  1883. }
  1884. }
  1885. }
  1886. }
  1887. }
  1888. /* Error handling, try to clean up a bit at least */
  1889. mst_destroy (stdin_mst);
  1890. stdin_mst = NULL;
  1891. sdp_close (dev.session);
  1892. (void) close (dev.fd_rfcomm);
  1893. if (-1 != sendsocket)
  1894. (void) close (sendsocket);
  1895. for (i = 0; i < crt_rfds; i++)
  1896. (void) close (rfds_list[i]);
  1897. for (i = 0; i < neighbours.size; i++)
  1898. (void) close (neighbours.fds[i]);
  1899. #else
  1900. struct HardwareInfos dev;
  1901. struct GNUNET_NETWORK_Handle *sendsocket;
  1902. struct GNUNET_NETWORK_FDSet *rfds;
  1903. struct GNUNET_NETWORK_FDSet *wfds;
  1904. struct GNUNET_NETWORK_Handle *rfds_list[MAX_PORTS];
  1905. char readbuf[MAXLINE] = { 0 };
  1906. SOCKADDR_BTH acc_addr = { 0 };
  1907. int addr_len = sizeof (SOCKADDR_BTH);
  1908. int broadcast, i, stdin_open, crt_rfds = 0;
  1909. HANDLE stdin_handle = GetStdHandle (STD_INPUT_HANDLE);
  1910. HANDLE stdout_handle = GetStdHandle (STD_OUTPUT_HANDLE);
  1911. struct MessageStreamTokenizer *stdin_mst;
  1912. /* check the handles */
  1913. if (stdin_handle == INVALID_HANDLE_VALUE)
  1914. {
  1915. fprintf (stderr, "Failed to get the stdin handle\n");
  1916. ExitProcess (2);
  1917. }
  1918. if (stdout_handle == INVALID_HANDLE_VALUE)
  1919. {
  1920. fprintf (stderr, "Failed to get the stdout handle\n");
  1921. ExitProcess (2);
  1922. }
  1923. /* initialize windows sockets */
  1924. initialize_windows_sockets();
  1925. // /* test bluetooth socket family support */ --> it return false because the GNUNET_NETWORK_test_pf should also receive the type of socket (BTHPROTO_RFCOMM)
  1926. // if (GNUNET_NETWORK_test_pf (AF_BTH) != GNUNET_OK)
  1927. // {
  1928. // fprintf (stderr, "AF_BTH family is not supported\n");
  1929. // ExitProcess (2);
  1930. // }
  1931. /* create the socket */
  1932. dev.handle = GNUNET_NETWORK_socket_create (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
  1933. if (dev.handle == NULL)
  1934. {
  1935. fprintf (stderr, "Failed to create RFCOMM socket: ");
  1936. print_last_error();
  1937. ExitProcess (2);
  1938. }
  1939. if (open_device (&dev) == -1)
  1940. {
  1941. fprintf (stderr, "Failed to open the device\n");
  1942. print_last_error();
  1943. if (GNUNET_NETWORK_socket_close (dev.handle) != GNUNET_OK)
  1944. {
  1945. fprintf (stderr, "Failed to close the socket!\n");
  1946. print_last_error();
  1947. }
  1948. ExitProcess (2);
  1949. }
  1950. if (GNUNET_OK != GNUNET_NETWORK_socket_set_blocking (dev.handle, 1) )
  1951. {
  1952. fprintf (stderr, "Failed to change the socket mode\n");
  1953. ExitProcess (2);
  1954. }
  1955. memset (&write_std, 0, sizeof (write_std));
  1956. memset (&write_pout, 0, sizeof (write_pout));
  1957. stdin_open = 1;
  1958. rfds = GNUNET_NETWORK_fdset_create ();
  1959. wfds = GNUNET_NETWORK_fdset_create ();
  1960. /* Send MAC address of the bluetooth interface to STDOUT first */
  1961. {
  1962. struct GNUNET_TRANSPORT_WLAN_HelperControlMessage macmsg;
  1963. macmsg.hdr.size = htons (sizeof (macmsg));
  1964. macmsg.hdr.type = htons (GNUNET_MESSAGE_TYPE_WLAN_HELPER_CONTROL);
  1965. GNUNET_memcpy (&macmsg.mac, &dev.pl_mac, sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress_Copy));
  1966. GNUNET_memcpy (write_std.buf, &macmsg, sizeof (macmsg));
  1967. write_std.size = sizeof (macmsg);
  1968. }
  1969. stdin_mst = mst_create (&stdin_send_hw, &dev);
  1970. stdin_open = 1;
  1971. int pos = 0;
  1972. int stdin_pos = -1;
  1973. int stdout_pos = -1;
  1974. while (1)
  1975. {
  1976. broadcast = 0;
  1977. pos = 0;
  1978. stdin_pos = -1;
  1979. stdout_pos = -1;
  1980. sendsocket = NULL; //FIXME ???memleaks
  1981. GNUNET_NETWORK_fdset_zero (rfds);
  1982. if ((0 == write_pout.size) && (1 == stdin_open))
  1983. {
  1984. stdin_pos = pos;
  1985. pos +=1;
  1986. GNUNET_NETWORK_fdset_handle_set (rfds, (struct GNUNET_DISK_FileHandle*) &stdin_handle);
  1987. }
  1988. if (0 == write_std.size)
  1989. {
  1990. pos += 1;
  1991. GNUNET_NETWORK_fdset_set (rfds, dev.handle);
  1992. }
  1993. for (i = 0; i < crt_rfds; i++)
  1994. {
  1995. pos += 1;
  1996. GNUNET_NETWORK_fdset_set (rfds, rfds_list[i]);
  1997. }
  1998. GNUNET_NETWORK_fdset_zero (wfds);
  1999. if (0 < write_std.size)
  2000. {
  2001. stdout_pos = pos;
  2002. GNUNET_NETWORK_fdset_handle_set (wfds, (struct GNUNET_DISK_FileHandle*) &stdout_handle);
  2003. // printf ("%s\n", write_std.buf);
  2004. // memset (write_std.buf, 0, write_std.size);
  2005. // write_std.size = 0;
  2006. }
  2007. if (0 < write_pout.size)
  2008. {
  2009. if (strcmp (argv[1], "ff:ff:ff:ff:ff:ff") == 0) {
  2010. fprintf(stderr, "LOG: BROADCAST! Skipping the message\n");
  2011. // skip the message
  2012. broadcast = 1;
  2013. memset (write_pout.buf, 0, write_pout.size);
  2014. write_pout.size = 0;
  2015. }
  2016. else
  2017. {
  2018. SOCKADDR_BTH addr;
  2019. fprintf (stderr, "LOG : has a new message for %s\n", argv[1]);
  2020. sendsocket = GNUNET_NETWORK_socket_create (AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM);
  2021. if (sendsocket == NULL)
  2022. {
  2023. fprintf (stderr, "Failed to create RFCOMM socket: \n");
  2024. print_last_error();
  2025. ExitProcess (2);
  2026. }
  2027. memset (&addr, 0, sizeof (addr));
  2028. //addr.addressFamily = AF_BTH;
  2029. if (SOCKET_ERROR ==
  2030. WSAStringToAddress (argv[1], AF_BTH, NULL, (LPSOCKADDR) &addr, &addr_len))
  2031. {
  2032. fprintf (stderr, "Failed to translate the address: ");
  2033. print_last_error();
  2034. ExitProcess ( 2 ) ;
  2035. }
  2036. addr.port = get_channel (argv[1]);
  2037. if (addr.port == -1)
  2038. {
  2039. fprintf (stderr, "Couldn't find the sdp service for the address: %s\n", argv[1]);
  2040. memset (write_pout.buf, 0, write_pout.size);
  2041. write_pout.size = 0;
  2042. broadcast = 1; //skipping the select part
  2043. }
  2044. else
  2045. {
  2046. if (GNUNET_OK != GNUNET_NETWORK_socket_connect (sendsocket, (LPSOCKADDR)&addr, addr_len))
  2047. {
  2048. fprintf (stderr, "Failed to connect: ");
  2049. print_last_error();
  2050. ExitProcess (2);
  2051. }
  2052. if (GNUNET_OK != GNUNET_NETWORK_socket_set_blocking (sendsocket, 1) )
  2053. {
  2054. fprintf (stderr, "Failed to change the socket mode\n");
  2055. ExitProcess (2);
  2056. }
  2057. GNUNET_NETWORK_fdset_set (wfds, sendsocket);
  2058. }
  2059. }
  2060. }
  2061. if (broadcast == 0)
  2062. {
  2063. int retval = GNUNET_NETWORK_socket_select (rfds, wfds, NULL, GNUNET_TIME_relative_get_forever_());
  2064. if (retval < 0)
  2065. {
  2066. fprintf (stderr, "Select error\n");
  2067. ExitProcess (2);
  2068. }
  2069. //if (GNUNET_NETWORK_fdset_isset (wfds, (struct GNUNET_NETWORK_Handle*)&stdout_handle))
  2070. if (retval == stdout_pos)
  2071. {
  2072. fprintf(stderr, "LOG : sends a message to STDOUT\n"); //FIXME: debugging message
  2073. //ssize_t ret;
  2074. //ret = GNUNET_NETWORK_socket_send ((struct GNUNET_NETWORK_Handle *)&stdout_handle, write_std.buf + write_std.pos, write_std.size - write_std.pos);
  2075. //ret = write (STDOUT_FILENO, write_std.buf + write_std.pos, write_std.size - write_std.pos);
  2076. DWORD ret;
  2077. if (FALSE == WriteFile (stdout_handle, write_std.buf + write_std.pos, write_std.size - write_std.pos, &ret, NULL))
  2078. {
  2079. fprintf (stderr, "Failed to write to STDOUT: ");
  2080. print_last_error();
  2081. break;
  2082. }
  2083. if (ret <= 0)
  2084. {
  2085. fprintf (stderr, "Failed to write to STDOUT\n");
  2086. ExitProcess (2);
  2087. }
  2088. write_std.pos += ret;
  2089. if (write_std.pos == write_std.size)
  2090. {
  2091. write_std.pos = 0;
  2092. write_std.size = 0;
  2093. }
  2094. }
  2095. if (sendsocket != NULL)
  2096. {
  2097. if (GNUNET_NETWORK_fdset_isset (wfds, sendsocket))
  2098. {
  2099. ssize_t ret;
  2100. ret = GNUNET_NETWORK_socket_send (sendsocket, write_pout.buf + write_pout.pos,
  2101. write_pout.size - write_pout.pos);
  2102. if (GNUNET_SYSERR == ret)
  2103. {
  2104. fprintf (stderr, "Failed to send to the socket. Closing the socket. Error: \n");
  2105. print_last_error();
  2106. if (GNUNET_NETWORK_socket_close (sendsocket) != GNUNET_OK)
  2107. {
  2108. fprintf (stderr, "Failed to close the sendsocket!\n");
  2109. print_last_error();
  2110. }
  2111. ExitProcess (2);
  2112. }
  2113. else
  2114. {
  2115. write_pout.pos += ret;
  2116. if ((write_pout.pos != write_pout.size) && (0 != ret))
  2117. {
  2118. /* we should not get partial sends with packet-oriented devices... */
  2119. fprintf (stderr, "Write error, partial send: %u/%u\n",
  2120. (unsigned int) write_pout.pos,
  2121. (unsigned int) write_pout.size);
  2122. break;
  2123. }
  2124. if (write_pout.pos == write_pout.size)
  2125. {
  2126. write_pout.pos = 0;
  2127. write_pout.size = 0;
  2128. }
  2129. fprintf(stderr, "LOG : sends a message to a DEVICE\n"); //FIXME: debugging message
  2130. }
  2131. }
  2132. }
  2133. //if (GNUNET_NETWORK_fdset_isset (rfds, (struct GNUNET_NETWORK_Handle*)&stdin_handle))
  2134. if (retval == stdin_pos)
  2135. {
  2136. //ssize_t ret;
  2137. //ret = GNUNET_NETWORK_socket_recv ((struct GNUNET_NETWORK_Handle *)&stdin_handle, readbuf, sizeof (write_pout.buf));
  2138. //ret = read (STDIN_FILENO, readbuf, sizeof (readbuf));
  2139. DWORD ret;
  2140. if (FALSE == ReadFile (stdin_handle, readbuf, sizeof (readbuf), &ret, NULL)) /* do nothing asynchronous */
  2141. {
  2142. fprintf (stderr, "Read error from STDIN: ");
  2143. print_last_error();
  2144. break;
  2145. }
  2146. if (0 == ret)
  2147. {
  2148. /* stop reading... */
  2149. stdin_open = 0;
  2150. } else {
  2151. mst_receive (stdin_mst, readbuf, ret);
  2152. fprintf (stderr, "LOG : receives a message from STDIN\n"); //FIXME: debugging message
  2153. }
  2154. }
  2155. else
  2156. if (GNUNET_NETWORK_fdset_isset (rfds, dev.handle))
  2157. {
  2158. fprintf (stderr, "LOG: accepting connection\n");
  2159. struct GNUNET_NETWORK_Handle *readsocket;
  2160. readsocket = GNUNET_NETWORK_socket_accept (dev.handle, (LPSOCKADDR)&acc_addr, &addr_len);
  2161. if (readsocket == NULL)
  2162. {
  2163. fprintf (stderr, "Accept error %d: ", GetLastError());
  2164. print_last_error();
  2165. ExitProcess (2);
  2166. }
  2167. else
  2168. {
  2169. if (GNUNET_OK != GNUNET_NETWORK_socket_set_blocking (readsocket, 1) )
  2170. {
  2171. fprintf (stderr, "Failed to change the socket mode\n");
  2172. ExitProcess (2);
  2173. }
  2174. GNUNET_NETWORK_fdset_set (rfds, readsocket);
  2175. if (crt_rfds < MAX_PORTS)
  2176. rfds_list[crt_rfds++] = readsocket;
  2177. else
  2178. {
  2179. fprintf (stderr, "The limit for the read file descriptors list was reached\n");
  2180. break;
  2181. }
  2182. }
  2183. }
  2184. else
  2185. for (i = 0; i < crt_rfds; i++)
  2186. {
  2187. if (GNUNET_NETWORK_fdset_isset (rfds, rfds_list[i]))
  2188. {
  2189. struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *rrm;
  2190. ssize_t ret;
  2191. fprintf (stderr, "LOG: reading something from the socket\n");//FIXME : debugging message
  2192. rrm = (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage *) write_std.buf;
  2193. ret = read_from_the_socket (rfds_list[i], (unsigned char *) &rrm->frame,
  2194. sizeof (write_std.buf)
  2195. - sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
  2196. + sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame),
  2197. rrm);
  2198. if (0 >= ret)
  2199. {
  2200. //TODO remove the socket from the list
  2201. if (GNUNET_NETWORK_socket_close (rfds_list[i]) != GNUNET_OK)
  2202. {
  2203. fprintf (stderr, "Failed to close the sendsocket!\n");
  2204. print_last_error();
  2205. }
  2206. fprintf (stderr, "Read error from raw socket: ");
  2207. print_last_error();
  2208. break;
  2209. }
  2210. if ((0 < ret) && (0 == mac_test (&rrm->frame, &dev)))
  2211. {
  2212. write_std.size = ret
  2213. + sizeof (struct GNUNET_TRANSPORT_WLAN_RadiotapReceiveMessage)
  2214. - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame);
  2215. rrm->header.size = htons (write_std.size);
  2216. rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER);
  2217. }
  2218. break;
  2219. }
  2220. }
  2221. }
  2222. }
  2223. mst_destroy (stdin_mst);
  2224. stdin_mst = NULL;
  2225. if (GNUNET_NETWORK_socket_close (dev.handle) != GNUNET_OK)
  2226. {
  2227. fprintf (stderr, "Failed to close the socket!\n");
  2228. print_last_error();
  2229. }
  2230. for (i = 0; i < crt_rfds; i++)
  2231. {
  2232. if (GNUNET_NETWORK_socket_close (rfds_list[i]) != GNUNET_OK)
  2233. {
  2234. fprintf (stderr, "Failed to close the socket!\n");
  2235. print_last_error();
  2236. }
  2237. }
  2238. WSACleanup();
  2239. #endif
  2240. return 1; /* we never exit 'normally' */
  2241. }