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_modsupport.h" // _PyArg_UnpackKeywords()
10
11 static int
12 sock_initobj_impl(PySocketSockObject *self, int family, int type, int proto,
13 PyObject *fdobj);
14
15 static int
sock_initobj(PyObject * self,PyObject * args,PyObject * kwargs)16 sock_initobj(PyObject *self, PyObject *args, PyObject *kwargs)
17 {
18 int return_value = -1;
19 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
20
21 #define NUM_KEYWORDS 4
22 static struct {
23 PyGC_Head _this_is_not_used;
24 PyObject_VAR_HEAD
25 PyObject *ob_item[NUM_KEYWORDS];
26 } _kwtuple = {
27 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
28 .ob_item = { &_Py_ID(family), &_Py_ID(type), &_Py_ID(proto), &_Py_ID(fileno), },
29 };
30 #undef NUM_KEYWORDS
31 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
32
33 #else // !Py_BUILD_CORE
34 # define KWTUPLE NULL
35 #endif // !Py_BUILD_CORE
36
37 static const char * const _keywords[] = {"family", "type", "proto", "fileno", NULL};
38 static _PyArg_Parser _parser = {
39 .keywords = _keywords,
40 .fname = "socket",
41 .kwtuple = KWTUPLE,
42 };
43 #undef KWTUPLE
44 PyObject *argsbuf[4];
45 PyObject * const *fastargs;
46 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
47 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
48 int family = -1;
49 int type = -1;
50 int proto = -1;
51 PyObject *fdobj = NULL;
52
53 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 4, 0, argsbuf);
54 if (!fastargs) {
55 goto exit;
56 }
57 if (!noptargs) {
58 goto skip_optional_pos;
59 }
60 if (fastargs[0]) {
61 family = PyLong_AsInt(fastargs[0]);
62 if (family == -1 && PyErr_Occurred()) {
63 goto exit;
64 }
65 if (!--noptargs) {
66 goto skip_optional_pos;
67 }
68 }
69 if (fastargs[1]) {
70 type = PyLong_AsInt(fastargs[1]);
71 if (type == -1 && PyErr_Occurred()) {
72 goto exit;
73 }
74 if (!--noptargs) {
75 goto skip_optional_pos;
76 }
77 }
78 if (fastargs[2]) {
79 proto = PyLong_AsInt(fastargs[2]);
80 if (proto == -1 && PyErr_Occurred()) {
81 goto exit;
82 }
83 if (!--noptargs) {
84 goto skip_optional_pos;
85 }
86 }
87 fdobj = fastargs[3];
88 skip_optional_pos:
89 return_value = sock_initobj_impl((PySocketSockObject *)self, family, type, proto, fdobj);
90
91 exit:
92 return return_value;
93 }
94
95 PyDoc_STRVAR(_socket_socket_ntohs__doc__,
96 "ntohs($self, x, /)\n"
97 "--\n"
98 "\n"
99 "Convert a 16-bit unsigned integer from network to host byte order.");
100
101 #define _SOCKET_SOCKET_NTOHS_METHODDEF \
102 {"ntohs", (PyCFunction)_socket_socket_ntohs, METH_O, _socket_socket_ntohs__doc__},
103
104 static PyObject *
105 _socket_socket_ntohs_impl(PySocketSockObject *self, int x);
106
107 static PyObject *
_socket_socket_ntohs(PySocketSockObject * self,PyObject * arg)108 _socket_socket_ntohs(PySocketSockObject *self, PyObject *arg)
109 {
110 PyObject *return_value = NULL;
111 int x;
112
113 x = PyLong_AsInt(arg);
114 if (x == -1 && PyErr_Occurred()) {
115 goto exit;
116 }
117 return_value = _socket_socket_ntohs_impl(self, x);
118
119 exit:
120 return return_value;
121 }
122
123 PyDoc_STRVAR(_socket_socket_htons__doc__,
124 "htons($self, x, /)\n"
125 "--\n"
126 "\n"
127 "Convert a 16-bit unsigned integer from host to network byte order.");
128
129 #define _SOCKET_SOCKET_HTONS_METHODDEF \
130 {"htons", (PyCFunction)_socket_socket_htons, METH_O, _socket_socket_htons__doc__},
131
132 static PyObject *
133 _socket_socket_htons_impl(PySocketSockObject *self, int x);
134
135 static PyObject *
_socket_socket_htons(PySocketSockObject * self,PyObject * arg)136 _socket_socket_htons(PySocketSockObject *self, PyObject *arg)
137 {
138 PyObject *return_value = NULL;
139 int x;
140
141 x = PyLong_AsInt(arg);
142 if (x == -1 && PyErr_Occurred()) {
143 goto exit;
144 }
145 return_value = _socket_socket_htons_impl(self, x);
146
147 exit:
148 return return_value;
149 }
150
151 PyDoc_STRVAR(_socket_socket_inet_aton__doc__,
152 "inet_aton($self, ip_addr, /)\n"
153 "--\n"
154 "\n"
155 "Convert an IP address in string format (123.45.67.89) to the 32-bit packed binary format used in low-level network functions.");
156
157 #define _SOCKET_SOCKET_INET_ATON_METHODDEF \
158 {"inet_aton", (PyCFunction)_socket_socket_inet_aton, METH_O, _socket_socket_inet_aton__doc__},
159
160 static PyObject *
161 _socket_socket_inet_aton_impl(PySocketSockObject *self, const char *ip_addr);
162
163 static PyObject *
_socket_socket_inet_aton(PySocketSockObject * self,PyObject * arg)164 _socket_socket_inet_aton(PySocketSockObject *self, PyObject *arg)
165 {
166 PyObject *return_value = NULL;
167 const char *ip_addr;
168
169 if (!PyUnicode_Check(arg)) {
170 _PyArg_BadArgument("inet_aton", "argument", "str", arg);
171 goto exit;
172 }
173 Py_ssize_t ip_addr_length;
174 ip_addr = PyUnicode_AsUTF8AndSize(arg, &ip_addr_length);
175 if (ip_addr == NULL) {
176 goto exit;
177 }
178 if (strlen(ip_addr) != (size_t)ip_addr_length) {
179 PyErr_SetString(PyExc_ValueError, "embedded null character");
180 goto exit;
181 }
182 return_value = _socket_socket_inet_aton_impl(self, ip_addr);
183
184 exit:
185 return return_value;
186 }
187
188 #if defined(HAVE_INET_NTOA)
189
190 PyDoc_STRVAR(_socket_socket_inet_ntoa__doc__,
191 "inet_ntoa($self, packed_ip, /)\n"
192 "--\n"
193 "\n"
194 "Convert an IP address from 32-bit packed binary format to string format.");
195
196 #define _SOCKET_SOCKET_INET_NTOA_METHODDEF \
197 {"inet_ntoa", (PyCFunction)_socket_socket_inet_ntoa, METH_O, _socket_socket_inet_ntoa__doc__},
198
199 static PyObject *
200 _socket_socket_inet_ntoa_impl(PySocketSockObject *self, Py_buffer *packed_ip);
201
202 static PyObject *
_socket_socket_inet_ntoa(PySocketSockObject * self,PyObject * arg)203 _socket_socket_inet_ntoa(PySocketSockObject *self, PyObject *arg)
204 {
205 PyObject *return_value = NULL;
206 Py_buffer packed_ip = {NULL, NULL};
207
208 if (PyObject_GetBuffer(arg, &packed_ip, PyBUF_SIMPLE) != 0) {
209 goto exit;
210 }
211 return_value = _socket_socket_inet_ntoa_impl(self, &packed_ip);
212
213 exit:
214 /* Cleanup for packed_ip */
215 if (packed_ip.obj) {
216 PyBuffer_Release(&packed_ip);
217 }
218
219 return return_value;
220 }
221
222 #endif /* defined(HAVE_INET_NTOA) */
223
224 #if (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS))
225
226 PyDoc_STRVAR(_socket_socket_if_nametoindex__doc__,
227 "if_nametoindex($self, oname, /)\n"
228 "--\n"
229 "\n"
230 "Returns the interface index corresponding to the interface name if_name.");
231
232 #define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF \
233 {"if_nametoindex", (PyCFunction)_socket_socket_if_nametoindex, METH_O, _socket_socket_if_nametoindex__doc__},
234
235 static PyObject *
236 _socket_socket_if_nametoindex_impl(PySocketSockObject *self, PyObject *oname);
237
238 static PyObject *
_socket_socket_if_nametoindex(PySocketSockObject * self,PyObject * arg)239 _socket_socket_if_nametoindex(PySocketSockObject *self, PyObject *arg)
240 {
241 PyObject *return_value = NULL;
242 PyObject *oname;
243
244 if (!PyUnicode_FSConverter(arg, &oname)) {
245 goto exit;
246 }
247 return_value = _socket_socket_if_nametoindex_impl(self, oname);
248
249 exit:
250 return return_value;
251 }
252
253 #endif /* (defined(HAVE_IF_NAMEINDEX) || defined(MS_WINDOWS)) */
254
255 #ifndef _SOCKET_SOCKET_INET_NTOA_METHODDEF
256 #define _SOCKET_SOCKET_INET_NTOA_METHODDEF
257 #endif /* !defined(_SOCKET_SOCKET_INET_NTOA_METHODDEF) */
258
259 #ifndef _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF
260 #define _SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF
261 #endif /* !defined(_SOCKET_SOCKET_IF_NAMETOINDEX_METHODDEF) */
262 /*[clinic end generated code: output=eb37b5d88a1e4661 input=a9049054013a1b77]*/
263