| 1 |
/*[clinic input] |
| 2 |
preserve |
| 3 |
[clinic start generated code]*/ |
| 4 |
|
| 5 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 6 |
# include "pycore_gc.h" // PyGC_Head |
| 7 |
# include "pycore_runtime.h" // _Py_ID() |
| 8 |
#endif |
| 9 |
#include "pycore_long.h" // _PyLong_UInt16_Converter() |
| 10 |
#include "pycore_modsupport.h" // _PyArg_UnpackKeywords() |
| 11 |
|
| 12 |
PyDoc_STRVAR(_socket_socket_close__doc__, |
| 13 |
"close($self, /)\n" |
| 14 |
"--\n" |
| 15 |
"\n" |
| 16 |
"close()\n" |
| 17 |
"\n" |
| 18 |
"Close the socket. It cannot be used after this call."); |
| 19 |
|
| 20 |
#define _SOCKET_SOCKET_CLOSE_METHODDEF \ |
| 21 |
{"close", (PyCFunction)_socket_socket_close, METH_NOARGS, _socket_socket_close__doc__}, |
| 22 |
|
| 23 |
static PyObject * |
| 24 |
_socket_socket_close_impl(PySocketSockObject *s); |
| 25 |
|
| 26 |
static PyObject * |
| 27 |
_socket_socket_close(PyObject *s, PyObject *Py_UNUSED(ignored)) |
| 28 |
{ |
| 29 |
return _socket_socket_close_impl((PySocketSockObject *)s); |
| 30 |
} |
| 31 |
|
| 32 |
static int |
| 33 |
sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto, |
| 34 |
PyObject *fdobj); |
| 35 |
|
| 36 |
static int |
| 37 |
sock_initobj(PyObject *self, PyObject *args, PyObject *kwargs) |
| 38 |
{ |
| 39 |
int return_value = -1; |
| 40 |
#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE) |
| 41 |
|
| 42 |
#define NUM_KEYWORDS 4 |
| 43 |
static struct { |
| 44 |
PyGC_Head _this_is_not_used; |
| 45 |
PyObject_VAR_HEAD |
| 46 |
Py_hash_t ob_hash; |
| 47 |
PyObject *ob_item[NUM_KEYWORDS]; |
| 48 |
} _kwtuple = { |
| 49 |
.ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS) |
| 50 |
.ob_hash = -1, |
| 51 |
.ob_item = { &_Py_ID(family), &_Py_ID(type), &_Py_ID(proto), &_Py_ID(fileno), }, |
| 52 |
}; |
| 53 |
#undef NUM_KEYWORDS |
| 54 |
#define KWTUPLE (&_kwtuple.ob_base.ob_base) |
| 55 |
|
| 56 |
#else // !Py_BUILD_CORE |
| 57 |
# define KWTUPLE NULL |
| 58 |
#endif // !Py_BUILD_CORE |
| 59 |
|
| 60 |
static const char * const _keywords[] = {"family", "type", "proto", "fileno", NULL}; |
| 61 |
static _PyArg_Parser _parser = { |
| 62 |
.keywords = _keywords, |
| 63 |
.fname = "socket", |
| 64 |
.kwtuple = KWTUPLE, |
| 65 |
}; |
| 66 |
#undef KWTUPLE |
| 67 |
PyObject *argsbuf[4]; |
| 68 |
PyObject * const *fastargs; |
| 69 |
Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 70 |
Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0; |
| 71 |
int family = -1; |
| 72 |
int type = -1; |
| 73 |
int proto = -1; |
| 74 |
PyObject *fdobj = NULL; |
| 75 |
|
| 76 |
fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, |
| 77 |
/*minpos*/ 0, /*maxpos*/ 4, /*minkw*/ 0, /*varpos*/ 0, argsbuf); |
| 78 |
if (!fastargs) { |
| 79 |
goto exit; |
| 80 |
} |
| 81 |
if (!noptargs) { |
| 82 |
goto skip_optional_pos; |
| 83 |
} |
| 84 |
if (fastargs[0]) { |
| 85 |
family = PyLong_AsInt(fastargs[0]); |
| 86 |
if (family == -1 && PyErr_Occurred()) { |
| 87 |
goto exit; |
| 88 |
} |
| 89 |
if (!--noptargs) { |
| 90 |
goto skip_optional_pos; |
| 91 |
} |
| 92 |
} |
| 93 |
if (fastargs[1]) { |
| 94 |
type = PyLong_AsInt(fastargs[1]); |
| 95 |
if (type == -1 && PyErr_Occurred()) { |
| 96 |
goto exit; |
| 97 |
} |
| 98 |
if (!--noptargs) { |
| 99 |
goto skip_optional_pos; |
| 100 |
} |
| 101 |
} |
| 102 |
if (fastargs[2]) { |
| 103 |
proto = PyLong_AsInt(fastargs[2]); |
| 104 |
if (proto == -1 && PyErr_Occurred()) { |
| 105 |
goto exit; |
| 106 |
} |
| 107 |
if (!--noptargs) { |
| 108 |
goto skip_optional_pos; |
| 109 |
} |
| 110 |
} |
| 111 |
fdobj = fastargs[3]; |
| 112 |
skip_optional_pos: |
| 113 |
return_value = sock_initobj_impl((PySocketSockObject *)self, family, type, proto, fdobj); |
| 114 |
|
| 115 |
exit: |
| 116 |
return return_value; |
| 117 |
} |
| 118 |
|
| 119 |
PyDoc_STRVAR(_socket_ntohs__doc__, |
| 120 |
"ntohs($module, integer, /)\n" |
| 121 |
"--\n" |
| 122 |
"\n" |
| 123 |
"Convert a 16-bit unsigned integer from network to host byte order."); |
| 124 |
|
| 125 |
#define _SOCKET_NTOHS_METHODDEF \ |
| 126 |
{"ntohs", (PyCFunction)_socket_ntohs, METH_O, _socket_ntohs__doc__}, |
| 127 |
|
| 128 |
static PyObject * |
| 129 |
_socket_ntohs_impl(PyObject *module, uint16_t x); |
| 130 |
|
| 131 |
static PyObject * |
| 132 |
_socket_ntohs(PyObject *module, PyObject *arg) |
| 133 |
{ |
| 134 |
PyObject *return_value = NULL; |
| 135 |
uint16_t x; |
| 136 |
|
| 137 |
if (!_PyLong_UInt16_Converter(arg, &x)) { |
| 138 |
goto exit; |
| 139 |
} |
| 140 |
return_value = _socket_ntohs_impl(module, x); |
| 141 |
|
| 142 |
exit: |
| 143 |
return return_value; |
| 144 |
} |
| 145 |
|
| 146 |
PyDoc_STRVAR(_socket_ntohl__doc__, |
| 147 |
"ntohl($module, integer, /)\n" |
| 148 |
"--\n" |
| 149 |
"\n" |
| 150 |
"Convert a 32-bit unsigned integer from network to host byte order."); |
| 151 |
|
| 152 |
#define _SOCKET_NTOHL_METHODDEF \ |
| 153 |
{"ntohl", (PyCFunction)_socket_ntohl, METH_O, _socket_ntohl__doc__}, |
| 154 |
|
| 155 |
static PyObject * |
| 156 |
_socket_ntohl_impl(PyObject *module, uint32_t x); |
| 157 |
|
| 158 |
static PyObject * |
| 159 |
_socket_ntohl(PyObject *module, PyObject *arg) |
| 160 |
{ |
| 161 |
PyObject *return_value = NULL; |
| 162 |
uint32_t x; |
| 163 |
|
| 164 |
if (!_PyLong_UInt32_Converter(arg, &x)) { |
| 165 |
goto exit; |
| 166 |
} |
| 167 |
return_value = _socket_ntohl_impl(module, x); |
| 168 |
|
| 169 |
exit: |
| 170 |
return return_value; |
| 171 |
} |
| 172 |
|
| 173 |
PyDoc_STRVAR(_socket_htons__doc__, |
| 174 |
"htons($module, integer, /)\n" |
| 175 |
"--\n" |
| 176 |
"\n" |
| 177 |
"Convert a 16-bit unsigned integer from host to network byte order."); |
| 178 |
|
| 179 |
#define _SOCKET_HTONS_METHODDEF \ |
| 180 |
{"htons", (PyCFunction)_socket_htons, METH_O, _socket_htons__doc__}, |
| 181 |
|
| 182 |
static PyObject * |
| 183 |
_socket_htons_impl(PyObject *module, uint16_t x); |
| 184 |
|
| 185 |
static PyObject * |
| 186 |
_socket_htons(PyObject *module, PyObject *arg) |
| 187 |
{ |
| 188 |
PyObject *return_value = NULL; |
| 189 |
uint16_t x; |
| 190 |
|
| 191 |
if (!_PyLong_UInt16_Converter(arg, &x)) { |
| 192 |
goto exit; |
| 193 |
} |
| 194 |
return_value = _socket_htons_impl(module, x); |
| 195 |
|
| 196 |
exit: |
| 197 |
return return_value; |
| 198 |
} |
| 199 |
|
| 200 |
PyDoc_STRVAR(_socket_htonl__doc__, |
| 201 |
"htonl($module, integer, /)\n" |
| 202 |
"--\n" |
| 203 |
"\n" |
| 204 |
"Convert a 32-bit unsigned integer from host to network byte order."); |
| 205 |
|
| 206 |
#define _SOCKET_HTONL_METHODDEF \ |
| 207 |
{"htonl", (PyCFunction)_socket_htonl, METH_O, _socket_htonl__doc__}, |
| 208 |
|
| 209 |
static PyObject * |
| 210 |
_socket_htonl_impl(PyObject *module, uint32_t x); |
| 211 |
|
| 212 |
static PyObject * |
| 213 |
_socket_htonl(PyObject *module, PyObject *arg) |
| 214 |
{ |
| 215 |
PyObject *return_value = NULL; |
| 216 |
uint32_t x; |
| 217 |
|
| 218 |
if (!_PyLong_UInt32_Converter(arg, &x)) { |
| 219 |
goto exit; |
| 220 |
} |
| 221 |
return_value = _socket_htonl_impl(module, x); |
| 222 |
|
| 223 |
exit: |
| 224 |
return return_value; |
| 225 |
} |
| 226 |
|
| 227 |
PyDoc_STRVAR(_socket_inet_aton__doc__, |
| 228 |
"inet_aton($module, ip_addr, /)\n" |
| 229 |
"--\n" |
| 230 |
"\n" |
| 231 |
"Convert an IP address in string format (123.45.67.89) to the 32-bit packed binary format used in low-level network functions."); |
| 232 |
|
| 233 |
#define _SOCKET_INET_ATON_METHODDEF \ |
| 234 |
{"inet_aton", (PyCFunction)_socket_inet_aton, METH_O, _socket_inet_aton__doc__}, |
| 235 |
|
| 236 |
static PyObject * |
| 237 |
_socket_inet_aton_impl(PyObject *module, const char *ip_addr); |
| 238 |
|
| 239 |
static PyObject * |
| 240 |
_socket_inet_aton(PyObject *module, PyObject *arg) |
| 241 |
{ |
| 242 |
PyObject *return_value = NULL; |
| 243 |
const char *ip_addr; |
| 244 |
|
| 245 |
if (!PyUnicode_Check(arg)) { |
| 246 |
_PyArg_BadArgument("inet_aton", "argument", "str", arg); |
| 247 |
goto exit; |
| 248 |
} |
| 249 |
Py_ssize_t ip_addr_length; |
| 250 |
ip_addr = PyUnicode_AsUTF8AndSize(arg, &ip_addr_length); |
| 251 |
if (ip_addr == NULL) { |
| 252 |
goto exit; |
| 253 |
} |
| 254 |
if (strlen(ip_addr) != (size_t)ip_addr_length) { |
| 255 |
PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 256 |
goto exit; |
| 257 |
} |
| 258 |
return_value = _socket_inet_aton_impl(module, ip_addr); |
| 259 |
|
| 260 |
exit: |
| 261 |
return return_value; |
| 262 |
} |
| 263 |
|
| 264 |
#if defined(HAVE_INET_NTOA) |
| 265 |
|
| 266 |
PyDoc_STRVAR(_socket_inet_ntoa__doc__, |
| 267 |
"inet_ntoa($module, packed_ip, /)\n" |
| 268 |
"--\n" |
| 269 |
"\n" |
| 270 |
"Convert an IP address from 32-bit packed binary format to string format."); |
| 271 |
|
| 272 |
#define _SOCKET_INET_NTOA_METHODDEF \ |
| 273 |
{"inet_ntoa", (PyCFunction)_socket_inet_ntoa, METH_O, _socket_inet_ntoa__doc__}, |
| 274 |
|
| 275 |
static PyObject * |
| 276 |
_socket_inet_ntoa_impl(PyObject *module, Py_buffer *packed_ip); |
| 277 |
|
| 278 |
static PyObject * |
| 279 |
_socket_inet_ntoa(PyObject *module, PyObject *arg) |
| 280 |
{ |
| 281 |
PyObject *return_value = NULL; |
| 282 |
Py_buffer packed_ip = {NULL, NULL}; |
| 283 |
|
| 284 |
if (PyObject_GetBuffer(arg, &packed_ip, PyBUF_SIMPLE) != 0) { |
| 285 |
goto exit; |
| 286 |
} |
| 287 |
return_value = _socket_inet_ntoa_impl(module, &packed_ip); |
| 288 |
|
| 289 |
exit: |
| 290 |
/* Cleanup for packed_ip */ |
| 291 |
if (packed_ip.obj) { |
| 292 |
PyBuffer_Release(&packed_ip); |
| 293 |
} |
| 294 |
|
| 295 |
return return_value; |
| 296 |
} |
| 297 |
|
| 298 |
#endif /* defined(HAVE_INET_NTOA) */ |
| 299 |
|
| 300 |
#if (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) |
| 301 |
|
| 302 |
PyDoc_STRVAR(_socket_if_nametoindex__doc__, |
| 303 |
"if_nametoindex($module, oname, /)\n" |
| 304 |
"--\n" |
| 305 |
"\n" |
| 306 |
"Returns the interface index corresponding to the interface name if_name."); |
| 307 |
|
| 308 |
#define _SOCKET_IF_NAMETOINDEX_METHODDEF \ |
| 309 |
{"if_nametoindex", (PyCFunction)_socket_if_nametoindex, METH_O, _socket_if_nametoindex__doc__}, |
| 310 |
|
| 311 |
static PyObject * |
| 312 |
_socket_if_nametoindex_impl(PyObject *module, PyObject *oname); |
| 313 |
|
| 314 |
static PyObject * |
| 315 |
_socket_if_nametoindex(PyObject *module, PyObject *arg) |
| 316 |
{ |
| 317 |
PyObject *return_value = NULL; |
| 318 |
PyObject *oname = NULL; |
| 319 |
|
| 320 |
if (!PyUnicode_FSConverter(arg, &oname)) { |
| 321 |
goto exit; |
| 322 |
} |
| 323 |
return_value = _socket_if_nametoindex_impl(module, oname); |
| 324 |
|
| 325 |
exit: |
| 326 |
/* Cleanup for oname */ |
| 327 |
Py_XDECREF(oname); |
| 328 |
|
| 329 |
return return_value; |
| 330 |
} |
| 331 |
|
| 332 |
#endif /* (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) */ |
| 333 |
|
| 334 |
#if (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) |
| 335 |
|
| 336 |
PyDoc_STRVAR(_socket_if_indextoname__doc__, |
| 337 |
"if_indextoname($module, if_index, /)\n" |
| 338 |
"--\n" |
| 339 |
"\n" |
| 340 |
"Returns the interface name corresponding to the interface index if_index."); |
| 341 |
|
| 342 |
#define _SOCKET_IF_INDEXTONAME_METHODDEF \ |
| 343 |
{"if_indextoname", (PyCFunction)_socket_if_indextoname, METH_O, _socket_if_indextoname__doc__}, |
| 344 |
|
| 345 |
static PyObject * |
| 346 |
_socket_if_indextoname_impl(PyObject *module, NET_IFINDEX index); |
| 347 |
|
| 348 |
static PyObject * |
| 349 |
_socket_if_indextoname(PyObject *module, PyObject *arg) |
| 350 |
{ |
| 351 |
PyObject *return_value = NULL; |
| 352 |
NET_IFINDEX index; |
| 353 |
|
| 354 |
if (!_PyLong_NetIfindex_Converter(arg, &index)) { |
| 355 |
goto exit; |
| 356 |
} |
| 357 |
return_value = _socket_if_indextoname_impl(module, index); |
| 358 |
|
| 359 |
exit: |
| 360 |
return return_value; |
| 361 |
} |
| 362 |
|
| 363 |
#endif /* (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) */ |
| 364 |
|
| 365 |
#ifndef _SOCKET_INET_NTOA_METHODDEF |
| 366 |
#define _SOCKET_INET_NTOA_METHODDEF |
| 367 |
#endif /* !defined(_SOCKET_INET_NTOA_METHODDEF) */ |
| 368 |
|
| 369 |
#ifndef _SOCKET_IF_NAMETOINDEX_METHODDEF |
| 370 |
#define _SOCKET_IF_NAMETOINDEX_METHODDEF |
| 371 |
#endif /* !defined(_SOCKET_IF_NAMETOINDEX_METHODDEF) */ |
| 372 |
|
| 373 |
#ifndef _SOCKET_IF_INDEXTONAME_METHODDEF |
| 374 |
#define _SOCKET_IF_INDEXTONAME_METHODDEF |
| 375 |
#endif /* !defined(_SOCKET_IF_INDEXTONAME_METHODDEF) */ |
| 376 |
/*[clinic end generated code: output=1bbfda36b02c29c2 input=a9049054013a1b77]*/ |