mirror of
https://github.com/python/cpython.git
synced 2025-10-23 01:43:53 +00:00
[3.13] gh-132099: Harmonize Bluetooth address handling (GH-132486) (GH-132497)
Now all protocols always accept the Bluetooth address as string and
getsockname() always returns the Bluetooth address as string.
* BTPROTO_SCO now accepts not only bytes, but str.
* BTPROTO_SCO now checks address for embedded null.
* On *BSD, BTPROTO_HCI now accepts str instead of bytes.
* On FreeBSD, getsockname() for BTPROTO_HCI now returns str instead of bytes.
* On NetBSD and DragonFly BSD, BTPROTO_HCI now checks address for embedded null.
(cherry picked from commit 1fc1df8dcc
)
This commit is contained in:
parent
a50aa3325b
commit
d321b6ec82
3 changed files with 58 additions and 38 deletions
|
@ -1495,7 +1495,7 @@ makesockaddr(SOCKET_T sockfd, struct sockaddr *addr, size_t addrlen, int proto)
|
|||
#elif defined(__FreeBSD__)
|
||||
const char *node = _BT_HCI_MEMB(a, node);
|
||||
size_t len = strnlen(node, sizeof(_BT_HCI_MEMB(a, node)));
|
||||
return PyBytes_FromStringAndSize(node, (Py_ssize_t)len);
|
||||
return PyUnicode_FromStringAndSize(node, (Py_ssize_t)len);
|
||||
#else
|
||||
return makebdaddr(&_BT_HCI_MEMB(a, bdaddr));
|
||||
#endif
|
||||
|
@ -2075,36 +2075,25 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
|
|||
return 0;
|
||||
}
|
||||
_BT_HCI_MEMB(addr, dev) = dev;
|
||||
#elif defined(__FreeBSD__)
|
||||
if (!PyBytes_Check(args)) {
|
||||
#else
|
||||
const char *straddr;
|
||||
if (!PyArg_Parse(args, "s", &straddr)) {
|
||||
PyErr_Format(PyExc_OSError, "%s: "
|
||||
"wrong node format", caller);
|
||||
"wrong format", caller);
|
||||
return 0;
|
||||
}
|
||||
const char *straddr = PyBytes_AS_STRING(args);
|
||||
size_t len = PyBytes_GET_SIZE(args);
|
||||
if (strlen(straddr) != len) {
|
||||
PyErr_Format(PyExc_ValueError, "%s: "
|
||||
"node contains embedded null character", caller);
|
||||
return 0;
|
||||
}
|
||||
if (len > sizeof(_BT_HCI_MEMB(addr, node))) {
|
||||
# if defined(__FreeBSD__)
|
||||
if (strlen(straddr) > sizeof(_BT_HCI_MEMB(addr, node))) {
|
||||
PyErr_Format(PyExc_ValueError, "%s: "
|
||||
"node too long", caller);
|
||||
return 0;
|
||||
}
|
||||
strncpy(_BT_HCI_MEMB(addr, node), straddr,
|
||||
sizeof(_BT_HCI_MEMB(addr, node)));
|
||||
#else
|
||||
const char *straddr;
|
||||
if (!PyBytes_Check(args)) {
|
||||
PyErr_Format(PyExc_OSError, "%s: "
|
||||
"wrong format", caller);
|
||||
return 0;
|
||||
}
|
||||
straddr = PyBytes_AS_STRING(args);
|
||||
# else
|
||||
if (setbdaddr(straddr, &_BT_HCI_MEMB(addr, bdaddr)) < 0)
|
||||
return 0;
|
||||
# endif
|
||||
#endif
|
||||
*len_ret = sizeof *addr;
|
||||
return 1;
|
||||
|
@ -2117,12 +2106,22 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
|
|||
struct sockaddr_sco *addr = &addrbuf->bt_sco;
|
||||
memset(addr, 0, sizeof(struct sockaddr_sco));
|
||||
_BT_SCO_MEMB(addr, family) = AF_BLUETOOTH;
|
||||
if (!PyBytes_Check(args)) {
|
||||
|
||||
if (PyBytes_Check(args)) {
|
||||
if (!PyArg_Parse(args, "y", &straddr)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if (PyUnicode_Check(args)) {
|
||||
if (!PyArg_Parse(args, "s", &straddr)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
PyErr_Format(PyExc_OSError,
|
||||
"%s(): wrong format", caller);
|
||||
return 0;
|
||||
}
|
||||
straddr = PyBytes_AS_STRING(args);
|
||||
if (setbdaddr(straddr, &_BT_SCO_MEMB(addr, bdaddr)) < 0)
|
||||
return 0;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue