gnunet-helper-transport-bluetooth.c 68 KB

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