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.
|
|
|
|
*/
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
#include <hio.h>
|
|
|
|
#include <hio-utl.h>
|
|
|
|
#include <hio-sck.h>
|
|
|
|
#include <hio-pro.h>
|
|
|
|
#include <hio-pipe.h>
|
|
|
|
#include <hio-thr.h>
|
|
|
|
#include <hio-dns.h>
|
|
|
|
#include <hio-nwif.h>
|
|
|
|
#include <hio-http.h>
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
#include <stdlib.h>
|
2016-01-30 05:24:23 +00:00
|
|
|
#include <string.h>
|
2016-01-26 16:02:20 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <signal.h>
|
2022-06-13 13:54:14 +00:00
|
|
|
#include <unistd.h>
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-04-25 14:07:28 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
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
|
2020-02-22 18:24:49 +00:00
|
|
|
# define USE_SSL
|
2016-04-17 17:00:58 +00:00
|
|
|
#endif
|
|
|
|
|
2016-03-23 13:54:15 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
struct mmgr_stat_t
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_oow_t total_count;
|
2016-02-04 15:06:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct mmgr_stat_t mmgr_stat_t;
|
|
|
|
|
|
|
|
static mmgr_stat_t mmgr_stat;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void* mmgr_alloc (hio_mmgr_t* mmgr, hio_oow_t size)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-04-24 17:30:43 +00:00
|
|
|
void* x;
|
|
|
|
|
2020-07-16 10:46:17 +00:00
|
|
|
#if 0
|
2020-02-16 11:03:25 +00:00
|
|
|
if (((mmgr_stat_t*)mmgr->ctx)->total_count > 3000)
|
2016-02-04 15:06:20 +00:00
|
|
|
{
|
|
|
|
printf ("CRITICAL ERROR ---> too many heap chunks...\n");
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_NULL;
|
2016-02-04 15:06:20 +00:00
|
|
|
}
|
2020-07-16 10:46:17 +00:00
|
|
|
#endif
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2016-04-24 17:30:43 +00:00
|
|
|
x = malloc (size);
|
2020-05-07 04:32:32 +00:00
|
|
|
if (x)
|
|
|
|
{
|
|
|
|
((mmgr_stat_t*)mmgr->ctx)->total_count++;
|
|
|
|
/*printf ("MMGR total_count INCed to %d => %p\n", ((mmgr_stat_t*)mmgr->ctx)->total_count, x);*/
|
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
return x;
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void* mmgr_realloc (hio_mmgr_t* mmgr, void* ptr, hio_oow_t size)
|
2016-01-30 05:24:23 +00:00
|
|
|
{
|
2020-05-07 04:32:32 +00:00
|
|
|
void* x;
|
|
|
|
|
|
|
|
x = realloc (ptr, size);
|
|
|
|
if (x && !ptr)
|
|
|
|
{
|
|
|
|
((mmgr_stat_t*)mmgr->ctx)->total_count++;
|
|
|
|
/*printf ("MMGR total_count INCed to %d => %p\n", ((mmgr_stat_t*)mmgr->ctx)->total_count, x);*/
|
|
|
|
}
|
|
|
|
return x;
|
2016-01-30 05:24:23 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void mmgr_free (hio_mmgr_t* mmgr, void* ptr)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
((mmgr_stat_t*)mmgr->ctx)->total_count--;
|
2020-05-07 04:32:32 +00:00
|
|
|
/*printf ("MMGR total_count DECed to %d => %p\n", ((mmgr_stat_t*)mmgr->ctx)->total_count, ptr);*/
|
2016-01-26 16:02:20 +00:00
|
|
|
return free (ptr);
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static hio_mmgr_t mmgr =
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
|
|
|
mmgr_alloc,
|
2016-01-30 05:24:23 +00:00
|
|
|
mmgr_realloc,
|
2016-01-26 16:02:20 +00:00
|
|
|
mmgr_free,
|
2016-02-04 15:06:20 +00:00
|
|
|
&mmgr_stat
|
|
|
|
};
|
|
|
|
|
2016-03-23 13:54:15 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
static void cleanup_openssl ()
|
|
|
|
{
|
|
|
|
/* ERR_remove_state() should be called for each thread if the application is thread */
|
|
|
|
ERR_remove_state (0);
|
|
|
|
#if defined(HAVE_ENGINE_CLEANUP)
|
|
|
|
ENGINE_cleanup ();
|
|
|
|
#endif
|
|
|
|
ERR_free_strings ();
|
|
|
|
EVP_cleanup ();
|
|
|
|
#if defined(HAVE_CRYPTO_CLEANUP_ALL_EX_DATA)
|
|
|
|
CRYPTO_cleanup_all_ex_data ();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
struct tcp_server_t
|
|
|
|
{
|
|
|
|
int tally;
|
2016-01-26 16:02:20 +00:00
|
|
|
};
|
2016-02-04 15:06:20 +00:00
|
|
|
typedef struct tcp_server_t tcp_server_t;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void tcp_sck_on_disconnect (hio_dev_sck_t* tcp)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
switch (HIO_DEV_SCK_GET_PROGRESS(tcp))
|
2016-01-30 05:24:23 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DEV_SCK_CONNECTING:
|
|
|
|
HIO_INFO1 (tcp->hio, "OUTGOING SESSION DISCONNECTED - FAILED TO CONNECT (%d) TO REMOTE SERVER\n", (int)tcp->hnd);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DEV_SCK_CONNECTING_SSL:
|
|
|
|
HIO_INFO1 (tcp->hio, "OUTGOING SESSION DISCONNECTED - FAILED TO SSL-CONNECT (%d) TO REMOTE SERVER\n", (int)tcp->hnd);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DEV_SCK_LISTENING:
|
|
|
|
HIO_INFO1 (tcp->hio, "SHUTTING DOWN THE SERVER SOCKET(%d)...\n", (int)tcp->hnd);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DEV_SCK_CONNECTED:
|
|
|
|
HIO_INFO1 (tcp->hio, "OUTGOING CLIENT CONNECTION GOT TORN DOWN(%d).......\n", (int)tcp->hnd);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DEV_SCK_ACCEPTING_SSL:
|
|
|
|
HIO_INFO1 (tcp->hio, "INCOMING SSL-ACCEPT GOT DISCONNECTED(%d) ....\n", (int)tcp->hnd);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DEV_SCK_ACCEPTED:
|
|
|
|
HIO_INFO1 (tcp->hio, "INCOMING CLIENT BEING SERVED GOT DISCONNECTED(%d).......\n", (int)tcp->hnd);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO2 (tcp->hio, "SOCKET DEVICE DISCONNECTED (%d - %x)\n", (int)tcp->hnd, (unsigned int)tcp->state);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void tcp_sck_on_connect (hio_dev_sck_t* tcp)
|
2019-01-16 10:42:20 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t buf1[128], buf2[128];
|
|
|
|
hio_bch_t k[50000];
|
|
|
|
hio_iovec_t iov[] =
|
2020-02-22 18:24:49 +00:00
|
|
|
{
|
|
|
|
{ "hello", 5 },
|
|
|
|
{ "world", 5 },
|
2021-07-22 07:30:20 +00:00
|
|
|
{ k, HIO_COUNTOF(k) },
|
|
|
|
{ "hio test", 8 },
|
|
|
|
{ k, HIO_COUNTOF(k) }
|
2020-02-22 18:24:49 +00:00
|
|
|
};
|
2020-02-23 16:11:32 +00:00
|
|
|
int i;
|
2016-04-18 14:21:23 +00:00
|
|
|
|
2020-02-23 16:11:32 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_skadtobcstr (tcp->hio, &tcp->localaddr, buf1, HIO_COUNTOF(buf1), HIO_SKAD_TO_BCSTR_ADDR | HIO_SKAD_TO_BCSTR_PORT);
|
|
|
|
hio_skadtobcstr (tcp->hio, &tcp->remoteaddr, buf2, HIO_COUNTOF(buf2), HIO_SKAD_TO_BCSTR_ADDR | HIO_SKAD_TO_BCSTR_PORT);
|
2016-04-18 14:21:23 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (tcp->state & HIO_DEV_SCK_CONNECTED)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO3 (tcp->hio, "DEVICE connected to a remote server... LOCAL %hs REMOTE %hs SCK: %d\n", buf1, buf2, tcp->hnd);
|
2016-01-28 16:44:47 +00:00
|
|
|
}
|
2021-07-22 07:30:20 +00:00
|
|
|
else if (tcp->state & HIO_DEV_SCK_ACCEPTED)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO3 (tcp->hio, "DEVICE accepted client device... .LOCAL %hs REMOTE %hs SCK: %d\n", buf1, buf2, tcp->hnd);
|
2016-01-28 16:44:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
for (i = 0; i < HIO_COUNTOF(k); i++) k[i] = 'A' + (i % 26);
|
2020-02-23 16:11:32 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
int sndbuf = 2000;
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_sck_setsockopt(tcp, SOL_SOCKET, SO_SNDBUF, &sndbuf, HIO_SIZEOF(sndbuf));
|
2020-02-23 16:11:32 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_dev_sck_writev(tcp, iov, HIO_COUNTOF(iov), HIO_NULL, HIO_NULL) <= -1)
|
2019-01-16 10:42:20 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_sck_halt (tcp);
|
2019-01-16 10:42:20 +00:00
|
|
|
}
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int tcp_sck_on_write (hio_dev_sck_t* tcp, hio_iolen_t wrlen, void* wrctx, const hio_skad_t* dstaddr)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
tcp_server_t* ts;
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_ntime_t tmout;
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2019-01-28 17:41:50 +00:00
|
|
|
if (wrlen <= -1)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (tcp->hio, "TCP_SCK_ON_WRITE(%d) >>> SEDING TIMED OUT...........\n", (int)tcp->hnd);
|
|
|
|
hio_dev_sck_halt (tcp);
|
2019-01-28 17:41:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ts = (tcp_server_t*)(tcp + 1);
|
|
|
|
if (wrlen == 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (tcp->hio, "TCP_SCK_ON_WRITE(%d) >>> CLOSED WRITING END\n", (int)tcp->hnd);
|
2019-01-28 17:41:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO3 (tcp->hio, "TCP_SCK_ON_WRITE(%d) >>> SENT MESSAGE %d of length %ld\n", (int)tcp->hnd, ts->tally, (long int)wrlen);
|
2019-01-28 17:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ts->tally++;
|
2021-07-22 07:30:20 +00:00
|
|
|
// if (ts->tally >= 2) hio_dev_sck_halt (tcp);
|
2019-01-28 17:41:50 +00:00
|
|
|
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INIT_NTIME (&tmout, 5, 0);
|
|
|
|
//hio_dev_sck_read (tcp, 1);
|
2019-01-28 17:41:50 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO3 (tcp->hio, "TCP_SCK_ON_WRITE(%d) >>> REQUESTING to READ with timeout of %ld.%08ld\n", (int)tcp->hnd, (long int)tmout.sec, (long int)tmout.nsec);
|
|
|
|
hio_dev_sck_timedread (tcp, 1, &tmout);
|
2019-01-28 17:41:50 +00:00
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int tcp_sck_on_read (hio_dev_sck_t* tcp, const void* buf, hio_iolen_t len, const hio_skad_t* srcaddr)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2016-04-24 17:30:43 +00:00
|
|
|
int n;
|
|
|
|
|
2019-01-17 08:09:19 +00:00
|
|
|
if (len <= -1)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (tcp->hio, "TCP_SCK_ON_READ(%d) STREAM DEVICE: TIMED OUT...\n", (int)tcp->hnd);
|
|
|
|
hio_dev_sck_halt (tcp);
|
2019-01-17 08:09:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (len <= 0)
|
2016-02-04 15:06:20 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (tcp->hio, "TCP_SCK_ON_READ(%d) STREAM DEVICE: EOF RECEIVED...\n", (int)tcp->hnd);
|
2016-02-04 15:06:20 +00:00
|
|
|
/* no outstanding request. but EOF */
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_sck_halt (tcp);
|
2016-02-04 15:06:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO2 (tcp->hio, "TCP_SCK_ON_READ(%d) - received %d bytes\n", (int)tcp->hnd, (int)len);
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2019-01-28 17:41:50 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_ntime_t tmout;
|
2016-04-24 17:30:43 +00:00
|
|
|
|
2019-01-28 17:41:50 +00:00
|
|
|
static char a ='A';
|
2019-01-29 08:38:12 +00:00
|
|
|
static char xxx[1000000];
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (xxx, a++ , HIO_SIZEOF(xxx));
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO2 (tcp->hio, "TCP_SCK_ON_READ(%d) >>> REQUESTING to write data of %d bytes\n", (int)tcp->hnd, HIO_SIZEOF(xxx));
|
|
|
|
//return hio_dev_sck_write (tcp, "HELLO", 5, HIO_NULL);
|
|
|
|
HIO_INIT_NTIME (&tmout, 5, 0);
|
|
|
|
n = hio_dev_sck_timedwrite(tcp, xxx, HIO_SIZEOF(xxx), &tmout, HIO_NULL, HIO_NULL);
|
2016-04-24 17:30:43 +00:00
|
|
|
|
2019-01-28 17:41:50 +00:00
|
|
|
if (n <= -1) return -1;
|
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (tcp->hio, "TCP_SCK_ON_READ(%d) - REQUESTING TO STOP READ\n", (int)tcp->hnd);
|
|
|
|
hio_dev_sck_read (tcp, 0);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2019-01-28 17:41:50 +00:00
|
|
|
#if 0
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (tcp->hio, "TCP_SCK_ON_READ(%d) - REQUESTING TO CLOSE WRITING END\n", (int)tcp->hnd);
|
2019-01-14 08:33:14 +00:00
|
|
|
/* post the write finisher - close the writing end */
|
2021-07-22 07:30:20 +00:00
|
|
|
n = hio_dev_sck_write(tcp, HIO_NULL, 0, HIO_NULL, HIO_NULL);
|
2016-02-04 15:06:20 +00:00
|
|
|
if (n <= -1) return -1;
|
2019-01-28 17:41:50 +00:00
|
|
|
#endif
|
2016-02-04 15:06:20 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2016-03-23 13:54:15 +00:00
|
|
|
/* return 1; let the main loop to read more greedily without consulting the multiplexer */
|
2016-01-28 16:44:47 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 13:54:15 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void pro_on_close (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = pro->hio;
|
|
|
|
if (sid == HIO_DEV_PRO_MASTER)
|
|
|
|
HIO_INFO1 (hio, "PROCESS(%d) CLOSE MASTER\n", (int)pro->child_pid);
|
2019-01-31 09:16:44 +00:00
|
|
|
else
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO2 (hio, "PROCESS(%d) CLOSE SLAVE[%d]\n", (int)pro->child_pid, sid);
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int pro_on_read (hio_dev_pro_t* pro, hio_dev_pro_sid_t sid, const void* data, hio_iolen_t dlen)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = pro->hio;
|
2019-01-31 09:16:44 +00:00
|
|
|
|
|
|
|
if (dlen <= -1)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "PROCESS(%d): READ TIMED OUT...\n", (int)pro->child_pid);
|
|
|
|
hio_dev_pro_halt (pro);
|
2019-01-31 09:16:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (dlen <= 0)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "PROCESS(%d): EOF RECEIVED...\n", (int)pro->child_pid);
|
2019-01-31 09:16:44 +00:00
|
|
|
/* no outstanding request. but EOF */
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_pro_halt (pro);
|
2019-01-31 09:16:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO5 (hio, "PROCESS(%d) READ DATA ON SLAVE[%d] len=%d [%.*hs]\n", (int)pro->child_pid, (int)sid, (int)dlen, dlen, (char*)data);
|
|
|
|
if (sid == HIO_DEV_PRO_OUT)
|
2019-01-31 09:16:44 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_pro_read (pro, sid, 0);
|
|
|
|
hio_dev_pro_write (pro, "HELLO\n", 6, HIO_NULL);
|
2019-01-31 09:16:44 +00:00
|
|
|
}
|
2016-04-25 14:07:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int pro_on_write (hio_dev_pro_t* pro, hio_iolen_t wrlen, void* wrctx)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = pro->hio;
|
|
|
|
hio_ntime_t tmout;
|
2019-01-31 09:16:44 +00:00
|
|
|
if (wrlen <= -1)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "PROCESS(%d): WRITE TIMED OUT...\n", (int)pro->child_pid);
|
|
|
|
hio_dev_pro_halt (pro);
|
2019-01-31 09:16:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_DEBUG2 (hio, "PROCESS(%d) wrote data of %d bytes\n", (int)pro->child_pid, (int)wrlen);
|
|
|
|
/*hio_dev_pro_read (pro, HIO_DEV_PRO_OUT, 1);*/
|
|
|
|
HIO_INIT_NTIME (&tmout, 5, 0);
|
|
|
|
hio_dev_pro_timedread (pro, HIO_DEV_PRO_OUT, 1, &tmout);
|
2016-04-25 14:07:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ========================================================================= */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int arp_sck_on_read (hio_dev_sck_t* dev, const void* data, hio_iolen_t dlen, const hio_skad_t* srcaddr)
|
2016-03-23 13:54:15 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_etharp_pkt_t* eap;
|
2016-04-25 16:15:36 +00:00
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (dlen < HIO_SIZEOF(*eap)) return 0; /* drop */
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
eap = (hio_etharp_pkt_t*)data;
|
2016-04-25 16:15:36 +00:00
|
|
|
|
2021-08-17 07:00:22 +00:00
|
|
|
printf ("ARP ON IFINDEX %d OPCODE: %d", hio_skad_get_ifindex(srcaddr), ntohs(eap->arphdr.opcode));
|
2016-04-25 16:15:36 +00:00
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
printf (" SHA: %02X:%02X:%02X:%02X:%02X:%02X", eap->arppld.sha[0], eap->arppld.sha[1], eap->arppld.sha[2], eap->arppld.sha[3], eap->arppld.sha[4], eap->arppld.sha[5]);
|
|
|
|
printf (" SPA: %d.%d.%d.%d", eap->arppld.spa[0], eap->arppld.spa[1], eap->arppld.spa[2], eap->arppld.spa[3]);
|
|
|
|
printf (" THA: %02X:%02X:%02X:%02X:%02X:%02X", eap->arppld.tha[0], eap->arppld.tha[1], eap->arppld.tha[2], eap->arppld.tha[3], eap->arppld.tha[4], eap->arppld.tha[5]);
|
|
|
|
printf (" TPA: %d.%d.%d.%d", eap->arppld.tpa[0], eap->arppld.tpa[1], eap->arppld.tpa[2], eap->arppld.tpa[3]);
|
|
|
|
printf ("\n");
|
2016-03-23 13:54:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int arp_sck_on_write (hio_dev_sck_t* dev, hio_iolen_t wrlen, void* wrctx, const hio_skad_t* dstaddr)
|
2016-03-23 13:54:15 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void arp_sck_on_connect (hio_dev_sck_t* dev)
|
2019-01-11 07:35:43 +00:00
|
|
|
{
|
2020-05-12 17:53:19 +00:00
|
|
|
printf ("STARTING UP ARP SOCKET %d...\n", dev->hnd);
|
2019-01-11 07:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void arp_sck_on_disconnect (hio_dev_sck_t* dev)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2020-05-12 17:53:19 +00:00
|
|
|
printf ("SHUTTING DOWN ARP SOCKET %d...\n", dev->hnd);
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int setup_arp_tester (hio_t* hio)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_skad_t ethdst;
|
|
|
|
hio_etharp_pkt_t etharp;
|
|
|
|
hio_dev_sck_make_t sck_make;
|
|
|
|
hio_dev_sck_t* sck;
|
2020-02-20 06:00:53 +00:00
|
|
|
unsigned int ifindex;
|
2020-02-11 09:02:46 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&sck_make, 0, HIO_SIZEOF(sck_make));
|
|
|
|
sck_make.type = HIO_DEV_SCK_ARP;
|
|
|
|
//sck_make.type = HIO_DEV_SCK_ARP_DGRAM;
|
2016-04-25 14:07:28 +00:00
|
|
|
sck_make.on_write = arp_sck_on_write;
|
|
|
|
sck_make.on_read = arp_sck_on_read;
|
2019-01-11 07:35:43 +00:00
|
|
|
sck_make.on_connect = arp_sck_on_connect;
|
2016-04-25 14:07:28 +00:00
|
|
|
sck_make.on_disconnect = arp_sck_on_disconnect;
|
2021-07-22 07:30:20 +00:00
|
|
|
sck = hio_dev_sck_make(hio, 0, &sck_make);
|
2016-04-25 14:07:28 +00:00
|
|
|
if (!sck)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "Cannot make arp socket device - %js\n", hio_geterrmsg(hio));
|
2016-04-25 14:07:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
//hio_bcstrtoifindex (hio, "enp0s25.3", &ifindex);
|
|
|
|
//hio_skad_init_for_eth (ðdst, ifindex, (hio_ethad_t*)"\xFF\xFF\xFF\xFF\xFF\xFF");
|
|
|
|
//hio_skad_init_for_eth (ðdst, ifindex, (hio_ethad_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
|
|
|
|
//hio_bcstrtoifindex (hio, "eno1", &ifindex);
|
|
|
|
//hio_skad_init_for_eth (ðdst, ifindex, (hio_ethad_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
|
|
|
|
hio_bcstrtoifindex (hio, "bce0", &ifindex);
|
|
|
|
hio_skad_init_for_eth (ðdst, ifindex, (hio_ethad_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (ðarp, 0, HIO_SIZEOF(etharp));
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memcpy (etharp.ethhdr.source, "\xB8\x6B\x23\x9C\x10\x76", HIO_ETHAD_LEN);
|
|
|
|
//memcpy (etharp.ethhdr.dest, "\xFF\xFF\xFF\xFF\xFF\xFF", HIO_ETHAD_LEN);
|
|
|
|
memcpy (etharp.ethhdr.dest, "\xAA\xBB\xFF\xCC\xDD\xFF", HIO_ETHAD_LEN);
|
|
|
|
etharp.ethhdr.proto = HIO_CONST_HTON16(HIO_ETHHDR_PROTO_ARP);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
etharp.arphdr.htype = HIO_CONST_HTON16(HIO_ARPHDR_HTYPE_ETH);
|
|
|
|
etharp.arphdr.ptype = HIO_CONST_HTON16(HIO_ARPHDR_PTYPE_IP4);
|
|
|
|
etharp.arphdr.hlen = HIO_ETHAD_LEN;
|
|
|
|
etharp.arphdr.plen = HIO_IP4AD_LEN;
|
|
|
|
etharp.arphdr.opcode = HIO_CONST_HTON16(HIO_ARPHDR_OPCODE_REQUEST);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memcpy (etharp.arppld.sha, "\xB8\x6B\x23\x9C\x10\x76", HIO_ETHAD_LEN);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_dev_sck_write(sck, ðarp, HIO_SIZEOF(etharp), HIO_NULL, ðdst) <= -1)
|
|
|
|
//if (hio_dev_sck_write (sck, ðarp.arphdr, HIO_SIZEOF(etharp) - HIO_SIZEOF(etharp.ethhdr), HIO_NULL, ðaddr) <= -1)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "Cannot write arp - %js\n", hio_geterrmsg(hio));
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-12 13:56:59 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2016-04-25 14:07:28 +00:00
|
|
|
struct icmpxtn_t
|
2016-04-12 13:56:59 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uint16_t icmp_seq;
|
|
|
|
hio_tmridx_t tmout_jobidx;
|
2016-04-25 14:07:28 +00:00
|
|
|
int reply_received;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct icmpxtn_t icmpxtn_t;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int schedule_icmp_wait (hio_dev_sck_t* dev);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void send_icmp (hio_dev_sck_t* dev, hio_uint16_t seq)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = dev->hio;
|
|
|
|
hio_skad_t dstaddr;
|
|
|
|
hio_icmphdr_t* icmphdr;
|
|
|
|
hio_uint8_t buf[512];
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
//hio_bcstrtoskad (hio, "192.168.9.1", &dstaddr);
|
|
|
|
hio_bcstrtoskad (hio, "192.168.1.1", &dstaddr);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset(buf, 0, HIO_SIZEOF(buf));
|
|
|
|
icmphdr = (hio_icmphdr_t*)buf;
|
|
|
|
icmphdr->type = HIO_ICMP_ECHO_REQUEST;
|
|
|
|
icmphdr->u.echo.id = HIO_CONST_HTON16(100);
|
|
|
|
icmphdr->u.echo.seq = hio_hton16(seq);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&buf[HIO_SIZEOF(*icmphdr)], 'A', HIO_SIZEOF(buf) - HIO_SIZEOF(*icmphdr));
|
|
|
|
icmphdr->checksum = hio_checksum_ip(icmphdr, HIO_SIZEOF(buf));
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_dev_sck_write(dev, buf, HIO_SIZEOF(buf), HIO_NULL, &dstaddr) <= -1)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "Cannot write icmp - %js\n", hio_geterrmsg(hio));
|
|
|
|
hio_dev_sck_halt (dev);
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
if (schedule_icmp_wait(dev) <= -1)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "Cannot schedule icmp wait - %js\n", hio_geterrmsg(hio));
|
|
|
|
hio_dev_sck_halt (dev);
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
2016-04-16 16:05:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void on_icmp_due (hio_t* hio, const hio_ntime_t* now, hio_tmrjob_t* tmrjob)
|
2016-04-16 16:05:57 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_sck_t* dev;
|
2016-04-25 14:07:28 +00:00
|
|
|
icmpxtn_t* icmpxtn;
|
|
|
|
|
|
|
|
dev = tmrjob->ctx;
|
|
|
|
icmpxtn = (icmpxtn_t*)(dev + 1);
|
|
|
|
|
|
|
|
if (icmpxtn->reply_received)
|
|
|
|
icmpxtn->reply_received = 0;
|
|
|
|
else
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO0 (hio, "NO IMCP reply received in time\n");
|
2016-04-25 14:07:28 +00:00
|
|
|
|
|
|
|
send_icmp (dev, ++icmpxtn->icmp_seq);
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int schedule_icmp_wait (hio_dev_sck_t* dev)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = dev->hio;
|
2016-04-25 14:07:28 +00:00
|
|
|
icmpxtn_t* icmpxtn;
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_tmrjob_t tmrjob;
|
|
|
|
hio_ntime_t fire_after;
|
2016-04-25 14:07:28 +00:00
|
|
|
|
|
|
|
icmpxtn = (icmpxtn_t*)(dev + 1);
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INIT_NTIME (&fire_after, 2, 0);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&tmrjob, 0, HIO_SIZEOF(tmrjob));
|
2016-04-25 14:07:28 +00:00
|
|
|
tmrjob.ctx = dev;
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_gettime (hio, &tmrjob.when);
|
|
|
|
HIO_ADD_NTIME (&tmrjob.when, &tmrjob.when, &fire_after);
|
2016-04-25 14:07:28 +00:00
|
|
|
tmrjob.handler = on_icmp_due;
|
|
|
|
tmrjob.idxptr = &icmpxtn->tmout_jobidx;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
assert (icmpxtn->tmout_jobidx == HIO_TMRIDX_INVALID);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
return (hio_instmrjob(dev->hio, &tmrjob) == HIO_TMRIDX_INVALID)? -1: 0;
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int icmp_sck_on_read (hio_dev_sck_t* dev, const void* data, hio_iolen_t dlen, const hio_skad_t* srcaddr)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
|
|
|
icmpxtn_t* icmpxtn;
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_iphdr_t* iphdr;
|
|
|
|
hio_icmphdr_t* icmphdr;
|
2016-04-25 14:07:28 +00:00
|
|
|
|
|
|
|
/* when received, the data contains the IP header.. */
|
|
|
|
icmpxtn = (icmpxtn_t*)(dev + 1);
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (dlen < HIO_SIZEOF(*iphdr) + HIO_SIZEOF(*icmphdr))
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
|
|
|
printf ("INVALID ICMP PACKET.. TOO SHORT...%d\n", (int)dlen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* TODO: consider IP options... */
|
2021-07-22 07:30:20 +00:00
|
|
|
iphdr = (hio_iphdr_t*)data;
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (iphdr->ihl * 4 + HIO_SIZEOF(*icmphdr) > dlen)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2016-04-26 05:54:48 +00:00
|
|
|
printf ("INVALID ICMP PACKET.. WRONG IHL...%d\n", (int)iphdr->ihl * 4);
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
icmphdr = (hio_icmphdr_t*)((hio_uint8_t*)data + (iphdr->ihl * 4));
|
2016-04-26 05:54:48 +00:00
|
|
|
|
|
|
|
/* TODO: check srcaddr against target */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (icmphdr->type == HIO_ICMP_ECHO_REPLY &&
|
|
|
|
hio_ntoh16(icmphdr->u.echo.seq) == icmpxtn->icmp_seq) /* TODO: more check.. echo.id.. */
|
2016-04-26 05:54:48 +00:00
|
|
|
{
|
|
|
|
icmpxtn->reply_received = 1;
|
2021-07-22 07:30:20 +00:00
|
|
|
printf ("ICMP REPLY RECEIVED...ID %d SEQ %d\n", (int)hio_ntoh16(icmphdr->u.echo.id), (int)hio_ntoh16(icmphdr->u.echo.seq));
|
2016-04-26 05:54:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
printf ("GARBAGE ICMP PACKET...LEN %d SEQ %d,%d\n", (int)dlen, (int)icmpxtn->icmp_seq, (int)hio_ntoh16(icmphdr->u.echo.seq));
|
2016-04-26 05:54:48 +00:00
|
|
|
}
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-12 13:56:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int icmp_sck_on_write (hio_dev_sck_t* dev, hio_iolen_t wrlen, void* wrctx, const hio_skad_t* dstaddr)
|
2016-04-12 13:56:59 +00:00
|
|
|
{
|
2016-04-25 14:07:28 +00:00
|
|
|
/*icmpxtn_t* icmpxtn;
|
|
|
|
|
|
|
|
icmpxtn = (icmpxtn_t*)(dev + 1); */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void icmp_sck_on_disconnect (hio_dev_sck_t* dev)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
|
|
|
icmpxtn_t* icmpxtn;
|
|
|
|
|
|
|
|
icmpxtn = (icmpxtn_t*)(dev + 1);
|
|
|
|
|
2020-05-12 17:53:19 +00:00
|
|
|
printf ("SHUTTING DOWN ICMP SOCKET %d...\n", dev->hnd);
|
2021-07-22 07:30:20 +00:00
|
|
|
if (icmpxtn->tmout_jobidx != HIO_TMRIDX_INVALID)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_deltmrjob (dev->hio, icmpxtn->tmout_jobidx);
|
|
|
|
icmpxtn->tmout_jobidx = HIO_TMRIDX_INVALID;
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int setup_ping4_tester (hio_t* hio)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_sck_make_t sck_make;
|
|
|
|
hio_dev_sck_t* sck;
|
2016-04-25 14:07:28 +00:00
|
|
|
icmpxtn_t* icmpxtn;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&sck_make, 0, HIO_SIZEOF(sck_make));
|
|
|
|
sck_make.type = HIO_DEV_SCK_ICMP4;
|
2016-04-25 14:07:28 +00:00
|
|
|
sck_make.on_write = icmp_sck_on_write;
|
|
|
|
sck_make.on_read = icmp_sck_on_read;
|
|
|
|
sck_make.on_disconnect = icmp_sck_on_disconnect;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
sck = hio_dev_sck_make (hio, HIO_SIZEOF(icmpxtn_t), &sck_make);
|
2016-04-25 14:07:28 +00:00
|
|
|
if (!sck)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "Cannot make ICMP4 socket device - %js\n", hio_geterrmsg(hio));
|
2016-04-25 14:07:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
icmpxtn = (icmpxtn_t*)(sck + 1);
|
2021-07-22 07:30:20 +00:00
|
|
|
icmpxtn->tmout_jobidx = HIO_TMRIDX_INVALID;
|
2016-04-25 14:07:28 +00:00
|
|
|
icmpxtn->icmp_seq = 0;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
/*TODO: hio_dev_sck_setbroadcast (sck, 1);*/
|
2016-04-25 14:07:28 +00:00
|
|
|
|
|
|
|
send_icmp (sck, ++icmpxtn->icmp_seq);
|
|
|
|
|
2016-04-12 13:56:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2016-03-23 13:54:15 +00:00
|
|
|
|
2020-05-21 10:15:57 +00:00
|
|
|
/* ========================================================================= */
|
2021-07-22 07:30:20 +00:00
|
|
|
static int pipe_on_read (hio_dev_pipe_t* dev, const void* data, hio_iolen_t dlen)
|
2020-05-21 10:15:57 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO3 (dev->hio, "PIPE READ %d bytes - [%.*s]\n", (int)dlen, (int)dlen, data);
|
2020-05-23 06:07:43 +00:00
|
|
|
return 0;
|
2020-05-21 10:15:57 +00:00
|
|
|
}
|
2021-07-22 07:30:20 +00:00
|
|
|
static int pipe_on_write (hio_dev_pipe_t* dev, hio_iolen_t wrlen, void* wrctx)
|
2020-05-21 10:15:57 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (dev->hio, "PIPE WRITTEN %d bytes\n", (int)wrlen);
|
2020-05-23 06:07:43 +00:00
|
|
|
return 0;
|
2020-05-21 10:15:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void pipe_on_close (hio_dev_pipe_t* dev, hio_dev_pipe_sid_t sid)
|
2020-05-21 10:15:57 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (dev->hio, "PIPE[%d] CLOSED \n", (int)sid);
|
2020-05-21 10:15:57 +00:00
|
|
|
}
|
2020-05-22 19:06:57 +00:00
|
|
|
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int thr_on_read (hio_dev_thr_t* dev, const void* data, hio_iolen_t dlen)
|
2020-05-22 19:06:57 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO3 (dev->hio, "THR READ FROM THR - %d bytes - [%.*s]\n", (int)dlen, (int)dlen, data);
|
|
|
|
//if (dlen == 0) hio_dev_halt(dev); /* EOF on the input. treat this as end of whole thread transaction */
|
2020-05-23 06:07:43 +00:00
|
|
|
return 0;
|
2020-05-22 19:06:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int thr_on_write (hio_dev_thr_t* dev, hio_iolen_t wrlen, void* wrctx)
|
2020-05-22 19:06:57 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (dev->hio, "THR WRITTEN TO THR - %d bytes\n", (int)wrlen);
|
2020-05-23 06:07:43 +00:00
|
|
|
return 0;
|
2020-05-22 19:06:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void thr_on_close (hio_dev_thr_t* dev, hio_dev_thr_sid_t sid)
|
2020-05-22 19:06:57 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (sid == HIO_DEV_THR_OUT) hio_dev_thr_haltslave (dev, HIO_DEV_THR_IN);
|
|
|
|
HIO_INFO1 (dev->hio, "THR[%d] CLOSED \n", (int)sid);
|
2020-05-22 19:06:57 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void thr_func (hio_t* hio, hio_dev_thr_iopair_t* iop, void* ctx)
|
2020-05-22 19:06:57 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bch_t buf[5];
|
2020-05-22 19:06:57 +00:00
|
|
|
ssize_t n;
|
|
|
|
|
2020-05-23 06:07:43 +00:00
|
|
|
static int x = 0;
|
|
|
|
int y;
|
|
|
|
int z = 0;
|
|
|
|
|
2020-05-24 01:18:32 +00:00
|
|
|
#if defined(__ATOMIC_RELAXED)
|
2020-05-23 06:07:43 +00:00
|
|
|
y = __atomic_add_fetch (&x, 1, __ATOMIC_RELAXED);
|
2020-05-24 01:18:32 +00:00
|
|
|
#else
|
|
|
|
// this is buggy..
|
|
|
|
y = ++x;
|
|
|
|
#endif
|
2020-05-23 06:07:43 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
while ((n = read(iop->rfd, buf, HIO_COUNTOF(buf)))> 0) write (iop->wfd, buf, n);
|
2020-05-22 19:06:57 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
sleep (1);
|
|
|
|
write (iop->wfd, "THR LOOPING", 11);
|
2020-05-23 06:07:43 +00:00
|
|
|
z++;
|
|
|
|
if ((y % 2) && (z >5))
|
|
|
|
{
|
2022-06-13 13:54:14 +00:00
|
|
|
write (iop->wfd, HIO_NULL, 0);
|
2020-05-23 06:07:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-05-22 19:06:57 +00:00
|
|
|
}
|
2020-05-23 06:07:43 +00:00
|
|
|
|
2020-05-22 19:06:57 +00:00
|
|
|
}
|
2020-02-11 09:02:46 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void on_dnc_resolve(hio_svc_dnc_t* dnc, hio_dns_msg_t* reqmsg, hio_errnum_t status, const void* data, hio_oow_t dlen)
|
2020-02-11 09:02:46 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_pkt_info_t* pi = (hio_dns_pkt_info_t*)data;
|
2020-02-13 09:31:02 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (pi) // status == HIO_ENOERR
|
2020-02-11 09:02:46 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_uint32_t i;
|
2020-02-13 09:31:02 +00:00
|
|
|
|
2020-02-14 10:06:29 +00:00
|
|
|
|
|
|
|
printf (">>>>>>>> RRDLEN = %d\n", (int)pi->_rrdlen);
|
2021-08-08 07:22:50 +00:00
|
|
|
printf (">>>>>>>> RCODE %s(%d) EDNS exist %d uplen %d version %d dnssecok %d qdcount %d ancount %d nscount %d arcount %d\n", hio_dns_rcode_to_bcstr(pi->hdr.rcode), pi->hdr.rcode, pi->edns.exist, pi->edns.uplen, pi->edns.version, pi->edns.dnssecok, pi->qdcount, pi->ancount, pi->nscount, pi->arcount);
|
2021-07-22 07:30:20 +00:00
|
|
|
if (pi->hdr.rcode == HIO_DNS_RCODE_BADCOOKIE)
|
2020-07-02 14:21:47 +00:00
|
|
|
{
|
2020-07-04 08:03:36 +00:00
|
|
|
/* TODO: must retry?? there shoudl be no RRs in the payload */
|
2021-07-22 07:30:20 +00:00
|
|
|
//if (hio_svc_dnc_resolve(dnc, hio_svc_dnc_getqnamefromreqmsg(reqmsg), hio_svc_dnc_getqtypefromreqmsg(qtype), hio_svc_dnc_getresolflagsfromreqmsg(resolve_flags), on_dnc_resolve, 0) >= 0) return;
|
2020-07-02 14:21:47 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_svc_dnc_checkclientcookie(dnc, reqmsg, pi) == 0)
|
2020-07-02 14:21:47 +00:00
|
|
|
{
|
|
|
|
/* client cookie is bad.. */
|
2021-07-22 07:30:20 +00:00
|
|
|
printf ("CLIENT COOKIE IS BAD>>>>>>>>>>>>>>>>>>>%d\n", hio_svc_dnc_checkclientcookie(dnc, reqmsg, pi));
|
2020-07-04 08:03:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
printf ("CLIENT COOKIE IS OK>>>>>>>>>>>>>>>>>>>%d\n", hio_svc_dnc_checkclientcookie(dnc, reqmsg, pi));
|
2020-07-02 14:21:47 +00:00
|
|
|
}
|
|
|
|
|
2021-08-08 07:22:50 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
//if (pi->hdr.rcode != HIO_DNS_RCODE_NOERROR) goto no_data;
|
2021-08-08 07:22:50 +00:00
|
|
|
if (pi->ancount < 0)
|
|
|
|
{
|
|
|
|
goto no_data;
|
|
|
|
}
|
2020-02-14 10:06:29 +00:00
|
|
|
|
2020-02-13 09:31:02 +00:00
|
|
|
for (i = 0; i < pi->ancount; i++)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_brr_t* brr = &pi->rr.an[i];
|
2020-02-14 10:06:29 +00:00
|
|
|
switch (pi->rr.an[i].rrtype)
|
2020-02-13 09:31:02 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DNS_RRT_A:
|
2020-02-14 10:06:29 +00:00
|
|
|
{
|
2020-02-20 10:07:12 +00:00
|
|
|
struct in_addr ia;
|
|
|
|
memcpy (&ia.s_addr, brr->dptr, brr->dlen);
|
|
|
|
printf ("^^^ GOT REPLY A........................ %d ", brr->dlen);
|
|
|
|
printf ("[%s]", inet_ntoa(ia));
|
2020-02-14 10:06:29 +00:00
|
|
|
printf ("\n");
|
|
|
|
goto done;
|
|
|
|
}
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DNS_RRT_AAAA:
|
2020-02-14 10:06:29 +00:00
|
|
|
{
|
2020-02-20 10:07:12 +00:00
|
|
|
struct in6_addr ia;
|
|
|
|
char buf[128];
|
|
|
|
memcpy (&ia.s6_addr, brr->dptr, brr->dlen);
|
|
|
|
printf ("^^^ GOT REPLY AAAA........................ %d ", brr->dlen);
|
2021-07-22 07:30:20 +00:00
|
|
|
printf ("[%s]", inet_ntop(AF_INET6, &ia, buf, HIO_COUNTOF(buf)));
|
2020-02-14 10:06:29 +00:00
|
|
|
printf ("\n");
|
|
|
|
goto done;
|
|
|
|
}
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DNS_RRT_CNAME:
|
2020-02-14 10:06:29 +00:00
|
|
|
printf ("^^^ GOT REPLY.... CNAME [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
goto done;
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DNS_RRT_MX:
|
2020-02-24 16:39:29 +00:00
|
|
|
printf ("^^^ GOT REPLY.... MX [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
goto done;
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DNS_RRT_NS:
|
2020-02-20 10:07:12 +00:00
|
|
|
printf ("^^^ GOT REPLY.... NS [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
goto done;
|
2021-07-22 07:30:20 +00:00
|
|
|
case HIO_DNS_RRT_PTR:
|
2020-02-20 10:07:12 +00:00
|
|
|
printf ("^^^ GOT REPLY.... PTR [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
goto done;
|
2020-02-14 10:06:29 +00:00
|
|
|
default:
|
2020-02-27 17:34:27 +00:00
|
|
|
goto no_data;
|
2020-02-13 09:31:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-27 17:34:27 +00:00
|
|
|
goto no_data;
|
2020-02-11 09:02:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-27 17:34:27 +00:00
|
|
|
no_data:
|
2021-07-22 07:30:20 +00:00
|
|
|
if (status == HIO_ETMOUT) printf ("XXXXXXXXXXXXXXXX TIMED OUT XXXXXXXXXXXXXXXXX\n");
|
2020-02-27 17:34:27 +00:00
|
|
|
else printf ("XXXXXXXXXXXXXXXXx NO REPLY DATA XXXXXXXXXXXXXXXXXXXXXXXXX\n");
|
2020-02-11 09:02:46 +00:00
|
|
|
}
|
2020-02-13 09:31:02 +00:00
|
|
|
|
|
|
|
done:
|
2020-07-02 14:21:47 +00:00
|
|
|
/* nothing special */
|
|
|
|
return;
|
2020-02-11 09:02:46 +00:00
|
|
|
}
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void on_dnc_resolve_brief (hio_svc_dnc_t* dnc, hio_dns_msg_t* reqmsg, hio_errnum_t status, const void* data, hio_oow_t dlen)
|
2020-02-14 10:06:29 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (data) /* status must be HIO_ENOERR */
|
2020-02-14 10:06:29 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_brr_t* brr = (hio_dns_brr_t*)data;
|
2020-02-14 10:06:29 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (brr->rrtype == HIO_DNS_RRT_AAAA)
|
2020-02-14 10:06:29 +00:00
|
|
|
{
|
|
|
|
struct in6_addr ia;
|
|
|
|
char buf[128];
|
|
|
|
memcpy (&ia.s6_addr, brr->dptr, brr->dlen);
|
|
|
|
printf ("^^^ SIMPLE -> GOT REPLY........................ %d ", brr->dlen);
|
2021-07-22 07:30:20 +00:00
|
|
|
printf ("[%s]", inet_ntop(AF_INET6, &ia, buf, HIO_COUNTOF(buf)));
|
2020-02-14 10:06:29 +00:00
|
|
|
printf ("\n");
|
|
|
|
}
|
2021-07-22 07:30:20 +00:00
|
|
|
else if (brr->rrtype == HIO_DNS_RRT_A)
|
2020-02-14 10:06:29 +00:00
|
|
|
{
|
|
|
|
struct in_addr ia;
|
|
|
|
memcpy (&ia.s_addr, brr->dptr, brr->dlen);
|
|
|
|
printf ("^^^ SIMPLE -> GOT REPLY........................ %d ", brr->dlen);
|
|
|
|
printf ("[%s]", inet_ntoa(ia));
|
|
|
|
printf ("\n");
|
|
|
|
}
|
2021-07-22 07:30:20 +00:00
|
|
|
else if (brr->rrtype == HIO_DNS_RRT_CNAME)
|
2020-02-14 10:06:29 +00:00
|
|
|
{
|
|
|
|
printf ("^^^ SIMPLE -> CNAME [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
}
|
2021-07-22 07:30:20 +00:00
|
|
|
else if (brr->rrtype == HIO_DNS_RRT_NS)
|
2020-02-17 14:54:21 +00:00
|
|
|
{
|
|
|
|
printf ("^^^ SIMPLE -> NS [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
}
|
2021-07-22 07:30:20 +00:00
|
|
|
else if (brr->rrtype == HIO_DNS_RRT_PTR)
|
2020-02-19 14:59:06 +00:00
|
|
|
{
|
|
|
|
printf ("^^^ SIMPLE -> PTR [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
}
|
2021-07-22 07:30:20 +00:00
|
|
|
else if (brr->rrtype == HIO_DNS_RRT_SOA)
|
2020-02-17 14:54:21 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_brrd_soa_t* soa = brr->dptr;
|
2020-02-17 14:54:21 +00:00
|
|
|
printf ("^^^ SIMPLE -> SOA [%s] [%s] [%u %u %u %u %u] %d\n", soa->mname, soa->rname, (unsigned)soa->serial, (unsigned)soa->refresh, (unsigned)soa->retry, (unsigned)soa->expire, (unsigned)soa->minimum, (int)brr->dlen);
|
|
|
|
}
|
2020-02-14 10:06:29 +00:00
|
|
|
else
|
|
|
|
{
|
2020-02-17 14:54:21 +00:00
|
|
|
printf ("^^^ SIMPLE -> UNKNOWN DATA... [%.*s] %d\n", (int)brr->dlen, brr->dptr, (int)brr->dlen);
|
2020-02-14 10:06:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (status == HIO_ETMOUT) printf ("QQQQQQQQQQQQQQQQQQQ TIMED OUT QQQQQQQQQQQQQQQQQ\n");
|
|
|
|
else printf ("QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ NO REPLY DATA QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQq - %d\n", hio_geterrnum(hio_svc_dnc_gethio(dnc)));
|
2020-02-14 10:06:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static int print_qparam (hio_bcs_t* key, hio_bcs_t* val, void* ctx)
|
2020-05-27 02:32:51 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
key->len = hio_perdec_http_bcs(key, key->ptr, HIO_NULL);
|
|
|
|
val->len = hio_perdec_http_bcs(val, val->ptr, HIO_NULL);
|
2020-05-27 02:32:51 +00:00
|
|
|
fprintf ((FILE*)ctx, "\t[%.*s] = [%.*s]\n", (int)key->len, key->ptr, (int)val->len, val->ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void on_htts_thr_request (hio_t* hio, hio_dev_thr_iopair_t* iop, hio_svc_htts_thr_func_info_t* tfi, void* ctx)
|
2020-05-26 01:13:34 +00:00
|
|
|
{
|
|
|
|
FILE* fp;
|
|
|
|
int i;
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (tfi->req_method != HIO_HTTP_GET)
|
2020-05-26 01:22:28 +00:00
|
|
|
{
|
|
|
|
write (iop->wfd, "Status: 405\r\n\r\n", 15); /* method not allowed */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-26 01:13:34 +00:00
|
|
|
fp = fdopen(iop->wfd, "w");
|
2020-05-26 01:22:28 +00:00
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
write (iop->wfd, "Status: 500\r\n\r\n", 15); /* internal server error */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-27 10:30:16 +00:00
|
|
|
fprintf (fp, "Status: 200\r\n");
|
2020-05-26 01:13:34 +00:00
|
|
|
fprintf (fp, "Content-Type: text/html\r\n\r\n");
|
|
|
|
|
|
|
|
fprintf (fp, "request path = %s\n", tfi->req_path);
|
2020-05-27 02:32:51 +00:00
|
|
|
if (tfi->req_param)
|
|
|
|
{
|
|
|
|
fprintf (fp, "request params:\n");
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_scan_http_qparam (tfi->req_param, print_qparam, fp);
|
2020-05-27 02:32:51 +00:00
|
|
|
}
|
2020-05-26 01:13:34 +00:00
|
|
|
for (i = 0; i < 100; i++) fprintf (fp, "%d * %d => %d\n", i, i, i * i);
|
|
|
|
|
|
|
|
fclose (fp);
|
2021-07-22 07:30:20 +00:00
|
|
|
iop->wfd = HIO_SYSHND_INVALID;
|
2020-05-26 01:13:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 16:14:36 +00:00
|
|
|
/* ========================================================================= */
|
2021-07-22 07:30:20 +00:00
|
|
|
int process_http_request (hio_svc_htts_t* htts, hio_dev_sck_t* csck, hio_htre_t* req)
|
2020-05-20 16:14:36 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio = hio_svc_htts_gethio(htts);
|
|
|
|
// hio_svc_htts_cli_t* cli = hio_dev_sck_getxtn(csck);
|
|
|
|
hio_http_method_t mth;
|
2020-05-20 16:14:36 +00:00
|
|
|
|
|
|
|
/* percent-decode the query path to the original buffer
|
|
|
|
* since i'm not going to need it in the original form
|
|
|
|
* any more. once it's decoded in the peek mode,
|
|
|
|
* the decoded query path is made available in the
|
|
|
|
* non-peek mode as well */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_DEBUG2 (hio, "[RAW-REQ] %s %s\n", hio_htre_getqmethodname(req), hio_htre_getqpath(req));
|
2020-05-20 16:14:36 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_htre_perdecqpath(req);
|
2020-05-20 16:14:36 +00:00
|
|
|
/* TODO: proper request logging */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_DEBUG2 (hio, "[REQ] %s %s\n", hio_htre_getqmethodname(req), hio_htre_getqpath(req));
|
2020-05-20 16:14:36 +00:00
|
|
|
|
|
|
|
#if 0
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_printf (HIO_T("================================\n"));
|
|
|
|
hio_printf (HIO_T("[%lu] %hs REQUEST ==> [%hs] version[%d.%d %hs] method[%hs]\n"),
|
2020-05-20 16:14:36 +00:00
|
|
|
(unsigned long)time(NULL),
|
2021-07-22 07:30:20 +00:00
|
|
|
(peek? HIO_MT("PEEK"): HIO_MT("HANDLE")),
|
|
|
|
hio_htre_getqpath(req),
|
|
|
|
hio_htre_getmajorversion(req),
|
|
|
|
hio_htre_getminorversion(req),
|
|
|
|
hio_htre_getverstr(req),
|
|
|
|
hio_htre_getqmethodname(req)
|
2020-05-20 16:14:36 +00:00
|
|
|
);
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_htre_getqparam(req))
|
|
|
|
hio_printf (HIO_T("PARAMS ==> [%hs]\n"), hio_htre_getqparam(req));
|
2020-05-20 16:14:36 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_htb_walk (&req->hdrtab, walk, HIO_NULL);
|
|
|
|
if (hio_htre_getcontentlen(req) > 0)
|
2020-05-20 16:14:36 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_printf (HIO_T("CONTENT [%.*S]\n"), (int)hio_htre_getcontentlen(req), hio_htre_getcontentptr(req));
|
2020-05-20 16:14:36 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
mth = hio_htre_getqmethodtype(req);
|
2020-05-20 16:14:36 +00:00
|
|
|
/* determine what to do once the header fields are all received.
|
|
|
|
* i don't want to delay this until the contents are received.
|
|
|
|
* if you don't like this behavior, you must implement your own
|
|
|
|
* callback function for request handling. */
|
|
|
|
#if 0
|
|
|
|
/* TODO support X-HTTP-Method-Override */
|
2021-07-22 07:30:20 +00:00
|
|
|
if (data.method == HIO_HTTP_POST)
|
2020-05-20 16:14:36 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
tmp = hio_htre_getheaderval(req, HIO_MT("X-HTTP-Method-Override"));
|
2020-05-20 16:14:36 +00:00
|
|
|
if (tmp)
|
|
|
|
{
|
|
|
|
/*while (tmp->next) tmp = tmp->next;*/ /* get the last value */
|
2021-07-22 07:30:20 +00:00
|
|
|
data.method = hio_mbstohttpmethod (tmp->ptr);
|
2020-05-20 16:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
2021-07-22 07:30:20 +00:00
|
|
|
if (mth == HIO_HTTP_CONNECT)
|
2020-05-20 16:14:36 +00:00
|
|
|
{
|
|
|
|
/* CONNECT method must not have content set.
|
|
|
|
* however, arrange to discard it if so.
|
|
|
|
*
|
|
|
|
* NOTE: CONNECT is implemented to ignore many headers like
|
|
|
|
* 'Expect: 100-continue' and 'Connection: keep-alive'. */
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_htre_discardcontent (req);
|
2020-05-20 16:14:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
/* this part can be checked in actual hio_svc_htts_doXXX() functions.
|
2020-05-20 16:14:36 +00:00
|
|
|
* some doXXX handlers may not require length for POST.
|
|
|
|
* it may be able to simply accept till EOF? or treat as if CONTENT_LENGTH is 0*/
|
2021-07-22 07:30:20 +00:00
|
|
|
if (mth == HIO_HTTP_POST && !(req->flags & (HIO_HTRE_ATTR_LENGTH | HIO_HTRE_ATTR_CHUNKED)))
|
2020-05-20 16:14:36 +00:00
|
|
|
{
|
|
|
|
/* POST without Content-Length nor not chunked */
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_htre_discardcontent (req);
|
2020-05-20 16:14:36 +00:00
|
|
|
/* 411 Length Required - can't keep alive. Force disconnect */
|
2021-07-22 07:30:20 +00:00
|
|
|
req->flags &= ~HIO_HTRE_ATTR_KEEPALIVE; /* to cause sendstatus() to close */
|
2022-10-09 16:41:07 +00:00
|
|
|
if (hio_svc_htts_sendstatus(htts, csck, req, HIO_HTTP_STATUS_LENGTH_REQUIRED, HIO_NULL) <= -1) goto oops;
|
2020-05-20 16:14:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
|
|
|
|
#endif
|
2022-06-17 05:35:30 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
const hio_bch_t* qpath = hio_htre_getqpath(req);
|
2020-05-26 01:13:34 +00:00
|
|
|
int x;
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_comp_bcstr_limited(qpath, "/thr/", 5, 1) == 0)
|
2022-10-09 16:41:07 +00:00
|
|
|
x = hio_svc_htts_dothr(htts, csck, req, on_htts_thr_request, HIO_NULL, 0);
|
2021-07-22 07:30:20 +00:00
|
|
|
else if (hio_comp_bcstr_limited(qpath, "/txt/", 5, 1) == 0)
|
2022-10-09 16:41:07 +00:00
|
|
|
x = hio_svc_htts_dotxt(htts, csck, req, HIO_HTTP_STATUS_OK, "text/plain", qpath, 0);
|
2021-07-22 07:30:20 +00:00
|
|
|
else if (hio_comp_bcstr_limited(qpath, "/cgi/", 5, 1) == 0)
|
2022-10-09 16:41:07 +00:00
|
|
|
x = hio_svc_htts_docgi(htts, csck, req, "", qpath + 4, 0);
|
2020-07-09 09:54:13 +00:00
|
|
|
else
|
2022-12-21 14:57:52 +00:00
|
|
|
x = hio_svc_htts_dofile(htts, csck, req, "", qpath, "text/plain", 0, HIO_NULL);
|
2020-05-26 01:13:34 +00:00
|
|
|
if (x <= -1) goto oops;
|
2022-06-17 05:35:30 +00:00
|
|
|
}
|
|
|
|
#if 0
|
|
|
|
}
|
|
|
|
#endif
|
2020-05-20 16:14:36 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_sck_halt (csck);
|
2020-05-20 16:14:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-03-23 13:54:15 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static hio_t* g_hio;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
|
|
|
static void handle_signal (int sig)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
if (g_hio) hio_stop (g_hio, HIO_STOPREQ_TERMINATION);
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static void send_test_query (hio_t* hio, const hio_ntime_t* now, hio_tmrjob_t* job)
|
2020-07-02 14:21:47 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
//if (!hio_svc_dnc_resolve((hio_svc_dnc_t*)job->ctx, "www.microsoft.com", HIO_DNS_RRT_CNAME, HIO_SVC_DNC_RESOLVE_FLAG_COOKIE, on_dnc_resolve, 0))
|
|
|
|
if (!hio_svc_dnc_resolve((hio_svc_dnc_t*)job->ctx, "mailserver.manyhost.net", HIO_DNS_RRT_A, HIO_SVC_DNC_RESOLVE_FLAG_COOKIE | HIO_SVC_DNC_RESOLVE_FLAG_DNSSEC, on_dnc_resolve, 0))
|
2020-07-02 14:21:47 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> mailserver.manyhost.net\n");
|
|
|
|
}
|
2020-07-04 08:03:36 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (!hio_svc_dnc_resolve((hio_svc_dnc_t*)job->ctx, "ns2.switch.ch", HIO_DNS_RRT_A, HIO_SVC_DNC_RESOLVE_FLAG_COOKIE | HIO_SVC_DNC_RESOLVE_FLAG_DNSSEC, on_dnc_resolve, 0))
|
2020-07-04 08:03:36 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> ns2.switch.ch\n");
|
|
|
|
}
|
2020-07-02 14:21:47 +00:00
|
|
|
}
|
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
int main (int argc, char* argv[])
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-04-17 17:00:58 +00:00
|
|
|
int i;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_t* hio;
|
|
|
|
hio_dev_sck_t* tcp[3];
|
2016-04-02 15:25:35 +00:00
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
struct sigaction sigact;
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_sck_connect_t tcp_conn;
|
|
|
|
hio_dev_sck_listen_t tcp_lstn;
|
|
|
|
hio_dev_sck_bind_t tcp_bind;
|
|
|
|
hio_dev_sck_make_t tcp_make;
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
tcp_server_t* ts;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
SSL_load_error_strings ();
|
|
|
|
SSL_library_init ();
|
|
|
|
#endif
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio = hio_open(&mmgr, 0, HIO_NULL, HIO_FEATURE_ALL, 512, HIO_NULL);
|
|
|
|
if (!hio)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
printf ("Cannot open hio\n");
|
2016-01-26 16:02:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
g_hio = hio;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&sigact, 0, HIO_SIZEOF(sigact));
|
2016-01-26 16:02:20 +00:00
|
|
|
sigact.sa_flags = SA_RESTART;
|
|
|
|
sigact.sa_handler = handle_signal;
|
2021-07-22 07:30:20 +00:00
|
|
|
sigaction (SIGINT, &sigact, HIO_NULL);
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&sigact, 0, HIO_SIZEOF(sigact));
|
2016-02-02 07:51:17 +00:00
|
|
|
sigact.sa_handler = SIG_IGN;
|
2021-07-22 07:30:20 +00:00
|
|
|
sigaction (SIGPIPE, &sigact, HIO_NULL);
|
2016-01-28 17:32:58 +00:00
|
|
|
|
2016-04-16 16:05:57 +00:00
|
|
|
/*
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&sigact, 0, HIO_SIZEOF(sigact));
|
2016-04-16 16:05:57 +00:00
|
|
|
sigact.sa_handler = SIG_IGN;
|
2021-07-22 07:30:20 +00:00
|
|
|
sigaction (SIGCHLD, &sigact, HIO_NULL);
|
2016-04-16 16:05:57 +00:00
|
|
|
*/
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
/*memset (&sin, 0, HIO_SIZEOF(sin));
|
2016-01-26 16:02:20 +00:00
|
|
|
sin.sin_family = AF_INET;
|
2016-02-02 07:51:17 +00:00
|
|
|
sin.sin_port = htons(1234); */
|
2016-01-26 16:02:20 +00:00
|
|
|
/*
|
2021-07-22 07:30:20 +00:00
|
|
|
udp = (hio_dev_udp_t*)hio_dev_make(hio, HIO_SIZEOF(*udp), &udp_mth, &udp_evcb, &sin);
|
2016-01-26 16:02:20 +00:00
|
|
|
if (!udp)
|
|
|
|
{
|
|
|
|
printf ("Cannot make udp\n");
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&tcp_make, 0, HIO_SIZEOF(tcp_make));
|
|
|
|
tcp_make.type = HIO_DEV_SCK_TCP4;
|
2016-04-02 15:25:35 +00:00
|
|
|
tcp_make.on_write = tcp_sck_on_write;
|
|
|
|
tcp_make.on_read = tcp_sck_on_read;
|
2019-01-11 07:35:43 +00:00
|
|
|
tcp_make.on_connect = tcp_sck_on_connect;
|
2016-04-25 14:07:28 +00:00
|
|
|
tcp_make.on_disconnect = tcp_sck_on_disconnect;
|
2021-07-22 07:30:20 +00:00
|
|
|
tcp[0] = hio_dev_sck_make(hio, HIO_SIZEOF(tcp_server_t), &tcp_make);
|
2016-01-30 05:24:23 +00:00
|
|
|
if (!tcp[0])
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "Cannot make tcp - %js\n", hio_geterrmsg(hio));
|
2016-01-26 16:02:20 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
ts = (tcp_server_t*)(tcp[0] + 1);
|
|
|
|
ts->tally = 0;
|
2016-01-30 19:08:28 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&tcp_conn, 0, HIO_SIZEOF(tcp_conn));
|
2019-01-30 10:18:58 +00:00
|
|
|
/* openssl s_server -accept 9999 -key localhost.key -cert localhost.crt */
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_bcstrtoskad(hio, "127.0.0.1:9999", &tcp_conn.remoteaddr);
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0);
|
|
|
|
tcp_conn.options = HIO_DEV_SCK_CONNECT_SSL;
|
|
|
|
if (hio_dev_sck_connect(tcp[0], &tcp_conn) <= -1)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "tcp[0] hio_dev_sck_connect() failed - %js\n", hio_geterrmsg(hio));
|
2016-04-21 15:07:58 +00:00
|
|
|
/* carry on regardless of failure */
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
/* -------------------------------------------------------------- */
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&tcp_make, 0, HIO_SIZEOF(tcp_make));
|
|
|
|
tcp_make.type = HIO_DEV_SCK_TCP4;
|
2016-04-02 15:25:35 +00:00
|
|
|
tcp_make.on_write = tcp_sck_on_write;
|
|
|
|
tcp_make.on_read = tcp_sck_on_read;
|
2019-01-11 07:35:43 +00:00
|
|
|
tcp_make.on_connect = tcp_sck_on_connect;
|
2016-04-25 14:07:28 +00:00
|
|
|
tcp_make.on_disconnect = tcp_sck_on_disconnect;
|
2016-04-17 17:00:58 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
tcp[1] = hio_dev_sck_make(hio, HIO_SIZEOF(tcp_server_t), &tcp_make);
|
2016-01-30 05:24:23 +00:00
|
|
|
if (!tcp[1])
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "Cannot make tcp[1] - %js\n", hio_geterrmsg(hio));
|
2016-01-26 16:02:20 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2016-02-04 15:06:20 +00:00
|
|
|
ts = (tcp_server_t*)(tcp[1] + 1);
|
|
|
|
ts->tally = 0;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&tcp_bind, 0, HIO_SIZEOF(tcp_bind));
|
|
|
|
hio_bcstrtoskad(hio, "0.0.0.0:1234", &tcp_bind.localaddr);
|
|
|
|
tcp_bind.options = HIO_DEV_SCK_BIND_REUSEADDR;
|
2016-04-17 17:00:58 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_dev_sck_bind(tcp[1],&tcp_bind) <= -1)
|
2016-04-17 17:00:58 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "tcp[1] hio_dev_sck_bind() failed - %js\n", hio_geterrmsg(hio));
|
2016-04-17 17:00:58 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&tcp_lstn, 0, HIO_SIZEOF(tcp_lstn));
|
2016-04-17 17:00:58 +00:00
|
|
|
tcp_lstn.backlogs = 100;
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_dev_sck_listen(tcp[1], &tcp_lstn) <= -1)
|
2016-04-17 17:00:58 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "tcp[1] hio_dev_sck_listen() failed - %js\n", hio_geterrmsg(hio));
|
2016-04-17 17:00:58 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2020-02-11 09:02:46 +00:00
|
|
|
|
2016-04-17 17:00:58 +00:00
|
|
|
/* -------------------------------------------------------------- */
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&tcp_make, 0, HIO_SIZEOF(tcp_make));
|
|
|
|
tcp_make.type = HIO_DEV_SCK_TCP4;
|
2016-04-17 17:00:58 +00:00
|
|
|
tcp_make.on_write = tcp_sck_on_write;
|
|
|
|
tcp_make.on_read = tcp_sck_on_read;
|
2019-01-11 07:35:43 +00:00
|
|
|
tcp_make.on_connect = tcp_sck_on_connect;
|
2016-04-25 14:07:28 +00:00
|
|
|
tcp_make.on_disconnect = tcp_sck_on_disconnect;
|
2016-04-17 17:00:58 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
tcp[2] = hio_dev_sck_make(hio, HIO_SIZEOF(tcp_server_t), &tcp_make);
|
2016-04-17 17:00:58 +00:00
|
|
|
if (!tcp[2])
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "Cannot make tcp[2] - %js\n", hio_geterrmsg(hio));
|
2016-04-17 17:00:58 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
ts = (tcp_server_t*)(tcp[2] + 1);
|
|
|
|
ts->tally = 0;
|
|
|
|
|
2020-02-11 09:02:46 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&tcp_bind, 0, HIO_SIZEOF(tcp_bind));
|
|
|
|
hio_bcstrtoskad(hio, "0.0.0.0:1235", &tcp_bind.localaddr);
|
|
|
|
tcp_bind.options = HIO_DEV_SCK_BIND_REUSEADDR /*| HIO_DEV_SCK_BIND_REUSEPORT |*/;
|
2019-04-12 03:13:07 +00:00
|
|
|
#if defined(USE_SSL)
|
2021-07-22 07:30:20 +00:00
|
|
|
tcp_bind.options |= HIO_DEV_SCK_BIND_SSL;
|
2018-12-16 17:37:59 +00:00
|
|
|
tcp_bind.ssl_certfile = "localhost.crt";
|
|
|
|
tcp_bind.ssl_keyfile = "localhost.key";
|
2019-04-12 03:13:07 +00:00
|
|
|
#endif
|
2016-03-30 16:31:56 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (hio_dev_sck_bind(tcp[2], &tcp_bind) <= -1)
|
2016-03-30 16:31:56 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "tcp[2] hio_dev_sck_bind() failed - %js\n", hio_geterrmsg(hio));
|
2016-03-30 16:31:56 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
tcp_lstn.backlogs = 100;
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INIT_NTIME (&tcp_lstn.accept_tmout, 5, 1);
|
|
|
|
if (hio_dev_sck_listen(tcp[2], &tcp_lstn) <= -1)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "tcp[2] hio_dev_sck_listen() failed - %js\n", hio_geterrmsg(hio));
|
2016-01-26 16:02:20 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2016-01-26 16:07:52 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
//hio_dev_sck_sendfile (tcp[2], fd, offset, count);
|
2016-03-23 13:54:15 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
setup_arp_tester(hio);
|
|
|
|
setup_ping4_tester(hio);
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2020-05-15 06:18:49 +00:00
|
|
|
#if 0
|
2016-04-17 17:00:58 +00:00
|
|
|
for (i = 0; i < 5; i++)
|
2016-04-12 13:56:59 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_pro_t* pro;
|
|
|
|
hio_dev_pro_make_t pro_make;
|
2016-04-12 13:56:59 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
memset (&pro_make, 0, HIO_SIZEOF(pro_make));
|
|
|
|
pro_make.flags = HIO_DEV_PRO_READOUT | HIO_DEV_PRO_READERR | HIO_DEV_PRO_WRITEIN /*| HIO_DEV_PRO_FORGET_CHILD*/;
|
2016-04-12 13:56:59 +00:00
|
|
|
//pro_make.cmd = "/bin/ls -laF /usr/bin";
|
2019-01-31 09:16:44 +00:00
|
|
|
//pro_make.cmd = "/bin/ls -laF";
|
|
|
|
pro_make.cmd = "./a";
|
2016-04-12 13:56:59 +00:00
|
|
|
pro_make.on_read = pro_on_read;
|
|
|
|
pro_make.on_write = pro_on_write;
|
2016-04-16 16:05:57 +00:00
|
|
|
pro_make.on_close = pro_on_close;
|
2016-04-12 13:56:59 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
pro = hio_dev_pro_make(hio, 0, &pro_make);
|
2016-04-12 13:56:59 +00:00
|
|
|
if (!pro)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "CANNOT CREATE PROCESS PIPE - %js\n", hio_geterrmsg(hio));
|
2016-04-12 13:56:59 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_pro_read (pro, HIO_DEV_PRO_OUT, 0);
|
|
|
|
//hio_dev_pro_read (pro, HIO_DEV_PRO_ERR, 0);
|
|
|
|
|
|
|
|
hio_dev_pro_write (pro, "MY HIO LIBRARY\n", 16, HIO_NULL);
|
|
|
|
//hio_dev_pro_killchild (pro);
|
|
|
|
//hio_dev_pro_close (pro, HIO_DEV_PRO_IN);
|
|
|
|
//hio_dev_pro_close (pro, HIO_DEV_PRO_OUT);
|
|
|
|
//hio_dev_pro_close (pro, HIO_DEV_PRO_ERR);
|
2016-04-12 13:56:59 +00:00
|
|
|
}
|
2019-01-30 10:18:58 +00:00
|
|
|
#endif
|
2019-02-13 10:41:33 +00:00
|
|
|
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_svc_dnc_t* dnc;
|
|
|
|
hio_svc_htts_t* htts;
|
|
|
|
hio_ntime_t send_tmout, reply_tmout;
|
|
|
|
hio_skad_t servaddr;
|
|
|
|
hio_dev_sck_bind_t htts_bind_info;
|
2020-02-14 16:55:04 +00:00
|
|
|
|
2020-02-16 11:03:25 +00:00
|
|
|
send_tmout.sec = 0;
|
|
|
|
send_tmout.nsec = 0;
|
2020-02-14 16:55:04 +00:00
|
|
|
reply_tmout.sec = 1;
|
|
|
|
reply_tmout.nsec = 0;
|
|
|
|
|
2021-08-08 07:22:50 +00:00
|
|
|
hio_bcstrtoskad (hio, "8.8.8.8:53", &servaddr);
|
|
|
|
//hio_bcstrtoskad (hio, "198.41.0.4:53", &servaddr); // a.root-servers.net
|
2021-07-22 07:30:20 +00:00
|
|
|
//hio_bcstrtoskad (hio, "130.59.31.29:53", &servaddr); // ns2.switch.ch
|
|
|
|
//hio_bcstrtoskad (hio, "134.119.216.86:53", &servaddr); // ns.manyhost.net
|
|
|
|
//hio_bcstrtoskad (hio, "[fe80::c7e2:bd6e:1209:ac1b]:1153", &servaddr);
|
|
|
|
//hio_bcstrtoskad (hio, "[fe80::c7e2:bd6e:1209:ac1b%eno1]:1153", &servaddr);
|
|
|
|
|
|
|
|
memset (&htts_bind_info, 0, HIO_SIZEOF(htts_bind_info));
|
|
|
|
//hio_bcstrtoskad (hio, "[""]:9988", &htts_bind_info.localaddr);
|
|
|
|
hio_bcstrtoskad (hio, "0.0.0.0:9988", &htts_bind_info.localaddr);
|
|
|
|
htts_bind_info.options = HIO_DEV_SCK_BIND_REUSEADDR | HIO_DEV_SCK_BIND_REUSEPORT | HIO_DEV_SCK_BIND_IGNERR;
|
|
|
|
//htts_bind_info.options |= HIO_DEV_SCK_BIND_SSL;
|
2020-07-19 18:55:35 +00:00
|
|
|
htts_bind_info.ssl_certfile = "localhost.crt";
|
|
|
|
htts_bind_info.ssl_keyfile = "localhost.key";
|
2020-05-05 15:12:08 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
dnc = hio_svc_dnc_start(hio, &servaddr, HIO_NULL, &send_tmout, &reply_tmout, 2); /* option - send to all, send one by one */
|
2020-07-02 14:21:47 +00:00
|
|
|
if (!dnc)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_INFO1 (hio, "UNABLE TO START DNC - %js\n", hio_geterrmsg(hio));
|
2020-07-02 14:21:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-07 05:08:40 +00:00
|
|
|
htts = hio_svc_htts_start(hio, 0, &htts_bind_info, 1, process_http_request);
|
2021-07-22 07:30:20 +00:00
|
|
|
if (htts) hio_svc_htts_setservernamewithbcstr (htts, "HIO-HTTP");
|
|
|
|
else HIO_INFO1 (hio, "UNABLE TO START HTTS - %js\n", hio_geterrmsg(hio));
|
2020-05-15 06:18:49 +00:00
|
|
|
|
2020-07-02 14:21:47 +00:00
|
|
|
#if 0
|
|
|
|
if (dnc)
|
2019-02-13 10:41:33 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_bqr_t qrs[] =
|
2019-02-14 10:08:32 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
{ "code.miflux.com", HIO_DNS_RRT_A, HIO_DNS_RRC_IN },
|
|
|
|
{ "code.miflux.com", HIO_DNS_RRT_AAAA, HIO_DNS_RRC_IN },
|
|
|
|
{ "code.abiyo.net", HIO_DNS_RRT_A, HIO_DNS_RRC_IN },
|
|
|
|
{ "code6.abiyo.net", HIO_DNS_RRT_AAAA, HIO_DNS_RRC_IN },
|
|
|
|
{ "abiyo.net", HIO_DNS_RRT_MX, HIO_DNS_RRC_IN }
|
2019-02-14 10:08:32 +00:00
|
|
|
};
|
2020-02-25 07:09:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_ip4ad_t rrdata_a = { { 4, 3, 2, 1 } };
|
|
|
|
hio_ip6ad_t rrdata_aaaa = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, }};
|
2020-02-25 07:09:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_brrd_soa_t miflux_soa_data =
|
2020-03-02 04:58:22 +00:00
|
|
|
{
|
|
|
|
"ns9.dnszi.com", "root.dnszi.com", 2019091905, 43200, 3600, 1209600, 1100
|
|
|
|
};
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_brr_t rrs[] =
|
2019-02-14 10:08:32 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
{ HIO_DNS_RR_PART_ANSWER, "code.miflux.com", HIO_DNS_RRT_A, HIO_DNS_RRC_IN, 86400, HIO_SIZEOF(rrdata_a), &rrdata_a },
|
|
|
|
{ HIO_DNS_RR_PART_ANSWER, "code.miflux.com", HIO_DNS_RRT_AAAA, HIO_DNS_RRC_IN, 86400, HIO_SIZEOF(rrdata_aaaa), &rrdata_aaaa },
|
|
|
|
{ HIO_DNS_RR_PART_ANSWER, "miflux.com", HIO_DNS_RRT_NS, HIO_DNS_RRC_IN, 86400, 0, "ns1.miflux.com" },
|
|
|
|
{ HIO_DNS_RR_PART_ANSWER, "miflux.com", HIO_DNS_RRT_NS, HIO_DNS_RRC_IN, 86400, 0, "ns2.miflux.com" },
|
|
|
|
{ HIO_DNS_RR_PART_AUTHORITY, "miflux.com", HIO_DNS_RRT_SOA, HIO_DNS_RRC_IN, 86400, HIO_SIZEOF(miflux_soa_data), &miflux_soa_data }, //,
|
|
|
|
//{ HIO_DNS_RR_PART_ANSERT, "www.miflux.com", HIO_DNS_RRT_CNAME, HIO_DNS_RRC_IN, 60, 15, "code.miflux.com" }
|
2019-02-14 10:08:32 +00:00
|
|
|
};
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_beopt_t beopt[] =
|
2019-04-10 09:05:56 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
{ HIO_DNS_EOPT_COOKIE, 8, "\x01\x02\x03\x04\0x05\x06\0x07\0x08" },
|
|
|
|
{ HIO_DNS_EOPT_NSID, 0, HIO_NULL }
|
2019-04-10 09:05:56 +00:00
|
|
|
};
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_bedns_t qedns =
|
2019-04-10 09:05:56 +00:00
|
|
|
{
|
|
|
|
4096, /* uplen */
|
|
|
|
|
|
|
|
0, /* edns version */
|
|
|
|
0, /* dnssec ok */
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_COUNTOF(beopt), /* number of edns options */
|
2019-04-10 09:05:56 +00:00
|
|
|
beopt
|
|
|
|
};
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_bhdr_t qhdr =
|
2019-04-10 09:05:56 +00:00
|
|
|
{
|
|
|
|
-1, /* id */
|
|
|
|
0, /* qr */
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_DNS_OPCODE_QUERY, /* opcode */
|
2019-04-10 09:05:56 +00:00
|
|
|
0, /* aa */
|
|
|
|
0, /* tc */
|
|
|
|
1, /* rd */
|
|
|
|
0, /* ra */
|
|
|
|
0, /* ad */
|
|
|
|
0, /* cd */
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_DNS_RCODE_NOERROR /* rcode */
|
2019-04-10 09:05:56 +00:00
|
|
|
};
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dns_bhdr_t rhdr =
|
2019-04-10 09:05:56 +00:00
|
|
|
{
|
|
|
|
0x1234, /* id */
|
|
|
|
1, /* qr */
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_DNS_OPCODE_QUERY, /* opcode */
|
2019-04-10 09:05:56 +00:00
|
|
|
|
|
|
|
0, /* aa */
|
|
|
|
0, /* tc */
|
|
|
|
0, /* rd */
|
|
|
|
1, /* ra */
|
|
|
|
0, /* ad */
|
|
|
|
0, /* cd */
|
2021-07-22 07:30:20 +00:00
|
|
|
HIO_DNS_RCODE_BADCOOKIE /* rcode */
|
2019-04-10 09:05:56 +00:00
|
|
|
};
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_svc_dnc_sendreq (dnc, &qhdr, &qrs[0], &qedns, 0, HIO_NULL, 0);
|
|
|
|
hio_svc_dnc_sendmsg (dnc, &rhdr, qrs, HIO_COUNTOF(qrs), rrs, HIO_COUNTOF(rrs), &qedns, 0, HIO_NULL, 0);
|
2019-02-13 10:41:33 +00:00
|
|
|
}
|
2020-02-20 10:07:12 +00:00
|
|
|
#endif
|
|
|
|
|
2020-07-02 14:21:47 +00:00
|
|
|
if (dnc)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_ntime_t x;
|
|
|
|
HIO_INIT_NTIME (&x, 5, 0);
|
|
|
|
hio_schedtmrjobafter (hio, &x, send_test_query, HIO_NULL, dnc);
|
2020-02-25 07:09:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (!hio_svc_dnc_resolve(dnc, "b.wild.com", HIO_DNS_RRT_A, HIO_SVC_DNC_RESOLVE_FLAG_PREFER_TCP, on_dnc_resolve, 0))
|
2020-07-02 14:21:47 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> a.wild.com\n");
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (!hio_svc_dnc_resolve(dnc, "www.microsoft.com", HIO_DNS_RRT_CNAME, HIO_SVC_DNC_RESOLVE_FLAG_COOKIE, on_dnc_resolve, 0))
|
2020-07-02 14:21:47 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> www.microsoft.com\n");
|
|
|
|
}
|
2020-07-04 08:03:36 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (!hio_svc_dnc_resolve(dnc, "ns2.switch.ch", HIO_DNS_RRT_CNAME, HIO_SVC_DNC_RESOLVE_FLAG_COOKIE, on_dnc_resolve, 0))
|
2020-07-04 08:03:36 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> ns2.switch.ch\n");
|
|
|
|
}
|
2020-07-02 14:21:47 +00:00
|
|
|
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
//if (!hio_svc_dnc_resolve(dnc, "www.microsoft.com", HIO_DNS_RRT_A, HIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
|
|
|
if (!hio_svc_dnc_resolve(dnc, "code.miflux.com", HIO_DNS_RRT_A, HIO_SVC_DNC_RESOLVE_FLAG_BRIEF | HIO_SVC_DNC_RESOLVE_FLAG_PREFER_TCP, on_dnc_resolve_brief, 0))
|
2020-07-02 14:21:47 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> code.miflux.com\n");
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (!hio_svc_dnc_resolve(dnc, "45.77.246.105.in-addr.arpa", HIO_DNS_RRT_PTR, HIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
2020-07-02 14:21:47 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> 45.77.246.105.in-addr.arpa.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2021-07-22 07:30:20 +00:00
|
|
|
if (!hio_svc_dnc_resolve(dnc, "1.1.1.1.in-addr.arpa", HIO_DNS_RRT_PTR, HIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
2020-07-02 14:21:47 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> 1.1.1.1.in-addr.arpa\n");
|
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
//if (!hio_svc_dnc_resolve(dnc, "ipv6.google.com", HIO_DNS_RRT_AAAA, HIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
|
|
|
if (!hio_svc_dnc_resolve(dnc, "google.com", HIO_DNS_RRT_SOA, HIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
|
|
|
//if (!hio_svc_dnc_resolve(dnc, "google.com", HIO_DNS_RRT_NS, HIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
2020-07-02 14:21:47 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> code.miflux.com\n");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2020-02-11 09:02:46 +00:00
|
|
|
|
2020-05-24 01:18:32 +00:00
|
|
|
#if 0
|
2020-05-21 10:15:57 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_pipe_t* pp;
|
|
|
|
hio_dev_pipe_make_t mi;
|
2020-05-21 10:15:57 +00:00
|
|
|
mi.on_read = pipe_on_read;
|
|
|
|
mi.on_write = pipe_on_write;
|
|
|
|
mi.on_close = pipe_on_close;
|
2021-07-22 07:30:20 +00:00
|
|
|
pp = hio_dev_pipe_make(hio, 0, &mi);
|
|
|
|
hio_dev_pipe_write (pp, "this is good", 12, HIO_NULL);
|
2020-05-22 19:06:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 06:07:43 +00:00
|
|
|
for (i = 0; i < 20; i++)
|
2020-05-22 19:06:57 +00:00
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_dev_thr_t* tt;
|
|
|
|
hio_dev_thr_make_t mi;
|
2020-05-22 19:06:57 +00:00
|
|
|
mi.thr_func = thr_func;
|
2021-07-22 07:30:20 +00:00
|
|
|
mi.thr_ctx = HIO_NULL;
|
2020-05-22 19:06:57 +00:00
|
|
|
mi.on_read = thr_on_read;
|
|
|
|
mi.on_write = thr_on_write;
|
|
|
|
mi.on_close = thr_on_close;
|
2021-07-22 07:30:20 +00:00
|
|
|
tt = hio_dev_thr_make(hio, 0, &mi);
|
|
|
|
hio_dev_thr_write (tt, "hello, world", 12, HIO_NULL);
|
|
|
|
hio_dev_thr_write (tt, HIO_NULL, 0, HIO_NULL);
|
2020-05-21 10:15:57 +00:00
|
|
|
}
|
2020-05-24 01:18:32 +00:00
|
|
|
#endif
|
2020-05-21 10:15:57 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_loop (hio);
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
/* TODO: let hio close it ... dnc is svc. sck is dev. */
|
|
|
|
if (htts) hio_svc_htts_stop (htts);
|
|
|
|
if (dnc) hio_svc_dnc_stop (dnc);
|
2019-02-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
g_hio = HIO_NULL;
|
|
|
|
hio_close (hio);
|
2016-04-17 17:00:58 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
cleanup_openssl ();
|
|
|
|
#endif
|
2016-01-30 05:24:23 +00:00
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
oops:
|
2021-07-22 07:30:20 +00:00
|
|
|
g_hio = HIO_NULL;
|
|
|
|
hio_close (hio);
|
2016-04-17 17:00:58 +00:00
|
|
|
#if defined(USE_SSL)
|
|
|
|
cleanup_openssl ();
|
|
|
|
#endif
|
2016-01-26 16:02:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-01-11 07:35:43 +00:00
|
|
|
|