2000-10-27 11:50:33 +02:00
|
|
|
/* fhandler_socket.cc. See fhandler.h for a description of the fhandler classes.
|
|
|
|
|
2002-01-01 17:25:31 +01:00
|
|
|
Copyright 2000, 2001, 2002 Red Hat, Inc.
|
2000-10-27 11:50:33 +02:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
/* #define DEBUG_NEST_ON 1 */
|
|
|
|
|
|
|
|
#define __INSIDE_CYGWIN_NET__
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/socket.h>
|
2001-05-15 10:42:15 +02:00
|
|
|
#include <asm/byteorder.h>
|
2000-10-27 11:50:33 +02:00
|
|
|
|
2001-04-09 09:21:32 +02:00
|
|
|
#include <stdlib.h>
|
2000-10-27 11:50:33 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2001-03-20 20:50:28 +01:00
|
|
|
#define USE_SYS_TYPES_FD_SET
|
2000-10-27 11:50:33 +02:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include "cygerrno.h"
|
2001-07-26 21:22:24 +02:00
|
|
|
#include "security.h"
|
2001-08-07 02:01:42 +02:00
|
|
|
#include "cygwin/version.h"
|
|
|
|
#include "perprocess.h"
|
2000-10-27 11:50:33 +02:00
|
|
|
#include "fhandler.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "path.h"
|
2000-10-27 11:50:33 +02:00
|
|
|
#include "dtable.h"
|
2001-04-18 23:10:15 +02:00
|
|
|
#include "cygheap.h"
|
2000-10-27 11:50:33 +02:00
|
|
|
#include "sigproc.h"
|
2002-02-09 11:40:48 +01:00
|
|
|
#include "wsock_event.h"
|
2000-10-27 11:50:33 +02:00
|
|
|
|
2001-04-09 09:21:32 +02:00
|
|
|
#define SECRET_EVENT_NAME "cygwin.local_socket.secret.%d.%08x-%08x-%08x-%08x"
|
2001-05-04 18:30:18 +02:00
|
|
|
#define ENTROPY_SOURCE_NAME "/dev/urandom"
|
|
|
|
#define ENTROPY_SOURCE_DEV_UNIT 9
|
|
|
|
|
2001-09-06 06:41:59 +02:00
|
|
|
fhandler_dev_random* entropy_source;
|
2001-04-09 09:21:32 +02:00
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
/**********************************************************************/
|
|
|
|
/* fhandler_socket */
|
|
|
|
|
2001-10-29 06:28:24 +01:00
|
|
|
fhandler_socket::fhandler_socket ()
|
2002-01-01 17:25:31 +01:00
|
|
|
: fhandler_base (FH_SOCKET), sun_path (NULL)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
|
|
|
set_need_fork_fixup ();
|
2001-11-05 07:09:15 +01:00
|
|
|
prot_info_ptr = (LPWSAPROTOCOL_INFOA) cmalloc (HEAP_BUF,
|
2001-09-07 23:32:07 +02:00
|
|
|
sizeof (WSAPROTOCOL_INFOA));
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fhandler_socket::~fhandler_socket ()
|
|
|
|
{
|
|
|
|
if (prot_info_ptr)
|
|
|
|
cfree (prot_info_ptr);
|
2002-01-01 17:25:31 +01:00
|
|
|
if (sun_path)
|
|
|
|
cfree (sun_path);
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
|
2001-04-09 09:21:32 +02:00
|
|
|
void
|
|
|
|
fhandler_socket::set_connect_secret ()
|
|
|
|
{
|
2001-05-04 18:30:18 +02:00
|
|
|
if (!entropy_source)
|
|
|
|
{
|
|
|
|
void *buf = malloc (sizeof (fhandler_dev_random));
|
2001-10-13 19:23:35 +02:00
|
|
|
entropy_source = new (buf) fhandler_dev_random (ENTROPY_SOURCE_DEV_UNIT);
|
2001-05-04 18:30:18 +02:00
|
|
|
}
|
|
|
|
if (entropy_source &&
|
2001-10-04 04:34:20 +02:00
|
|
|
!entropy_source->open (NULL, O_RDONLY))
|
2001-05-04 18:30:18 +02:00
|
|
|
{
|
|
|
|
delete entropy_source;
|
|
|
|
entropy_source = NULL;
|
|
|
|
}
|
|
|
|
if (!entropy_source ||
|
|
|
|
(entropy_source->read (connect_secret, sizeof (connect_secret)) !=
|
|
|
|
sizeof (connect_secret)))
|
|
|
|
bzero ((char*) connect_secret, sizeof (connect_secret));
|
2001-04-09 09:21:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_socket::get_connect_secret (char* buf)
|
|
|
|
{
|
|
|
|
__small_sprintf (buf, "%08x-%08x-%08x-%08x",
|
|
|
|
connect_secret [0], connect_secret [1],
|
|
|
|
connect_secret [2], connect_secret [3]);
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE
|
|
|
|
fhandler_socket::create_secret_event (int* secret)
|
|
|
|
{
|
|
|
|
char buf [128];
|
|
|
|
int* secret_ptr = (secret ? : connect_secret);
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
int sin_len = sizeof (sin);
|
|
|
|
|
|
|
|
if (getsockname (get_socket (), (struct sockaddr*) &sin, &sin_len))
|
|
|
|
{
|
|
|
|
debug_printf ("error getting local socket name (%d)", WSAGetLastError ());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
__small_sprintf (buf, SECRET_EVENT_NAME, sin.sin_port,
|
|
|
|
secret_ptr [0], secret_ptr [1],
|
|
|
|
secret_ptr [2], secret_ptr [3]);
|
2001-08-16 16:34:27 +02:00
|
|
|
secret_event = CreateEvent (get_inheritance(true), FALSE, FALSE, buf);
|
2001-04-09 09:21:32 +02:00
|
|
|
if (!secret_event && GetLastError () == ERROR_ALREADY_EXISTS)
|
|
|
|
secret_event = OpenEvent (EVENT_ALL_ACCESS, FALSE, buf);
|
|
|
|
|
|
|
|
if (secret_event)
|
2001-06-20 08:50:13 +02:00
|
|
|
ProtectHandle (secret_event);
|
2001-04-09 09:21:32 +02:00
|
|
|
|
|
|
|
return secret_event;
|
|
|
|
}
|
|
|
|
|
2001-06-20 08:50:13 +02:00
|
|
|
void
|
|
|
|
fhandler_socket::signal_secret_event ()
|
|
|
|
{
|
2001-10-13 19:23:35 +02:00
|
|
|
if (!secret_event)
|
|
|
|
debug_printf ("no secret event?");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetEvent (secret_event);
|
|
|
|
debug_printf ("signaled secret_event");
|
|
|
|
}
|
2001-06-20 08:50:13 +02:00
|
|
|
}
|
|
|
|
|
2001-04-09 09:21:32 +02:00
|
|
|
void
|
|
|
|
fhandler_socket::close_secret_event ()
|
|
|
|
{
|
|
|
|
if (secret_event)
|
|
|
|
ForceCloseHandle (secret_event);
|
|
|
|
secret_event = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket::check_peer_secret_event (struct sockaddr_in* peer, int* secret)
|
|
|
|
{
|
|
|
|
char buf [128];
|
|
|
|
HANDLE ev;
|
|
|
|
int* secret_ptr = (secret ? : connect_secret);
|
|
|
|
|
|
|
|
__small_sprintf (buf, SECRET_EVENT_NAME, peer->sin_port,
|
2001-09-07 23:32:07 +02:00
|
|
|
secret_ptr [0], secret_ptr [1],
|
|
|
|
secret_ptr [2], secret_ptr [3]);
|
2001-08-16 16:34:27 +02:00
|
|
|
ev = CreateEvent (&sec_all_nih, FALSE, FALSE, buf);
|
2001-04-09 09:21:32 +02:00
|
|
|
if (!ev && GetLastError () == ERROR_ALREADY_EXISTS)
|
|
|
|
{
|
|
|
|
debug_printf ("%s event already exist");
|
|
|
|
ev = OpenEvent (EVENT_ALL_ACCESS, FALSE, buf);
|
|
|
|
}
|
|
|
|
|
2001-06-20 08:50:13 +02:00
|
|
|
signal_secret_event ();
|
|
|
|
|
2001-04-09 09:21:32 +02:00
|
|
|
if (ev)
|
|
|
|
{
|
|
|
|
DWORD rc = WaitForSingleObject (ev, 10000);
|
|
|
|
debug_printf ("WFSO rc=%d", rc);
|
|
|
|
CloseHandle (ev);
|
|
|
|
return (rc == WAIT_OBJECT_0 ? 1 : 0 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
void
|
|
|
|
fhandler_socket::fixup_before_fork_exec (DWORD win_proc_id)
|
|
|
|
{
|
2001-10-13 19:23:35 +02:00
|
|
|
if (!winsock2_active)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2001-10-13 19:23:35 +02:00
|
|
|
fhandler_base::fixup_before_fork_exec (win_proc_id);
|
|
|
|
debug_printf ("Without Winsock 2.0");
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
2001-10-13 19:23:35 +02:00
|
|
|
else if (!WSADuplicateSocketA (get_socket (), win_proc_id, prot_info_ptr))
|
2001-10-30 08:43:46 +01:00
|
|
|
debug_printf ("WSADuplicateSocket went fine, sock %p, win_proc_id %d, prot_info_ptr %p",
|
|
|
|
get_socket (), win_proc_id, prot_info_ptr);
|
2000-10-27 11:50:33 +02:00
|
|
|
else
|
|
|
|
{
|
2001-10-30 08:43:46 +01:00
|
|
|
debug_printf ("WSADuplicateSocket error, sock %p, win_proc_id %d, prot_info_ptr %p",
|
2001-11-05 07:09:15 +01:00
|
|
|
get_socket (), win_proc_id, prot_info_ptr);
|
2001-10-13 19:23:35 +02:00
|
|
|
set_winsock_errno ();
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-14 06:14:24 +02:00
|
|
|
extern "C" void __stdcall load_wsock32 ();
|
2000-10-27 11:50:33 +02:00
|
|
|
void
|
|
|
|
fhandler_socket::fixup_after_fork (HANDLE parent)
|
|
|
|
{
|
2001-10-13 19:23:35 +02:00
|
|
|
SOCKET new_sock;
|
2000-10-27 11:50:33 +02:00
|
|
|
|
|
|
|
debug_printf ("WSASocket begin, dwServiceFlags1=%d",
|
|
|
|
prot_info_ptr->dwServiceFlags1);
|
2001-10-13 19:23:35 +02:00
|
|
|
|
|
|
|
if ((new_sock = WSASocketA (FROM_PROTOCOL_INFO,
|
|
|
|
FROM_PROTOCOL_INFO,
|
|
|
|
FROM_PROTOCOL_INFO,
|
|
|
|
prot_info_ptr, 0, 0)) == INVALID_SOCKET)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
|
|
|
debug_printf ("WSASocket error");
|
|
|
|
set_winsock_errno ();
|
|
|
|
}
|
2001-10-13 19:23:35 +02:00
|
|
|
else if (!new_sock && !winsock2_active)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2001-10-14 06:14:24 +02:00
|
|
|
load_wsock32 ();
|
2001-10-13 19:23:35 +02:00
|
|
|
fhandler_base::fixup_after_fork (parent);
|
|
|
|
debug_printf ("Without Winsock 2.0");
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-10-30 08:43:46 +01:00
|
|
|
debug_printf ("WSASocket went fine new_sock %p, old_sock %p", new_sock, get_io_handle ());
|
2001-10-13 19:23:35 +02:00
|
|
|
set_io_handle ((HANDLE) new_sock);
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
2001-10-13 19:23:35 +02:00
|
|
|
|
2001-04-09 09:21:32 +02:00
|
|
|
if (secret_event)
|
|
|
|
fork_fixup (parent, secret_event, "secret_event");
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
|
2001-09-20 13:29:48 +02:00
|
|
|
void
|
|
|
|
fhandler_socket::fixup_after_exec (HANDLE parent)
|
|
|
|
{
|
2001-10-13 19:23:35 +02:00
|
|
|
debug_printf ("here");
|
2001-09-20 13:29:48 +02:00
|
|
|
if (!get_close_on_exec ())
|
|
|
|
fixup_after_fork (parent);
|
2001-10-13 19:23:35 +02:00
|
|
|
#if 0
|
|
|
|
else if (!winsock2_active)
|
2001-09-20 13:29:48 +02:00
|
|
|
closesocket (get_socket ());
|
2001-10-13 19:23:35 +02:00
|
|
|
#endif
|
2001-09-20 13:29:48 +02:00
|
|
|
}
|
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
int
|
|
|
|
fhandler_socket::dup (fhandler_base *child)
|
|
|
|
{
|
2001-10-13 19:23:35 +02:00
|
|
|
debug_printf ("here");
|
2000-10-27 11:50:33 +02:00
|
|
|
fhandler_socket *fhs = (fhandler_socket *) child;
|
2001-04-09 09:21:32 +02:00
|
|
|
fhs->addr_family = addr_family;
|
2000-10-27 11:50:33 +02:00
|
|
|
fhs->set_io_handle (get_io_handle ());
|
2001-10-30 08:43:46 +01:00
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
fhs->fixup_before_fork_exec (GetCurrentProcessId ());
|
2001-10-13 19:23:35 +02:00
|
|
|
if (winsock2_active)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
|
|
|
fhs->fixup_after_fork (hMainProc);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return fhandler_base::dup (child);
|
|
|
|
}
|
|
|
|
|
2001-10-13 19:23:35 +02:00
|
|
|
int __stdcall
|
|
|
|
fhandler_socket::fstat (struct stat *buf, path_conv *pc)
|
|
|
|
{
|
|
|
|
fhandler_disk_file fh;
|
|
|
|
fh.set_name (get_name (), get_win32_name ());
|
|
|
|
return fh.fstat (buf, pc);
|
|
|
|
}
|
|
|
|
|
2002-02-09 11:40:48 +01:00
|
|
|
int
|
|
|
|
fhandler_socket::recv (void *ptr, size_t len, unsigned int flags)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2002-02-09 11:40:48 +01:00
|
|
|
int res = -1;
|
|
|
|
wsock_event wsock_evt;
|
|
|
|
LPWSAOVERLAPPED ovr;
|
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
sigframe thisframe (mainthread);
|
2002-02-09 11:40:48 +01:00
|
|
|
if (is_nonblocking () || !(ovr = wsock_evt.prepare ()))
|
|
|
|
{
|
|
|
|
debug_printf ("Fallback to winsock 1 recv call");
|
|
|
|
if ((res = ::recv (get_socket (), (char *) ptr, len, flags))
|
|
|
|
== SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
set_winsock_errno ();
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WSABUF wsabuf = { len, (char *) ptr };
|
|
|
|
DWORD ret = 0;
|
|
|
|
if (WSARecv (get_socket (), &wsabuf, 1, &ret, (DWORD *)&flags,
|
|
|
|
ovr, NULL) != SOCKET_ERROR)
|
|
|
|
res = ret;
|
|
|
|
else if ((res = WSAGetLastError ()) != WSA_IO_PENDING)
|
|
|
|
{
|
|
|
|
set_winsock_errno ();
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
else if ((res = wsock_evt.wait (get_socket (), (DWORD *)&flags)) == -1)
|
|
|
|
set_winsock_errno ();
|
|
|
|
}
|
2000-10-27 11:50:33 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-02-09 11:40:48 +01:00
|
|
|
int __stdcall
|
|
|
|
fhandler_socket::read (void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
return recv (ptr, len, 0);
|
|
|
|
}
|
2002-02-08 12:54:10 +01:00
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
int
|
2002-02-09 11:40:48 +01:00
|
|
|
fhandler_socket::send (const void *ptr, size_t len, unsigned int flags)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2002-02-09 11:40:48 +01:00
|
|
|
int res = -1;
|
|
|
|
wsock_event wsock_evt;
|
|
|
|
LPWSAOVERLAPPED ovr;
|
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
sigframe thisframe (mainthread);
|
2002-02-09 11:40:48 +01:00
|
|
|
if (is_nonblocking () || !(ovr = wsock_evt.prepare ()))
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2002-02-09 11:40:48 +01:00
|
|
|
debug_printf ("Fallback to winsock 1 send call");
|
|
|
|
if ((res = ::send (get_socket (), (const char *) ptr, len, flags))
|
|
|
|
== SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
set_winsock_errno ();
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WSABUF wsabuf = { len, (char *) ptr };
|
|
|
|
DWORD ret = 0;
|
|
|
|
if (WSASend (get_socket (), &wsabuf, 1, &ret, (DWORD)flags,
|
|
|
|
ovr, NULL) != SOCKET_ERROR)
|
|
|
|
res = ret;
|
|
|
|
else if ((res = WSAGetLastError ()) != WSA_IO_PENDING)
|
|
|
|
{
|
|
|
|
set_winsock_errno ();
|
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
else if ((res = wsock_evt.wait (get_socket (), (DWORD *)&flags)) == -1)
|
|
|
|
set_winsock_errno ();
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-02-09 11:40:48 +01:00
|
|
|
int
|
|
|
|
fhandler_socket::write (const void *ptr, size_t len)
|
|
|
|
{
|
|
|
|
return send (ptr, len, 0);
|
|
|
|
}
|
|
|
|
|
2000-10-27 11:50:33 +02:00
|
|
|
/* Cygwin internal */
|
|
|
|
int
|
|
|
|
fhandler_socket::close ()
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
sigframe thisframe (mainthread);
|
|
|
|
|
2001-08-14 09:41:45 +02:00
|
|
|
/* HACK to allow a graceful shutdown even if shutdown() hasn't been
|
|
|
|
called by the application. Note that this isn't the ultimate
|
|
|
|
solution but it helps in many cases. */
|
|
|
|
struct linger linger;
|
|
|
|
linger.l_onoff = 1;
|
2001-09-05 11:35:08 +02:00
|
|
|
linger.l_linger = 240; /* seconds. default 2MSL value according to MSDN. */
|
2001-08-14 09:41:45 +02:00
|
|
|
setsockopt (get_socket (), SOL_SOCKET, SO_LINGER,
|
|
|
|
(const char *)&linger, sizeof linger);
|
|
|
|
|
2001-10-30 12:48:36 +01:00
|
|
|
while ((res = closesocket (get_socket ()))
|
2001-11-05 07:09:15 +01:00
|
|
|
&& WSAGetLastError () == WSAEWOULDBLOCK)
|
2001-10-30 08:43:46 +01:00
|
|
|
continue;
|
2001-10-30 12:48:36 +01:00
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
set_winsock_errno ();
|
|
|
|
res = -1;
|
|
|
|
}
|
2000-10-27 11:50:33 +02:00
|
|
|
|
2001-04-09 09:21:32 +02:00
|
|
|
close_secret_event ();
|
|
|
|
|
2001-08-14 09:41:45 +02:00
|
|
|
debug_printf ("%d = fhandler_socket::close()", res);
|
2000-10-27 11:50:33 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ASYNC_MASK (FD_READ|FD_WRITE|FD_OOB|FD_ACCEPT|FD_CONNECT)
|
|
|
|
|
|
|
|
/* Cygwin internal */
|
|
|
|
int
|
|
|
|
fhandler_socket::ioctl (unsigned int cmd, void *p)
|
|
|
|
{
|
|
|
|
extern int get_ifconf (struct ifconf *ifc, int what); /* net.cc */
|
|
|
|
int res;
|
2001-02-07 23:50:50 +01:00
|
|
|
struct ifconf ifc, *ifcp;
|
|
|
|
struct ifreq *ifr, *ifrp;
|
2000-10-27 11:50:33 +02:00
|
|
|
sigframe thisframe (mainthread);
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case SIOCGIFCONF:
|
2001-02-07 23:50:50 +01:00
|
|
|
ifcp = (struct ifconf *) p;
|
|
|
|
if (!ifcp)
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
2001-02-07 23:50:50 +01:00
|
|
|
res = get_ifconf (ifcp, cmd);
|
2000-10-27 11:50:33 +02:00
|
|
|
if (res)
|
|
|
|
debug_printf ("error in get_ifconf\n");
|
|
|
|
break;
|
|
|
|
case SIOCGIFFLAGS:
|
|
|
|
ifr = (struct ifreq *) p;
|
|
|
|
if (ifr == 0)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ifr->ifr_flags = IFF_NOTRAILERS | IFF_UP | IFF_RUNNING;
|
2001-05-15 10:42:15 +02:00
|
|
|
if (ntohl (((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr)
|
2001-09-07 23:32:07 +02:00
|
|
|
== INADDR_LOOPBACK)
|
|
|
|
ifr->ifr_flags |= IFF_LOOPBACK;
|
2000-10-27 11:50:33 +02:00
|
|
|
else
|
2001-09-07 23:32:07 +02:00
|
|
|
ifr->ifr_flags |= IFF_BROADCAST;
|
2000-10-27 11:50:33 +02:00
|
|
|
res = 0;
|
|
|
|
break;
|
|
|
|
case SIOCGIFBRDADDR:
|
|
|
|
case SIOCGIFNETMASK:
|
|
|
|
case SIOCGIFADDR:
|
2001-02-07 23:50:50 +01:00
|
|
|
case SIOCGIFHWADDR:
|
|
|
|
case SIOCGIFMETRIC:
|
|
|
|
case SIOCGIFMTU:
|
2000-10-27 11:50:33 +02:00
|
|
|
{
|
2001-02-07 23:50:50 +01:00
|
|
|
ifc.ifc_len = 2048;
|
|
|
|
ifc.ifc_buf = (char *) alloca (2048);
|
2000-10-27 11:50:33 +02:00
|
|
|
|
2001-02-07 23:50:50 +01:00
|
|
|
ifr = (struct ifreq *) p;
|
2000-10-27 11:50:33 +02:00
|
|
|
if (ifr == 0)
|
|
|
|
{
|
|
|
|
debug_printf ("ifr == NULL\n");
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = get_ifconf (&ifc, cmd);
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
debug_printf ("error in get_ifconf\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug_printf (" name: %s\n", ifr->ifr_name);
|
|
|
|
for (ifrp = ifc.ifc_req;
|
|
|
|
(caddr_t) ifrp < ifc.ifc_buf + ifc.ifc_len;
|
|
|
|
++ifrp)
|
|
|
|
{
|
|
|
|
debug_printf ("testname: %s\n", ifrp->ifr_name);
|
|
|
|
if (! strcmp (ifrp->ifr_name, ifr->ifr_name))
|
|
|
|
{
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case SIOCGIFADDR:
|
|
|
|
ifr->ifr_addr = ifrp->ifr_addr;
|
|
|
|
break;
|
|
|
|
case SIOCGIFBRDADDR:
|
|
|
|
ifr->ifr_broadaddr = ifrp->ifr_broadaddr;
|
|
|
|
break;
|
|
|
|
case SIOCGIFNETMASK:
|
|
|
|
ifr->ifr_netmask = ifrp->ifr_netmask;
|
|
|
|
break;
|
2001-02-07 23:50:50 +01:00
|
|
|
case SIOCGIFHWADDR:
|
|
|
|
ifr->ifr_hwaddr = ifrp->ifr_hwaddr;
|
|
|
|
break;
|
|
|
|
case SIOCGIFMETRIC:
|
|
|
|
ifr->ifr_metric = ifrp->ifr_metric;
|
|
|
|
break;
|
|
|
|
case SIOCGIFMTU:
|
|
|
|
ifr->ifr_mtu = ifrp->ifr_mtu;
|
|
|
|
break;
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((caddr_t) ifrp >= ifc.ifc_buf + ifc.ifc_len)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FIOASYNC:
|
|
|
|
res = WSAAsyncSelect (get_socket (), gethwnd (), WM_ASYNCIO,
|
|
|
|
*(int *) p ? ASYNC_MASK : 0);
|
|
|
|
syscall_printf ("Async I/O on socket %s",
|
|
|
|
*(int *) p ? "started" : "cancelled");
|
|
|
|
set_async (*(int *) p);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* We must cancel WSAAsyncSelect (if any) before setting socket to
|
|
|
|
* blocking mode
|
|
|
|
*/
|
|
|
|
if (cmd == FIONBIO && *(int *) p == 0)
|
|
|
|
WSAAsyncSelect (get_socket (), gethwnd (), 0, 0);
|
|
|
|
res = ioctlsocket (get_socket (), cmd, (unsigned long *) p);
|
|
|
|
if (res == SOCKET_ERROR)
|
|
|
|
set_winsock_errno ();
|
|
|
|
if (cmd == FIONBIO)
|
|
|
|
{
|
|
|
|
syscall_printf ("socket is now %sblocking",
|
2001-10-13 19:23:35 +02:00
|
|
|
*(int *) p ? "non" : "");
|
2000-10-27 11:50:33 +02:00
|
|
|
/* Start AsyncSelect if async socket unblocked */
|
|
|
|
if (*(int *) p && get_async ())
|
|
|
|
WSAAsyncSelect (get_socket (), gethwnd (), WM_ASYNCIO, ASYNC_MASK);
|
2001-08-14 09:41:45 +02:00
|
|
|
|
2001-08-15 09:49:15 +02:00
|
|
|
set_nonblocking (*(int *) p);
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
syscall_printf ("%d = ioctl_socket (%x, %x)", res, cmd, p);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fhandler_socket::fcntl (int cmd, void *arg)
|
|
|
|
{
|
|
|
|
int res = 0;
|
|
|
|
int request, current;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case F_SETFL:
|
|
|
|
{
|
2001-09-07 23:32:07 +02:00
|
|
|
/* Carefully test for the O_NONBLOCK or deprecated OLD_O_NDELAY flag.
|
2001-08-07 02:01:42 +02:00
|
|
|
Set only the flag that has been passed in. If both are set, just
|
|
|
|
record O_NONBLOCK. */
|
2001-08-14 09:41:45 +02:00
|
|
|
int new_flags = (int) arg & O_NONBLOCK_MASK;
|
2001-08-07 02:01:42 +02:00
|
|
|
if ((new_flags & OLD_O_NDELAY) && (new_flags & O_NONBLOCK))
|
|
|
|
new_flags = O_NONBLOCK;
|
2001-09-07 23:32:07 +02:00
|
|
|
current = get_flags () & O_NONBLOCK_MASK;
|
2001-08-14 09:41:45 +02:00
|
|
|
request = new_flags ? 1 : 0;
|
2001-09-07 23:32:07 +02:00
|
|
|
if (!!current != !!new_flags && (res = ioctl (FIONBIO, &request)))
|
|
|
|
break;
|
2001-08-14 09:41:45 +02:00
|
|
|
set_flags ((get_flags () & ~O_NONBLOCK_MASK) | new_flags);
|
2001-09-07 23:32:07 +02:00
|
|
|
break;
|
2000-10-27 11:50:33 +02:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
res = fhandler_base::fcntl (cmd, arg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-06-22 22:53:45 +02:00
|
|
|
void
|
|
|
|
fhandler_socket::set_close_on_exec (int val)
|
|
|
|
{
|
2001-10-13 19:23:35 +02:00
|
|
|
if (!winsock2_active) /* < Winsock 2.0 */
|
2001-06-22 22:53:45 +02:00
|
|
|
set_inheritance (get_handle (), val);
|
|
|
|
set_close_on_exec_flag (val);
|
|
|
|
debug_printf ("set close_on_exec for %s to %d", get_name (), val);
|
|
|
|
}
|
2002-01-01 17:25:31 +01:00
|
|
|
|
|
|
|
void
|
|
|
|
fhandler_socket::set_sun_path (const char *path)
|
|
|
|
{
|
|
|
|
if (sun_path)
|
|
|
|
cfree (sun_path);
|
|
|
|
sun_path = cstrdup (path);
|
|
|
|
}
|