sockaddr: decode bluetooth address type names

* xlat/bdaddr_types.in: New file.
* sockaddr.c: Include xlat/bdaddr_types.h.
(print_sockaddr_data_bt): Decode struct sockaddr_l2.l2_bdaddr_type field
using constants from bdaddr_types xlat.
* tests/net-sockaddr.c (check_l2): Check decoding
of struct sockaddr_l2.l2_bdaddr_type field.
This commit is contained in:
Eugene Syromyatnikov 2018-05-06 20:28:09 +02:00 committed by Dmitry V. Levin
parent 17dbd04e65
commit 9da6cfcc46
3 changed files with 40 additions and 6 deletions

View File

@ -54,6 +54,7 @@
#include "xlat/ethernet_protocols.h"
#include "xlat/af_packet_types.h"
#include "xlat/bdaddr_types.h"
#include "xlat/hci_channels.h"
#define SIZEOF_SA_FAMILY sizeof(((struct sockaddr *) 0)->sa_family)
@ -327,12 +328,14 @@ print_sockaddr_data_bt(const void *const buf, const int addrlen)
const struct sockaddr_l2 *const l2 = buf;
tprintf("l2_psm=htobs(%hu)"
", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
", l2_cid=htobs(%hu), l2_bdaddr_type=%u",
", l2_cid=htobs(%hu), l2_bdaddr_type=",
btohs(l2->l2_psm),
l2->l2_bdaddr.b[0], l2->l2_bdaddr.b[1],
l2->l2_bdaddr.b[2], l2->l2_bdaddr.b[3],
l2->l2_bdaddr.b[4], l2->l2_bdaddr.b[5],
btohs(l2->l2_cid), l2->l2_bdaddr_type);
btohs(l2->l2_cid));
printxval_index(bdaddr_types, l2->l2_bdaddr_type,
"BDADDR_???");
break;
}
default:

View File

@ -436,25 +436,52 @@ check_l2(void)
{
const unsigned short h_psm = 12345;
const unsigned short h_cid = 13579;
const struct sockaddr_l2 c_l2 = {
struct sockaddr_l2 c_l2 = {
.l2_family = AF_BLUETOOTH,
.l2_psm = htobs(h_psm),
.l2_bdaddr.b = "abcdef",
.l2_cid = htobs(h_cid),
.l2_bdaddr_type = 42
.l2_bdaddr_type = 0xce,
};
void *l2 = tail_memdup(&c_l2, sizeof(c_l2));
unsigned int len = sizeof(c_l2);
int ret = connect(-1, l2, len);
printf("connect(-1, {sa_family=AF_BLUETOOTH"
", l2_psm=htobs(%hu)"
", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
", l2_cid=htobs(%hu), l2_bdaddr_type=%u}"
", l2_cid=htobs(%hu), l2_bdaddr_type=0xce /* BDADDR_??? */}"
", %u) = %d EBADF (%m)\n", h_psm,
c_l2.l2_bdaddr.b[0], c_l2.l2_bdaddr.b[1],
c_l2.l2_bdaddr.b[2], c_l2.l2_bdaddr.b[3],
c_l2.l2_bdaddr.b[4], c_l2.l2_bdaddr.b[5],
h_cid, c_l2.l2_bdaddr_type, len, ret);
h_cid, len, ret);
c_l2.l2_bdaddr_type = BDADDR_LE_RANDOM;
memcpy(l2, &c_l2, sizeof(c_l2));
ret = connect(-1, l2, len);
printf("connect(-1, {sa_family=AF_BLUETOOTH"
", l2_psm=htobs(%hu)"
", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
", l2_cid=htobs(%hu), l2_bdaddr_type=BDADDR_LE_RANDOM}"
", %u) = %d EBADF (%m)\n", h_psm,
c_l2.l2_bdaddr.b[0], c_l2.l2_bdaddr.b[1],
c_l2.l2_bdaddr.b[2], c_l2.l2_bdaddr.b[3],
c_l2.l2_bdaddr.b[4], c_l2.l2_bdaddr.b[5],
h_cid, len, ret);
c_l2.l2_bdaddr_type = 3;
memcpy(l2, &c_l2, sizeof(c_l2));
ret = connect(-1, l2, len);
printf("connect(-1, {sa_family=AF_BLUETOOTH"
", l2_psm=htobs(%hu)"
", l2_bdaddr=%02x:%02x:%02x:%02x:%02x:%02x"
", l2_cid=htobs(%hu), l2_bdaddr_type=0x3 /* BDADDR_??? */}"
", %u) = %d EBADF (%m)\n", h_psm,
c_l2.l2_bdaddr.b[0], c_l2.l2_bdaddr.b[1],
c_l2.l2_bdaddr.b[2], c_l2.l2_bdaddr.b[3],
c_l2.l2_bdaddr.b[4], c_l2.l2_bdaddr.b[5],
h_cid, len, ret);
}
#endif

4
xlat/bdaddr_types.in Normal file
View File

@ -0,0 +1,4 @@
#value_indexed
BDADDR_BREDR 0
BDADDR_LE_PUBLIC 1
BDADDR_LE_RANDOM 2