Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
c4fcfa34
Kaydet (Commit)
c4fcfa34
authored
Haz 07, 2002
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Major cleanup. Renamed static methods to avoid Py prefix. Other misc
cleanup as well, e.g. renamed NTinit to os_init.
üst
be8db07a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
290 additions
and
296 deletions
+290
-296
socketmodule.c
Modules/socketmodule.c
+290
-296
No files found.
Modules/socketmodule.c
Dosyayı görüntüle @
c4fcfa34
/* Socket module */
/*
This module provides an interface to Berkeley socket IPC.
Limitations:
-
o
nly AF_INET, AF_INET6 and AF_UNIX address families are supported in a
-
O
nly AF_INET, AF_INET6 and AF_UNIX address families are supported in a
portable manner, though AF_PACKET is supported under Linux.
- no read/write operations (use sendall/recv or makefile instead)
- additional restrictions apply on Windows (compensated for by socket.py)
- No read/write operations (use sendall/recv or makefile instead).
- Additional restrictions apply on some non-Unix platforms (compensated
for by socket.py).
Module interface:
...
...
@@ -46,34 +48,53 @@ Module interface:
networking code, but accepted since they are returned by the
getsockname() method.
Socket methods:
- s.accept() --> new socket object, sockaddr
- s.bind(sockaddr) --> None
- s.close() --> None
- s.connect(sockaddr) --> None
- s.connect_ex(sockaddr) --> 0 or errno (handy for e.g. async connect)
- s.fileno() --> file descriptor
- s.dup() --> same as socket.fromfd(os.dup(s.fileno(), ...)
- s.getpeername() --> sockaddr
- s.getsockname() --> sockaddr
- s.getsockopt(level, optname[, buflen]) --> int or string
- s.listen(backlog) --> None
- s.makefile([mode[, bufsize]]) --> file object
- s.recv(buflen [,flags]) --> string
- s.recvfrom(buflen [,flags]) --> string, sockaddr
- s.send(string [,flags]) --> nbytes
- s.sendall(string [,flags]) # tries to send everything in a loop
- s.sendto(string, [flags,] sockaddr) --> nbytes
- s.setblocking(0 | 1) --> None
- s.settimeout(None | float) -> None # Argument in seconds
- s.gettimeout() -> None or float seconds
- s.setsockopt(level, optname, value) --> None
- s.shutdown(how) --> None
- repr(s) --> "<socket object, fd=%d, family=%d, type=%d, protocol=%d>"
Local naming conventions:
- names starting with sock_ are socket object methods
- names starting with socket_ are module-level functions
- names starting with PySocket are exported through socketmodule.h
*/
/* Socket object documentation */
static
char
sock_doc
[]
=
"socket([family[, type[, proto]]]) -> socket object
\n
\
\n
\
Open a socket of the given type. The family argument specifies the
\n
\
address family; it defaults to AF_INET. The type argument specifies
\n
\
whether this is a stream (SOCK_STREAM, this is the default)
\n
\
or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,
\n
\
specifying the default protocol. Keyword arguments are accepted.
\n
\
\n
\
A socket object represents one endpoint of a network connection.
\n
\
\n
\
Methods of socket objects (keyword arguments not allowed):
\n
\
\n
\
accept() -- accept a connection, returning new socket and client address
\n
\
bind(addr) -- bind the socket to a local address
\n
\
close() -- close the socket
\n
\
connect(addr) -- connect the socket to a remote address
\n
\
connect_ex(addr) -- connect, return an error code instead of an exception
\n
\
dup() -- return a new socket object identical to the current one [*]
\n
\
fileno() -- return underlying file descriptor
\n
\
getpeername() -- return remote address [*]
\n
\
getsockname() -- return local address
\n
\
getsockopt(level, optname[, buflen]) -- get socket options
\n
\
gettimeout() -- return timeout or None
\n
\
listen(n) -- start listening for incoming connections
\n
\
makefile([mode, [bufsize]]) -- return a file object for the socket [*]
\n
\
recv(buflen[, flags]) -- receive data
\n
\
recvfrom(buflen[, flags]) -- receive data and sender's address
\n
\
sendall(data[, flags]) -- send all data
\n
\
send(data[, flags]) -- send data, may not send all of it
\n
\
sendto(data[, flags], addr) -- send data to a given address
\n
\
setblocking(0 | 1) -- set or clear the blocking I/O flag
\n
\
setsockopt(level, optname, value) -- set socket options
\n
\
settimeout(None | float) -- set or clear the timeout
\n
\
shutdown(how) -- shut down traffic in one or both directions
\n
\
\n
\
[*] not available on all platforms!"
;
#include "Python.h"
/* XXX This is a terrible mess of of platform-dependent preprocessor hacks.
...
...
@@ -237,10 +258,9 @@ const char *inet_ntop(int af, const void *src, char *dst, socklen_t size);
/* Global variable holding the exception type for errors detected
by this module (but not argument type or memory errors, etc.). */
static
PyObject
*
PySocket_Error
;
static
PyObject
*
PyH_Error
;
static
PyObject
*
PyGAI_Error
;
static
PyObject
*
socket_error
;
static
PyObject
*
socket_herror
;
static
PyObject
*
socket_gaierror
;
#ifdef RISCOS
/* Global variable which is !=0 if Python is running in a RISC OS taskwindow */
...
...
@@ -248,16 +268,16 @@ static int taskwindow;
#endif
/* A forward reference to the socket type object.
The
PySocketSock_T
ype variable contains pointers to various functions,
some of which call
PySocketSock_New(), which uses PySocketSock_T
ype, so
The
sock_t
ype variable contains pointers to various functions,
some of which call
new_sockobject(), which uses sock_t
ype, so
there has to be a circular reference. */
staticforward
PyTypeObject
PySocketSock_T
ype
;
staticforward
PyTypeObject
sock_t
ype
;
/* Convenience function to raise an error according to errno
and return a NULL pointer from a function. */
static
PyObject
*
PySocket_Er
r
(
void
)
set_erro
r
(
void
)
{
#ifdef MS_WINDOWS
int
err_no
=
WSAGetLastError
();
...
...
@@ -336,7 +356,7 @@ PySocket_Err(void)
v
=
Py_BuildValue
(
"(is)"
,
err_no
,
msg
);
if
(
v
!=
NULL
)
{
PyErr_SetObject
(
PySocket_E
rror
,
v
);
PyErr_SetObject
(
socket_e
rror
,
v
);
Py_DECREF
(
v
);
}
return
NULL
;
...
...
@@ -371,7 +391,7 @@ PySocket_Err(void)
}
v
=
Py_BuildValue
(
"(is)"
,
myerrorcode
,
outbuf
);
if
(
v
!=
NULL
)
{
PyErr_SetObject
(
PySocket_E
rror
,
v
);
PyErr_SetObject
(
socket_e
rror
,
v
);
Py_DECREF
(
v
);
}
return
NULL
;
...
...
@@ -379,12 +399,12 @@ PySocket_Err(void)
}
#endif
return
PyErr_SetFromErrno
(
PySocket_E
rror
);
return
PyErr_SetFromErrno
(
socket_e
rror
);
}
static
PyObject
*
PyH_Er
r
(
int
h_error
)
set_herro
r
(
int
h_error
)
{
PyObject
*
v
;
...
...
@@ -394,7 +414,7 @@ PyH_Err(int h_error)
v
=
Py_BuildValue
(
"(is)"
,
h_error
,
"host not found"
);
#endif
if
(
v
!=
NULL
)
{
PyErr_SetObject
(
PyH_E
rror
,
v
);
PyErr_SetObject
(
socket_he
rror
,
v
);
Py_DECREF
(
v
);
}
...
...
@@ -403,14 +423,14 @@ PyH_Err(int h_error)
static
PyObject
*
PyGAI_Er
r
(
int
error
)
set_gaierro
r
(
int
error
)
{
PyObject
*
v
;
#ifdef EAI_SYSTEM
/* EAI_SYSTEM is not available on Windows XP. */
if
(
error
==
EAI_SYSTEM
)
return
PySocket_Er
r
();
return
set_erro
r
();
#endif
#ifdef HAVE_GAI_STRERROR
...
...
@@ -419,7 +439,7 @@ PyGAI_Err(int error)
v
=
Py_BuildValue
(
"(is)"
,
error
,
"getaddrinfo failed"
);
#endif
if
(
v
!=
NULL
)
{
PyErr_SetObject
(
PyGAI_E
rror
,
v
);
PyErr_SetObject
(
socket_gaie
rror
,
v
);
Py_DECREF
(
v
);
}
...
...
@@ -439,7 +459,7 @@ timeout_err(void)
#endif
if
(
v
!=
NULL
)
{
PyErr_SetObject
(
PySocket_E
rror
,
v
);
PyErr_SetObject
(
socket_e
rror
,
v
);
Py_DECREF
(
v
);
}
...
...
@@ -541,7 +561,7 @@ init_sockobject(PySocketSockObject *s,
s
->
sock_blocking
=
1
;
/* Start in blocking mode */
s
->
sock_timeout
=
-
1
.
0
;
/* Start without timeout */
s
->
errorhandler
=
&
PySocket_Er
r
;
s
->
errorhandler
=
&
set_erro
r
;
#ifdef RISCOS
if
(
taskwindow
)
socketioctl
(
s
->
sock_fd
,
0x80046679
,
(
u_long
*
)
&
block
);
...
...
@@ -555,11 +575,11 @@ init_sockobject(PySocketSockObject *s,
in NEWOBJ()). */
static
PySocketSockObject
*
PySocketSock_New
(
SOCKET_T
fd
,
int
family
,
int
type
,
int
proto
)
new_sockobject
(
SOCKET_T
fd
,
int
family
,
int
type
,
int
proto
)
{
PySocketSockObject
*
s
;
s
=
(
PySocketSockObject
*
)
PyType_GenericNew
(
&
PySocketSock_T
ype
,
NULL
,
NULL
);
PyType_GenericNew
(
&
sock_t
ype
,
NULL
,
NULL
);
if
(
s
!=
NULL
)
init_sockobject
(
s
,
fd
,
family
,
type
,
proto
);
return
s
;
...
...
@@ -594,7 +614,7 @@ setipaddr(char *name, struct sockaddr *addr_ret, int af)
hints
.
ai_flags
=
AI_PASSIVE
;
error
=
getaddrinfo
(
NULL
,
"0"
,
&
hints
,
&
res
);
if
(
error
)
{
PyGAI_Er
r
(
error
);
set_gaierro
r
(
error
);
return
-
1
;
}
switch
(
res
->
ai_family
)
{
...
...
@@ -608,13 +628,13 @@ setipaddr(char *name, struct sockaddr *addr_ret, int af)
#endif
default:
freeaddrinfo
(
res
);
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
"unsupported address family"
);
return
-
1
;
}
if
(
res
->
ai_next
)
{
freeaddrinfo
(
res
);
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
"wildcard resolved to multiple address"
);
return
-
1
;
}
...
...
@@ -625,7 +645,7 @@ setipaddr(char *name, struct sockaddr *addr_ret, int af)
if
(
name
[
0
]
==
'<'
&&
strcmp
(
name
,
"<broadcast>"
)
==
0
)
{
struct
sockaddr_in
*
sin
;
if
(
af
!=
PF_INET
&&
af
!=
PF_UNSPEC
)
{
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
"address family mismatched"
);
return
-
1
;
}
...
...
@@ -650,7 +670,7 @@ setipaddr(char *name, struct sockaddr *addr_ret, int af)
}
#endif
if
(
error
)
{
PyGAI_Er
r
(
error
);
set_gaierro
r
(
error
);
return
-
1
;
}
memcpy
((
char
*
)
addr_ret
,
res
->
ai_addr
,
res
->
ai_addrlen
);
...
...
@@ -663,7 +683,7 @@ setipaddr(char *name, struct sockaddr *addr_ret, int af)
return
16
;
#endif
default:
PyErr_SetString
(
PySocket_E
rror
,
"unknown address family"
);
PyErr_SetString
(
socket_e
rror
,
"unknown address family"
);
return
-
1
;
}
}
...
...
@@ -682,7 +702,7 @@ makeipaddr(struct sockaddr *addr, int addrlen)
error
=
getnameinfo
(
addr
,
addrlen
,
buf
,
sizeof
(
buf
),
NULL
,
0
,
NI_NUMERICHOST
);
if
(
error
)
{
PyGAI_Er
r
(
error
);
set_gaierro
r
(
error
);
return
NULL
;
}
return
PyString_FromString
(
buf
);
...
...
@@ -808,7 +828,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
if
(
!
PyArg_Parse
(
args
,
"t#"
,
&
path
,
&
len
))
return
0
;
if
(
len
>
sizeof
addr
->
sun_path
)
{
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
"AF_UNIX path too long"
);
return
0
;
}
...
...
@@ -905,7 +925,7 @@ getsockaddrarg(PySocketSockObject *s, PyObject *args,
/* More cases here... */
default:
PyErr_SetString
(
PySocket_E
rror
,
"getsockaddrarg: bad family"
);
PyErr_SetString
(
socket_e
rror
,
"getsockaddrarg: bad family"
);
return
0
;
}
...
...
@@ -954,7 +974,7 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
/* More cases here... */
default:
PyErr_SetString
(
PySocket_E
rror
,
"getsockaddrlen: bad family"
);
PyErr_SetString
(
socket_e
rror
,
"getsockaddrlen: bad family"
);
return
0
;
}
...
...
@@ -964,7 +984,7 @@ getsockaddrlen(PySocketSockObject *s, socklen_t *len_ret)
/* s.accept() method */
static
PyObject
*
PySocketS
ock_accept
(
PySocketSockObject
*
s
)
s
ock_accept
(
PySocketSockObject
*
s
)
{
char
addrbuf
[
256
];
SOCKET_T
newfd
;
...
...
@@ -1025,10 +1045,10 @@ PySocketSock_accept(PySocketSockObject *s)
/* Create the new object with unspecified family,
to avoid calls to bind() etc. on it. */
sock
=
(
PyObject
*
)
PySocketSock_New
(
newfd
,
s
->
sock_family
,
s
->
sock_type
,
s
->
sock_proto
);
sock
=
(
PyObject
*
)
new_sockobject
(
newfd
,
s
->
sock_family
,
s
->
sock_type
,
s
->
sock_proto
);
if
(
sock
==
NULL
)
{
SOCKETCLOSE
(
newfd
);
...
...
@@ -1057,7 +1077,7 @@ info is a pair (hostaddr, port).";
/* s.setblocking(1 | 0) method */
static
PyObject
*
PySocketS
ock_setblocking
(
PySocketSockObject
*
s
,
PyObject
*
arg
)
s
ock_setblocking
(
PySocketSockObject
*
s
,
PyObject
*
arg
)
{
int
block
;
...
...
@@ -1086,7 +1106,7 @@ This uses the FIONBIO ioctl with the O_NDELAY flag.";
Causes an exception to be raised when the given time has
elapsed when performing a blocking socket operation. */
static
PyObject
*
PySocketS
ock_settimeout
(
PySocketSockObject
*
s
,
PyObject
*
arg
)
s
ock_settimeout
(
PySocketSockObject
*
s
,
PyObject
*
arg
)
{
double
value
;
...
...
@@ -1132,7 +1152,7 @@ giving seconds, or None. Setting a timeout of None disables timeout.";
/* s.gettimeout() method.
Returns the timeout associated with a socket. */
static
PyObject
*
PySocketS
ock_gettimeout
(
PySocketSockObject
*
s
)
s
ock_gettimeout
(
PySocketSockObject
*
s
)
{
if
(
s
->
sock_timeout
<
0
.
0
)
{
Py_INCREF
(
Py_None
);
...
...
@@ -1153,7 +1173,7 @@ operations are disabled.";
/* s.sleeptaskw(1 | 0) method */
static
PyObject
*
PySocketS
ock_sleeptaskw
(
PySocketSockObject
*
s
,
PyObject
*
args
)
s
ock_sleeptaskw
(
PySocketSockObject
*
s
,
PyObject
*
args
)
{
int
block
;
int
delay_flag
;
...
...
@@ -1179,7 +1199,7 @@ Allow sleeps in taskwindows.";
use optional built-in module 'struct' to encode the string. */
static
PyObject
*
PySocketS
ock_setsockopt
(
PySocketSockObject
*
s
,
PyObject
*
args
)
s
ock_setsockopt
(
PySocketSockObject
*
s
,
PyObject
*
args
)
{
int
level
;
int
optname
;
...
...
@@ -1219,7 +1239,7 @@ The value argument can either be an integer or a string.";
use optional built-in module 'struct' to decode the string. */
static
PyObject
*
PySocketS
ock_getsockopt
(
PySocketSockObject
*
s
,
PyObject
*
args
)
s
ock_getsockopt
(
PySocketSockObject
*
s
,
PyObject
*
args
)
{
int
level
;
int
optname
;
...
...
@@ -1229,7 +1249,7 @@ PySocketSock_getsockopt(PySocketSockObject *s, PyObject *args)
#ifdef __BEOS__
/* We have incomplete socket support. */
PyErr_SetString
(
PySocket_E
rror
,
"getsockopt not supported"
);
PyErr_SetString
(
socket_e
rror
,
"getsockopt not supported"
);
return
NULL
;
#else
...
...
@@ -1247,7 +1267,7 @@ PySocketSock_getsockopt(PySocketSockObject *s, PyObject *args)
return
PyInt_FromLong
(
flag
);
}
if
(
buflen
<=
0
||
buflen
>
1024
)
{
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
"getsockopt buflen out of range"
);
return
NULL
;
}
...
...
@@ -1276,7 +1296,7 @@ string of that length; otherwise it is an integer.";
/* s.bind(sockaddr) method */
static
PyObject
*
PySocketS
ock_bind
(
PySocketSockObject
*
s
,
PyObject
*
addro
)
s
ock_bind
(
PySocketSockObject
*
s
,
PyObject
*
addro
)
{
struct
sockaddr
*
addr
;
int
addrlen
;
...
...
@@ -1306,7 +1326,7 @@ sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])";
will surely fail. */
static
PyObject
*
PySocketS
ock_close
(
PySocketSockObject
*
s
)
s
ock_close
(
PySocketSockObject
*
s
)
{
SOCKET_T
fd
;
...
...
@@ -1329,7 +1349,7 @@ Close the socket. It cannot be used after this call.";
/* s.connect(sockaddr) method */
static
PyObject
*
PySocketS
ock_connect
(
PySocketSockObject
*
s
,
PyObject
*
addro
)
s
ock_connect
(
PySocketSockObject
*
s
,
PyObject
*
addro
)
{
struct
sockaddr
*
addr
;
int
addrlen
;
...
...
@@ -1396,7 +1416,7 @@ is a pair (host, port).";
/* s.connect_ex(sockaddr) method */
static
PyObject
*
PySocketS
ock_connect_ex
(
PySocketSockObject
*
s
,
PyObject
*
addro
)
s
ock_connect_ex
(
PySocketSockObject
*
s
,
PyObject
*
addro
)
{
struct
sockaddr
*
addr
;
int
addrlen
;
...
...
@@ -1467,7 +1487,7 @@ instead of raising an exception when an error occurs.";
/* s.fileno() method */
static
PyObject
*
PySocketS
ock_fileno
(
PySocketSockObject
*
s
)
s
ock_fileno
(
PySocketSockObject
*
s
)
{
#if SIZEOF_SOCKET_T <= SIZEOF_LONG
return
PyInt_FromLong
((
long
)
s
->
sock_fd
);
...
...
@@ -1486,7 +1506,7 @@ Return the integer file descriptor of the socket.";
/* s.dup() method */
static
PyObject
*
PySocketS
ock_dup
(
PySocketSockObject
*
s
)
s
ock_dup
(
PySocketSockObject
*
s
)
{
SOCKET_T
newfd
;
PyObject
*
sock
;
...
...
@@ -1494,10 +1514,10 @@ PySocketSock_dup(PySocketSockObject *s)
newfd
=
dup
(
s
->
sock_fd
);
if
(
newfd
<
0
)
return
s
->
errorhandler
();
sock
=
(
PyObject
*
)
PySocketSock_New
(
newfd
,
s
->
sock_family
,
s
->
sock_type
,
s
->
sock_proto
);
sock
=
(
PyObject
*
)
new_sockobject
(
newfd
,
s
->
sock_family
,
s
->
sock_type
,
s
->
sock_proto
);
if
(
sock
==
NULL
)
SOCKETCLOSE
(
newfd
);
return
sock
;
...
...
@@ -1514,7 +1534,7 @@ Return a new socket object connected to the same system resource.";
/* s.getsockname() method */
static
PyObject
*
PySocketS
ock_getsockname
(
PySocketSockObject
*
s
)
s
ock_getsockname
(
PySocketSockObject
*
s
)
{
char
addrbuf
[
256
];
int
res
;
...
...
@@ -1542,7 +1562,7 @@ info is a pair (hostaddr, port).";
/* s.getpeername() method */
static
PyObject
*
PySocketS
ock_getpeername
(
PySocketSockObject
*
s
)
s
ock_getpeername
(
PySocketSockObject
*
s
)
{
char
addrbuf
[
256
];
int
res
;
...
...
@@ -1571,7 +1591,7 @@ info is a pair (hostaddr, port).";
/* s.listen(n) method */
static
PyObject
*
PySocketS
ock_listen
(
PySocketSockObject
*
s
,
PyObject
*
arg
)
s
ock_listen
(
PySocketSockObject
*
s
,
PyObject
*
arg
)
{
int
backlog
;
int
res
;
...
...
@@ -1607,7 +1627,7 @@ will allow before refusing new connections.";
The mode argument specifies 'r' or 'w' passed to fdopen(). */
static
PyObject
*
PySocketS
ock_makefile
(
PySocketSockObject
*
s
,
PyObject
*
args
)
s
ock_makefile
(
PySocketSockObject
*
s
,
PyObject
*
args
)
{
extern
int
fclose
(
FILE
*
);
char
*
mode
=
"r"
;
...
...
@@ -1656,7 +1676,7 @@ The mode and buffersize arguments are as for the built-in open() function.";
/* s.recv(nbytes [,flags]) method */
static
PyObject
*
PySocketS
ock_recv
(
PySocketSockObject
*
s
,
PyObject
*
args
)
s
ock_recv
(
PySocketSockObject
*
s
,
PyObject
*
args
)
{
int
len
,
n
,
flags
=
0
;
PyObject
*
buf
;
...
...
@@ -1706,7 +1726,7 @@ the remote end is closed and all data is read, return the empty string.";
/* s.recvfrom(nbytes [,flags]) method */
static
PyObject
*
PySocketS
ock_recvfrom
(
PySocketSockObject
*
s
,
PyObject
*
args
)
s
ock_recvfrom
(
PySocketSockObject
*
s
,
PyObject
*
args
)
{
char
addrbuf
[
256
];
PyObject
*
buf
=
NULL
;
...
...
@@ -1774,7 +1794,7 @@ Like recv(buffersize, flags) but also return the sender's address info.";
/* s.send(data [,flags]) method */
static
PyObject
*
PySocketS
ock_send
(
PySocketSockObject
*
s
,
PyObject
*
args
)
s
ock_send
(
PySocketSockObject
*
s
,
PyObject
*
args
)
{
char
*
buf
;
int
len
,
n
,
flags
=
0
;
...
...
@@ -1809,7 +1829,7 @@ sent; this may be less than len(data) if the network is busy.";
/* s.sendall(data [,flags]) method */
static
PyObject
*
PySocketS
ock_sendall
(
PySocketSockObject
*
s
,
PyObject
*
args
)
s
ock_sendall
(
PySocketSockObject
*
s
,
PyObject
*
args
)
{
char
*
buf
;
int
len
,
n
,
flags
=
0
;
...
...
@@ -1853,7 +1873,7 @@ to tell how much data has been sent.";
/* s.sendto(data, [flags,] sockaddr) method */
static
PyObject
*
PySocketS
ock_sendto
(
PySocketSockObject
*
s
,
PyObject
*
args
)
s
ock_sendto
(
PySocketSockObject
*
s
,
PyObject
*
args
)
{
PyObject
*
addro
;
char
*
buf
;
...
...
@@ -1897,7 +1917,7 @@ For IP sockets, the address is a pair (hostaddr, port).";
/* s.shutdown(how) method */
static
PyObject
*
PySocketS
ock_shutdown
(
PySocketSockObject
*
s
,
PyObject
*
arg
)
s
ock_shutdown
(
PySocketSockObject
*
s
,
PyObject
*
arg
)
{
int
how
;
int
res
;
...
...
@@ -1923,59 +1943,59 @@ of the socket (flag == 1), or both ends (flag == 2).";
/* List of methods for socket objects */
static
PyMethodDef
PySocketS
ock_methods
[]
=
{
{
"accept"
,
(
PyCFunction
)
PySocketS
ock_accept
,
METH_NOARGS
,
static
PyMethodDef
s
ock_methods
[]
=
{
{
"accept"
,
(
PyCFunction
)
s
ock_accept
,
METH_NOARGS
,
accept_doc
},
{
"bind"
,
(
PyCFunction
)
PySocketS
ock_bind
,
METH_O
,
{
"bind"
,
(
PyCFunction
)
s
ock_bind
,
METH_O
,
bind_doc
},
{
"close"
,
(
PyCFunction
)
PySocketS
ock_close
,
METH_NOARGS
,
{
"close"
,
(
PyCFunction
)
s
ock_close
,
METH_NOARGS
,
close_doc
},
{
"connect"
,
(
PyCFunction
)
PySocketS
ock_connect
,
METH_O
,
{
"connect"
,
(
PyCFunction
)
s
ock_connect
,
METH_O
,
connect_doc
},
{
"connect_ex"
,
(
PyCFunction
)
PySocketS
ock_connect_ex
,
METH_O
,
{
"connect_ex"
,
(
PyCFunction
)
s
ock_connect_ex
,
METH_O
,
connect_ex_doc
},
#ifndef NO_DUP
{
"dup"
,
(
PyCFunction
)
PySocketS
ock_dup
,
METH_NOARGS
,
{
"dup"
,
(
PyCFunction
)
s
ock_dup
,
METH_NOARGS
,
dup_doc
},
#endif
{
"fileno"
,
(
PyCFunction
)
PySocketS
ock_fileno
,
METH_NOARGS
,
{
"fileno"
,
(
PyCFunction
)
s
ock_fileno
,
METH_NOARGS
,
fileno_doc
},
#ifdef HAVE_GETPEERNAME
{
"getpeername"
,
(
PyCFunction
)
PySocketS
ock_getpeername
,
{
"getpeername"
,
(
PyCFunction
)
s
ock_getpeername
,
METH_NOARGS
,
getpeername_doc
},
#endif
{
"getsockname"
,
(
PyCFunction
)
PySocketS
ock_getsockname
,
{
"getsockname"
,
(
PyCFunction
)
s
ock_getsockname
,
METH_NOARGS
,
getsockname_doc
},
{
"getsockopt"
,
(
PyCFunction
)
PySocketS
ock_getsockopt
,
METH_VARARGS
,
{
"getsockopt"
,
(
PyCFunction
)
s
ock_getsockopt
,
METH_VARARGS
,
getsockopt_doc
},
{
"listen"
,
(
PyCFunction
)
PySocketS
ock_listen
,
METH_O
,
{
"listen"
,
(
PyCFunction
)
s
ock_listen
,
METH_O
,
listen_doc
},
#ifndef NO_DUP
{
"makefile"
,
(
PyCFunction
)
PySocketS
ock_makefile
,
METH_VARARGS
,
{
"makefile"
,
(
PyCFunction
)
s
ock_makefile
,
METH_VARARGS
,
makefile_doc
},
#endif
{
"recv"
,
(
PyCFunction
)
PySocketS
ock_recv
,
METH_VARARGS
,
{
"recv"
,
(
PyCFunction
)
s
ock_recv
,
METH_VARARGS
,
recv_doc
},
{
"recvfrom"
,
(
PyCFunction
)
PySocketS
ock_recvfrom
,
METH_VARARGS
,
{
"recvfrom"
,
(
PyCFunction
)
s
ock_recvfrom
,
METH_VARARGS
,
recvfrom_doc
},
{
"send"
,
(
PyCFunction
)
PySocketS
ock_send
,
METH_VARARGS
,
{
"send"
,
(
PyCFunction
)
s
ock_send
,
METH_VARARGS
,
send_doc
},
{
"sendall"
,
(
PyCFunction
)
PySocketS
ock_sendall
,
METH_VARARGS
,
{
"sendall"
,
(
PyCFunction
)
s
ock_sendall
,
METH_VARARGS
,
sendall_doc
},
{
"sendto"
,
(
PyCFunction
)
PySocketS
ock_sendto
,
METH_VARARGS
,
{
"sendto"
,
(
PyCFunction
)
s
ock_sendto
,
METH_VARARGS
,
sendto_doc
},
{
"setblocking"
,
(
PyCFunction
)
PySocketS
ock_setblocking
,
METH_O
,
{
"setblocking"
,
(
PyCFunction
)
s
ock_setblocking
,
METH_O
,
setblocking_doc
},
{
"settimeout"
,
(
PyCFunction
)
PySocketS
ock_settimeout
,
METH_O
,
{
"settimeout"
,
(
PyCFunction
)
s
ock_settimeout
,
METH_O
,
settimeout_doc
},
{
"gettimeout"
,
(
PyCFunction
)
PySocketS
ock_gettimeout
,
METH_NOARGS
,
{
"gettimeout"
,
(
PyCFunction
)
s
ock_gettimeout
,
METH_NOARGS
,
gettimeout_doc
},
{
"setsockopt"
,
(
PyCFunction
)
PySocketS
ock_setsockopt
,
METH_VARARGS
,
{
"setsockopt"
,
(
PyCFunction
)
s
ock_setsockopt
,
METH_VARARGS
,
setsockopt_doc
},
{
"shutdown"
,
(
PyCFunction
)
PySocketS
ock_shutdown
,
METH_O
,
{
"shutdown"
,
(
PyCFunction
)
s
ock_shutdown
,
METH_O
,
shutdown_doc
},
#ifdef RISCOS
{
"sleeptaskw"
,
(
PyCFunction
)
PySocketS
ock_sleeptaskw
,
METH_VARARGS
,
{
"sleeptaskw"
,
(
PyCFunction
)
s
ock_sleeptaskw
,
METH_VARARGS
,
sleeptaskw_doc
},
#endif
{
NULL
,
NULL
}
/* sentinel */
...
...
@@ -1986,7 +2006,7 @@ static PyMethodDef PySocketSock_methods[] = {
First close the file description. */
static
void
PySocketS
ock_dealloc
(
PySocketSockObject
*
s
)
s
ock_dealloc
(
PySocketSockObject
*
s
)
{
if
(
s
->
sock_fd
!=
-
1
)
(
void
)
SOCKETCLOSE
(
s
->
sock_fd
);
...
...
@@ -1995,7 +2015,7 @@ PySocketSock_dealloc(PySocketSockObject *s)
static
PyObject
*
PySocketS
ock_repr
(
PySocketSockObject
*
s
)
s
ock_repr
(
PySocketSockObject
*
s
)
{
char
buf
[
512
];
#if SIZEOF_SOCKET_T > SIZEOF_LONG
...
...
@@ -2022,7 +2042,7 @@ PySocketSock_repr(PySocketSockObject *s)
/* Create a new, uninitialized socket object. */
static
PyObject
*
PySocketS
ock_new
(
PyTypeObject
*
type
,
PyObject
*
args
,
PyObject
*
kwds
)
s
ock_new
(
PyTypeObject
*
type
,
PyObject
*
args
,
PyObject
*
kwds
)
{
PyObject
*
new
;
...
...
@@ -2030,7 +2050,7 @@ PySocketSock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
if
(
new
!=
NULL
)
{
((
PySocketSockObject
*
)
new
)
->
sock_fd
=
-
1
;
((
PySocketSockObject
*
)
new
)
->
sock_timeout
=
-
1
.
0
;
((
PySocketSockObject
*
)
new
)
->
errorhandler
=
&
PySocket_Er
r
;
((
PySocketSockObject
*
)
new
)
->
errorhandler
=
&
set_erro
r
;
}
return
new
;
}
...
...
@@ -2040,7 +2060,7 @@ PySocketSock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
/*ARGSUSED*/
static
int
PySocketS
ock_init
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwds
)
s
ock_init
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwds
)
{
PySocketSockObject
*
s
=
(
PySocketSockObject
*
)
self
;
SOCKET_T
fd
;
...
...
@@ -2062,7 +2082,7 @@ PySocketSock_init(PyObject *self, PyObject *args, PyObject *kwds)
if
(
fd
<
0
)
#endif
{
PySocket_Er
r
();
set_erro
r
();
return
-
1
;
}
init_sockobject
(
s
,
fd
,
family
,
type
,
proto
);
...
...
@@ -2079,55 +2099,18 @@ PySocketSock_init(PyObject *self, PyObject *args, PyObject *kwds)
/* Type object for socket objects. */
static
char
socket_doc
[]
=
"socket([family[, type[, proto]]]) -> socket object
\n
\
\n
\
Open a socket of the given type. The family argument specifies the
\n
\
address family; it defaults to AF_INET. The type argument specifies
\n
\
whether this is a stream (SOCK_STREAM, this is the default)
\n
\
or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,
\n
\
specifying the default protocol.
\n
\
\n
\
A socket represents one endpoint of a network connection.
\n
\
\n
\
Methods:
\n
\
\n
\
accept() -- accept a connection, returning new socket and client address
\n
\
bind() -- bind the socket to a local address
\n
\
close() -- close the socket
\n
\
connect() -- connect the socket to a remote address
\n
\
connect_ex() -- connect, return an error code instead of an exception
\n
\
dup() -- return a new socket object identical to the current one (*)
\n
\
fileno() -- return underlying file descriptor
\n
\
getpeername() -- return remote address (*)
\n
\
getsockname() -- return local address
\n
\
getsockopt() -- get socket options
\n
\
listen() -- start listening for incoming connections
\n
\
makefile() -- return a file object corresponding to the socket (*)
\n
\
recv() -- receive data
\n
\
recvfrom() -- receive data and sender's address
\n
\
send() -- send data, may not send all of it
\n
\
sendall() -- send all data
\n
\
sendto() -- send data to a given address
\n
\
setblocking() -- set or clear the blocking I/O flag
\n
\
settimeout() -- set or clear the timeout
\n
\
setsockopt() -- set socket options
\n
\
shutdown() -- shut down traffic in one or both directions
\n
\
\n
\
(*) not available on all platforms!)"
;
static
PyTypeObject
PySocketSock_Type
=
{
static
PyTypeObject
sock_type
=
{
PyObject_HEAD_INIT
(
0
)
/* Must fill in type value later */
0
,
/* ob_size */
"_socket.socket"
,
/* tp_name */
sizeof
(
PySocketSockObject
),
/* tp_basicsize */
0
,
/* tp_itemsize */
(
destructor
)
PySocketSock_dealloc
,
/* tp_dealloc */
(
destructor
)
sock_dealloc
,
/* tp_dealloc */
0
,
/* tp_print */
0
,
/* tp_getattr */
0
,
/* tp_setattr */
0
,
/* tp_compare */
(
reprfunc
)
PySocketSock_repr
,
/* tp_repr */
(
reprfunc
)
sock_repr
,
/* tp_repr */
0
,
/* tp_as_number */
0
,
/* tp_as_sequence */
0
,
/* tp_as_mapping */
...
...
@@ -2138,14 +2121,14 @@ static PyTypeObject PySocketSock_Type = {
0
,
/* tp_setattro */
0
,
/* tp_as_buffer */
Py_TPFLAGS_DEFAULT
|
Py_TPFLAGS_BASETYPE
,
/* tp_flags */
sock
et
_doc
,
/* tp_doc */
sock_doc
,
/* tp_doc */
0
,
/* tp_traverse */
0
,
/* tp_clear */
0
,
/* tp_richcompare */
0
,
/* tp_weaklistoffset */
0
,
/* tp_iter */
0
,
/* tp_iternext */
PySocketSock_methods
,
/* tp_methods */
sock_methods
,
/* tp_methods */
0
,
/* tp_members */
0
,
/* tp_getset */
0
,
/* tp_base */
...
...
@@ -2153,9 +2136,9 @@ static PyTypeObject PySocketSock_Type = {
0
,
/* tp_descr_get */
0
,
/* tp_descr_set */
0
,
/* tp_dictoffset */
PySocketSock_init
,
/* tp_init */
sock_init
,
/* tp_init */
0
,
/* set below */
/* tp_alloc */
PySocketSock_new
,
/* tp_new */
sock_new
,
/* tp_new */
0
,
/* set below */
/* tp_free */
};
...
...
@@ -2164,7 +2147,7 @@ static PyTypeObject PySocketSock_Type = {
/*ARGSUSED*/
static
PyObject
*
PyS
ocket_gethostname
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_gethostname
(
PyObject
*
self
,
PyObject
*
args
)
{
char
buf
[
1024
];
int
res
;
...
...
@@ -2174,7 +2157,7 @@ PySocket_gethostname(PyObject *self, PyObject *args)
res
=
gethostname
(
buf
,
(
int
)
sizeof
buf
-
1
);
Py_END_ALLOW_THREADS
if
(
res
<
0
)
return
PySocket_Er
r
();
return
set_erro
r
();
buf
[
sizeof
buf
-
1
]
=
'\0'
;
return
PyString_FromString
(
buf
);
}
...
...
@@ -2189,7 +2172,7 @@ Return the current host name.";
/*ARGSUSED*/
static
PyObject
*
PyS
ocket_gethostbyname
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_gethostbyname
(
PyObject
*
self
,
PyObject
*
args
)
{
char
*
name
;
struct
sockaddr_storage
addrbuf
;
...
...
@@ -2222,9 +2205,9 @@ gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
if
(
h
==
NULL
)
{
/* Let's get real error message to return */
#ifndef RISCOS
PyH_Er
r
(
h_errno
);
set_herro
r
(
h_errno
);
#else
PyErr_SetString
(
PySocket_E
rror
,
"host not found"
);
PyErr_SetString
(
socket_e
rror
,
"host not found"
);
#endif
return
NULL
;
}
...
...
@@ -2232,11 +2215,11 @@ gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
if
(
h
->
h_addrtype
!=
af
)
{
#ifdef HAVE_STRERROR
/* Let's get real error message to return */
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
(
char
*
)
strerror
(
EAFNOSUPPORT
));
#else
PyErr_SetString
(
PySocket_E
rror
,
socket_e
rror
,
"Address family not supported by protocol family"
);
#endif
return
NULL
;
...
...
@@ -2318,7 +2301,7 @@ gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
#endif
default:
/* can't happen */
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
"unsupported address family"
);
return
NULL
;
}
...
...
@@ -2346,7 +2329,7 @@ gethost_common(struct hostent *h, struct sockaddr *addr, int alen, int af)
/*ARGSUSED*/
static
PyObject
*
PyS
ocket_gethostbyname_ex
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_gethostbyname_ex
(
PyObject
*
self
,
PyObject
*
args
)
{
char
*
name
;
struct
hostent
*
h
;
...
...
@@ -2414,7 +2397,7 @@ for a host. The host argument is a string giving a host name or IP number.";
/*ARGSUSED*/
static
PyObject
*
PyS
ocket_gethostbyaddr
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_gethostbyaddr
(
PyObject
*
self
,
PyObject
*
args
)
{
#ifdef ENABLE_IPV6
struct
sockaddr_storage
addr
;
...
...
@@ -2462,7 +2445,7 @@ PySocket_gethostbyaddr(PyObject *self, PyObject *args)
break
;
#endif
default:
PyErr_SetString
(
PySocket_E
rror
,
"unsupported address family"
);
PyErr_SetString
(
socket_e
rror
,
"unsupported address family"
);
return
NULL
;
}
Py_BEGIN_ALLOW_THREADS
...
...
@@ -2506,7 +2489,7 @@ for a host. The host argument is a string giving a host name or IP number.";
/*ARGSUSED*/
static
PyObject
*
PyS
ocket_getservbyname
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_getservbyname
(
PyObject
*
self
,
PyObject
*
args
)
{
char
*
name
,
*
proto
;
struct
servent
*
sp
;
...
...
@@ -2516,7 +2499,7 @@ PySocket_getservbyname(PyObject *self, PyObject *args)
sp
=
getservbyname
(
name
,
proto
);
Py_END_ALLOW_THREADS
if
(
sp
==
NULL
)
{
PyErr_SetString
(
PySocket_E
rror
,
"service/proto not found"
);
PyErr_SetString
(
socket_e
rror
,
"service/proto not found"
);
return
NULL
;
}
return
PyInt_FromLong
((
long
)
ntohs
(
sp
->
s_port
));
...
...
@@ -2535,13 +2518,13 @@ The protocol name should be 'tcp' or 'udp'.";
/*ARGSUSED*/
static
PyObject
*
PyS
ocket_getprotobyname
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_getprotobyname
(
PyObject
*
self
,
PyObject
*
args
)
{
char
*
name
;
struct
protoent
*
sp
;
#ifdef __BEOS__
/* Not available in BeOS yet. - [cjh] */
PyErr_SetString
(
PySocket_E
rror
,
"getprotobyname not supported"
);
PyErr_SetString
(
socket_e
rror
,
"getprotobyname not supported"
);
return
NULL
;
#else
if
(
!
PyArg_ParseTuple
(
args
,
"s:getprotobyname"
,
&
name
))
...
...
@@ -2550,7 +2533,7 @@ PySocket_getprotobyname(PyObject *self, PyObject *args)
sp
=
getprotobyname
(
name
);
Py_END_ALLOW_THREADS
if
(
sp
==
NULL
)
{
PyErr_SetString
(
PySocket_E
rror
,
"protocol not found"
);
PyErr_SetString
(
socket_e
rror
,
"protocol not found"
);
return
NULL
;
}
return
PyInt_FromLong
((
long
)
sp
->
p_proto
);
...
...
@@ -2570,7 +2553,7 @@ Return the protocol number for the named protocol. (Rarely used.)";
/*ARGSUSED*/
static
PyObject
*
PyS
ocket_fromfd
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_fromfd
(
PyObject
*
self
,
PyObject
*
args
)
{
PySocketSockObject
*
s
;
SOCKET_T
fd
;
...
...
@@ -2581,8 +2564,8 @@ PySocket_fromfd(PyObject *self, PyObject *args)
/* Dup the fd so it and the socket can be closed independently */
fd
=
dup
(
fd
);
if
(
fd
<
0
)
return
PySocket_Er
r
();
s
=
PySocketSock_New
(
fd
,
family
,
type
,
proto
);
return
set_erro
r
();
s
=
new_sockobject
(
fd
,
family
,
type
,
proto
);
/* From now on, ignore SIGPIPE and let the error checking
do the work. */
#ifdef SIGPIPE
...
...
@@ -2601,7 +2584,7 @@ The remaining arguments are the same as for socket().";
static
PyObject
*
PyS
ocket_ntohs
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_ntohs
(
PyObject
*
self
,
PyObject
*
args
)
{
int
x1
,
x2
;
...
...
@@ -2619,7 +2602,7 @@ Convert a 16-bit integer from network to host byte order.";
static
PyObject
*
PyS
ocket_ntohl
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_ntohl
(
PyObject
*
self
,
PyObject
*
args
)
{
int
x1
,
x2
;
...
...
@@ -2637,7 +2620,7 @@ Convert a 32-bit integer from network to host byte order.";
static
PyObject
*
PyS
ocket_htons
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_htons
(
PyObject
*
self
,
PyObject
*
args
)
{
int
x1
,
x2
;
...
...
@@ -2655,7 +2638,7 @@ Convert a 16-bit integer from host to network byte order.";
static
PyObject
*
PyS
ocket_htonl
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_htonl
(
PyObject
*
self
,
PyObject
*
args
)
{
int
x1
,
x2
;
...
...
@@ -2680,7 +2663,7 @@ Convert an IP address in string format (123.45.67.89) to the 32-bit packed\n\
binary format used in low-level network functions."
;
static
PyObject
*
PyS
ocket_inet_aton
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_inet_aton
(
PyObject
*
self
,
PyObject
*
args
)
{
#ifndef INADDR_NONE
#define INADDR_NONE (-1)
...
...
@@ -2696,7 +2679,7 @@ PySocket_inet_aton(PyObject *self, PyObject *args)
packed_addr
=
inet_addr
(
ip_addr
);
if
(
packed_addr
==
INADDR_NONE
)
{
/* invalid address */
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
"illegal IP address string passed to inet_aton"
);
return
NULL
;
}
...
...
@@ -2711,7 +2694,7 @@ static char inet_ntoa_doc[] =
Convert an IP address from 32-bit packed binary format to string format"
;
static
PyObject
*
PyS
ocket_inet_ntoa
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_inet_ntoa
(
PyObject
*
self
,
PyObject
*
args
)
{
char
*
packed_str
;
int
addr_len
;
...
...
@@ -2722,7 +2705,7 @@ PySocket_inet_ntoa(PyObject *self, PyObject *args)
}
if
(
addr_len
!=
sizeof
(
packed_addr
))
{
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
"packed IP wrong length for inet_ntoa"
);
return
NULL
;
}
...
...
@@ -2736,7 +2719,7 @@ PySocket_inet_ntoa(PyObject *self, PyObject *args)
/*ARGSUSED*/
static
PyObject
*
PyS
ocket_getaddrinfo
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_getaddrinfo
(
PyObject
*
self
,
PyObject
*
args
)
{
struct
addrinfo
hints
,
*
res
;
struct
addrinfo
*
res0
=
NULL
;
...
...
@@ -2763,7 +2746,7 @@ PySocket_getaddrinfo(PyObject *self, PyObject *args)
}
else
if
(
pobj
==
Py_None
)
{
pptr
=
(
char
*
)
NULL
;
}
else
{
PyErr_SetString
(
PySocket_E
rror
,
"Int or String expected"
);
PyErr_SetString
(
socket_e
rror
,
"Int or String expected"
);
return
NULL
;
}
memset
(
&
hints
,
0
,
sizeof
(
hints
));
...
...
@@ -2773,7 +2756,7 @@ PySocket_getaddrinfo(PyObject *self, PyObject *args)
hints
.
ai_flags
=
flags
;
error
=
getaddrinfo
(
hptr
,
pptr
,
&
hints
,
&
res0
);
if
(
error
)
{
PyGAI_Er
r
(
error
);
set_gaierro
r
(
error
);
return
NULL
;
}
...
...
@@ -2815,7 +2798,7 @@ Resolve host and port into addrinfo struct.";
/*ARGSUSED*/
static
PyObject
*
PyS
ocket_getnameinfo
(
PyObject
*
self
,
PyObject
*
args
)
s
ocket_getnameinfo
(
PyObject
*
self
,
PyObject
*
args
)
{
PyObject
*
sa
=
(
PyObject
*
)
NULL
;
int
flags
;
...
...
@@ -2838,11 +2821,11 @@ PySocket_getnameinfo(PyObject *self, PyObject *args)
hints
.
ai_socktype
=
SOCK_DGRAM
;
/* make numeric port happy */
error
=
getaddrinfo
(
hostp
,
pbuf
,
&
hints
,
&
res
);
if
(
error
)
{
PyGAI_Er
r
(
error
);
set_gaierro
r
(
error
);
goto
fail
;
}
if
(
res
->
ai_next
)
{
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
"sockaddr resolved to multiple addresses"
);
goto
fail
;
}
...
...
@@ -2852,7 +2835,7 @@ PySocket_getnameinfo(PyObject *self, PyObject *args)
char
*
t1
;
int
t2
;
if
(
PyArg_ParseTuple
(
sa
,
"si"
,
&
t1
,
&
t2
)
==
0
)
{
PyErr_SetString
(
PySocket_E
rror
,
PyErr_SetString
(
socket_e
rror
,
"IPv4 sockaddr must be 2 tuple"
);
goto
fail
;
}
...
...
@@ -2872,7 +2855,7 @@ PySocket_getnameinfo(PyObject *self, PyObject *args)
error
=
getnameinfo
(
res
->
ai_addr
,
res
->
ai_addrlen
,
hbuf
,
sizeof
(
hbuf
),
pbuf
,
sizeof
(
pbuf
),
flags
);
if
(
error
)
{
PyGAI_Er
r
(
error
);
set_gaierro
r
(
error
);
goto
fail
;
}
ret
=
Py_BuildValue
(
"ss"
,
hbuf
,
pbuf
);
...
...
@@ -2890,64 +2873,83 @@ Get host and port for a sockaddr.";
/* List of functions exported by this module. */
static
PyMethodDef
PyS
ocket_methods
[]
=
{
{
"gethostbyname"
,
PyS
ocket_gethostbyname
,
static
PyMethodDef
s
ocket_methods
[]
=
{
{
"gethostbyname"
,
s
ocket_gethostbyname
,
METH_VARARGS
,
gethostbyname_doc
},
{
"gethostbyname_ex"
,
PyS
ocket_gethostbyname_ex
,
{
"gethostbyname_ex"
,
s
ocket_gethostbyname_ex
,
METH_VARARGS
,
ghbn_ex_doc
},
{
"gethostbyaddr"
,
PyS
ocket_gethostbyaddr
,
{
"gethostbyaddr"
,
s
ocket_gethostbyaddr
,
METH_VARARGS
,
gethostbyaddr_doc
},
{
"gethostname"
,
PyS
ocket_gethostname
,
{
"gethostname"
,
s
ocket_gethostname
,
METH_VARARGS
,
gethostname_doc
},
{
"getservbyname"
,
PyS
ocket_getservbyname
,
{
"getservbyname"
,
s
ocket_getservbyname
,
METH_VARARGS
,
getservbyname_doc
},
{
"getprotobyname"
,
PyS
ocket_getprotobyname
,
{
"getprotobyname"
,
s
ocket_getprotobyname
,
METH_VARARGS
,
getprotobyname_doc
},
#ifndef NO_DUP
{
"fromfd"
,
PyS
ocket_fromfd
,
{
"fromfd"
,
s
ocket_fromfd
,
METH_VARARGS
,
fromfd_doc
},
#endif
{
"ntohs"
,
PyS
ocket_ntohs
,
{
"ntohs"
,
s
ocket_ntohs
,
METH_VARARGS
,
ntohs_doc
},
{
"ntohl"
,
PyS
ocket_ntohl
,
{
"ntohl"
,
s
ocket_ntohl
,
METH_VARARGS
,
ntohl_doc
},
{
"htons"
,
PyS
ocket_htons
,
{
"htons"
,
s
ocket_htons
,
METH_VARARGS
,
htons_doc
},
{
"htonl"
,
PyS
ocket_htonl
,
{
"htonl"
,
s
ocket_htonl
,
METH_VARARGS
,
htonl_doc
},
{
"inet_aton"
,
PyS
ocket_inet_aton
,
{
"inet_aton"
,
s
ocket_inet_aton
,
METH_VARARGS
,
inet_aton_doc
},
{
"inet_ntoa"
,
PyS
ocket_inet_ntoa
,
{
"inet_ntoa"
,
s
ocket_inet_ntoa
,
METH_VARARGS
,
inet_ntoa_doc
},
{
"getaddrinfo"
,
PyS
ocket_getaddrinfo
,
{
"getaddrinfo"
,
s
ocket_getaddrinfo
,
METH_VARARGS
,
getaddrinfo_doc
},
{
"getnameinfo"
,
PyS
ocket_getnameinfo
,
{
"getnameinfo"
,
s
ocket_getnameinfo
,
METH_VARARGS
,
getnameinfo_doc
},
{
NULL
,
NULL
}
/* Sentinel */
};
#ifdef RISCOS
#define OS_INIT_DEFINED
static
int
os_init
(
void
)
{
_kernel_swi_regs
r
;
r
.
r
[
0
]
=
0
;
_kernel_swi
(
0x43380
,
&
r
,
&
r
);
taskwindow
=
r
.
r
[
0
];
return
0
;
}
#endif
/* RISCOS */
#ifdef MS_WINDOWS
#define OS_INIT_DEFINED
/* Additional initialization and cleanup for
NT/
Windows */
/* Additional initialization and cleanup for Windows */
static
void
NT
cleanup
(
void
)
os_
cleanup
(
void
)
{
WSACleanup
();
}
static
int
NT
init
(
void
)
os_
init
(
void
)
{
WSADATA
WSAData
;
int
ret
;
char
buf
[
100
];
ret
=
WSAStartup
(
0x0101
,
&
WSAData
);
switch
(
ret
)
{
case
0
:
/*
n
o error */
atexit
(
NT
cleanup
);
return
1
;
case
0
:
/*
N
o error */
atexit
(
os_
cleanup
);
return
1
;
/* Success */
case
WSASYSNOTREADY
:
PyErr_SetString
(
PyExc_ImportError
,
"WSAStartup failed: network not ready"
);
...
...
@@ -2964,75 +2966,74 @@ NTinit(void)
PyErr_SetString
(
PyExc_ImportError
,
buf
);
break
;
}
return
0
;
return
0
;
/* Failure */
}
#endif
/* MS_WINDOWS */
#if defined(PYOS_OS2)
/* Additional initialization and cleanup for OS/2 */
#ifdef PYOS_OS2
#define OS_INIT_DEFINED
static
void
OS2cleanup
(
void
)
{
/* No cleanup is necessary for OS/2 Sockets */
}
/* Additional initialization for OS/2 */
static
int
OS2
init
(
void
)
os_
init
(
void
)
{
#if
!defined(PYCC_GCC)
#if
ndef PYCC_GCC
char
reason
[
64
];
int
rc
=
sock_init
();
if
(
rc
==
0
)
{
atexit
(
OS2cleanup
);
return
1
;
/* Indicate Success */
return
1
;
/* Success */
}
PyOS_snprintf
(
reason
,
sizeof
(
reason
),
"OS/2 TCP/IP Error# %d"
,
sock_errno
());
PyErr_SetString
(
PyExc_ImportError
,
reason
);
return
0
;
/*
Indicate
Failure */
return
0
;
/* Failure */
#else
/*
n
o need to initialise sockets with GCC/EMX */
return
1
;
/*
N
o need to initialise sockets with GCC/EMX */
return
1
;
/* Success */
#endif
}
#endif
/* PYOS_OS2 */
#ifndef OS_INIT_DEFINED
static
int
os_init
(
void
)
{
return
1
;
/* Success */
}
#endif
/* C API table - always add new things to the end for binary
compatibility. */
static
PySocketModule_APIObject
PySocketModuleAPI
=
{
&
PySocketSock_T
ype
,
&
sock_t
ype
,
};
/* Initialize this module.
This is called when the first 'import socket' is done,
via a table in config.c, if config.c is compiled with USE_SOCKET
defined.
For MS_WINDOWS (which means any Windows variant), this module
is actually called "_socket", and there's a wrapper "socket.py"
which implements some missing functionality (such as makefile(),
dup() and fromfd()). The import of "_socket" may fail with an
ImportError exception if initialization of WINSOCK fails. When
WINSOCK is initialized succesfully, a call to WSACleanup() is
scheduled to be made at exit time.
/* Initialize the _socket module.
For OS/2, this module is also called "_socket" and uses a wrapper
"socket.py" which implements that functionality that is missing
when PC operating systems don't put socket descriptors in the
operating system's filesystem layer.
*/
This module is actually called "_socket", and there's a wrapper
"socket.py" which implements some additional functionality. On some
platforms (e.g. Windows and OS/2), socket.py also implements a
wrapper for the socket type that provides missing functionality such
as makefile(), dup() and fromfd(). The import of "_socket" may fail
with an ImportError exception if os-specific initialization fails.
On Windows, this does WINSOCK initialization. When WINSOCK is
initialized succesfully, a call to WSACleanup() is scheduled to be
made at exit time.
*/
static
char
module
_doc
[]
=
static
char
socket
_doc
[]
=
"Implementation module for socket operations. See the socket module
\n
\
for documentation."
;
...
...
@@ -3040,53 +3041,42 @@ DL_EXPORT(void)
init_socket
(
void
)
{
PyObject
*
m
;
#ifdef RISCOS
_kernel_swi_regs
r
;
r
.
r
[
0
]
=
0
;
_kernel_swi
(
0x43380
,
&
r
,
&
r
);
taskwindow
=
r
.
r
[
0
];
#else
#ifdef MS_WINDOWS
if
(
!
NTinit
())
return
;
#else
#if defined(PYOS_OS2)
if
(
!
OS2init
())
if
(
!
os_init
())
return
;
#endif
/* PYOS_OS2 */
#endif
/* MS_WINDOWS */
#endif
/* RISCOS */
PySocketSock_Type
.
ob_type
=
&
PyType_Type
;
PySocketSock_Type
.
tp_getattro
=
PyObject_GenericGetAttr
;
PySocketSock_Type
.
tp_alloc
=
PyType_GenericAlloc
;
PySocketSock_Type
.
tp_free
=
PyObject_Del
;
sock_type
.
ob_type
=
&
PyType_Type
;
sock_type
.
tp_getattro
=
PyObject_GenericGetAttr
;
sock_type
.
tp_alloc
=
PyType_GenericAlloc
;
sock_type
.
tp_free
=
PyObject_Del
;
m
=
Py_InitModule3
(
PySocket_MODULE_NAME
,
PyS
ocket_methods
,
module
_doc
);
s
ocket_methods
,
socket
_doc
);
PySocket_E
rror
=
PyErr_NewException
(
"socket.error"
,
NULL
,
NULL
);
if
(
PySocket_E
rror
==
NULL
)
socket_e
rror
=
PyErr_NewException
(
"socket.error"
,
NULL
,
NULL
);
if
(
socket_e
rror
==
NULL
)
return
;
Py_INCREF
(
PySocket_Error
);
PyModule_AddObject
(
m
,
"error"
,
PySocket_Error
);
PyH_Error
=
PyErr_NewException
(
"socket.herror"
,
PySocket_Error
,
NULL
);
if
(
PyH_Error
==
NULL
)
Py_INCREF
(
socket_error
);
PyModule_AddObject
(
m
,
"error"
,
socket_error
);
socket_herror
=
PyErr_NewException
(
"socket.herror"
,
socket_error
,
NULL
);
if
(
socket_herror
==
NULL
)
return
;
Py_INCREF
(
PyH_E
rror
);
PyModule_AddObject
(
m
,
"herror"
,
PyH_E
rror
);
PyGAI_Error
=
PyErr_NewException
(
"socket.gaierror"
,
PySocket_E
rror
,
Py_INCREF
(
socket_he
rror
);
PyModule_AddObject
(
m
,
"herror"
,
socket_he
rror
);
socket_gaierror
=
PyErr_NewException
(
"socket.gaierror"
,
socket_e
rror
,
NULL
);
if
(
PyGAI_E
rror
==
NULL
)
if
(
socket_gaie
rror
==
NULL
)
return
;
Py_INCREF
(
PyGAI_E
rror
);
PyModule_AddObject
(
m
,
"gaierror"
,
PyGAI_E
rror
);
Py_INCREF
((
PyObject
*
)
&
PySocketSock_T
ype
);
Py_INCREF
(
socket_gaie
rror
);
PyModule_AddObject
(
m
,
"gaierror"
,
socket_gaie
rror
);
Py_INCREF
((
PyObject
*
)
&
sock_t
ype
);
if
(
PyModule_AddObject
(
m
,
"SocketType"
,
(
PyObject
*
)
&
PySocketSock_T
ype
)
!=
0
)
(
PyObject
*
)
&
sock_t
ype
)
!=
0
)
return
;
Py_INCREF
((
PyObject
*
)
&
PySocketSock_T
ype
);
Py_INCREF
((
PyObject
*
)
&
sock_t
ype
);
if
(
PyModule_AddObject
(
m
,
"socket"
,
(
PyObject
*
)
&
PySocketSock_T
ype
)
!=
0
)
(
PyObject
*
)
&
sock_t
ype
)
!=
0
)
return
;
/* Export C API */
...
...
@@ -3669,12 +3659,15 @@ init_socket(void)
#endif
}
/* Simplistic emulation code for inet_pton that only works for IPv4 */
#ifndef HAVE_INET_PTON
/* Simplistic emulation code for inet_pton that only works for IPv4 */
int
inet_pton
(
int
af
,
const
char
*
src
,
void
*
dst
)
inet_pton
(
int
af
,
const
char
*
src
,
void
*
dst
)
{
if
(
af
==
AF_INET
){
if
(
af
==
AF_INET
)
{
long
packed_addr
;
packed_addr
=
inet_addr
(
src
);
if
(
packed_addr
==
INADDR_NONE
)
...
...
@@ -3700,4 +3693,5 @@ inet_ntop(int af, const void *src, char *dst, socklen_t size)
/* Should set errno to EAFNOSUPPORT */
return
NULL
;
}
#endif
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment