2016-01-26 16:07:52 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
2020-02-20 15:35:16 +00:00
|
|
|
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
|
2016-01-26 16:07:52 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAfRRANTIES
|
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2020-05-25 08:04:30 +00:00
|
|
|
#include <mio-sck.h>
|
2018-12-12 13:15:54 +00:00
|
|
|
#include "mio-prv.h"
|
2016-02-02 15:40:09 +00:00
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <fcntl.h>
|
2016-01-28 16:44:47 +00:00
|
|
|
#include <unistd.h>
|
2016-01-26 16:02:20 +00:00
|
|
|
#include <errno.h>
|
2016-03-30 16:31:56 +00:00
|
|
|
|
2016-01-28 16:44:47 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2016-04-25 16:15:36 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_NETPACKET_PACKET_H)
|
|
|
|
# include <netpacket/packet.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_NET_IF_DL_H)
|
|
|
|
# include <net/if_dl.h>
|
|
|
|
#endif
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
#if defined(__linux__)
|
|
|
|
# include <limits.h>
|
|
|
|
# if defined(HAVE_LINUX_NETFILTER_IPV4_H)
|
|
|
|
# include <linux/netfilter_ipv4.h> /* SO_ORIGINAL_DST */
|
|
|
|
# endif
|
|
|
|
# if !defined(SO_ORIGINAL_DST)
|
|
|
|
# define SO_ORIGINAL_DST 80
|
|
|
|
# endif
|
|
|
|
# if !defined(IP_TRANSPARENT)
|
|
|
|
# define IP_TRANSPARENT 19
|
|
|
|
# endif
|
|
|
|
# if !defined(SO_REUSEPORT)
|
|
|
|
# define SO_REUSEPORT 15
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
#if defined(HAVE_OPENSSL_SSL_H) && defined(HAVE_SSL)
|
|
|
|
# include <openssl/ssl.h>
|
|
|
|
# if defined(HAVE_OPENSSL_ERR_H)
|
|
|
|
# include <openssl/err.h>
|
|
|
|
# endif
|
|
|
|
# if defined(HAVE_OPENSSL_ENGINE_H)
|
|
|
|
# include <openssl/engine.h>
|
|
|
|
# endif
|
|
|
|
# define USE_SSL
|
|
|
|
#endif
|
|
|
|
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2016-03-29 15:02:01 +00:00
|
|
|
/* ========================================================================= */
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
int mio_makesckasync (mio_t* mio, mio_sckhnd_t sck)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
return mio_makesyshndasync (mio, (mio_syshnd_t)sck);
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
static void close_async_socket (mio_t* mio, mio_sckhnd_t sck)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2019-01-11 07:35:43 +00:00
|
|
|
close (sck);
|
|
|
|
}
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
static mio_sckhnd_t open_async_socket (mio_t* mio, int domain, int type, int proto)
|
|
|
|
{
|
|
|
|
mio_sckhnd_t sck = MIO_SCKHND_INVALID;
|
|
|
|
int flags;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
|
|
|
|
type |= SOCK_NONBLOCK;
|
|
|
|
type |= SOCK_CLOEXEC;
|
|
|
|
open_socket:
|
|
|
|
#endif
|
|
|
|
sck = socket(domain, type, proto);
|
|
|
|
if (sck == MIO_SCKHND_INVALID)
|
2016-04-06 13:49:47 +00:00
|
|
|
{
|
2019-01-11 07:35:43 +00:00
|
|
|
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
|
|
|
|
if (errno == EINVAL && (type & (SOCK_NONBLOCK | SOCK_CLOEXEC)))
|
2016-04-06 13:49:47 +00:00
|
|
|
{
|
2019-01-11 07:35:43 +00:00
|
|
|
type &= ~(SOCK_NONBLOCK | SOCK_CLOEXEC);
|
|
|
|
goto open_socket;
|
2016-04-06 13:49:47 +00:00
|
|
|
}
|
2019-01-11 07:35:43 +00:00
|
|
|
#endif
|
|
|
|
goto oops;
|
2016-04-06 13:49:47 +00:00
|
|
|
}
|
2019-01-11 07:35:43 +00:00
|
|
|
else
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2019-01-11 07:35:43 +00:00
|
|
|
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC)
|
|
|
|
if (type & (SOCK_NONBLOCK | SOCK_CLOEXEC)) goto done;
|
|
|
|
#endif
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
2016-03-23 13:54:15 +00:00
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
flags = fcntl(sck, F_GETFD, 0);
|
|
|
|
if (flags == -1) goto oops;
|
|
|
|
#if defined(FD_CLOEXEC)
|
|
|
|
flags |= FD_CLOEXEC;
|
2016-01-26 16:02:20 +00:00
|
|
|
#endif
|
2019-01-11 07:35:43 +00:00
|
|
|
#if defined(O_NONBLOCK)
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
#endif
|
|
|
|
if (fcntl(sck, F_SETFD, flags) == -1) goto oops;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
done:
|
2016-01-26 16:02:20 +00:00
|
|
|
return sck;
|
2019-01-11 07:35:43 +00:00
|
|
|
|
|
|
|
oops:
|
|
|
|
if (sck != MIO_SCKHND_INVALID) close (sck);
|
2019-01-27 02:09:22 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2019-01-11 07:35:43 +00:00
|
|
|
return MIO_SCKHND_INVALID;
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-02 15:25:35 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
static mio_devaddr_t* skad_to_devaddr (mio_dev_sck_t* dev, const mio_skad_t* sckaddr, mio_devaddr_t* devaddr)
|
2016-03-30 16:31:56 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
if (sckaddr)
|
2016-03-30 16:31:56 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
devaddr->ptr = (void*)sckaddr;
|
2020-02-20 06:00:53 +00:00
|
|
|
devaddr->len = mio_skad_size(sckaddr);
|
2016-04-18 14:21:23 +00:00
|
|
|
return devaddr;
|
2016-03-30 16:31:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
return MIO_NULL;
|
2016-03-30 16:31:56 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
static MIO_INLINE mio_skad_t* devaddr_to_skad (mio_dev_sck_t* dev, const mio_devaddr_t* devaddr, mio_skad_t* sckaddr)
|
2016-03-30 16:31:56 +00:00
|
|
|
{
|
2020-02-20 06:00:53 +00:00
|
|
|
return (mio_skad_t*)devaddr->ptr;
|
2016-03-30 16:31:56 +00:00
|
|
|
}
|
2016-04-02 15:25:35 +00:00
|
|
|
|
2016-03-29 15:02:01 +00:00
|
|
|
/* ========================================================================= */
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2019-01-31 09:16:44 +00:00
|
|
|
#define IS_STATEFUL(sck) ((sck)->dev_cap & MIO_DEV_CAP_STREAM)
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2016-03-29 15:02:01 +00:00
|
|
|
struct sck_type_map_t
|
|
|
|
{
|
|
|
|
int domain;
|
|
|
|
int type;
|
|
|
|
int proto;
|
2019-01-31 09:16:44 +00:00
|
|
|
int extra_dev_cap;
|
2016-03-30 07:06:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct sck_type_map_t sck_type_map[] =
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
/* MIO_DEV_SCK_TCP4 */
|
2019-01-31 09:16:44 +00:00
|
|
|
{ AF_INET, SOCK_STREAM, 0, MIO_DEV_CAP_STREAM },
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
/* MIO_DEV_SCK_TCP6 */
|
2019-01-31 09:16:44 +00:00
|
|
|
{ AF_INET6, SOCK_STREAM, 0, MIO_DEV_CAP_STREAM },
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
/* MIO_DEV_SCK_UPD4 */
|
2016-03-30 07:06:54 +00:00
|
|
|
{ AF_INET, SOCK_DGRAM, 0, 0 },
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
/* MIO_DEV_SCK_UDP6 */
|
2016-03-30 07:06:54 +00:00
|
|
|
{ AF_INET6, SOCK_DGRAM, 0, 0 },
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2016-04-25 16:15:36 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
#if defined(AF_PACKET) && (MIO_SIZEOF_STRUCT_SOCKADDR_LL > 0)
|
|
|
|
/* MIO_DEV_SCK_ARP - Ethernet type is 2 bytes long. Protocol must be specified in the network byte order */
|
|
|
|
{ AF_PACKET, SOCK_RAW, MIO_CONST_HTON16(MIO_ETHHDR_PROTO_ARP), 0 },
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2019-04-12 03:53:32 +00:00
|
|
|
/* MIO_DEV_SCK_ARP_DGRAM */
|
2018-12-12 13:15:54 +00:00
|
|
|
{ AF_PACKET, SOCK_DGRAM, MIO_CONST_HTON16(MIO_ETHHDR_PROTO_ARP), 0 },
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
#elif defined(AF_LINK) && (MIO_SIZEOF_STRUCT_SOCKADDR_DL > 0)
|
|
|
|
/* MIO_DEV_SCK_ARP */
|
|
|
|
{ AF_LINK, SOCK_RAW, MIO_CONST_HTON16(MIO_ETHHDR_PROTO_ARP), 0 },
|
2016-04-25 16:15:36 +00:00
|
|
|
|
2019-04-12 03:53:32 +00:00
|
|
|
/* MIO_DEV_SCK_ARP_DGRAM */
|
2018-12-12 13:15:54 +00:00
|
|
|
{ AF_LINK, SOCK_DGRAM, MIO_CONST_HTON16(MIO_ETHHDR_PROTO_ARP), 0 },
|
2016-04-25 16:15:36 +00:00
|
|
|
#else
|
2016-04-26 05:54:48 +00:00
|
|
|
{ -1, 0, 0, 0 },
|
|
|
|
{ -1, 0, 0, 0 },
|
2016-04-25 16:15:36 +00:00
|
|
|
#endif
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
/* MIO_DEV_SCK_ICMP4 - IP protocol field is 1 byte only. no byte order conversion is needed */
|
2016-04-25 14:07:28 +00:00
|
|
|
{ AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, },
|
2016-03-30 16:31:56 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
/* MIO_DEV_SCK_ICMP6 - IP protocol field is 1 byte only. no byte order conversion is needed */
|
2016-04-25 14:07:28 +00:00
|
|
|
{ AF_INET6, SOCK_RAW, IPPROTO_ICMP, 0, }
|
2016-03-29 15:02:01 +00:00
|
|
|
};
|
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
/* ======================================================================== */
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static void connect_timedout (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* job)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)job->ctx;
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_ASSERT (mio, IS_STATEFUL(rdev));
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (rdev->state & MIO_DEV_SCK_CONNECTING)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
/* the state check for MIO_DEV_TCP_CONNECTING is actually redundant
|
2016-03-30 07:06:54 +00:00
|
|
|
* as it must not be fired after it gets connected. the timer job
|
|
|
|
* doesn't need to be deleted when it gets connected for this check
|
|
|
|
* here. this libarary, however, deletes the job when it gets
|
|
|
|
* connected. */
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_halt (rdev);
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static void ssl_accept_timedout (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* job)
|
2016-04-20 15:59:11 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)job->ctx;
|
2016-04-20 15:59:11 +00:00
|
|
|
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_ASSERT (mio, IS_STATEFUL(rdev));
|
2016-04-20 15:59:11 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (rdev->state & MIO_DEV_SCK_ACCEPTING_SSL)
|
2016-04-20 15:59:11 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_halt(rdev);
|
2016-04-20 15:59:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static void ssl_connect_timedout (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* job)
|
2016-04-21 15:07:58 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)job->ctx;
|
2016-04-21 15:07:58 +00:00
|
|
|
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_ASSERT (mio, IS_STATEFUL(rdev));
|
2016-04-21 15:07:58 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (rdev->state & MIO_DEV_SCK_CONNECTING_SSL)
|
2016-04-21 15:07:58 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_halt(rdev);
|
2016-04-21 15:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int schedule_timer_job_at (mio_dev_sck_t* dev, const mio_ntime_t* fire_at, mio_tmrjob_handler_t handler)
|
2016-04-21 15:07:58 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_tmrjob_t tmrjob;
|
2016-04-21 15:07:58 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
MIO_MEMSET (&tmrjob, 0, MIO_SIZEOF(tmrjob));
|
2016-04-21 15:07:58 +00:00
|
|
|
tmrjob.ctx = dev;
|
2016-04-22 06:31:12 +00:00
|
|
|
tmrjob.when = *fire_at;
|
2016-04-21 15:07:58 +00:00
|
|
|
|
|
|
|
tmrjob.handler = handler;
|
|
|
|
tmrjob.idxptr = &dev->tmrjob_index;
|
|
|
|
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_ASSERT (dev->mio, dev->tmrjob_index == MIO_TMRIDX_INVALID);
|
2018-12-12 13:15:54 +00:00
|
|
|
dev->tmrjob_index = mio_instmrjob (dev->mio, &tmrjob);
|
|
|
|
return dev->tmrjob_index == MIO_TMRIDX_INVALID? -1: 0;
|
2016-04-21 15:07:58 +00:00
|
|
|
}
|
2016-04-22 06:31:12 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int schedule_timer_job_after (mio_dev_sck_t* dev, const mio_ntime_t* fire_after, mio_tmrjob_handler_t handler)
|
2016-04-22 06:31:12 +00:00
|
|
|
{
|
2019-01-30 10:18:58 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_ntime_t fire_at;
|
2016-04-22 06:31:12 +00:00
|
|
|
|
2019-01-30 10:18:58 +00:00
|
|
|
MIO_ASSERT (mio, MIO_IS_POS_NTIME(fire_after));
|
2016-04-22 06:31:12 +00:00
|
|
|
|
2019-01-30 10:18:58 +00:00
|
|
|
mio_gettime (mio, &fire_at);
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_ADD_NTIME (&fire_at, &fire_at, fire_after);
|
2016-04-22 06:31:12 +00:00
|
|
|
|
2019-01-30 10:18:58 +00:00
|
|
|
return schedule_timer_job_at(dev, &fire_at, handler);
|
2016-04-22 06:31:12 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
/* ======================================================================== */
|
2019-01-28 08:13:06 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
static void set_ssl_error (mio_t* mio, int sslerr)
|
|
|
|
{
|
|
|
|
mio_bch_t emsg[128];
|
|
|
|
ERR_error_string_n (sslerr, emsg, MIO_COUNTOF(emsg));
|
|
|
|
mio_seterrbfmt (mio, MIO_ESYSERR, "%hs", emsg);
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_sck_make (mio_dev_t* dev, void* ctx)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
|
|
|
mio_dev_sck_make_t* arg = (mio_dev_sck_make_t*)ctx;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
MIO_ASSERT (mio, arg->type >= 0 && arg->type < MIO_COUNTOF(sck_type_map));
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2016-04-26 05:54:48 +00:00
|
|
|
if (sck_type_map[arg->type].domain <= -1)
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_ENOIMPL); /* TODO: better error info? */
|
2016-04-26 05:54:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
rdev->hnd = open_async_socket(mio, sck_type_map[arg->type].domain, sck_type_map[arg->type].type, sck_type_map[arg->type].proto);
|
|
|
|
if (rdev->hnd == MIO_SCKHND_INVALID) goto oops;
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2019-01-31 09:16:44 +00:00
|
|
|
rdev->dev_cap = MIO_DEV_CAP_IN | MIO_DEV_CAP_OUT | sck_type_map[arg->type].extra_dev_cap;
|
2016-03-29 15:02:01 +00:00
|
|
|
rdev->on_write = arg->on_write;
|
|
|
|
rdev->on_read = arg->on_read;
|
2019-01-11 07:35:43 +00:00
|
|
|
rdev->on_connect = arg->on_connect;
|
2016-04-25 14:07:28 +00:00
|
|
|
rdev->on_disconnect = arg->on_disconnect;
|
2016-03-30 07:06:54 +00:00
|
|
|
rdev->type = arg->type;
|
2018-12-12 13:15:54 +00:00
|
|
|
rdev->tmrjob_index = MIO_TMRIDX_INVALID;
|
2016-04-02 15:25:35 +00:00
|
|
|
|
2016-03-29 15:02:01 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
2020-05-12 17:53:19 +00:00
|
|
|
if (rdev->hnd != MIO_SCKHND_INVALID)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2020-05-12 17:53:19 +00:00
|
|
|
close_async_socket (mio, rdev->hnd);
|
|
|
|
rdev->hnd = MIO_SCKHND_INVALID;
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_sck_make_client (mio_dev_t* dev, void* ctx)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2020-05-24 02:04:07 +00:00
|
|
|
mio_syshnd_t* clisckhnd = (mio_syshnd_t*)ctx;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
/* create a socket device that is made of a socket connection
|
|
|
|
* on a listening socket.
|
|
|
|
* nothing special is done here except setting the socket handle.
|
2016-04-18 14:21:23 +00:00
|
|
|
* most of the initialization is done by the listening socket device
|
|
|
|
* after a client socket has been created. */
|
|
|
|
|
2020-05-24 02:04:07 +00:00
|
|
|
rdev->hnd = *clisckhnd;
|
2018-12-12 13:15:54 +00:00
|
|
|
rdev->tmrjob_index = MIO_TMRIDX_INVALID;
|
2016-04-20 15:59:11 +00:00
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
if (mio_makesckasync(mio, rdev->hnd) <= -1) return -1;
|
2016-04-18 14:21:23 +00:00
|
|
|
#if defined(FD_CLOEXEC)
|
|
|
|
{
|
2020-05-12 17:53:19 +00:00
|
|
|
int flags = fcntl(rdev->hnd, F_GETFD, 0);
|
|
|
|
if (fcntl(rdev->hnd, F_SETFD, flags | FD_CLOEXEC) == -1)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmtwithsyserr (mio, 0, errno, "unable to set FD_CLOEXEC");
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-30 07:06:54 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_sck_kill (mio_dev_t* dev, int force)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
|
|
|
if (IS_STATEFUL(rdev))
|
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
/*if (MIO_DEV_SCK_GET_PROGRESS(rdev))
|
2016-04-25 14:07:28 +00:00
|
|
|
{*/
|
2020-05-12 17:53:19 +00:00
|
|
|
/* for MIO_DEV_SCK_CONNECTING, MIO_DEV_SCK_CONNECTING_SSL, and MIO_DEV_SCK_ACCEPTING_SSL
|
|
|
|
* on_disconnect() is called without corresponding on_connect().
|
|
|
|
* it is the same if connect or accept has not been called. */
|
2016-03-30 07:06:54 +00:00
|
|
|
if (rdev->on_disconnect) rdev->on_disconnect (rdev);
|
2016-04-25 14:07:28 +00:00
|
|
|
/*}*/
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (rdev->tmrjob_index != MIO_TMRIDX_INVALID)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_deltmrjob (mio, rdev->tmrjob_index);
|
|
|
|
MIO_ASSERT (mio, rdev->tmrjob_index == MIO_TMRIDX_INVALID);
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
MIO_ASSERT (mio, rdev->state == 0);
|
|
|
|
MIO_ASSERT (mio, rdev->tmrjob_index == MIO_TMRIDX_INVALID);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
|
|
|
if (rdev->on_disconnect) rdev->on_disconnect (rdev);
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
if (rdev->ssl)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
SSL_shutdown ((SSL*)rdev->ssl); /* is this needed? */
|
|
|
|
SSL_free ((SSL*)rdev->ssl);
|
2018-12-12 13:15:54 +00:00
|
|
|
rdev->ssl = MIO_NULL;
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
if (!(rdev->state & (MIO_DEV_SCK_ACCEPTED | MIO_DEV_SCK_ACCEPTING_SSL)) && rdev->ssl_ctx)
|
2016-04-17 17:00:58 +00:00
|
|
|
{
|
2016-04-20 15:59:11 +00:00
|
|
|
SSL_CTX_free ((SSL_CTX*)rdev->ssl_ctx);
|
2018-12-12 13:15:54 +00:00
|
|
|
rdev->ssl_ctx = MIO_NULL;
|
2016-04-17 17:00:58 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
if (rdev->hnd != MIO_SCKHND_INVALID)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2020-05-12 17:53:19 +00:00
|
|
|
close_async_socket (mio, rdev->hnd);
|
|
|
|
rdev->hnd = MIO_SCKHND_INVALID;
|
2016-04-18 14:21:23 +00:00
|
|
|
}
|
|
|
|
|
2016-04-16 16:05:57 +00:00
|
|
|
return 0;
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static mio_syshnd_t dev_sck_getsyshnd (mio_dev_t* dev)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2020-05-12 17:53:19 +00:00
|
|
|
return (mio_syshnd_t)rdev->hnd;
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_sck_read_stateful (mio_dev_t* dev, void* buf, mio_iolen_t* len, mio_devaddr_t* srcaddr)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
if (rdev->ssl)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
int x;
|
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
x = SSL_read((SSL*)rdev->ssl, buf, *len);
|
2016-04-18 14:21:23 +00:00
|
|
|
if (x <= -1)
|
|
|
|
{
|
2019-01-23 09:37:23 +00:00
|
|
|
int err = SSL_get_error((SSL*)rdev->ssl, x);
|
2016-04-18 14:21:23 +00:00
|
|
|
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) return 0;
|
2019-01-28 08:13:06 +00:00
|
|
|
set_ssl_error (mio, err);
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*len = x;
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
2016-04-18 14:21:23 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
ssize_t x;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
x = recv(rdev->hnd, buf, *len, 0);
|
2016-04-18 14:21:23 +00:00
|
|
|
if (x == -1)
|
|
|
|
{
|
|
|
|
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data available */
|
|
|
|
if (errno == EINTR) return 0;
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*len = x;
|
|
|
|
#if defined(USE_SSL)
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-30 07:06:54 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_sck_read_stateless (mio_dev_t* dev, void* buf, mio_iolen_t* len, mio_devaddr_t* srcaddr)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
|
|
|
mio_scklen_t srcaddrlen;
|
2016-03-30 07:06:54 +00:00
|
|
|
ssize_t x;
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
srcaddrlen = MIO_SIZEOF(rdev->remoteaddr);
|
2020-05-12 17:53:19 +00:00
|
|
|
x = recvfrom(rdev->hnd, buf, *len, 0, (struct sockaddr*)&rdev->remoteaddr, &srcaddrlen);
|
2016-03-29 15:02:01 +00:00
|
|
|
if (x <= -1)
|
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data available */
|
2016-03-29 15:02:01 +00:00
|
|
|
if (errno == EINTR) return 0;
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2016-03-29 15:02:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
srcaddr->ptr = &rdev->remoteaddr;
|
|
|
|
srcaddr->len = srcaddrlen;
|
2016-03-30 16:31:56 +00:00
|
|
|
|
2016-03-29 15:02:01 +00:00
|
|
|
*len = x;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_sck_write_stateful (mio_dev_t* dev, const void* data, mio_iolen_t* len, const mio_devaddr_t* dstaddr)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
if (rdev->ssl)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
int x;
|
|
|
|
|
|
|
|
if (*len <= 0)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
/* it's a writing finish indicator. close the writing end of
|
|
|
|
* the socket, probably leaving it in the half-closed state */
|
2019-01-28 08:13:06 +00:00
|
|
|
if ((x = SSL_shutdown((SSL*)rdev->ssl)) == -1)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
set_ssl_error (mio, SSL_get_error((SSL*)rdev->ssl, x));
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
x = SSL_write((SSL*)rdev->ssl, data, *len);
|
2016-04-18 14:21:23 +00:00
|
|
|
if (x <= -1)
|
|
|
|
{
|
|
|
|
int err = SSL_get_error ((SSL*)rdev->ssl, x);
|
|
|
|
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) return 0;
|
2019-01-28 08:13:06 +00:00
|
|
|
set_ssl_error (mio, err);
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
*len = x;
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
2016-04-18 14:21:23 +00:00
|
|
|
else
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
#endif
|
|
|
|
ssize_t x;
|
|
|
|
int flags = 0;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
if (*len <= 0)
|
|
|
|
{
|
2020-05-07 04:32:32 +00:00
|
|
|
/* the write handler for a stream device must handle a zero-length
|
|
|
|
* writing request specially. it's a writing finish indicator. close
|
|
|
|
* the writing end of the socket, probably leaving it in the half-closed state */
|
2020-05-12 17:53:19 +00:00
|
|
|
if (shutdown(rdev->hnd, SHUT_WR) == -1)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-07 04:32:32 +00:00
|
|
|
/* it must return a non-zero positive value. if it returns 0, this request
|
|
|
|
* gets enqueued by the core. we must aovid it */
|
2016-04-18 14:21:23 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: flags MSG_DONTROUTE, MSG_DONTWAIT, MSG_MORE, MSG_OOB, MSG_NOSIGNAL */
|
|
|
|
#if defined(MSG_NOSIGNAL)
|
|
|
|
flags |= MSG_NOSIGNAL;
|
|
|
|
#endif
|
2020-05-12 17:53:19 +00:00
|
|
|
x = send(rdev->hnd, data, *len, flags);
|
2016-04-18 14:21:23 +00:00
|
|
|
if (x == -1)
|
|
|
|
{
|
|
|
|
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */
|
|
|
|
if (errno == EINTR) return 0;
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*len = x;
|
|
|
|
#if defined(USE_SSL)
|
|
|
|
}
|
|
|
|
#endif
|
2016-03-30 07:06:54 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-22 18:24:49 +00:00
|
|
|
|
|
|
|
static int dev_sck_writev_stateful (mio_dev_t* dev, const mio_iovec_t* iov, mio_iolen_t* iovcnt, const mio_devaddr_t* dstaddr)
|
|
|
|
{
|
|
|
|
mio_t* mio = dev->mio;
|
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
|
|
|
|
2020-02-23 16:11:32 +00:00
|
|
|
#if defined(USE_SSL)
|
2020-02-22 18:24:49 +00:00
|
|
|
if (rdev->ssl)
|
|
|
|
{
|
|
|
|
int x;
|
2020-02-23 16:11:32 +00:00
|
|
|
mio_iolen_t i, nwritten;
|
2020-02-22 18:24:49 +00:00
|
|
|
|
|
|
|
if (*iovcnt <= 0)
|
|
|
|
{
|
|
|
|
/* it's a writing finish indicator. close the writing end of
|
|
|
|
* the socket, probably leaving it in the half-closed state */
|
|
|
|
if ((x = SSL_shutdown((SSL*)rdev->ssl)) == -1)
|
|
|
|
{
|
|
|
|
set_ssl_error (mio, SSL_get_error((SSL*)rdev->ssl, x));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-23 16:11:32 +00:00
|
|
|
nwritten = 0;
|
|
|
|
for (i = 0; i < *iovcnt; i++)
|
2020-02-22 18:24:49 +00:00
|
|
|
{
|
2020-02-23 16:11:32 +00:00
|
|
|
/* no SSL_writev. invoke multiple calls to SSL_write().
|
|
|
|
* since the write function is for the stateful connection,
|
|
|
|
* mutiple calls shouldn't really matter */
|
|
|
|
x = SSL_write((SSL*)rdev->ssl, iov[i].iov_ptr, iov[i].iov_len);
|
|
|
|
if (x <= -1)
|
|
|
|
{
|
|
|
|
int err = SSL_get_error ((SSL*)rdev->ssl, x);
|
|
|
|
if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) return 0;
|
|
|
|
set_ssl_error (mio, err);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
nwritten += x;
|
2020-02-22 18:24:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 16:11:32 +00:00
|
|
|
*iovcnt = nwritten;
|
2020-02-22 18:24:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#endif
|
|
|
|
ssize_t x;
|
|
|
|
int flags = 0;
|
|
|
|
struct msghdr msg;
|
|
|
|
|
|
|
|
if (*iovcnt <= 0)
|
|
|
|
{
|
|
|
|
/* it's a writing finish indicator. close the writing end of
|
|
|
|
* the socket, probably leaving it in the half-closed state */
|
2020-05-12 17:53:19 +00:00
|
|
|
if (shutdown(rdev->hnd, SHUT_WR) == -1)
|
2020-02-22 18:24:49 +00:00
|
|
|
{
|
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: flags MSG_DONTROUTE, MSG_DONTWAIT, MSG_MORE, MSG_OOB, MSG_NOSIGNAL */
|
|
|
|
#if defined(MSG_NOSIGNAL)
|
|
|
|
flags |= MSG_NOSIGNAL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(HAVE_SENDMSG)
|
|
|
|
MIO_MEMSET (&msg, 0, MIO_SIZEOF(msg));
|
|
|
|
msg.msg_iov = (struct iovec*)iov;
|
|
|
|
msg.msg_iovlen = *iovcnt;
|
2020-05-12 17:53:19 +00:00
|
|
|
x = sendmsg(rdev->hnd, &msg, flags);
|
2020-02-22 18:24:49 +00:00
|
|
|
#else
|
2020-05-12 17:53:19 +00:00
|
|
|
x = writev(rdev->hnd, (const struct iovec*)iov, *iovcnt);
|
2020-02-22 18:24:49 +00:00
|
|
|
#endif
|
|
|
|
if (x == -1)
|
|
|
|
{
|
|
|
|
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */
|
|
|
|
if (errno == EINTR) return 0;
|
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*iovcnt = x;
|
2020-02-23 16:11:32 +00:00
|
|
|
#if defined(USE_SSL)
|
2020-02-22 18:24:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_sck_write_stateless (mio_dev_t* dev, const void* data, mio_iolen_t* len, const mio_devaddr_t* dstaddr)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2016-03-29 15:02:01 +00:00
|
|
|
ssize_t x;
|
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
x = sendto(rdev->hnd, data, *len, 0, dstaddr->ptr, dstaddr->len);
|
2016-03-29 15:02:01 +00:00
|
|
|
if (x <= -1)
|
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */
|
2016-03-29 15:02:01 +00:00
|
|
|
if (errno == EINTR) return 0;
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2016-03-29 15:02:01 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*len = x;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-22 18:24:49 +00:00
|
|
|
static int dev_sck_writev_stateless (mio_dev_t* dev, const mio_iovec_t* iov, mio_iolen_t* iovcnt, const mio_devaddr_t* dstaddr)
|
|
|
|
{
|
|
|
|
mio_t* mio = dev->mio;
|
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
|
|
|
struct msghdr msg;
|
|
|
|
ssize_t x;
|
2020-05-07 04:32:32 +00:00
|
|
|
|
2020-02-22 18:24:49 +00:00
|
|
|
MIO_MEMSET (&msg, 0, MIO_SIZEOF(msg));
|
|
|
|
if (MIO_LIKELY(dstaddr))
|
|
|
|
{
|
|
|
|
msg.msg_name = dstaddr->ptr;
|
|
|
|
msg.msg_namelen = dstaddr->len;
|
|
|
|
}
|
|
|
|
msg.msg_iov = (struct iovec*)iov;
|
|
|
|
msg.msg_iovlen = *iovcnt;
|
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
x = sendmsg(rdev->hnd, &msg, 0);
|
2020-02-22 18:24:49 +00:00
|
|
|
if (x <= -1)
|
|
|
|
{
|
|
|
|
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0; /* no data can be written */
|
|
|
|
if (errno == EINTR) return 0;
|
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*iovcnt = x;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-04-21 13:26:14 +00:00
|
|
|
#if defined(USE_SSL)
|
2016-04-22 06:31:12 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int do_ssl (mio_dev_sck_t* dev, int (*ssl_func)(SSL*))
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = dev->mio;
|
|
|
|
int ret, watcher_cmd, watcher_events;
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
MIO_ASSERT (mio, dev->ssl_ctx);
|
2016-04-21 13:26:14 +00:00
|
|
|
|
|
|
|
if (!dev->ssl)
|
|
|
|
{
|
|
|
|
SSL* ssl;
|
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
ssl = SSL_new(dev->ssl_ctx);
|
2016-04-21 13:26:14 +00:00
|
|
|
if (!ssl)
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
set_ssl_error (mio, ERR_get_error());
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
if (SSL_set_fd(ssl, dev->hnd) == 0)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
set_ssl_error (mio, ERR_get_error());
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SSL_set_read_ahead (ssl, 0);
|
2016-04-22 06:31:12 +00:00
|
|
|
|
2016-04-21 13:26:14 +00:00
|
|
|
dev->ssl = ssl;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
watcher_cmd = MIO_DEV_WATCH_RENEW;
|
2020-05-12 17:53:19 +00:00
|
|
|
watcher_events = MIO_DEV_EVENT_IN;
|
2016-04-22 06:31:12 +00:00
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
ret = ssl_func((SSL*)dev->ssl);
|
2016-04-21 13:26:14 +00:00
|
|
|
if (ret <= 0)
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
int err = SSL_get_error(dev->ssl, ret);
|
2016-04-22 06:31:12 +00:00
|
|
|
if (err == SSL_ERROR_WANT_READ)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
/* handshaking isn't complete */
|
2016-04-22 06:31:12 +00:00
|
|
|
ret = 0;
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
2016-04-22 06:31:12 +00:00
|
|
|
else if (err == SSL_ERROR_WANT_WRITE)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2016-04-22 06:31:12 +00:00
|
|
|
/* handshaking isn't complete */
|
2018-12-12 13:15:54 +00:00
|
|
|
watcher_cmd = MIO_DEV_WATCH_UPDATE;
|
|
|
|
watcher_events = MIO_DEV_EVENT_IN | MIO_DEV_EVENT_OUT;
|
2016-04-22 06:31:12 +00:00
|
|
|
ret = 0;
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
2016-04-22 06:31:12 +00:00
|
|
|
else
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
set_ssl_error (mio, err);
|
2016-04-22 06:31:12 +00:00
|
|
|
ret = -1;
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-22 06:31:12 +00:00
|
|
|
else
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2016-04-22 06:31:12 +00:00
|
|
|
ret = 1; /* accepted */
|
|
|
|
}
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2020-03-03 08:49:24 +00:00
|
|
|
if (mio_dev_watch((mio_dev_t*)dev, watcher_cmd, watcher_events) <= -1)
|
2016-04-22 06:31:12 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_stop (mio, MIO_STOPREQ_WATCHER_ERROR);
|
2016-04-22 06:31:12 +00:00
|
|
|
ret = -1;
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
2016-04-24 17:30:43 +00:00
|
|
|
|
2016-04-22 06:31:12 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static MIO_INLINE int connect_ssl (mio_dev_sck_t* dev)
|
2016-04-22 06:31:12 +00:00
|
|
|
{
|
2020-05-12 17:53:19 +00:00
|
|
|
return do_ssl(dev, SSL_connect);
|
2016-04-22 06:31:12 +00:00
|
|
|
}
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static MIO_INLINE int accept_ssl (mio_dev_sck_t* dev)
|
2016-04-22 06:31:12 +00:00
|
|
|
{
|
2020-05-12 17:53:19 +00:00
|
|
|
return do_ssl(dev, SSL_accept);
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2019-01-27 02:09:22 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_BIND:
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_bind_t* bnd = (mio_dev_sck_bind_t*)arg;
|
2016-03-30 07:06:54 +00:00
|
|
|
int x;
|
2016-04-17 17:00:58 +00:00
|
|
|
#if defined(USE_SSL)
|
2018-12-12 13:15:54 +00:00
|
|
|
SSL_CTX* ssl_ctx = MIO_NULL;
|
2016-04-17 17:00:58 +00:00
|
|
|
#endif
|
2018-12-12 13:15:54 +00:00
|
|
|
if (MIO_DEV_SCK_GET_PROGRESS(rdev))
|
2016-04-21 15:07:58 +00:00
|
|
|
{
|
|
|
|
/* can't bind again */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EPERM, "operation in progress. not allowed to bind again");
|
2016-04-21 15:07:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (bnd->options & MIO_DEV_SCK_BIND_BROADCAST)
|
2016-04-02 15:52:12 +00:00
|
|
|
{
|
|
|
|
int v = 1;
|
2020-05-12 17:53:19 +00:00
|
|
|
if (setsockopt(rdev->hnd, SOL_SOCKET, SO_BROADCAST, &v, MIO_SIZEOF(v)) == -1)
|
2016-04-02 15:52:12 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmtwithsyserr (mio, 0, errno, "unable to set SO_BROADCAST");
|
2016-04-02 15:52:12 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (bnd->options & MIO_DEV_SCK_BIND_REUSEADDR)
|
2016-04-02 15:52:12 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
#if defined(SO_REUSEADDR)
|
2016-04-02 15:52:12 +00:00
|
|
|
int v = 1;
|
2020-05-12 17:53:19 +00:00
|
|
|
if (setsockopt(rdev->hnd, SOL_SOCKET, SO_REUSEADDR, &v, MIO_SIZEOF(v)) == -1)
|
2016-04-02 15:52:12 +00:00
|
|
|
{
|
2020-05-24 01:18:32 +00:00
|
|
|
if (!(bnd->options & MIO_DEV_SCK_BIND_IGNERR))
|
|
|
|
{
|
|
|
|
mio_seterrbfmtwithsyserr (mio, 0, errno, "unable to set SO_REUSEADDR");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-02 15:52:12 +00:00
|
|
|
}
|
2019-04-12 02:45:35 +00:00
|
|
|
/* ignore it if not available
|
2016-04-18 14:21:23 +00:00
|
|
|
#else
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_ENOIMPL);
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
2019-04-12 02:45:35 +00:00
|
|
|
*/
|
2016-04-18 14:21:23 +00:00
|
|
|
#endif
|
2016-04-02 15:52:12 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (bnd->options & MIO_DEV_SCK_BIND_REUSEPORT)
|
2016-04-17 17:00:58 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
#if defined(SO_REUSEPORT)
|
|
|
|
int v = 1;
|
2020-05-12 17:53:19 +00:00
|
|
|
if (setsockopt(rdev->hnd, SOL_SOCKET, SO_REUSEPORT, &v, MIO_SIZEOF(v)) == -1)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2020-05-24 01:18:32 +00:00
|
|
|
if (!(bnd->options & MIO_DEV_SCK_BIND_IGNERR))
|
|
|
|
{
|
|
|
|
mio_seterrbfmtwithsyserr (mio, 0, errno, "unable to set SO_REUSEPORT");
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-18 14:21:23 +00:00
|
|
|
}
|
2019-04-12 02:45:35 +00:00
|
|
|
/* ignore it if not available
|
2016-04-18 14:21:23 +00:00
|
|
|
#else
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_ENOIMPL);
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
2019-04-12 02:45:35 +00:00
|
|
|
*/
|
2016-04-18 14:21:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
2016-04-17 17:00:58 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (bnd->options & MIO_DEV_SCK_BIND_TRANSPARENT)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
|
|
|
#if defined(IP_TRANSPARENT)
|
|
|
|
int v = 1;
|
2020-05-12 17:53:19 +00:00
|
|
|
if (setsockopt(rdev->hnd, SOL_IP, IP_TRANSPARENT, &v, MIO_SIZEOF(v)) == -1)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmtwithsyserr (mio, 0, errno, "unable to set IP_TRANSPARENT");
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-04-12 02:45:35 +00:00
|
|
|
/* ignore it if not available
|
2016-04-18 14:21:23 +00:00
|
|
|
#else
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_ENOIMPL);
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
2019-04-12 02:45:35 +00:00
|
|
|
*/
|
2016-04-18 14:21:23 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-04-21 15:07:58 +00:00
|
|
|
if (rdev->ssl_ctx)
|
|
|
|
{
|
2019-01-27 02:09:22 +00:00
|
|
|
#if defined(USE_SSL)
|
2016-04-21 15:07:58 +00:00
|
|
|
SSL_CTX_free (rdev->ssl_ctx);
|
2019-01-27 02:09:22 +00:00
|
|
|
#endif
|
2018-12-12 13:15:54 +00:00
|
|
|
rdev->ssl_ctx = MIO_NULL;
|
2016-04-21 15:07:58 +00:00
|
|
|
|
|
|
|
if (rdev->ssl)
|
|
|
|
{
|
2019-01-27 02:09:22 +00:00
|
|
|
#if defined(USE_SSL)
|
2016-04-21 15:07:58 +00:00
|
|
|
SSL_free (rdev->ssl);
|
2019-01-27 02:09:22 +00:00
|
|
|
#endif
|
2018-12-12 13:15:54 +00:00
|
|
|
rdev->ssl = MIO_NULL;
|
2016-04-21 15:07:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (bnd->options & MIO_DEV_SCK_BIND_SSL)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
|
|
|
#if defined(USE_SSL)
|
2016-04-20 15:59:11 +00:00
|
|
|
if (!bnd->ssl_certfile || !bnd->ssl_keyfile)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EINVAL, "SSL certficate/key file not set");
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-23 09:37:23 +00:00
|
|
|
ssl_ctx = SSL_CTX_new(SSLv23_server_method());
|
2016-04-18 14:21:23 +00:00
|
|
|
if (!ssl_ctx)
|
2016-04-17 17:00:58 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
set_ssl_error (mio, ERR_get_error());
|
2016-04-17 17:00:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-23 09:37:23 +00:00
|
|
|
if (SSL_CTX_use_certificate_file(ssl_ctx, bnd->ssl_certfile, SSL_FILETYPE_PEM) == 0 ||
|
|
|
|
SSL_CTX_use_PrivateKey_file(ssl_ctx, bnd->ssl_keyfile, SSL_FILETYPE_PEM) == 0 ||
|
|
|
|
SSL_CTX_check_private_key(ssl_ctx) == 0 /*||
|
2020-05-12 17:53:19 +00:00
|
|
|
SSL_CTX_use_certificate_chain_file(ssl_ctx, bnd->chainfile) == 0*/)
|
2016-04-17 17:00:58 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
set_ssl_error (mio, ERR_get_error());
|
2016-04-18 14:21:23 +00:00
|
|
|
SSL_CTX_free (ssl_ctx);
|
2016-04-17 17:00:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
SSL_CTX_set_read_ahead (ssl_ctx, 0);
|
2016-04-24 17:30:43 +00:00
|
|
|
SSL_CTX_set_mode (ssl_ctx, SSL_CTX_get_mode(ssl_ctx) |
|
|
|
|
/*SSL_MODE_ENABLE_PARTIAL_WRITE |*/
|
|
|
|
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
|
|
|
|
|
2016-04-22 06:31:12 +00:00
|
|
|
SSL_CTX_set_options(ssl_ctx, SSL_OP_NO_SSLv2); /* no outdated SSLv2 by default */
|
2016-04-18 14:21:23 +00:00
|
|
|
#else
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_ENOIMPL);
|
2016-04-17 17:00:58 +00:00
|
|
|
return -1;
|
2016-04-18 14:21:23 +00:00
|
|
|
#endif
|
2016-04-17 17:00:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
x = bind(rdev->hnd, (struct sockaddr*)&bnd->localaddr, mio_skad_size(&bnd->localaddr));
|
2016-03-30 07:06:54 +00:00
|
|
|
if (x == -1)
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2016-04-18 14:21:23 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
if (ssl_ctx) SSL_CTX_free (ssl_ctx);
|
|
|
|
#endif
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
rdev->localaddr = bnd->localaddr;
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
#if defined(USE_SSL)
|
2016-04-20 15:59:11 +00:00
|
|
|
rdev->ssl_ctx = ssl_ctx;
|
2016-04-17 17:00:58 +00:00
|
|
|
#endif
|
2016-04-18 14:21:23 +00:00
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_CONNECT:
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_connect_t* conn = (mio_dev_sck_connect_t*)arg;
|
2016-04-18 14:21:23 +00:00
|
|
|
struct sockaddr* sa = (struct sockaddr*)&conn->remoteaddr;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_scklen_t sl;
|
2016-03-30 07:06:54 +00:00
|
|
|
int x;
|
2016-04-20 15:59:11 +00:00
|
|
|
#if defined(USE_SSL)
|
2018-12-12 13:15:54 +00:00
|
|
|
SSL_CTX* ssl_ctx = MIO_NULL;
|
2016-04-20 15:59:11 +00:00
|
|
|
#endif
|
2016-04-21 15:07:58 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (MIO_DEV_SCK_GET_PROGRESS(rdev))
|
2016-04-21 15:07:58 +00:00
|
|
|
{
|
|
|
|
/* can't connect again */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EPERM, "operation in progress. disallowed to connect again");
|
2016-04-21 15:07:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
if (!IS_STATEFUL(rdev))
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EPERM, "disallowed to connect stateless device");
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (sa->sa_family == AF_INET) sl = MIO_SIZEOF(struct sockaddr_in);
|
|
|
|
else if (sa->sa_family == AF_INET6) sl = MIO_SIZEOF(struct sockaddr_in6);
|
2016-03-30 07:06:54 +00:00
|
|
|
else
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EINVAL, "unknown address family %d", sa->sa_family);
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-04-21 13:26:14 +00:00
|
|
|
#if defined(USE_SSL)
|
2016-04-21 15:07:58 +00:00
|
|
|
if (rdev->ssl_ctx)
|
|
|
|
{
|
|
|
|
if (rdev->ssl)
|
|
|
|
{
|
|
|
|
SSL_free (rdev->ssl);
|
2018-12-12 13:15:54 +00:00
|
|
|
rdev->ssl = MIO_NULL;
|
2016-04-21 15:07:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SSL_CTX_free (rdev->ssl_ctx);
|
2018-12-12 13:15:54 +00:00
|
|
|
rdev->ssl_ctx = MIO_NULL;
|
2016-04-21 15:07:58 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (conn->options & MIO_DEV_SCK_CONNECT_SSL)
|
2016-04-20 15:59:11 +00:00
|
|
|
{
|
|
|
|
ssl_ctx = SSL_CTX_new(SSLv23_client_method());
|
|
|
|
if (!ssl_ctx)
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
set_ssl_error (mio, ERR_get_error());
|
2016-04-20 15:59:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-04-22 06:31:12 +00:00
|
|
|
|
|
|
|
SSL_CTX_set_read_ahead (ssl_ctx, 0);
|
2016-04-24 17:30:43 +00:00
|
|
|
SSL_CTX_set_mode (ssl_ctx, SSL_CTX_get_mode(ssl_ctx) |
|
|
|
|
/* SSL_MODE_ENABLE_PARTIAL_WRITE | */
|
|
|
|
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER);
|
2016-04-20 15:59:11 +00:00
|
|
|
}
|
2016-04-21 13:26:14 +00:00
|
|
|
#endif
|
2020-02-25 07:09:20 +00:00
|
|
|
/* the socket is already non-blocking */
|
2016-04-21 13:26:14 +00:00
|
|
|
/*{
|
2020-05-12 17:53:19 +00:00
|
|
|
int flags = fcntl (rdev->hnd, F_GETFL);
|
|
|
|
fcntl (rdev->hnd, F_SETFL, flags & ~O_NONBLOCK);
|
2016-04-21 13:26:14 +00:00
|
|
|
}*/
|
2020-05-12 17:53:19 +00:00
|
|
|
x = connect(rdev->hnd, sa, sl);
|
2016-04-21 13:26:14 +00:00
|
|
|
/*{
|
2020-05-12 17:53:19 +00:00
|
|
|
int flags = fcntl (rdev->hnd, F_GETFL);
|
|
|
|
fcntl (rdev->hnd, F_SETFL, flags | O_NONBLOCK);
|
2016-04-21 13:26:14 +00:00
|
|
|
}*/
|
2020-02-25 07:09:20 +00:00
|
|
|
if (x <= -1)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2016-04-18 14:21:23 +00:00
|
|
|
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-14 08:33:14 +00:00
|
|
|
if (mio_dev_watch((mio_dev_t*)rdev, MIO_DEV_WATCH_UPDATE, MIO_DEV_EVENT_IN | MIO_DEV_EVENT_OUT) <= -1)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
/* watcher update failure. it's critical */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_stop (mio, MIO_STOPREQ_WATCHER_ERROR);
|
2016-04-21 13:26:14 +00:00
|
|
|
goto oops_connect;
|
|
|
|
}
|
|
|
|
else
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_INIT_NTIME (&rdev->tmout, 0, 0); /* just in case */
|
2016-04-22 06:31:12 +00:00
|
|
|
|
2019-01-27 02:09:22 +00:00
|
|
|
if (MIO_IS_POS_NTIME(&conn->connect_tmout))
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-23 09:37:23 +00:00
|
|
|
if (schedule_timer_job_after(rdev, &conn->connect_tmout, connect_timedout) <= -1)
|
2016-04-22 06:31:12 +00:00
|
|
|
{
|
|
|
|
goto oops_connect;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* update rdev->tmout to the deadline of the connect timeout job */
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_ASSERT (mio, rdev->tmrjob_index != MIO_TMRIDX_INVALID);
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_gettmrjobdeadline (mio, rdev->tmrjob_index, &rdev->tmout);
|
2016-04-22 06:31:12 +00:00
|
|
|
}
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
rdev->remoteaddr = conn->remoteaddr;
|
2016-04-20 15:59:11 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
rdev->ssl_ctx = ssl_ctx;
|
|
|
|
#endif
|
2018-12-12 13:15:54 +00:00
|
|
|
MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_CONNECTING);
|
2016-03-30 07:06:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2016-04-21 13:26:14 +00:00
|
|
|
|
|
|
|
oops_connect:
|
2019-01-14 08:33:14 +00:00
|
|
|
if (mio_dev_watch((mio_dev_t*)rdev, MIO_DEV_WATCH_UPDATE, MIO_DEV_EVENT_IN) <= -1)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
/* watcher update failure. it's critical */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_stop (mio, MIO_STOPREQ_WATCHER_ERROR);
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
2016-04-20 15:59:11 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
if (ssl_ctx) SSL_CTX_free (ssl_ctx);
|
|
|
|
#endif
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-04-21 13:26:14 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* connected immediately */
|
2016-04-20 15:59:11 +00:00
|
|
|
|
2020-02-25 07:09:20 +00:00
|
|
|
/* don't call on_connect() callback even though the connection has been established.
|
|
|
|
* i don't want on_connect() to be called within the this function. */
|
|
|
|
if (mio_dev_watch((mio_dev_t*)rdev, MIO_DEV_WATCH_UPDATE, MIO_DEV_EVENT_IN | MIO_DEV_EVENT_OUT) <= -1)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2020-02-25 07:09:20 +00:00
|
|
|
/* watcher update failure. it's critical */
|
|
|
|
mio_stop (mio, MIO_STOPREQ_WATCHER_ERROR);
|
|
|
|
goto oops_connect;
|
|
|
|
}
|
2016-04-22 06:31:12 +00:00
|
|
|
|
2020-02-25 07:09:20 +00:00
|
|
|
/* as i know it's connected already,
|
|
|
|
* i don't schedule a connection timeout job */
|
2016-04-21 15:07:58 +00:00
|
|
|
|
2020-02-25 07:09:20 +00:00
|
|
|
rdev->remoteaddr = conn->remoteaddr;
|
2016-04-21 13:26:14 +00:00
|
|
|
#if defined(USE_SSL)
|
2020-02-25 07:09:20 +00:00
|
|
|
rdev->ssl_ctx = ssl_ctx;
|
2016-04-21 13:26:14 +00:00
|
|
|
#endif
|
2020-02-25 07:09:20 +00:00
|
|
|
MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_CONNECTING);
|
2016-04-21 13:26:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_LISTEN:
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_listen_t* lstn = (mio_dev_sck_listen_t*)arg;
|
2016-03-30 07:06:54 +00:00
|
|
|
int x;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (MIO_DEV_SCK_GET_PROGRESS(rdev))
|
2016-04-21 15:07:58 +00:00
|
|
|
{
|
|
|
|
/* can't listen again */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EPERM, "operation in progress. disallowed to listen again");
|
2016-04-21 15:07:58 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
if (!IS_STATEFUL(rdev))
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EPERM, "disallowed to listen on stateless device");
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
x = listen(rdev->hnd, lstn->backlogs);
|
2016-03-30 07:06:54 +00:00
|
|
|
if (x == -1)
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-07 10:10:33 +00:00
|
|
|
rdev->tmout = lstn->accept_tmout;
|
|
|
|
if (lstn->options & MIO_DEV_SCK_LISTEN_LENIENT) rdev->state |= MIO_DEV_SCK_LENIENT;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_LISTENING);
|
2016-03-30 07:06:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 15:02:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static mio_dev_mth_t dev_sck_methods_stateless =
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
|
|
|
dev_sck_make,
|
|
|
|
dev_sck_kill,
|
|
|
|
dev_sck_getsyshnd,
|
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
dev_sck_read_stateless,
|
|
|
|
dev_sck_write_stateless,
|
2020-02-22 18:24:49 +00:00
|
|
|
dev_sck_writev_stateless,
|
2016-03-29 15:02:01 +00:00
|
|
|
dev_sck_ioctl, /* ioctl */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static mio_dev_mth_t dev_sck_methods_stateful =
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
|
|
|
dev_sck_make,
|
|
|
|
dev_sck_kill,
|
|
|
|
dev_sck_getsyshnd,
|
|
|
|
|
|
|
|
dev_sck_read_stateful,
|
|
|
|
dev_sck_write_stateful,
|
2020-02-22 18:24:49 +00:00
|
|
|
dev_sck_writev_stateful,
|
2016-03-30 07:06:54 +00:00
|
|
|
dev_sck_ioctl, /* ioctl */
|
|
|
|
};
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static mio_dev_mth_t dev_mth_clisck =
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
|
|
|
dev_sck_make_client,
|
|
|
|
dev_sck_kill,
|
|
|
|
dev_sck_getsyshnd,
|
|
|
|
|
|
|
|
dev_sck_read_stateful,
|
|
|
|
dev_sck_write_stateful,
|
2020-02-22 18:24:49 +00:00
|
|
|
dev_sck_writev_stateful,
|
2016-03-30 07:06:54 +00:00
|
|
|
dev_sck_ioctl
|
|
|
|
};
|
2016-03-29 15:02:01 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int harvest_outgoing_connection (mio_dev_sck_t* rdev)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = rdev->mio;
|
2016-04-21 13:26:14 +00:00
|
|
|
int errcode;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_scklen_t len;
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
MIO_ASSERT (mio, !(rdev->state & MIO_DEV_SCK_CONNECTED));
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
len = MIO_SIZEOF(errcode);
|
2020-05-12 17:53:19 +00:00
|
|
|
if (getsockopt(rdev->hnd, SOL_SOCKET, SO_ERROR, (char*)&errcode, &len) == -1)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmtwithsyserr (mio, 0, errno, "unable to get SO_ERROR");
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (errcode == 0)
|
|
|
|
{
|
2020-02-20 06:00:53 +00:00
|
|
|
mio_skad_t localaddr;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_scklen_t addrlen;
|
2016-04-21 13:26:14 +00:00
|
|
|
|
|
|
|
/* connected */
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (rdev->tmrjob_index != MIO_TMRIDX_INVALID)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_deltmrjob (mio, rdev->tmrjob_index);
|
|
|
|
MIO_ASSERT (mio, rdev->tmrjob_index == MIO_TMRIDX_INVALID);
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
addrlen = MIO_SIZEOF(localaddr);
|
2020-05-12 17:53:19 +00:00
|
|
|
if (getsockname(rdev->hnd, (struct sockaddr*)&localaddr, &addrlen) == 0) rdev->localaddr = localaddr;
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2020-05-08 09:48:26 +00:00
|
|
|
if (mio_dev_watch((mio_dev_t*)rdev, MIO_DEV_WATCH_RENEW, MIO_DEV_EVENT_IN) <= -1)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
/* watcher update failure. it's critical */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_stop (mio, MIO_STOPREQ_WATCHER_ERROR);
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(USE_SSL)
|
|
|
|
if (rdev->ssl_ctx)
|
|
|
|
{
|
|
|
|
int x;
|
2019-01-28 08:13:06 +00:00
|
|
|
MIO_ASSERT (mio, !rdev->ssl); /* must not be SSL-connected yet */
|
2016-04-21 13:26:14 +00:00
|
|
|
|
|
|
|
x = connect_ssl (rdev);
|
|
|
|
if (x <= -1) return -1;
|
|
|
|
if (x == 0)
|
|
|
|
{
|
2016-04-21 15:07:58 +00:00
|
|
|
/* underlying socket connected but not SSL-connected */
|
2018-12-12 13:15:54 +00:00
|
|
|
MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_CONNECTING_SSL);
|
2016-04-21 15:07:58 +00:00
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
MIO_ASSERT (mio, rdev->tmrjob_index == MIO_TMRIDX_INVALID);
|
2016-04-21 15:07:58 +00:00
|
|
|
|
2016-04-22 06:31:12 +00:00
|
|
|
/* rdev->tmout has been set to the deadline of the connect task
|
|
|
|
* when the CONNECT IOCTL command has been executed. use the
|
2016-04-24 17:30:43 +00:00
|
|
|
* same deadline here */
|
2019-01-27 02:09:22 +00:00
|
|
|
if (MIO_IS_POS_NTIME(&rdev->tmout) &&
|
2019-01-16 10:42:20 +00:00
|
|
|
schedule_timer_job_at(rdev, &rdev->tmout, ssl_connect_timedout) <= -1)
|
2016-04-21 15:07:58 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_halt ((mio_dev_t*)rdev);
|
2016-04-21 15:07:58 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 13:26:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto ssl_connected;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ssl_connected:
|
|
|
|
#endif
|
2018-12-12 13:15:54 +00:00
|
|
|
MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_CONNECTED);
|
2019-01-16 10:42:20 +00:00
|
|
|
if (rdev->on_connect) rdev->on_connect (rdev);
|
2016-04-21 13:26:14 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (errcode == EINPROGRESS || errcode == EWOULDBLOCK)
|
|
|
|
{
|
|
|
|
/* still in progress */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errcode);
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int accept_incoming_connection (mio_dev_sck_t* rdev)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2019-01-27 02:09:22 +00:00
|
|
|
mio_t* mio = rdev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_sckhnd_t clisck;
|
2020-02-20 06:00:53 +00:00
|
|
|
mio_skad_t remoteaddr;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_scklen_t addrlen;
|
|
|
|
mio_dev_sck_t* clidev;
|
2019-01-11 07:35:43 +00:00
|
|
|
int flags;
|
2016-04-21 13:26:14 +00:00
|
|
|
|
|
|
|
/* this is a server(lisening) socket */
|
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
#if defined(SOCK_NONBLOCK) && defined(SOCK_CLOEXEC) && defined(HAVE_ACCEPT4)
|
|
|
|
flags = SOCK_NONBLOCK | SOCK_CLOEXEC;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
addrlen = MIO_SIZEOF(remoteaddr);
|
2020-05-12 17:53:19 +00:00
|
|
|
clisck = accept4(rdev->hnd, (struct sockaddr*)&remoteaddr, &addrlen, flags);
|
2019-01-11 07:35:43 +00:00
|
|
|
if (clisck == MIO_SCKHND_INVALID)
|
|
|
|
{
|
|
|
|
if (errno != ENOSYS)
|
|
|
|
{
|
|
|
|
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0;
|
|
|
|
if (errno == EINTR) return 0; /* if interrupted by a signal, treat it as if it's EINPROGRESS */
|
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2019-01-11 07:35:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// go on for the normal 3-parameter accept
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
goto accept_done;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
addrlen = MIO_SIZEOF(remoteaddr);
|
2020-05-12 17:53:19 +00:00
|
|
|
clisck = accept(rdev->hnd, (struct sockaddr*)&remoteaddr, &addrlen);
|
2018-12-12 13:15:54 +00:00
|
|
|
if (clisck == MIO_SCKHND_INVALID)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
if (errno == EINPROGRESS || errno == EWOULDBLOCK || errno == EAGAIN) return 0;
|
|
|
|
if (errno == EINTR) return 0; /* if interrupted by a signal, treat it as if it's EINPROGRESS */
|
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errno);
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
accept_done:
|
2016-04-21 13:26:14 +00:00
|
|
|
/* use rdev->dev_size when instantiating a client sck device
|
2018-12-12 13:15:54 +00:00
|
|
|
* instead of MIO_SIZEOF(mio_dev_sck_t). therefore, the
|
2016-04-21 13:26:14 +00:00
|
|
|
* extension area as big as that of the master sck device
|
|
|
|
* is created in the client sck device */
|
2019-02-22 08:44:51 +00:00
|
|
|
clidev = (mio_dev_sck_t*)mio_dev_make(mio, rdev->dev_size, &dev_mth_clisck, rdev->dev_evcb, &clisck);
|
2020-05-05 15:12:08 +00:00
|
|
|
if (MIO_UNLIKELY(!clidev))
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
close (clisck);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
MIO_ASSERT (mio, clidev->hnd == clisck);
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2019-01-31 09:16:44 +00:00
|
|
|
clidev->dev_cap |= MIO_DEV_CAP_IN | MIO_DEV_CAP_OUT | MIO_DEV_CAP_STREAM;
|
2016-04-21 13:26:14 +00:00
|
|
|
clidev->remoteaddr = remoteaddr;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
addrlen = MIO_SIZEOF(clidev->localaddr);
|
2016-04-21 13:26:14 +00:00
|
|
|
if (getsockname(clisck, (struct sockaddr*)&clidev->localaddr, &addrlen) == -1) clidev->localaddr = rdev->localaddr;
|
|
|
|
|
|
|
|
#if defined(SO_ORIGINAL_DST)
|
|
|
|
/* if REDIRECT is used, SO_ORIGINAL_DST returns the original
|
|
|
|
* destination address. When REDIRECT is not used, it returnes
|
|
|
|
* the address of the local socket. In this case, it should
|
|
|
|
* be same as the result of getsockname(). */
|
2018-12-12 13:15:54 +00:00
|
|
|
addrlen = MIO_SIZEOF(clidev->orgdstaddr);
|
2019-01-28 08:13:06 +00:00
|
|
|
if (getsockopt(clisck, SOL_IP, SO_ORIGINAL_DST, &clidev->orgdstaddr, &addrlen) == -1) clidev->orgdstaddr = rdev->localaddr;
|
2016-04-21 13:26:14 +00:00
|
|
|
#else
|
|
|
|
clidev->orgdstaddr = rdev->localaddr;
|
|
|
|
#endif
|
|
|
|
|
2020-02-20 10:07:12 +00:00
|
|
|
if (!mio_equal_skads(&clidev->orgdstaddr, &clidev->localaddr, 0))
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
clidev->state |= MIO_DEV_SCK_INTERCEPTED;
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
2020-02-20 06:00:53 +00:00
|
|
|
else if (mio_skad_port(&clidev->localaddr) != mio_skad_port(&rdev->localaddr))
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
/* When TPROXY is used, getsockname() and SO_ORIGNAL_DST return
|
|
|
|
* the same addresses. however, the port number may be different
|
|
|
|
* as a typical TPROXY rule is set to change the port number.
|
|
|
|
* However, this check is fragile if the server port number is
|
|
|
|
* set to 0.
|
|
|
|
*
|
|
|
|
* Take note that the above assumption gets wrong if the TPROXY
|
|
|
|
* rule doesn't change the port number. so it won't be able
|
|
|
|
* to handle such a TPROXYed packet without port transformation. */
|
2018-12-12 13:15:54 +00:00
|
|
|
clidev->state |= MIO_DEV_SCK_INTERCEPTED;
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
|
|
|
#if 0
|
2019-01-11 07:35:43 +00:00
|
|
|
else if ((clidev->initial_ifindex = resolve_ifindex(fd, clidev->localaddr)) <= -1)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
/* the local_address is not one of a local address.
|
|
|
|
* it's probably proxied. */
|
2018-12-12 13:15:54 +00:00
|
|
|
clidev->state |= MIO_DEV_SCK_INTERCEPTED;
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* inherit some event handlers from the parent.
|
|
|
|
* you can still change them inside the on_connect handler */
|
|
|
|
clidev->on_connect = rdev->on_connect;
|
|
|
|
clidev->on_disconnect = rdev->on_disconnect;
|
|
|
|
clidev->on_write = rdev->on_write;
|
|
|
|
clidev->on_read = rdev->on_read;
|
|
|
|
|
2020-05-05 15:12:08 +00:00
|
|
|
/* inherit the contents of the extension area */
|
|
|
|
MIO_ASSERT (mio, rdev->dev_size == clidev->dev_size);
|
|
|
|
MIO_MEMCPY (mio_dev_sck_getxtn(clidev), mio_dev_sck_getxtn(rdev), rdev->dev_size - MIO_SIZEOF(mio_dev_sck_t));
|
|
|
|
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_ASSERT (mio, clidev->tmrjob_index == MIO_TMRIDX_INVALID);
|
2016-04-21 13:26:14 +00:00
|
|
|
|
|
|
|
if (rdev->ssl_ctx)
|
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
MIO_DEV_SCK_SET_PROGRESS (clidev, MIO_DEV_SCK_ACCEPTING_SSL);
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_ASSERT (mio, clidev->state & MIO_DEV_SCK_ACCEPTING_SSL);
|
2016-04-21 13:26:14 +00:00
|
|
|
/* actual SSL acceptance must be completed in the client device */
|
|
|
|
|
|
|
|
/* let the client device know the SSL context to use */
|
|
|
|
clidev->ssl_ctx = rdev->ssl_ctx;
|
|
|
|
|
2019-01-27 02:09:22 +00:00
|
|
|
if (MIO_IS_POS_NTIME(&rdev->tmout) &&
|
2020-05-05 15:12:08 +00:00
|
|
|
schedule_timer_job_after(clidev, &rdev->tmout, ssl_accept_timedout) <= -1)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2016-04-21 15:07:58 +00:00
|
|
|
/* timer job scheduling failed. halt the device */
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_halt ((mio_dev_t*)clidev);
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
MIO_DEV_SCK_SET_PROGRESS (clidev, MIO_DEV_SCK_ACCEPTED);
|
2019-01-16 10:42:20 +00:00
|
|
|
/*if (clidev->on_connect(clidev) <= -1) mio_dev_sck_halt (clidev);*/
|
|
|
|
if (clidev->on_connect) clidev->on_connect (clidev);
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_evcb_sck_ready_stateful (mio_dev_t* dev, int events)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (events & MIO_DEV_EVENT_ERR)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
|
|
|
int errcode;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_scklen_t len;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
len = MIO_SIZEOF(errcode);
|
2020-05-12 17:53:19 +00:00
|
|
|
if (getsockopt(rdev->hnd, SOL_SOCKET, SO_ERROR, (char*)&errcode, &len) == -1)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
|
|
|
/* the error number is set to the socket error code.
|
|
|
|
* errno resulting from getsockopt() doesn't reflect the actual
|
|
|
|
* socket error. so errno is not used to set the error number.
|
2018-12-12 13:15:54 +00:00
|
|
|
* instead, the generic device error MIO_EDEVERRR is used */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EDEVERR, "device error - unable to get SO_ERROR");
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrwithsyserr (mio, 0, errcode);
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
/* this socket can connect */
|
2018-12-12 13:15:54 +00:00
|
|
|
switch (MIO_DEV_SCK_GET_PROGRESS(rdev))
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_CONNECTING:
|
|
|
|
if (events & MIO_DEV_EVENT_HUP)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2016-04-21 13:26:14 +00:00
|
|
|
/* device hang-up */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_EDEVHUP);
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
else if (events & (MIO_DEV_EVENT_PRI | MIO_DEV_EVENT_IN))
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2016-04-21 13:26:14 +00:00
|
|
|
/* invalid event masks. generic device error */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EDEVERR, "device error - invalid event mask");
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
else if (events & MIO_DEV_EVENT_OUT)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2016-04-21 13:26:14 +00:00
|
|
|
/* when connected, the socket becomes writable */
|
2019-01-28 08:13:06 +00:00
|
|
|
return harvest_outgoing_connection(rdev);
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-21 13:26:14 +00:00
|
|
|
return 0; /* success but don't invoke on_read() */
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_CONNECTING_SSL:
|
2016-04-21 13:26:14 +00:00
|
|
|
#if defined(USE_SSL)
|
2018-12-12 13:15:54 +00:00
|
|
|
if (events & MIO_DEV_EVENT_HUP)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2016-04-21 13:26:14 +00:00
|
|
|
/* device hang-up */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_EDEVHUP);
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
else if (events & MIO_DEV_EVENT_PRI)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2016-04-21 13:26:14 +00:00
|
|
|
/* invalid event masks. generic device error */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EDEVERR, "device error - invalid event mask");
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
else if (events & (MIO_DEV_EVENT_IN | MIO_DEV_EVENT_OUT))
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
int x;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
x = connect_ssl(rdev);
|
2016-04-21 13:26:14 +00:00
|
|
|
if (x <= -1) return -1;
|
|
|
|
if (x == 0) return 0; /* not SSL-Connected */
|
2016-04-18 14:21:23 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (rdev->tmrjob_index != MIO_TMRIDX_INVALID)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_deltmrjob (rdev->mio, rdev->tmrjob_index);
|
|
|
|
rdev->tmrjob_index = MIO_TMRIDX_INVALID;
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_CONNECTED);
|
2019-01-16 10:42:20 +00:00
|
|
|
if (rdev->on_connect) rdev->on_connect (rdev);
|
2016-04-21 13:26:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-04-18 14:21:23 +00:00
|
|
|
else
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
return 0; /* success. no actual I/O yet */
|
|
|
|
}
|
2016-04-18 14:21:23 +00:00
|
|
|
#else
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_EINTERN);
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
2016-04-18 14:21:23 +00:00
|
|
|
#endif
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_LISTENING:
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (events & MIO_DEV_EVENT_HUP)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2016-04-21 13:26:14 +00:00
|
|
|
/* device hang-up */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_EDEVHUP);
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
2016-04-18 14:21:23 +00:00
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
else if (events & (MIO_DEV_EVENT_PRI | MIO_DEV_EVENT_OUT))
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EDEVERR, "device error - invalid event mask");
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
2016-04-18 14:21:23 +00:00
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
else if (events & MIO_DEV_EVENT_IN)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2020-05-07 04:32:32 +00:00
|
|
|
if (rdev->state & MIO_DEV_SCK_LENIENT)
|
|
|
|
{
|
|
|
|
accept_incoming_connection(rdev);
|
|
|
|
return 0; /* return ok to the core regardless of accept()'s result */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* [NOTE] if the accept operation fails, the core also kills this listening device. */
|
|
|
|
return accept_incoming_connection(rdev);
|
|
|
|
}
|
2016-04-18 14:21:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-04-21 13:26:14 +00:00
|
|
|
return 0; /* success but don't invoke on_read() */
|
2016-04-18 14:21:23 +00:00
|
|
|
}
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_ACCEPTING_SSL:
|
2016-04-21 13:26:14 +00:00
|
|
|
#if defined(USE_SSL)
|
2018-12-12 13:15:54 +00:00
|
|
|
if (events & MIO_DEV_EVENT_HUP)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2016-04-21 13:26:14 +00:00
|
|
|
/* device hang-up */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_EDEVHUP);
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
else if (events & MIO_DEV_EVENT_PRI)
|
2016-04-18 14:21:23 +00:00
|
|
|
{
|
2016-04-21 13:26:14 +00:00
|
|
|
/* invalid event masks. generic device error */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EDEVERR, "device error - invalid event mask");
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
else if (events & (MIO_DEV_EVENT_IN | MIO_DEV_EVENT_OUT))
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
int x;
|
2020-05-12 17:53:19 +00:00
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
x = accept_ssl(rdev);
|
2016-04-21 13:26:14 +00:00
|
|
|
if (x <= -1) return -1;
|
2020-05-12 17:53:19 +00:00
|
|
|
if (x == 0) return 0; /* not SSL-accepted yet */
|
2016-04-18 14:21:23 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (rdev->tmrjob_index != MIO_TMRIDX_INVALID)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_deltmrjob (rdev->mio, rdev->tmrjob_index);
|
|
|
|
rdev->tmrjob_index = MIO_TMRIDX_INVALID;
|
2016-04-21 13:26:14 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
MIO_DEV_SCK_SET_PROGRESS (rdev, MIO_DEV_SCK_ACCEPTED);
|
2019-01-16 10:42:20 +00:00
|
|
|
/*if (rdev->on_connect(rdev) <= -1) mio_dev_sck_halt (rdev);*/
|
|
|
|
if (rdev->on_connect) rdev->on_connect (rdev);
|
2016-04-18 14:21:23 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-21 13:26:14 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0; /* no reading or writing yet */
|
|
|
|
}
|
|
|
|
#else
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_EINTERN);
|
2016-04-18 14:21:23 +00:00
|
|
|
return -1;
|
2016-04-21 13:26:14 +00:00
|
|
|
#endif
|
2016-04-20 15:59:11 +00:00
|
|
|
|
2016-04-18 14:21:23 +00:00
|
|
|
|
2016-04-21 13:26:14 +00:00
|
|
|
default:
|
2018-12-12 13:15:54 +00:00
|
|
|
if (events & MIO_DEV_EVENT_HUP)
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
if (events & (MIO_DEV_EVENT_PRI | MIO_DEV_EVENT_IN | MIO_DEV_EVENT_OUT))
|
2016-04-21 13:26:14 +00:00
|
|
|
{
|
|
|
|
/* probably half-open? */
|
|
|
|
return 1;
|
|
|
|
}
|
2016-04-18 14:21:23 +00:00
|
|
|
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_EDEVHUP);
|
2016-04-21 13:26:14 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2016-04-21 13:26:14 +00:00
|
|
|
return 1; /* the device is ok. carry on reading or writing */
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_evcb_sck_ready_stateless (mio_dev_t* dev, int events)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (events & MIO_DEV_EVENT_ERR)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
|
|
|
int errcode;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_scklen_t len;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
len = MIO_SIZEOF(errcode);
|
2020-05-12 17:53:19 +00:00
|
|
|
if (getsockopt (rdev->hnd, SOL_SOCKET, SO_ERROR, (char*)&errcode, &len) == -1)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
|
|
|
/* the error number is set to the socket error code.
|
|
|
|
* errno resulting from getsockopt() doesn't reflect the actual
|
|
|
|
* socket error. so errno is not used to set the error number.
|
2018-12-12 13:15:54 +00:00
|
|
|
* instead, the generic device error MIO_EDEVERRR is used */
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrbfmt (mio, MIO_EDEVERR, "device error - unable to get SO_ERROR");
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-27 02:09:22 +00:00
|
|
|
mio_seterrwithsyserr (rdev->mio, 0, errcode);
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
else if (events & MIO_DEV_EVENT_HUP)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_EDEVHUP);
|
2016-03-30 07:06:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
return 1; /* the device is ok. carry on reading or writing */
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_evcb_sck_on_read_stateful (mio_dev_t* dev, const void* data, mio_iolen_t dlen, const mio_devaddr_t* srcaddr)
|
2016-03-30 16:31:56 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2019-01-28 08:13:06 +00:00
|
|
|
return rdev->on_read(rdev, data, dlen, MIO_NULL);
|
2016-03-30 16:31:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_evcb_sck_on_write_stateful (mio_dev_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_devaddr_t* dstaddr)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2019-01-28 08:13:06 +00:00
|
|
|
return rdev->on_write(rdev, wrlen, wrctx, MIO_NULL);
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_evcb_sck_on_read_stateless (mio_dev_t* dev, const void* data, mio_iolen_t dlen, const mio_devaddr_t* srcaddr)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2019-01-28 08:13:06 +00:00
|
|
|
return rdev->on_read(rdev, data, dlen, srcaddr->ptr);
|
2016-03-30 16:31:56 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int dev_evcb_sck_on_write_stateless (mio_dev_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_devaddr_t* dstaddr)
|
2016-03-30 16:31:56 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev = (mio_dev_sck_t*)dev;
|
2019-01-28 08:13:06 +00:00
|
|
|
return rdev->on_write(rdev, wrlen, wrctx, dstaddr->ptr);
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static mio_dev_evcb_t dev_sck_event_callbacks_stateful =
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2016-03-30 07:06:54 +00:00
|
|
|
dev_evcb_sck_ready_stateful,
|
2016-03-30 16:31:56 +00:00
|
|
|
dev_evcb_sck_on_read_stateful,
|
|
|
|
dev_evcb_sck_on_write_stateful
|
2016-03-29 15:02:01 +00:00
|
|
|
};
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static mio_dev_evcb_t dev_sck_event_callbacks_stateless =
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
|
|
|
dev_evcb_sck_ready_stateless,
|
2016-03-30 16:31:56 +00:00
|
|
|
dev_evcb_sck_on_read_stateless,
|
|
|
|
dev_evcb_sck_on_write_stateless
|
2016-03-30 07:06:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* ========================================================================= */
|
|
|
|
|
2018-12-12 14:16:49 +00:00
|
|
|
mio_dev_sck_t* mio_dev_sck_make (mio_t* mio, mio_oow_t xtnsize, const mio_dev_sck_make_t* info)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_t* rdev;
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (info->type < 0 && info->type >= MIO_COUNTOF(sck_type_map))
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-28 08:13:06 +00:00
|
|
|
mio_seterrnum (mio, MIO_EINVAL);
|
2018-12-12 13:15:54 +00:00
|
|
|
return MIO_NULL;
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 09:16:44 +00:00
|
|
|
if (sck_type_map[info->type].extra_dev_cap & MIO_DEV_CAP_STREAM) /* can't use the IS_STATEFUL() macro yet */
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-02-22 08:44:51 +00:00
|
|
|
rdev = (mio_dev_sck_t*)mio_dev_make(
|
2020-05-05 15:12:08 +00:00
|
|
|
mio, MIO_SIZEOF(mio_dev_sck_t) + xtnsize,
|
2016-04-06 13:49:47 +00:00
|
|
|
&dev_sck_methods_stateful, &dev_sck_event_callbacks_stateful, (void*)info);
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-02-22 08:44:51 +00:00
|
|
|
rdev = (mio_dev_sck_t*)mio_dev_make(
|
2018-12-12 13:15:54 +00:00
|
|
|
mio, MIO_SIZEOF(mio_dev_sck_t) + xtnsize,
|
2016-04-06 13:49:47 +00:00
|
|
|
&dev_sck_methods_stateless, &dev_sck_event_callbacks_stateless, (void*)info);
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rdev;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
int mio_dev_sck_bind (mio_dev_sck_t* dev, mio_dev_sck_bind_t* info)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-11 07:35:43 +00:00
|
|
|
return mio_dev_ioctl((mio_dev_t*)dev, MIO_DEV_SCK_BIND, info);
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
int mio_dev_sck_connect (mio_dev_sck_t* dev, mio_dev_sck_connect_t* info)
|
2016-03-30 07:06:54 +00:00
|
|
|
{
|
2019-01-11 07:35:43 +00:00
|
|
|
return mio_dev_ioctl((mio_dev_t*)dev, MIO_DEV_SCK_CONNECT, info);
|
2016-03-30 07:06:54 +00:00
|
|
|
}
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
int mio_dev_sck_listen (mio_dev_sck_t* dev, mio_dev_sck_listen_t* info)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2019-01-11 07:35:43 +00:00
|
|
|
return mio_dev_ioctl((mio_dev_t*)dev, MIO_DEV_SCK_LISTEN, info);
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
int mio_dev_sck_write (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, void* wrctx, const mio_skad_t* dstaddr)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_devaddr_t devaddr;
|
2020-02-20 06:00:53 +00:00
|
|
|
return mio_dev_write((mio_dev_t*)dev, data, dlen, wrctx, skad_to_devaddr(dev, dstaddr, &devaddr));
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
|
|
|
|
2020-02-22 18:24:49 +00:00
|
|
|
int mio_dev_sck_writev (mio_dev_sck_t* dev, mio_iovec_t* iov, mio_iolen_t iovcnt, void* wrctx, const mio_skad_t* dstaddr)
|
|
|
|
{
|
|
|
|
mio_devaddr_t devaddr;
|
|
|
|
return mio_dev_writev((mio_dev_t*)dev, iov, iovcnt, wrctx, skad_to_devaddr(dev, dstaddr, &devaddr));
|
|
|
|
}
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
int mio_dev_sck_timedwrite (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_ntime_t* tmout, void* wrctx, const mio_skad_t* dstaddr)
|
2016-03-29 15:02:01 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_devaddr_t devaddr;
|
2020-02-20 06:00:53 +00:00
|
|
|
return mio_dev_timedwrite((mio_dev_t*)dev, data, dlen, tmout, wrctx, skad_to_devaddr(dev, dstaddr, &devaddr));
|
2016-03-29 15:02:01 +00:00
|
|
|
}
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2020-02-22 18:24:49 +00:00
|
|
|
int mio_dev_sck_timedwritev (mio_dev_sck_t* dev, mio_iovec_t* iov, mio_iolen_t iovcnt, const mio_ntime_t* tmout, void* wrctx, const mio_skad_t* dstaddr)
|
2020-02-21 16:58:41 +00:00
|
|
|
{
|
|
|
|
mio_devaddr_t devaddr;
|
2020-02-22 18:24:49 +00:00
|
|
|
return mio_dev_timedwritev((mio_dev_t*)dev, iov, iovcnt, tmout, wrctx, skad_to_devaddr(dev, dstaddr, &devaddr));
|
2020-02-21 16:58:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2020-02-23 16:11:32 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
int mio_dev_sck_setsockopt (mio_dev_sck_t* dev, int level, int optname, void* optval, mio_scklen_t optlen)
|
|
|
|
{
|
2020-05-12 17:53:19 +00:00
|
|
|
return setsockopt(dev->hnd, level, optname, optval, optlen);
|
2020-02-23 16:11:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int mio_dev_sck_getsockopt (mio_dev_sck_t* dev, int level, int optname, void* optval, mio_scklen_t* optlen)
|
|
|
|
{
|
2020-05-12 17:53:19 +00:00
|
|
|
return getsockopt(dev->hnd, level, optname, optval, optlen);
|
2020-02-23 16:11:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 09:11:39 +00:00
|
|
|
int mio_dev_sck_shutdown (mio_dev_sck_t* dev, int how)
|
|
|
|
{
|
|
|
|
switch (how & (MIO_DEV_SCK_SHUTDOWN_READ | MIO_DEV_SCK_SHUTDOWN_WRITE))
|
|
|
|
{
|
|
|
|
case (MIO_DEV_SCK_SHUTDOWN_READ | MIO_DEV_SCK_SHUTDOWN_WRITE):
|
|
|
|
how = SHUT_RDWR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MIO_DEV_SCK_SHUTDOWN_READ:
|
|
|
|
how = SHUT_RD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MIO_DEV_SCK_SHUTDOWN_WRITE:
|
|
|
|
how = SHUT_WR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
mio_seterrnum (dev->mio, MIO_EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shutdown(dev->hnd, how);
|
|
|
|
}
|
|
|
|
|
2016-04-25 14:07:28 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2020-05-19 09:11:39 +00:00
|
|
|
mio_uint16_t mio_checksum_ip (const void* hdr, mio_oow_t len)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_uint32_t sum = 0;
|
|
|
|
mio_uint16_t *ptr = (mio_uint16_t*)hdr;
|
2016-04-25 14:07:28 +00:00
|
|
|
|
|
|
|
while (len > 1)
|
|
|
|
{
|
|
|
|
sum += *ptr++;
|
|
|
|
if (sum & 0x80000000)
|
|
|
|
sum = (sum & 0xFFFF) + (sum >> 16);
|
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (sum >> 16) sum = (sum & 0xFFFF) + (sum >> 16);
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
return (mio_uint16_t)~sum;
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
2019-04-12 03:13:07 +00:00
|
|
|
|