[newlib-cygwin] Cygwin: sockets: move type and proto checks into fhandler_socket classes
Corinna Vinschen
corinna@sourceware.org
Mon Feb 26 16:59:00 GMT 2018
https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=d35bd22992cc08d5c04ff822959bacd863abf41b
commit d35bd22992cc08d5c04ff822959bacd863abf41b
Author: Corinna Vinschen <corinna@vinschen.de>
Date: Mon Feb 26 17:56:47 2018 +0100
Cygwin: sockets: move type and proto checks into fhandler_socket classes
Encapsulation required
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
Diff:
---
winsup/cygwin/fhandler_socket_inet.cc | 7 +++++++
winsup/cygwin/fhandler_socket_local.cc | 20 ++++++++++++++++++++
winsup/cygwin/fhandler_socket_unix.cc | 20 ++++++++++++++++++++
winsup/cygwin/net.cc | 25 -------------------------
4 files changed, 47 insertions(+), 25 deletions(-)
diff --git a/winsup/cygwin/fhandler_socket_inet.cc b/winsup/cygwin/fhandler_socket_inet.cc
index 42a3bd2..b0dc658 100644
--- a/winsup/cygwin/fhandler_socket_inet.cc
+++ b/winsup/cygwin/fhandler_socket_inet.cc
@@ -708,6 +708,13 @@ fhandler_socket_inet::socket (int af, int type, int protocol, int flags)
SOCKET sock;
int ret;
+ /* This test should be covered by ::socket, but make sure we don't
+ accidentally try anything else. */
+ if (type != SOCK_STREAM && type != SOCK_DGRAM && type != SOCK_RAW)
+ {
+ set_errno (EINVAL);
+ return -1;
+ }
sock = ::socket (af, type, protocol);
if (sock == INVALID_SOCKET)
{
diff --git a/winsup/cygwin/fhandler_socket_local.cc b/winsup/cygwin/fhandler_socket_local.cc
index d88476d..ad2df65 100644
--- a/winsup/cygwin/fhandler_socket_local.cc
+++ b/winsup/cygwin/fhandler_socket_local.cc
@@ -241,6 +241,16 @@ fhandler_socket_local::socket (int af, int type, int protocol, int flags)
SOCKET sock;
int ret;
+ if (type != SOCK_STREAM && type != SOCK_DGRAM)
+ {
+ set_errno (EINVAL);
+ return -1;
+ }
+ if (protocol != 0)
+ {
+ set_errno (EPROTONOSUPPORT);
+ return -1;
+ }
sock = ::socket (AF_INET, type, protocol);
if (sock == INVALID_SOCKET)
{
@@ -265,6 +275,16 @@ fhandler_socket_local::socketpair (int af, int type, int protocol, int flags,
fhandler_socket_local *fh_out = reinterpret_cast<fhandler_socket_local *>
(_fh_out);
+ if (type != SOCK_STREAM && type != SOCK_DGRAM)
+ {
+ set_errno (EINVAL);
+ return -1;
+ }
+ if (protocol != 0)
+ {
+ set_errno (EPROTONOSUPPORT);
+ return -1;
+ }
/* create listening socket */
sock = ::socket (AF_INET, type, 0);
if (sock == INVALID_SOCKET)
diff --git a/winsup/cygwin/fhandler_socket_unix.cc b/winsup/cygwin/fhandler_socket_unix.cc
index 48d0d4c..1e4d339 100644
--- a/winsup/cygwin/fhandler_socket_unix.cc
+++ b/winsup/cygwin/fhandler_socket_unix.cc
@@ -229,6 +229,16 @@ fhandler_socket_unix::dup (fhandler_base *child, int flags)
int
fhandler_socket_unix::socket (int af, int type, int protocol, int flags)
{
+ if (type != SOCK_STREAM && type != SOCK_DGRAM)
+ {
+ set_errno (EINVAL);
+ return -1;
+ }
+ if (protocol != 0)
+ {
+ set_errno (EPROTONOSUPPORT);
+ return -1;
+ }
set_errno (EAFNOSUPPORT);
return -1;
}
@@ -237,6 +247,16 @@ int
fhandler_socket_unix::socketpair (int af, int type, int protocol, int flags,
fhandler_socket *fh_out)
{
+ if (type != SOCK_STREAM && type != SOCK_DGRAM)
+ {
+ set_errno (EINVAL);
+ return -1;
+ }
+ if (protocol != 0)
+ {
+ set_errno (EPROTONOSUPPORT);
+ return -1;
+ }
set_errno (EAFNOSUPPORT);
return -1;
}
diff --git a/winsup/cygwin/net.cc b/winsup/cygwin/net.cc
index bd0a169..4fcc577 100644
--- a/winsup/cygwin/net.cc
+++ b/winsup/cygwin/net.cc
@@ -517,25 +517,10 @@ cygwin_socket (int af, int type, int protocol)
{
case AF_LOCAL:
case AF_UNIX:
- if (type != SOCK_STREAM && type != SOCK_DGRAM)
- {
- set_errno (EINVAL);
- goto done;
- }
- if (protocol != 0)
- {
- set_errno (EPROTONOSUPPORT);
- goto done;
- }
dev = (af == AF_LOCAL) ? af_local_dev : af_unix_dev;
break;
case AF_INET:
case AF_INET6:
- if (type != SOCK_STREAM && type != SOCK_DGRAM && type != SOCK_RAW)
- {
- set_errno (EINVAL);
- goto done;
- }
dev = af_inet_dev;
break;
default:
@@ -2314,16 +2299,6 @@ socketpair (int af, int type, int protocol, int *sb)
{
case AF_LOCAL:
case AF_UNIX:
- if (type != SOCK_STREAM && type != SOCK_DGRAM)
- {
- set_errno (EINVAL);
- goto done;
- }
- if (protocol != 0)
- {
- set_errno (EPROTONOSUPPORT);
- goto done;
- }
dev = (af == AF_LOCAL) ? af_local_dev : af_unix_dev;
break;
default:
More information about the Cygwin-cvs
mailing list