diff options
author | Claudiu Olteanu <claudiu@140774ce-b5e7-0310-ab8b-a85725594a96> | 2013-08-04 18:19:43 +0000 |
---|---|---|
committer | Claudiu Olteanu <claudiu@140774ce-b5e7-0310-ab8b-a85725594a96> | 2013-08-04 18:19:43 +0000 |
commit | b15362b6534debb8098d797cf4a847599b03228d (patch) | |
tree | f7891f1220509aa7fba9396d9e3aeeaa9477da90 /src/transport/gnunet-helper-transport-bluetooth.c | |
parent | 7725cd78de93ce9eefe51c2fa52ce1bd1b0ce636 (diff) | |
download | gnunet-b15362b6534debb8098d797cf4a847599b03228d.tar.gz gnunet-b15362b6534debb8098d797cf4a847599b03228d.zip |
Extending the testcases to use bluetooth
Diffstat (limited to 'src/transport/gnunet-helper-transport-bluetooth.c')
-rw-r--r-- | src/transport/gnunet-helper-transport-bluetooth.c | 168 |
1 files changed, 71 insertions, 97 deletions
diff --git a/src/transport/gnunet-helper-transport-bluetooth.c b/src/transport/gnunet-helper-transport-bluetooth.c index 48e63dd75..1f5add92d 100644 --- a/src/transport/gnunet-helper-transport-bluetooth.c +++ b/src/transport/gnunet-helper-transport-bluetooth.c | |||
@@ -54,9 +54,9 @@ | |||
54 | 54 | ||
55 | 55 | ||
56 | /** | 56 | /** |
57 | * Maximum number of loops without inquiring for a new devices. | 57 | * Maximum number of loops without inquiring for new devices. |
58 | */ | 58 | */ |
59 | #define MAX_LOOPS 3 | 59 | #define MAX_LOOPS 5 |
60 | 60 | ||
61 | /** | 61 | /** |
62 | * struct for storing the information of the hardware. There is only | 62 | * struct for storing the information of the hardware. There is only |
@@ -285,7 +285,7 @@ mst_receive (struct MessageStreamTokenizer *mst, | |||
285 | do_align: | 285 | do_align: |
286 | if (mst->pos < mst->off) | 286 | if (mst->pos < mst->off) |
287 | { | 287 | { |
288 | fprintf (stderr, "We processed too many bytes!\n"); | 288 | //fprintf (stderr, "We processed too many bytes!\n"); |
289 | return GNUNET_SYSERR; | 289 | return GNUNET_SYSERR; |
290 | } | 290 | } |
291 | if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) || | 291 | if ((mst->curr_buf - mst->off < sizeof (struct GNUNET_MessageHeader)) || |
@@ -696,7 +696,7 @@ static int | |||
696 | get_channel(struct HardwareInfos *dev, bdaddr_t dest) | 696 | get_channel(struct HardwareInfos *dev, bdaddr_t dest) |
697 | { | 697 | { |
698 | /** | 698 | /** |
699 | * 1. detect all nearby devices //FIXME : Connect directly to the device with the service | 699 | * 1. detect all nearby devices |
700 | * 2. for each device: | 700 | * 2. for each device: |
701 | * 2.1. connect to the SDP server running | 701 | * 2.1. connect to the SDP server running |
702 | * 2.2. get a list of service records with the specific UUID | 702 | * 2.2. get a list of service records with the specific UUID |
@@ -803,7 +803,7 @@ read_from_the_socket (int sock, | |||
803 | memset (ri, 0, sizeof (*ri)); | 803 | memset (ri, 0, sizeof (*ri)); |
804 | ri->ri_channel = rc_addr.rc_channel; | 804 | ri->ri_channel = rc_addr.rc_channel; |
805 | 805 | ||
806 | /* detect CRC32 at the end */ | 806 | /* Detect CRC32 at the end */ |
807 | if (0 == check_crc_buf_osdep (tmpbuf, count - sizeof (uint32_t))) | 807 | if (0 == check_crc_buf_osdep (tmpbuf, count - sizeof (uint32_t))) |
808 | { | 808 | { |
809 | count -= sizeof(uint32_t); | 809 | count -= sizeof(uint32_t); |
@@ -822,25 +822,13 @@ read_from_the_socket (int sock, | |||
822 | */ | 822 | */ |
823 | static int | 823 | static int |
824 | open_device (struct HardwareInfos *dev) | 824 | open_device (struct HardwareInfos *dev) |
825 | { | 825 | { |
826 | /** | ||
827 | * 1. Open a HCI socket (if RFCOMM protocol is used. If not, the HCI socket is | ||
828 | * saved in dev->rfcomm). | ||
829 | * 2. Find the device id (request a list with all the devices and find the one | ||
830 | * with the dev->iface name) | ||
831 | * 3. If the interface is down try to get it up | ||
832 | * 4. Bind the RFCOMM socket to the interface using the bind_socket() method and register | ||
833 | * a SDP service | ||
834 | * 5. For now use a hard coded port number(channel) value | ||
835 | * FIXME : if I use HCI sockets , should I enable RAW_SOCKET MODE?!?!?! | ||
836 | */ | ||
837 | |||
838 | int i, dev_id = -1, fd_hci; | 826 | int i, dev_id = -1, fd_hci; |
839 | struct | 827 | struct |
840 | { | 828 | { |
841 | struct hci_dev_list_req list; | 829 | struct hci_dev_list_req list; |
842 | struct hci_dev_req dev[HCI_MAX_DEV]; | 830 | struct hci_dev_req dev[HCI_MAX_DEV]; |
843 | } request; //used for detecting the local devices | 831 | } request; //used for detecting the local devices |
844 | struct sockaddr_rc rc_addr = { 0 }; //used for binding | 832 | struct sockaddr_rc rc_addr = { 0 }; //used for binding |
845 | 833 | ||
846 | /* Initialize the neighbour structure */ | 834 | /* Initialize the neighbour structure */ |
@@ -848,6 +836,7 @@ open_device (struct HardwareInfos *dev) | |||
848 | for (i = 0; i < MAX_PORTS; i++) | 836 | for (i = 0; i < MAX_PORTS; i++) |
849 | neighbours.fds[i] = -1; | 837 | neighbours.fds[i] = -1; |
850 | 838 | ||
839 | /* Open a HCI socket */ | ||
851 | fd_hci = socket (AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); | 840 | fd_hci = socket (AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI); |
852 | 841 | ||
853 | if (fd_hci < 0) | 842 | if (fd_hci < 0) |
@@ -888,14 +877,13 @@ open_device (struct HardwareInfos *dev) | |||
888 | dev_id = dev_info.dev_id; //the device was found | 877 | dev_id = dev_info.dev_id; //the device was found |
889 | /** | 878 | /** |
890 | * Copy the MAC address to the device structure | 879 | * Copy the MAC address to the device structure |
891 | * FIXME: probably this is not the best solution | ||
892 | */ | 880 | */ |
893 | memcpy (&dev->pl_mac, &dev_info.bdaddr, sizeof (bdaddr_t)); | 881 | memcpy (&dev->pl_mac, &dev_info.bdaddr, sizeof (bdaddr_t)); |
894 | 882 | ||
895 | /* Check if the interface is UP */ | 883 | /* Check if the interface is UP */ |
896 | if (hci_test_bit (HCI_UP, (void *) &dev_info.flags) == 0) | 884 | if (hci_test_bit (HCI_UP, (void *) &dev_info.flags) == 0) |
897 | { | 885 | { |
898 | /* Bring interface up */ //FIXME should I check if is HCI_RUNNING ?!?!??! | 886 | /* Bring interface up */ |
899 | if (ioctl (fd_hci, HCIDEVUP, dev_info.dev_id)) | 887 | if (ioctl (fd_hci, HCIDEVUP, dev_info.dev_id)) |
900 | { | 888 | { |
901 | fprintf (stderr, "ioctl(HCIDEVUP) on interface `%.*s' failed: %s\n", | 889 | fprintf (stderr, "ioctl(HCIDEVUP) on interface `%.*s' failed: %s\n", |
@@ -923,10 +911,6 @@ open_device (struct HardwareInfos *dev) | |||
923 | } | 911 | } |
924 | 912 | ||
925 | } | 913 | } |
926 | |||
927 | //FIXME : Sniff mode!?! | ||
928 | //FIXME : RAW MODE?!? | ||
929 | |||
930 | break; | 914 | break; |
931 | } | 915 | } |
932 | 916 | ||
@@ -969,7 +953,7 @@ open_device (struct HardwareInfos *dev) | |||
969 | { | 953 | { |
970 | fprintf (stderr, "Failed to listen on socket for interface `%.*s': %s\n", IFNAMSIZ, | 954 | fprintf (stderr, "Failed to listen on socket for interface `%.*s': %s\n", IFNAMSIZ, |
971 | dev->iface, strerror (errno)); | 955 | dev->iface, strerror (errno)); |
972 | return 3; | 956 | return 1; |
973 | } | 957 | } |
974 | 958 | ||
975 | 959 | ||
@@ -1112,8 +1096,8 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1112 | inquiry_devices: //skip the conditions and force a inquiry for new devices | 1096 | inquiry_devices: //skip the conditions and force a inquiry for new devices |
1113 | { | 1097 | { |
1114 | /** | 1098 | /** |
1115 | * It means that I sent HELLO message to all the devices from the list so I should search | 1099 | * It means that I sent HELLO messages to all the devices from the list and I should search |
1116 | * for another devices or that this is the first time when I do a search for devices. | 1100 | * for another ones or that this is the first time when I do a search. |
1117 | */ | 1101 | */ |
1118 | inquiry_info *devices = NULL; | 1102 | inquiry_info *devices = NULL; |
1119 | int i, responses, max_responses = MAX_PORTS; | 1103 | int i, responses, max_responses = MAX_PORTS; |
@@ -1121,7 +1105,7 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1121 | /* sanity checks */ | 1105 | /* sanity checks */ |
1122 | if (neighbours.size >= MAX_PORTS) | 1106 | if (neighbours.size >= MAX_PORTS) |
1123 | { | 1107 | { |
1124 | fprintf (stderr, "%s reached the top limit for the discovarable devices\n", dev->iface); | 1108 | fprintf (stderr, "%.*s reached the top limit for the discovarable devices\n", IFNAMSIZ, dev->iface); |
1125 | return 2; | 1109 | return 2; |
1126 | } | 1110 | } |
1127 | 1111 | ||
@@ -1134,7 +1118,7 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1134 | neighbours.dev_id = hci_devid (addr); | 1118 | neighbours.dev_id = hci_devid (addr); |
1135 | if (neighbours.dev_id < 0) | 1119 | if (neighbours.dev_id < 0) |
1136 | { | 1120 | { |
1137 | fprintf (stderr, "Failed to get the device id for interface %s : %s\n", | 1121 | fprintf (stderr, "Failed to get the device id for interface %.*s : %s\n", IFNAMSIZ, |
1138 | dev->iface, strerror (errno)); | 1122 | dev->iface, strerror (errno)); |
1139 | return 1; | 1123 | return 1; |
1140 | } | 1124 | } |
@@ -1143,7 +1127,7 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1143 | devices = malloc (max_responses * sizeof (inquiry_info)); | 1127 | devices = malloc (max_responses * sizeof (inquiry_info)); |
1144 | if (devices == NULL) | 1128 | if (devices == NULL) |
1145 | { | 1129 | { |
1146 | fprintf (stderr, "Failed to allocate memory for inquiry info list on interface %s\n", | 1130 | fprintf (stderr, "Failed to allocate memory for inquiry info list on interface %.*s\n", IFNAMSIZ, |
1147 | dev->iface); | 1131 | dev->iface); |
1148 | return 1; | 1132 | return 1; |
1149 | } | 1133 | } |
@@ -1151,15 +1135,15 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1151 | responses = hci_inquiry (neighbours.dev_id, 8, max_responses, NULL, &devices, IREQ_CACHE_FLUSH); | 1135 | responses = hci_inquiry (neighbours.dev_id, 8, max_responses, NULL, &devices, IREQ_CACHE_FLUSH); |
1152 | if (responses < 0) | 1136 | if (responses < 0) |
1153 | { | 1137 | { |
1154 | fprintf (stderr, "Failed to inquiry on interface %s\n", dev->iface); | 1138 | fprintf (stderr, "Failed to inquiry on interface %.*s\n", IFNAMSIZ, dev->iface); |
1155 | return 1; | 1139 | return 1; |
1156 | } | 1140 | } |
1157 | 1141 | ||
1158 | fprintf (stderr, "Found %d devices\n", responses); //FIXME delete it after debugging stage | 1142 | fprintf (stderr, "LOG : Found %d devices\n", responses); //FIXME delete it after debugging stage |
1159 | 1143 | ||
1160 | if (responses == 0) | 1144 | if (responses == 0) |
1161 | { | 1145 | { |
1162 | fprintf (stderr, "No devices discoverable\n"); | 1146 | fprintf (stderr, "LOG : No devices discoverable\n"); |
1163 | return 1; | 1147 | return 1; |
1164 | } | 1148 | } |
1165 | 1149 | ||
@@ -1171,7 +1155,8 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1171 | /* sanity check */ | 1155 | /* sanity check */ |
1172 | if (i >= MAX_PORTS) | 1156 | if (i >= MAX_PORTS) |
1173 | { | 1157 | { |
1174 | fprintf (stderr, "%s reached the top limit for the discoverable devices (after inquiry)\n", dev->iface); | 1158 | fprintf (stderr, "%.*s reached the top limit for the discoverable devices (after inquiry)\n", IFNAMSIZ, |
1159 | dev->iface); | ||
1175 | return 2; | 1160 | return 2; |
1176 | } | 1161 | } |
1177 | 1162 | ||
@@ -1181,7 +1166,7 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1181 | if (memcmp (&(devices + i)->bdaddr, &(neighbours.devices[j]), sizeof (bdaddr_t)) == 0) | 1166 | if (memcmp (&(devices + i)->bdaddr, &(neighbours.devices[j]), sizeof (bdaddr_t)) == 0) |
1182 | { | 1167 | { |
1183 | found = 1; | 1168 | found = 1; |
1184 | fprintf (stderr, "the device already exists on the list\n"); //FIXME debugging message | 1169 | fprintf (stderr, "LOG : the device already exists on the list\n"); //FIXME debugging message |
1185 | break; | 1170 | break; |
1186 | } | 1171 | } |
1187 | } | 1172 | } |
@@ -1191,7 +1176,7 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1191 | char addr[19] = { 0 }; | 1176 | char addr[19] = { 0 }; |
1192 | 1177 | ||
1193 | ba2str (&(devices +i)->bdaddr, addr); | 1178 | ba2str (&(devices +i)->bdaddr, addr); |
1194 | fprintf (stderr, "%s was added to the list\n", addr); //FIXME debugging message | 1179 | fprintf (stderr, "LOG : %s was added to the list\n", addr); //FIXME debugging message |
1195 | memcpy (&(neighbours.devices[neighbours.size++]), &(devices + i)->bdaddr, sizeof (bdaddr_t)); | 1180 | memcpy (&(neighbours.devices[neighbours.size++]), &(devices + i)->bdaddr, sizeof (bdaddr_t)); |
1196 | } | 1181 | } |
1197 | } | 1182 | } |
@@ -1218,14 +1203,14 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1218 | addr_rc.rc_channel = get_channel (dev, addr_rc.rc_bdaddr); | 1203 | addr_rc.rc_channel = get_channel (dev, addr_rc.rc_bdaddr); |
1219 | 1204 | ||
1220 | *sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); | 1205 | *sendsocket = socket (AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); |
1221 | 1206 | //TODO adauga un label aici si intoarcete de cateva ori daca nu reuseste | |
1222 | if (connect (*sendsocket, (struct sockaddr *)&addr_rc, sizeof (addr_rc)) == 0) | 1207 | if (connect (*sendsocket, (struct sockaddr *)&addr_rc, sizeof (addr_rc)) == 0) |
1223 | { | 1208 | { |
1224 | neighbours.fds[neighbours.pos++] = *sendsocket; | 1209 | neighbours.fds[neighbours.pos++] = *sendsocket; |
1225 | connection_successful = 1; | 1210 | connection_successful = 1; |
1226 | char addr[19] = { 0 }; | 1211 | char addr[19] = { 0 }; |
1227 | ba2str (&(neighbours.devices[neighbours.pos - 1]), addr); | 1212 | ba2str (&(neighbours.devices[neighbours.pos - 1]), addr); |
1228 | fprintf (stderr, "Connected to %s\n", addr); | 1213 | fprintf (stderr, "LOG : Connected to %s\n", addr); |
1229 | 1214 | ||
1230 | break; | 1215 | break; |
1231 | } | 1216 | } |
@@ -1234,10 +1219,10 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1234 | char addr[19] = { 0 }; | 1219 | char addr[19] = { 0 }; |
1235 | errno_copy = errno; //Save a copy for later | 1220 | errno_copy = errno; //Save a copy for later |
1236 | ba2str (&(neighbours.devices[neighbours.pos]), addr); | 1221 | ba2str (&(neighbours.devices[neighbours.pos]), addr); |
1237 | fprintf (stderr, "Couldn't connect on device %s, error : %s\n", addr, strerror(errno)); | 1222 | fprintf (stderr, "LOG : Couldn't connect on device %s, error : %s\n", addr, strerror(errno)); |
1238 | if (errno != ECONNREFUSED) //FIXME nu merge! | 1223 | if (errno != ECONNREFUSED) //FIXME nu merge! |
1239 | { | 1224 | { |
1240 | fprintf (stderr, "Removes %d device from the list\n", neighbours.pos); | 1225 | fprintf (stderr, "LOG : Removes %d device from the list\n", neighbours.pos); |
1241 | /* Remove the device from the list */ | 1226 | /* Remove the device from the list */ |
1242 | memcpy (&neighbours.devices[neighbours.pos], &neighbours.devices[neighbours.size - 1], sizeof (bdaddr_t)); | 1227 | memcpy (&neighbours.devices[neighbours.pos], &neighbours.devices[neighbours.size - 1], sizeof (bdaddr_t)); |
1243 | memset (&neighbours.devices[neighbours.size - 1], 0, sizeof (bdaddr_t)); | 1228 | memset (&neighbours.devices[neighbours.size - 1], 0, sizeof (bdaddr_t)); |
@@ -1259,7 +1244,7 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1259 | } | 1244 | } |
1260 | else | 1245 | else |
1261 | { | 1246 | { |
1262 | fprintf (stderr, "Search for a new device\n"); //FIXME debugging message | 1247 | fprintf (stderr, "LOG : Search for a new device\n"); //FIXME debugging message |
1263 | neighbours.pos += 1; | 1248 | neighbours.pos += 1; |
1264 | } | 1249 | } |
1265 | } | 1250 | } |
@@ -1272,7 +1257,7 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1272 | 1257 | ||
1273 | if (searching_devices_count == MAX_LOOPS) | 1258 | if (searching_devices_count == MAX_LOOPS) |
1274 | { | 1259 | { |
1275 | fprintf (stderr, "Force to inquiry for new devices\n"); | 1260 | fprintf (stderr, "LOG : Force to inquiry for new devices\n"); |
1276 | searching_devices_count = 0; | 1261 | searching_devices_count = 0; |
1277 | goto inquiry_devices; | 1262 | goto inquiry_devices; |
1278 | } | 1263 | } |
@@ -1290,7 +1275,7 @@ send_broadcast (struct HardwareInfos *dev, int *sendsocket) | |||
1290 | { | 1275 | { |
1291 | if (errno_copy == ECONNREFUSED) | 1276 | if (errno_copy == ECONNREFUSED) |
1292 | { | 1277 | { |
1293 | fprintf (stderr, "No device found. Go back and search again\n"); //FIXME debugging message | 1278 | fprintf (stderr, "LOG : No device found. Go back and search again\n"); //FIXME debugging message |
1294 | new_device = 1; | 1279 | new_device = 1; |
1295 | loops += 1; | 1280 | loops += 1; |
1296 | goto search_for_devices; | 1281 | goto search_for_devices; |
@@ -1443,27 +1428,23 @@ main (int argc, char *argv[]) | |||
1443 | FD_ZERO (&rfds); | 1428 | FD_ZERO (&rfds); |
1444 | if ((0 == write_pout.size) && (1 == stdin_open)) | 1429 | if ((0 == write_pout.size) && (1 == stdin_open)) |
1445 | { | 1430 | { |
1446 | // fprintf (stderr, "LOG : %s adds STDIN to rfds\n", dev.iface); //FIXME: debugging message | ||
1447 | FD_SET (STDIN_FILENO, &rfds); | 1431 | FD_SET (STDIN_FILENO, &rfds); |
1448 | maxfd = MAX (maxfd, STDIN_FILENO); | 1432 | maxfd = MAX (maxfd, STDIN_FILENO); |
1449 | } | 1433 | } |
1450 | if (0 == write_std.size) | 1434 | if (0 == write_std.size) |
1451 | { | 1435 | { |
1452 | // fprintf (stderr, "LOG : %s adds fd_rfcomm to rfds\n", dev.iface); //FIXME: debugging message | ||
1453 | FD_SET (dev.fd_rfcomm, &rfds); | 1436 | FD_SET (dev.fd_rfcomm, &rfds); |
1454 | maxfd = MAX (maxfd, dev.fd_rfcomm); | 1437 | maxfd = MAX (maxfd, dev.fd_rfcomm); |
1455 | } | 1438 | } |
1456 | 1439 | ||
1457 | for (i = 0; i < crt_rfds; i++) // it can receive messages from multiple devices | 1440 | for (i = 0; i < crt_rfds; i++) // it can receive messages from multiple devices |
1458 | { | 1441 | { |
1459 | // fprintf (stderr, "LOG : %s adds extra fds to rfds\n", dev.iface); //FIXME: debugging message | ||
1460 | FD_SET (rfds_list[i], &rfds); | 1442 | FD_SET (rfds_list[i], &rfds); |
1461 | maxfd = MAX (maxfd, rfds_list[i]); | 1443 | maxfd = MAX (maxfd, rfds_list[i]); |
1462 | } | 1444 | } |
1463 | FD_ZERO (&wfds); | 1445 | FD_ZERO (&wfds); |
1464 | if (0 < write_std.size) | 1446 | if (0 < write_std.size) |
1465 | { | 1447 | { |
1466 | // fprintf (stderr, "LOG : %s adds STDOUT to wfds\n", dev.iface); //FIXME: debugging message | ||
1467 | FD_SET (STDOUT_FILENO, &wfds); | 1448 | FD_SET (STDOUT_FILENO, &wfds); |
1468 | maxfd = MAX (maxfd, STDOUT_FILENO); | 1449 | maxfd = MAX (maxfd, STDOUT_FILENO); |
1469 | } | 1450 | } |
@@ -1471,14 +1452,13 @@ main (int argc, char *argv[]) | |||
1471 | { | 1452 | { |
1472 | struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *frame; | 1453 | struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *frame; |
1473 | /* Get the destination address */ | 1454 | /* Get the destination address */ |
1474 | //FIXME : not sure if this is correct | ||
1475 | frame = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *) write_pout.buf; | 1455 | frame = (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame *) write_pout.buf; |
1476 | 1456 | ||
1477 | if (memcmp (&frame->addr1, &dev.pl_mac, | 1457 | if (memcmp (&frame->addr1, &dev.pl_mac, |
1478 | sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) | 1458 | sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) |
1479 | { | 1459 | { |
1480 | fprintf (stderr, "LOG : %s has a message for him:)\n", dev.iface); //FIXME: debugging message | 1460 | broadcast = 1; |
1481 | memset (&write_pout, 0, sizeof (write_pout)); // clear the buffer | 1461 | memset (&write_pout, 0, sizeof (write_pout)); //clear the buffer |
1482 | } | 1462 | } |
1483 | else if (memcmp (&frame->addr1, &broadcast_address, | 1463 | else if (memcmp (&frame->addr1, &broadcast_address, |
1484 | sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) | 1464 | sizeof (struct GNUNET_TRANSPORT_WLAN_MacAddress)) == 0) |
@@ -1491,7 +1471,7 @@ main (int argc, char *argv[]) | |||
1491 | { | 1471 | { |
1492 | broadcast = 1; | 1472 | broadcast = 1; |
1493 | memset (&write_pout, 0, sizeof (write_pout)); //remove the message | 1473 | memset (&write_pout, 0, sizeof (write_pout)); //remove the message |
1494 | fprintf (stderr, "Skip the broadcast message (pos %d, size %d)\n", neighbours.pos, neighbours.size); | 1474 | fprintf (stderr, "LOG : Skip the broadcast message (pos %d, size %d)\n", neighbours.pos, neighbours.size); |
1495 | } | 1475 | } |
1496 | else | 1476 | else |
1497 | { | 1477 | { |
@@ -1502,17 +1482,22 @@ main (int argc, char *argv[]) | |||
1502 | else | 1482 | else |
1503 | { | 1483 | { |
1504 | int found = 0; | 1484 | int found = 0; |
1485 | int pos = 0; | ||
1505 | /* Search if the address already exists on the list */ | 1486 | /* Search if the address already exists on the list */ |
1506 | for (i = 0; i < neighbours.size; i++) | 1487 | for (i = 0; i < neighbours.size; i++) |
1507 | { | 1488 | { |
1508 | if (memcmp (&frame->addr1, &(neighbours.devices[i]), sizeof (bdaddr_t)) == 0 && neighbours.fds[i] != -1) | 1489 | if (memcmp (&frame->addr1, &(neighbours.devices[i]), sizeof (bdaddr_t)) == 0) |
1509 | { | 1490 | { |
1510 | found = 1; | 1491 | pos = i; |
1511 | FD_SET (neighbours.fds[i], &wfds); | 1492 | if (neighbours.fds[i] != -1) |
1512 | maxfd = MAX (maxfd, neighbours.fds[i]); | 1493 | { |
1513 | sendsocket = neighbours.fds[i]; | 1494 | found = 1; //save the position where it was found |
1514 | fprintf (stderr, "LOG: the address was found in the list\n"); | 1495 | FD_SET (neighbours.fds[i], &wfds); |
1515 | break; | 1496 | maxfd = MAX (maxfd, neighbours.fds[i]); |
1497 | sendsocket = neighbours.fds[i]; | ||
1498 | fprintf (stderr, "LOG: the address was found in the list\n"); | ||
1499 | break; | ||
1500 | } | ||
1516 | } | 1501 | } |
1517 | } | 1502 | } |
1518 | if (found == 0) | 1503 | if (found == 0) |
@@ -1537,11 +1522,6 @@ main (int argc, char *argv[]) | |||
1537 | addr.rc_family = AF_BLUETOOTH; | 1522 | addr.rc_family = AF_BLUETOOTH; |
1538 | addr.rc_channel = get_channel (&dev, addr.rc_bdaddr); | 1523 | addr.rc_channel = get_channel (&dev, addr.rc_bdaddr); |
1539 | 1524 | ||
1540 | /*** | ||
1541 | *TODO: use a NON-BLOCKING socket | ||
1542 | * sock_flags = fcntl (sendsocket, F_GETFL, 0); | ||
1543 | * fcntl( sendsocket, F_SETFL, sock_flags | O_NONBLOCK); | ||
1544 | */ | ||
1545 | int tries = 0; | 1525 | int tries = 0; |
1546 | connect_retry: | 1526 | connect_retry: |
1547 | status = connect (sendsocket, (struct sockaddr *) &addr, sizeof (addr)); | 1527 | status = connect (sendsocket, (struct sockaddr *) &addr, sizeof (addr)); |
@@ -1549,19 +1529,19 @@ main (int argc, char *argv[]) | |||
1549 | { | 1529 | { |
1550 | if (errno == ECONNREFUSED && tries < 2) | 1530 | if (errno == ECONNREFUSED && tries < 2) |
1551 | { | 1531 | { |
1552 | fprintf (stderr, "%s failed to connect. Trying again!\n", dev.iface); | 1532 | fprintf (stderr, "LOG : %.*s failed to connect. Trying again!\n", IFNAMSIZ, dev.iface); |
1553 | tries++; | 1533 | tries++; |
1554 | goto connect_retry; | 1534 | goto connect_retry; |
1555 | } | 1535 | } |
1556 | else if (errno == EBADF) | 1536 | else if (errno == EBADF) |
1557 | { | 1537 | { |
1558 | fprintf (stderr, "%s failed to connect : %s. Skip it!\n", dev.iface, strerror (errno)); | 1538 | fprintf (stderr, "LOG : %s failed to connect : %s. Skip it!\n", dev.iface, strerror (errno)); |
1559 | memset (&write_pout, 0, sizeof (write_pout)); | 1539 | memset (&write_pout, 0, sizeof (write_pout)); |
1560 | broadcast = 1; | 1540 | broadcast = 1; |
1561 | } | 1541 | } |
1562 | else | 1542 | else |
1563 | { | 1543 | { |
1564 | fprintf (stderr, "%s failed to connect : %s. Try again later!\n", dev.iface, strerror (errno)); | 1544 | fprintf (stderr, "LOG : %s failed to connect : %s. Try again later!\n", dev.iface, strerror (errno)); |
1565 | memset (&write_pout, 0, sizeof (write_pout)); | 1545 | memset (&write_pout, 0, sizeof (write_pout)); |
1566 | broadcast = 1; | 1546 | broadcast = 1; |
1567 | } | 1547 | } |
@@ -1571,16 +1551,23 @@ main (int argc, char *argv[]) | |||
1571 | { | 1551 | { |
1572 | FD_SET (sendsocket, &wfds); | 1552 | FD_SET (sendsocket, &wfds); |
1573 | maxfd = MAX (maxfd, sendsocket); | 1553 | maxfd = MAX (maxfd, sendsocket); |
1574 | fprintf (stderr, "Connection successful\n"); | 1554 | fprintf (stderr, "LOG : Connection successful\n"); |
1575 | /* Add the new device to the discovered devices list */ | 1555 | if (pos != 0) // save the socket |
1576 | if (neighbours.size < MAX_PORTS) | ||
1577 | { | 1556 | { |
1578 | neighbours.fds[neighbours.size] = sendsocket; | 1557 | neighbours.fds[pos] = sendsocket; |
1579 | memcpy (&(neighbours.devices[neighbours.size++]), &addr.rc_bdaddr, sizeof (bdaddr_t)); | ||
1580 | } | 1558 | } |
1581 | else | 1559 | else |
1582 | { | 1560 | { |
1583 | fprintf (stderr, "The top limit for the discovarable devices' list was reached\n"); | 1561 | /* Add the new device to the discovered devices list */ |
1562 | if (neighbours.size < MAX_PORTS) | ||
1563 | { | ||
1564 | neighbours.fds[neighbours.size] = sendsocket; | ||
1565 | memcpy (&(neighbours.devices[neighbours.size++]), &addr.rc_bdaddr, sizeof (bdaddr_t)); | ||
1566 | } | ||
1567 | else | ||
1568 | { | ||
1569 | fprintf (stderr, "The top limit for the discovarable devices' list was reached\n"); | ||
1570 | } | ||
1584 | } | 1571 | } |
1585 | } | 1572 | } |
1586 | } | 1573 | } |
@@ -1591,7 +1578,7 @@ main (int argc, char *argv[]) | |||
1591 | { | 1578 | { |
1592 | /* Select a fd which is ready for action :) */ | 1579 | /* Select a fd which is ready for action :) */ |
1593 | { | 1580 | { |
1594 | int retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL); //FIXME : when a device close the connection remove the socket from the list | 1581 | int retval = select (maxfd + 1, &rfds, &wfds, NULL, NULL); |
1595 | if ((-1 == retval) && (EINTR == errno)) | 1582 | if ((-1 == retval) && (EINTR == errno)) |
1596 | continue; | 1583 | continue; |
1597 | if (0 > retval && errno != EBADF) // we handle BADF errors later | 1584 | if (0 > retval && errno != EBADF) // we handle BADF errors later |
@@ -1629,8 +1616,7 @@ main (int argc, char *argv[]) | |||
1629 | if (0 > ret) //FIXME should I check first the error type? | 1616 | if (0 > ret) //FIXME should I check first the error type? |
1630 | { | 1617 | { |
1631 | fprintf (stderr, "Failed to write to bluetooth device: %s. Closing the socket!\n", | 1618 | fprintf (stderr, "Failed to write to bluetooth device: %s. Closing the socket!\n", |
1632 | strerror (errno)); | 1619 | strerror (errno)); |
1633 | |||
1634 | for (i = 0; i < neighbours.size; i++) | 1620 | for (i = 0; i < neighbours.size; i++) |
1635 | { | 1621 | { |
1636 | if (neighbours.fds[i] == sendsocket) | 1622 | if (neighbours.fds[i] == sendsocket) |
@@ -1640,15 +1626,17 @@ main (int argc, char *argv[]) | |||
1640 | break; | 1626 | break; |
1641 | } | 1627 | } |
1642 | } | 1628 | } |
1643 | //memset (&(write_pout.buf + write_std.pos), 0, (write_pout.size - write_pout.pos)) // FIXME should I remove the message? or try to resend it | 1629 | /* Remove the message */ |
1644 | //write_pour.pos = 0 ; write_pout.size = 0; | 1630 | memset (&write_pout.buf + write_std.pos, 0, (write_pout.size - write_pout.pos)); |
1631 | write_pout.pos = 0 ; | ||
1632 | write_pout.size = 0; | ||
1645 | } | 1633 | } |
1646 | else | 1634 | else |
1647 | { | 1635 | { |
1648 | write_pout.pos += ret; | 1636 | write_pout.pos += ret; |
1649 | if ((write_pout.pos != write_pout.size) && (0 != ret)) | 1637 | if ((write_pout.pos != write_pout.size) && (0 != ret)) |
1650 | { | 1638 | { |
1651 | /* we should not get partial sends with packet-oriented devices... */ | 1639 | /* We should not get partial sends with packet-oriented devices... */ |
1652 | fprintf (stderr, "Write error, partial send: %u/%u\n", | 1640 | fprintf (stderr, "Write error, partial send: %u/%u\n", |
1653 | (unsigned int) write_pout.pos, | 1641 | (unsigned int) write_pout.pos, |
1654 | (unsigned int) write_pout.size); | 1642 | (unsigned int) write_pout.size); |
@@ -1664,7 +1652,7 @@ main (int argc, char *argv[]) | |||
1664 | } | 1652 | } |
1665 | } | 1653 | } |
1666 | } | 1654 | } |
1667 | for (i = 0; i <= maxfd; i++) //FIXME it should be incremented | 1655 | for (i = 0; i <= maxfd; i++) |
1668 | { | 1656 | { |
1669 | if (FD_ISSET (i, &rfds)) | 1657 | if (FD_ISSET (i, &rfds)) |
1670 | { | 1658 | { |
@@ -1675,7 +1663,7 @@ main (int argc, char *argv[]) | |||
1675 | if (0 > ret) | 1663 | if (0 > ret) |
1676 | { | 1664 | { |
1677 | fprintf (stderr, "Read error from STDIN: %s\n", strerror (errno)); | 1665 | fprintf (stderr, "Read error from STDIN: %s\n", strerror (errno)); |
1678 | break; | 1666 | break; break; |
1679 | } | 1667 | } |
1680 | if (0 == ret) | 1668 | if (0 == ret) |
1681 | { | 1669 | { |
@@ -1698,9 +1686,9 @@ main (int argc, char *argv[]) | |||
1698 | fprintf(stderr, "LOG : %s accepts a message\n", dev.iface); //FIXME: debugging message | 1686 | fprintf(stderr, "LOG : %s accepts a message\n", dev.iface); //FIXME: debugging message |
1699 | if (readsocket == -1) | 1687 | if (readsocket == -1) |
1700 | { | 1688 | { |
1701 | fprintf (stderr, "Failed to accept a connection on interface: %s\n", | 1689 | fprintf (stderr, "Failed to accept a connection on interface: %.*s\n", IFNAMSIZ, |
1702 | strerror (errno)); | 1690 | strerror (errno)); |
1703 | return -1; //FIXME probably I should ignore the error and keep the process alive | 1691 | break; |
1704 | } | 1692 | } |
1705 | else | 1693 | else |
1706 | { | 1694 | { |
@@ -1758,20 +1746,6 @@ main (int argc, char *argv[]) | |||
1758 | - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame); | 1746 | - sizeof (struct GNUNET_TRANSPORT_WLAN_Ieee80211Frame); |
1759 | rrm->header.size = htons (write_std.size); | 1747 | rrm->header.size = htons (write_std.size); |
1760 | rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER); | 1748 | rrm->header.type = htons (GNUNET_MESSAGE_TYPE_WLAN_DATA_FROM_HELPER); |
1761 | |||
1762 | /* Remove the socket from the list */ | ||
1763 | int j; | ||
1764 | for (j = 0; j < crt_rfds; j++) | ||
1765 | { | ||
1766 | if (i == rfds_list[crt_rfds]) | ||
1767 | { | ||
1768 | rfds_list[j] ^= rfds_list[crt_rfds]; | ||
1769 | rfds_list[crt_rfds] ^= rfds_list[j]; | ||
1770 | rfds_list[j] ^= rfds_list[crt_rfds]; | ||
1771 | crt_rfds -= 1; | ||
1772 | break; | ||
1773 | } | ||
1774 | } | ||
1775 | } | 1749 | } |
1776 | } | 1750 | } |
1777 | } | 1751 | } |