summaryrefslogtreecommitdiffstats
path: root/Modules/socketmodule.c
diff options
context:
space:
mode:
authorSteve Dower <steve.dower@microsoft.com>2019-05-23 15:45:22 (GMT)
committerGitHub <noreply@github.com>2019-05-23 15:45:22 (GMT)
commitb82e17e626f7b1cd98aada0b1ebb65cb9f8fb184 (patch)
tree5370a2a075707cb0b37ce135cad6ffe23da424c4 /Modules/socketmodule.c
parente788057a9188ff37e232729815dfda2529079420 (diff)
downloadcpython-b82e17e626f7b1cd98aada0b1ebb65cb9f8fb184.zip
cpython-b82e17e626f7b1cd98aada0b1ebb65cb9f8fb184.tar.gz
cpython-b82e17e626f7b1cd98aada0b1ebb65cb9f8fb184.tar.bz2
bpo-36842: Implement PEP 578 (GH-12613)
Adds sys.audit, sys.addaudithook, io.open_code, and associated C APIs.
Diffstat (limited to 'Modules/socketmodule.c')
-rw-r--r--Modules/socketmodule.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index c024542..74cdc0f 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3053,6 +3053,11 @@ sock_bind(PySocketSockObject *s, PyObject *addro)
if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen, "bind")) {
return NULL;
}
+
+ if (PySys_Audit("socket.bind", "OO", s, addro) < 0) {
+ return NULL;
+ }
+
Py_BEGIN_ALLOW_THREADS
res = bind(s->sock_fd, SAS2SA(&addrbuf), addrlen);
Py_END_ALLOW_THREADS
@@ -3219,6 +3224,10 @@ sock_connect(PySocketSockObject *s, PyObject *addro)
return NULL;
}
+ if (PySys_Audit("socket.connect", "OO", s, addro) < 0) {
+ return NULL;
+ }
+
res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 1);
if (res < 0)
return NULL;
@@ -3246,6 +3255,10 @@ sock_connect_ex(PySocketSockObject *s, PyObject *addro)
return NULL;
}
+ if (PySys_Audit("socket.connect", "OO", s, addro) < 0) {
+ return NULL;
+ }
+
res = internal_connect(s, SAS2SA(&addrbuf), addrlen, 0);
if (res < 0)
return NULL;
@@ -4248,6 +4261,10 @@ sock_sendto(PySocketSockObject *s, PyObject *args)
return NULL;
}
+ if (PySys_Audit("socket.sendto", "OO", s, addro) < 0) {
+ return NULL;
+ }
+
ctx.buf = pbuf.buf;
ctx.len = pbuf.len;
ctx.flags = flags;
@@ -4379,8 +4396,15 @@ sock_sendmsg(PySocketSockObject *s, PyObject *args)
{
goto finally;
}
+ if (PySys_Audit("socket.sendmsg", "OO", s, addr_arg) < 0) {
+ return NULL;
+ }
msg.msg_name = &addrbuf;
msg.msg_namelen = addrlen;
+ } else {
+ if (PySys_Audit("socket.sendmsg", "OO", s, Py_None) < 0) {
+ return NULL;
+ }
}
/* Fill in an iovec for each message part, and save the Py_buffer
@@ -5030,6 +5054,17 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
&family, &type, &proto, &fdobj))
return -1;
+#ifdef MS_WINDOWS
+ /* In this case, we don't use the family, type and proto args */
+ if (fdobj != NULL && fdobj != Py_None)
+#endif
+ {
+ if (PySys_Audit("socket.__new__", "Oiii",
+ s, family, type, proto) < 0) {
+ return -1;
+ }
+ }
+
if (fdobj != NULL && fdobj != Py_None) {
#ifdef MS_WINDOWS
/* recreate a socket that was duplicated */
@@ -5042,6 +5077,12 @@ sock_initobj(PyObject *self, PyObject *args, PyObject *kwds)
return -1;
}
memcpy(&info, PyBytes_AS_STRING(fdobj), sizeof(info));
+
+ if (PySys_Audit("socket()", "iii", info.iAddressFamily,
+ info.iSocketType, info.iProtocol) < 0) {
+ return -1;
+ }
+
Py_BEGIN_ALLOW_THREADS
fd = WSASocketW(FROM_PROTOCOL_INFO, FROM_PROTOCOL_INFO,
FROM_PROTOCOL_INFO, &info, 0, WSA_FLAG_OVERLAPPED);
@@ -5284,6 +5325,10 @@ static PyTypeObject sock_type = {
static PyObject *
socket_gethostname(PyObject *self, PyObject *unused)
{
+ if (PySys_Audit("socket.gethostname", NULL) < 0) {
+ return NULL;
+ }
+
#ifdef MS_WINDOWS
/* Don't use winsock's gethostname, as this returns the ANSI
version of the hostname, whereas we need a Unicode string.
@@ -5362,6 +5407,11 @@ extern int sethostname(const char *, size_t);
return NULL;
flag = 1;
}
+
+ if (PySys_Audit("socket.sethostname", "(O)", hnobj) < 0) {
+ return NULL;
+ }
+
res = PyObject_GetBuffer(hnobj, &buf, PyBUF_SIMPLE);
if (!res) {
res = sethostname(buf.buf, buf.len);
@@ -5387,6 +5437,9 @@ socket_gethostbyname(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "et:gethostbyname", "idna", &name))
return NULL;
+ if (PySys_Audit("socket.gethostbyname", "O", args) < 0) {
+ goto finally;
+ }
if (setipaddr(name, (struct sockaddr *)&addrbuf, sizeof(addrbuf), AF_INET) < 0)
goto finally;
ret = make_ipv4_addr(&addrbuf);
@@ -5571,6 +5624,9 @@ socket_gethostbyname_ex(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "et:gethostbyname_ex", "idna", &name))
return NULL;
+ if (PySys_Audit("socket.gethostbyname", "O", args) < 0) {
+ goto finally;
+ }
if (setipaddr(name, SAS2SA(&addr), sizeof(addr), AF_INET) < 0)
goto finally;
Py_BEGIN_ALLOW_THREADS
@@ -5649,6 +5705,9 @@ socket_gethostbyaddr(PyObject *self, PyObject *args)
if (!PyArg_ParseTuple(args, "et:gethostbyaddr", "idna", &ip_num))
return NULL;
+ if (PySys_Audit("socket.gethostbyaddr", "O", args) < 0) {
+ goto finally;
+ }
af = AF_UNSPEC;
if (setipaddr(ip_num, sa, sizeof(addr), af) < 0)
goto finally;
@@ -5720,6 +5779,11 @@ socket_getservbyname(PyObject *self, PyObject *args)
struct servent *sp;
if (!PyArg_ParseTuple(args, "s|s:getservbyname", &name, &proto))
return NULL;
+
+ if (PySys_Audit("socket.getservbyname", "ss", name, proto) < 0) {
+ return NULL;
+ }
+
Py_BEGIN_ALLOW_THREADS
sp = getservbyname(name, proto);
Py_END_ALLOW_THREADS
@@ -5757,6 +5821,11 @@ socket_getservbyport(PyObject *self, PyObject *args)
"getservbyport: port must be 0-65535.");
return NULL;
}
+
+ if (PySys_Audit("socket.getservbyport", "is", port, proto) < 0) {
+ return NULL;
+ }
+
Py_BEGIN_ALLOW_THREADS
sp = getservbyport(htons((short)port), proto);
Py_END_ALLOW_THREADS
@@ -6392,6 +6461,12 @@ socket_getaddrinfo(PyObject *self, PyObject *args, PyObject* kwargs)
pptr = "00";
}
#endif
+
+ if (PySys_Audit("socket.getaddrinfo", "OOiii",
+ hobj, pobj, family, socktype, protocol) < 0) {
+ return NULL;
+ }
+
memset(&hints, 0, sizeof(hints));
hints.ai_family = family;
hints.ai_socktype = socktype;
@@ -6483,6 +6558,11 @@ socket_getnameinfo(PyObject *self, PyObject *args)
"getnameinfo(): flowinfo must be 0-1048575.");
return NULL;
}
+
+ if (PySys_Audit("socket.getnameinfo", "(O)", sa) < 0) {
+ return NULL;
+ }
+
PyOS_snprintf(pbuf, sizeof(pbuf), "%d", port);
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;