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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
#include <mio.h>
|
2019-01-24 09:53:10 +00:00
|
|
|
#include <mio-utl.h>
|
2018-12-12 13:15:54 +00:00
|
|
|
#include <mio-sck.h>
|
|
|
|
#include <mio-pro.h>
|
2019-02-13 10:41:33 +00:00
|
|
|
#include <mio-dns.h>
|
2020-02-20 06:00:53 +00:00
|
|
|
#include <mio-nwif.h>
|
2020-05-05 15:12:08 +00:00
|
|
|
#include <mio-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>
|
|
|
|
|
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
|
|
|
|
{
|
2018-12-12 14:16:49 +00:00
|
|
|
mio_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;
|
|
|
|
|
2018-12-12 14:16:49 +00:00
|
|
|
static void* mmgr_alloc (mio_mmgr_t* mmgr, mio_oow_t size)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2016-04-24 17:30:43 +00:00
|
|
|
void* x;
|
|
|
|
|
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");
|
2018-12-12 13:15:54 +00:00
|
|
|
return MIO_NULL;
|
2016-02-04 15:06:20 +00:00
|
|
|
}
|
|
|
|
|
2016-04-24 17:30:43 +00:00
|
|
|
x = malloc (size);
|
2016-02-04 15:06:20 +00:00
|
|
|
if (x) ((mmgr_stat_t*)mmgr->ctx)->total_count++;
|
|
|
|
return x;
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 14:16:49 +00:00
|
|
|
static void* mmgr_realloc (mio_mmgr_t* mmgr, void* ptr, mio_oow_t size)
|
2016-01-30 05:24:23 +00:00
|
|
|
{
|
|
|
|
return realloc (ptr, size);
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static void mmgr_free (mio_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--;
|
2016-01-26 16:02:20 +00:00
|
|
|
return free (ptr);
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static mio_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
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static void tcp_sck_on_disconnect (mio_dev_sck_t* tcp)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
switch (MIO_DEV_SCK_GET_PROGRESS(tcp))
|
2016-01-30 05:24:23 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_CONNECTING:
|
2019-01-28 17:41:50 +00:00
|
|
|
MIO_INFO1 (tcp->mio, "OUTGOING SESSION DISCONNECTED - FAILED TO CONNECT (%d) TO REMOTE SERVER\n", (int)tcp->sck);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_CONNECTING_SSL:
|
2019-01-28 17:41:50 +00:00
|
|
|
MIO_INFO1 (tcp->mio, "OUTGOING SESSION DISCONNECTED - FAILED TO SSL-CONNECT (%d) TO REMOTE SERVER\n", (int)tcp->sck);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_LISTENING:
|
2019-01-28 17:41:50 +00:00
|
|
|
MIO_INFO1 (tcp->mio, "SHUTTING DOWN THE SERVER SOCKET(%d)...\n", (int)tcp->sck);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_CONNECTED:
|
2019-01-28 17:41:50 +00:00
|
|
|
MIO_INFO1 (tcp->mio, "OUTGOING CLIENT CONNECTION GOT TORN DOWN(%d).......\n", (int)tcp->sck);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_ACCEPTING_SSL:
|
2019-01-28 17:41:50 +00:00
|
|
|
MIO_INFO1 (tcp->mio, "INCOMING SSL-ACCEPT GOT DISCONNECTED(%d) ....\n", (int)tcp->sck);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
case MIO_DEV_SCK_ACCEPTED:
|
2019-01-28 17:41:50 +00:00
|
|
|
MIO_INFO1 (tcp->mio, "INCOMING CLIENT BEING SERVED GOT DISCONNECTED(%d).......\n", (int)tcp->sck);
|
2016-04-21 13:26:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2019-01-28 17:41:50 +00:00
|
|
|
MIO_INFO2 (tcp->mio, "SOCKET DEVICE DISCONNECTED (%d - %x)\n", (int)tcp->sck, (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
|
|
|
|
2019-01-16 10:42:20 +00:00
|
|
|
static void tcp_sck_on_connect (mio_dev_sck_t* tcp)
|
|
|
|
{
|
2018-12-12 14:16:49 +00:00
|
|
|
mio_bch_t buf1[128], buf2[128];
|
2020-02-23 16:11:32 +00:00
|
|
|
mio_bch_t k[50000];
|
2020-02-22 18:24:49 +00:00
|
|
|
mio_iovec_t iov[] =
|
|
|
|
{
|
|
|
|
{ "hello", 5 },
|
|
|
|
{ "world", 5 },
|
2020-02-23 16:11:32 +00:00
|
|
|
{ k, MIO_COUNTOF(k) },
|
|
|
|
{ "mio test", 8 },
|
|
|
|
{ k, MIO_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
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
mio_skadtobcstr (tcp->mio, &tcp->localaddr, buf1, MIO_COUNTOF(buf1), MIO_SKAD_TO_BCSTR_ADDR | MIO_SKAD_TO_BCSTR_PORT);
|
|
|
|
mio_skadtobcstr (tcp->mio, &tcp->remoteaddr, buf2, MIO_COUNTOF(buf2), MIO_SKAD_TO_BCSTR_ADDR | MIO_SKAD_TO_BCSTR_PORT);
|
2016-04-18 14:21:23 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (tcp->state & MIO_DEV_SCK_CONNECTED)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2020-02-20 06:00:53 +00:00
|
|
|
MIO_INFO3 (tcp->mio, "DEVICE connected to a remote server... LOCAL %hs REMOTE %hs SCK: %d\n", buf1, buf2, tcp->sck);
|
2016-01-28 16:44:47 +00:00
|
|
|
}
|
2018-12-12 13:15:54 +00:00
|
|
|
else if (tcp->state & MIO_DEV_SCK_ACCEPTED)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2020-02-20 06:00:53 +00:00
|
|
|
MIO_INFO3 (tcp->mio, "DEVICE accepted client device... .LOCAL %hs REMOTE %hs SCK: %d\n", buf1, buf2, tcp->sck);
|
2016-01-28 16:44:47 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 16:11:32 +00:00
|
|
|
for (i = 0; i < MIO_COUNTOF(k); i++) k[i] = 'A' + (i % 26);
|
|
|
|
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
int sndbuf = 2000;
|
|
|
|
mio_dev_sck_setsockopt(tcp, SOL_SOCKET, SO_SNDBUF, &sndbuf, MIO_SIZEOF(sndbuf));
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2020-02-22 18:24:49 +00:00
|
|
|
if (mio_dev_sck_writev(tcp, iov, MIO_COUNTOF(iov), MIO_NULL, MIO_NULL) <= -1)
|
2019-01-16 10:42:20 +00:00
|
|
|
{
|
|
|
|
mio_dev_sck_halt (tcp);
|
|
|
|
}
|
2016-01-26 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
static int tcp_sck_on_write (mio_dev_sck_t* tcp, mio_iolen_t wrlen, void* wrctx, const mio_skad_t* dstaddr)
|
2016-01-28 16:44:47 +00:00
|
|
|
{
|
2016-02-04 15:06:20 +00:00
|
|
|
tcp_server_t* ts;
|
2019-01-17 08:09:19 +00:00
|
|
|
mio_ntime_t tmout;
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2019-01-28 17:41:50 +00:00
|
|
|
if (wrlen <= -1)
|
|
|
|
{
|
|
|
|
MIO_INFO1 (tcp->mio, "TCP_SCK_ON_WRITE(%d) >>> SEDING TIMED OUT...........\n", (int)tcp->sck);
|
|
|
|
mio_dev_sck_halt (tcp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ts = (tcp_server_t*)(tcp + 1);
|
|
|
|
if (wrlen == 0)
|
|
|
|
{
|
|
|
|
MIO_INFO1 (tcp->mio, "TCP_SCK_ON_WRITE(%d) >>> CLOSED WRITING END\n", (int)tcp->sck);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MIO_INFO3 (tcp->mio, "TCP_SCK_ON_WRITE(%d) >>> SENT MESSAGE %d of length %ld\n", (int)tcp->sck, ts->tally, (long int)wrlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
ts->tally++;
|
|
|
|
// if (ts->tally >= 2) mio_dev_sck_halt (tcp);
|
|
|
|
|
|
|
|
|
|
|
|
MIO_INIT_NTIME (&tmout, 5, 0);
|
|
|
|
//mio_dev_sck_read (tcp, 1);
|
|
|
|
|
|
|
|
MIO_INFO3 (tcp->mio, "TCP_SCK_ON_WRITE(%d) >>> REQUESTING to READ with timeout of %ld.%08ld\n", (int)tcp->sck, (long int)tmout.sec, (long int)tmout.nsec);
|
|
|
|
mio_dev_sck_timedread (tcp, 1, &tmout);
|
|
|
|
}
|
2016-01-28 16:44:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
static int tcp_sck_on_read (mio_dev_sck_t* tcp, const void* buf, mio_iolen_t len, const mio_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)
|
|
|
|
{
|
2019-01-28 17:41:50 +00:00
|
|
|
MIO_INFO1 (tcp->mio, "TCP_SCK_ON_READ(%d) STREAM DEVICE: TIMED OUT...\n", (int)tcp->sck);
|
2019-01-17 08:09:19 +00:00
|
|
|
mio_dev_sck_halt (tcp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (len <= 0)
|
2016-02-04 15:06:20 +00:00
|
|
|
{
|
2019-01-28 17:41:50 +00:00
|
|
|
MIO_INFO1 (tcp->mio, "TCP_SCK_ON_READ(%d) STREAM DEVICE: EOF RECEIVED...\n", (int)tcp->sck);
|
2016-02-04 15:06:20 +00:00
|
|
|
/* no outstanding request. but EOF */
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_halt (tcp);
|
2016-02-04 15:06:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-28 17:41:50 +00:00
|
|
|
MIO_INFO2 (tcp->mio, "TCP_SCK_ON_READ(%d) - received %d bytes\n", (int)tcp->sck, (int)len);
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2019-01-28 17:41:50 +00:00
|
|
|
{
|
|
|
|
mio_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];
|
|
|
|
memset (xxx, a++ , MIO_SIZEOF(xxx));
|
2016-02-04 15:06:20 +00:00
|
|
|
|
2019-01-29 08:38:12 +00:00
|
|
|
MIO_INFO2 (tcp->mio, "TCP_SCK_ON_READ(%d) >>> REQUESTING to write data of %d bytes\n", (int)tcp->sck, MIO_SIZEOF(xxx));
|
2019-01-28 17:41:50 +00:00
|
|
|
//return mio_dev_sck_write (tcp, "HELLO", 5, MIO_NULL);
|
|
|
|
MIO_INIT_NTIME (&tmout, 5, 0);
|
2019-01-29 08:38:12 +00:00
|
|
|
n = mio_dev_sck_timedwrite(tcp, xxx, MIO_SIZEOF(xxx), &tmout, MIO_NULL, MIO_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
|
|
|
|
2019-01-29 08:38:12 +00:00
|
|
|
MIO_INFO1 (tcp->mio, "TCP_SCK_ON_READ(%d) - REQUESTING TO STOP READ\n", (int)tcp->sck);
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_read (tcp, 0);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2019-01-28 17:41:50 +00:00
|
|
|
#if 0
|
|
|
|
MIO_INFO1 (tcp->mio, "TCP_SCK_ON_READ(%d) - REQUESTING TO CLOSE WRITING END\n", (int)tcp->sck);
|
2019-01-14 08:33:14 +00:00
|
|
|
/* post the write finisher - close the writing end */
|
|
|
|
n = mio_dev_sck_write(tcp, MIO_NULL, 0, MIO_NULL, MIO_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
|
|
|
/* ========================================================================= */
|
|
|
|
|
2019-01-31 09:16:44 +00:00
|
|
|
static void pro_on_close (mio_dev_pro_t* pro, mio_dev_pro_sid_t sid)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2019-01-31 09:16:44 +00:00
|
|
|
mio_t* mio = pro->mio;
|
|
|
|
if (sid == MIO_DEV_PRO_MASTER)
|
|
|
|
MIO_INFO1 (mio, "PROCESS(%d) CLOSE MASTER\n", (int)pro->child_pid);
|
|
|
|
else
|
|
|
|
MIO_INFO2 (mio, "PROCESS(%d) CLOSE SLAVE[%d]\n", (int)pro->child_pid, sid);
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-31 09:16:44 +00:00
|
|
|
static int pro_on_read (mio_dev_pro_t* pro, mio_dev_pro_sid_t sid, const void* data, mio_iolen_t dlen)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2019-01-31 09:16:44 +00:00
|
|
|
mio_t* mio = pro->mio;
|
|
|
|
|
|
|
|
if (dlen <= -1)
|
|
|
|
{
|
|
|
|
MIO_INFO1 (mio, "PROCESS(%d): READ TIMED OUT...\n", (int)pro->child_pid);
|
|
|
|
mio_dev_pro_halt (pro);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (dlen <= 0)
|
|
|
|
{
|
|
|
|
MIO_INFO1 (mio, "PROCESS(%d): EOF RECEIVED...\n", (int)pro->child_pid);
|
|
|
|
/* no outstanding request. but EOF */
|
|
|
|
mio_dev_pro_halt (pro);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MIO_INFO5 (mio, "PROCESS(%d) READ DATA ON SLAVE[%d] len=%d [%.*hs]\n", (int)pro->child_pid, (int)sid, (int)dlen, dlen, (char*)data);
|
|
|
|
if (sid == MIO_DEV_PRO_OUT)
|
|
|
|
{
|
|
|
|
mio_dev_pro_read (pro, sid, 0);
|
|
|
|
mio_dev_pro_write (pro, "HELLO\n", 6, MIO_NULL);
|
|
|
|
}
|
2016-04-25 14:07:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-31 09:16:44 +00:00
|
|
|
static int pro_on_write (mio_dev_pro_t* pro, mio_iolen_t wrlen, void* wrctx)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2019-01-31 09:16:44 +00:00
|
|
|
mio_t* mio = pro->mio;
|
2019-01-31 13:27:37 +00:00
|
|
|
mio_ntime_t tmout;
|
2019-01-31 09:16:44 +00:00
|
|
|
if (wrlen <= -1)
|
|
|
|
{
|
|
|
|
MIO_INFO1 (mio, "PROCESS(%d): WRITE TIMED OUT...\n", (int)pro->child_pid);
|
|
|
|
mio_dev_pro_halt (pro);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MIO_DEBUG2 (mio, "PROCESS(%d) wrote data of %d bytes\n", (int)pro->child_pid, (int)wrlen);
|
2019-01-31 13:27:37 +00:00
|
|
|
/*mio_dev_pro_read (pro, MIO_DEV_PRO_OUT, 1);*/
|
|
|
|
MIO_INIT_NTIME (&tmout, 5, 0);
|
|
|
|
mio_dev_pro_timedread (pro, MIO_DEV_PRO_OUT, 1, &tmout);
|
2016-04-25 14:07:28 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ========================================================================= */
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
static int arp_sck_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_skad_t* srcaddr)
|
2016-03-23 13:54:15 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_etharp_pkt_t* eap;
|
2016-04-25 16:15:36 +00:00
|
|
|
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (dlen < MIO_SIZEOF(*eap)) return 0; /* drop */
|
2016-03-30 07:06:54 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
eap = (mio_etharp_pkt_t*)data;
|
2016-04-25 16:15:36 +00:00
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
printf ("ARP ON IFINDEX %d OPCODE: %d", mio_skad_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;
|
|
|
|
}
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
static int arp_sck_on_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_skad_t* dstaddr)
|
2016-03-23 13:54:15 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-16 10:42:20 +00:00
|
|
|
static void arp_sck_on_connect (mio_dev_sck_t* dev)
|
2019-01-11 07:35:43 +00:00
|
|
|
{
|
|
|
|
printf ("STARTING UP ARP SOCKET %d...\n", dev->sck);
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static void arp_sck_on_disconnect (mio_dev_sck_t* dev)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
|
|
|
printf ("SHUTTING DOWN ARP SOCKET %d...\n", dev->sck);
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int setup_arp_tester (mio_t* mio)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2020-02-20 06:00:53 +00:00
|
|
|
mio_skad_t ethdst;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_etharp_pkt_t etharp;
|
|
|
|
mio_dev_sck_make_t sck_make;
|
|
|
|
mio_dev_sck_t* sck;
|
2020-02-20 06:00:53 +00:00
|
|
|
unsigned int ifindex;
|
2020-02-11 09:02:46 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&sck_make, 0, MIO_SIZEOF(sck_make));
|
|
|
|
sck_make.type = MIO_DEV_SCK_ARP;
|
|
|
|
//sck_make.type = MIO_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;
|
2019-02-11 09:24:16 +00:00
|
|
|
sck = mio_dev_sck_make(mio, 0, &sck_make);
|
2016-04-25 14:07:28 +00:00
|
|
|
if (!sck)
|
|
|
|
{
|
2019-04-12 03:53:32 +00:00
|
|
|
MIO_INFO1 (mio, "Cannot make arp socket device - %js\n", mio_geterrmsg(mio));
|
2016-04-25 14:07:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
//mio_bcstrtoifindex (mio, "enp0s25.3", &ifindex);
|
|
|
|
//mio_skad_init_for_eth (ðdst, ifindex, (mio_ethaddr_t*)"\xFF\xFF\xFF\xFF\xFF\xFF");
|
|
|
|
//mio_skad_init_for_eth (ðdst, ifindex, (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
|
|
|
|
mio_bcstrtoifindex (mio, "eno1", &ifindex);
|
|
|
|
mio_skad_init_for_eth (ðdst, ifindex, (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2019-01-14 08:33:14 +00:00
|
|
|
memset (ðarp, 0, MIO_SIZEOF(etharp));
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memcpy (etharp.ethhdr.source, "\xB8\x6B\x23\x9C\x10\x76", MIO_ETHADDR_LEN);
|
|
|
|
//memcpy (etharp.ethhdr.dest, "\xFF\xFF\xFF\xFF\xFF\xFF", MIO_ETHADDR_LEN);
|
|
|
|
memcpy (etharp.ethhdr.dest, "\xAA\xBB\xFF\xCC\xDD\xFF", MIO_ETHADDR_LEN);
|
|
|
|
etharp.ethhdr.proto = MIO_CONST_HTON16(MIO_ETHHDR_PROTO_ARP);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
etharp.arphdr.htype = MIO_CONST_HTON16(MIO_ARPHDR_HTYPE_ETH);
|
|
|
|
etharp.arphdr.ptype = MIO_CONST_HTON16(MIO_ARPHDR_PTYPE_IP4);
|
|
|
|
etharp.arphdr.hlen = MIO_ETHADDR_LEN;
|
|
|
|
etharp.arphdr.plen = MIO_IP4ADDR_LEN;
|
|
|
|
etharp.arphdr.opcode = MIO_CONST_HTON16(MIO_ARPHDR_OPCODE_REQUEST);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memcpy (etharp.arppld.sha, "\xB8\x6B\x23\x9C\x10\x76", MIO_ETHADDR_LEN);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2019-01-14 08:33:14 +00:00
|
|
|
if (mio_dev_sck_write(sck, ðarp, MIO_SIZEOF(etharp), MIO_NULL, ðdst) <= -1)
|
|
|
|
//if (mio_dev_sck_write (sck, ðarp.arphdr, MIO_SIZEOF(etharp) - MIO_SIZEOF(etharp.ethhdr), MIO_NULL, ðaddr) <= -1)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2019-04-12 03:53:32 +00:00
|
|
|
MIO_INFO1 (mio, "Cannot write arp - %js\n", mio_geterrmsg(mio));
|
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
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_uint16_t icmp_seq;
|
|
|
|
mio_tmridx_t tmout_jobidx;
|
2016-04-25 14:07:28 +00:00
|
|
|
int reply_received;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct icmpxtn_t icmpxtn_t;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int schedule_icmp_wait (mio_dev_sck_t* dev);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static void send_icmp (mio_dev_sck_t* dev, mio_uint16_t seq)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2019-04-12 03:53:32 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2020-02-20 06:00:53 +00:00
|
|
|
mio_skad_t dstaddr;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_icmphdr_t* icmphdr;
|
|
|
|
mio_uint8_t buf[512];
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2020-02-20 06:21:53 +00:00
|
|
|
mio_bcstrtoskad (mio, "192.168.9.1", &dstaddr);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset(buf, 0, MIO_SIZEOF(buf));
|
|
|
|
icmphdr = (mio_icmphdr_t*)buf;
|
|
|
|
icmphdr->type = MIO_ICMP_ECHO_REQUEST;
|
|
|
|
icmphdr->u.echo.id = MIO_CONST_HTON16(100);
|
|
|
|
icmphdr->u.echo.seq = mio_hton16(seq);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&buf[MIO_SIZEOF(*icmphdr)], 'A', MIO_SIZEOF(buf) - MIO_SIZEOF(*icmphdr));
|
|
|
|
icmphdr->checksum = mio_checksumip (icmphdr, MIO_SIZEOF(buf));
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2019-01-16 04:37:53 +00:00
|
|
|
if (mio_dev_sck_write(dev, buf, MIO_SIZEOF(buf), MIO_NULL, &dstaddr) <= -1)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2019-04-12 03:53:32 +00:00
|
|
|
MIO_INFO1 (mio, "Cannot write icmp - %js\n", mio_geterrmsg(mio));
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_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
|
|
|
{
|
2019-04-12 03:53:32 +00:00
|
|
|
MIO_INFO1 (mio, "Cannot schedule icmp wait - %js\n", mio_geterrmsg(mio));
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_halt (dev);
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
2016-04-16 16:05:57 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static void on_icmp_due (mio_t* mio, const mio_ntime_t* now, mio_tmrjob_t* tmrjob)
|
2016-04-16 16:05:57 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_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
|
2019-04-12 03:53:32 +00:00
|
|
|
MIO_INFO0 (mio, "NO IMCP reply received in time\n");
|
2016-04-25 14:07:28 +00:00
|
|
|
|
|
|
|
send_icmp (dev, ++icmpxtn->icmp_seq);
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int schedule_icmp_wait (mio_dev_sck_t* dev)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2019-01-30 10:18:58 +00:00
|
|
|
mio_t* mio = dev->mio;
|
2016-04-25 14:07:28 +00:00
|
|
|
icmpxtn_t* icmpxtn;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_tmrjob_t tmrjob;
|
|
|
|
mio_ntime_t fire_after;
|
2016-04-25 14:07:28 +00:00
|
|
|
|
|
|
|
icmpxtn = (icmpxtn_t*)(dev + 1);
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_INIT_NTIME (&fire_after, 2, 0);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&tmrjob, 0, MIO_SIZEOF(tmrjob));
|
2016-04-25 14:07:28 +00:00
|
|
|
tmrjob.ctx = dev;
|
2019-01-30 10:18:58 +00:00
|
|
|
mio_gettime (mio, &tmrjob.when);
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_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;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
assert (icmpxtn->tmout_jobidx == MIO_TMRIDX_INVALID);
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2019-01-17 08:51:10 +00:00
|
|
|
return (mio_instmrjob(dev->mio, &tmrjob) == MIO_TMRIDX_INVALID)? -1: 0;
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
static int icmp_sck_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_skad_t* srcaddr)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
|
|
|
icmpxtn_t* icmpxtn;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_iphdr_t* iphdr;
|
|
|
|
mio_icmphdr_t* icmphdr;
|
2016-04-25 14:07:28 +00:00
|
|
|
|
|
|
|
/* when received, the data contains the IP header.. */
|
|
|
|
icmpxtn = (icmpxtn_t*)(dev + 1);
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (dlen < MIO_SIZEOF(*iphdr) + MIO_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... */
|
2018-12-12 13:15:54 +00:00
|
|
|
iphdr = (mio_iphdr_t*)data;
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (iphdr->ihl * 4 + MIO_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
|
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
icmphdr = (mio_icmphdr_t*)((mio_uint8_t*)data + (iphdr->ihl * 4));
|
2016-04-26 05:54:48 +00:00
|
|
|
|
|
|
|
/* TODO: check srcaddr against target */
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
if (icmphdr->type == MIO_ICMP_ECHO_REPLY &&
|
|
|
|
mio_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;
|
2018-12-12 13:15:54 +00:00
|
|
|
printf ("ICMP REPLY RECEIVED...ID %d SEQ %d\n", (int)mio_ntoh16(icmphdr->u.echo.id), (int)mio_ntoh16(icmphdr->u.echo.seq));
|
2016-04-26 05:54:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
printf ("GARBAGE ICMP PACKET...LEN %d SEQ %d,%d\n", (int)dlen, (int)icmpxtn->icmp_seq, (int)mio_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-02-20 06:00:53 +00:00
|
|
|
static int icmp_sck_on_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_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;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static void icmp_sck_on_disconnect (mio_dev_sck_t* dev)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
|
|
|
icmpxtn_t* icmpxtn;
|
|
|
|
|
|
|
|
icmpxtn = (icmpxtn_t*)(dev + 1);
|
|
|
|
|
|
|
|
printf ("SHUTTING DOWN ICMP SOCKET %d...\n", dev->sck);
|
2018-12-12 13:15:54 +00:00
|
|
|
if (icmpxtn->tmout_jobidx != MIO_TMRIDX_INVALID)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_deltmrjob (dev->mio, icmpxtn->tmout_jobidx);
|
|
|
|
icmpxtn->tmout_jobidx = MIO_TMRIDX_INVALID;
|
2016-04-25 14:07:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static int setup_ping4_tester (mio_t* mio)
|
2016-04-25 14:07:28 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_make_t sck_make;
|
|
|
|
mio_dev_sck_t* sck;
|
2016-04-25 14:07:28 +00:00
|
|
|
icmpxtn_t* icmpxtn;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&sck_make, 0, MIO_SIZEOF(sck_make));
|
|
|
|
sck_make.type = MIO_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;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
sck = mio_dev_sck_make (mio, MIO_SIZEOF(icmpxtn_t), &sck_make);
|
2016-04-25 14:07:28 +00:00
|
|
|
if (!sck)
|
|
|
|
{
|
2019-04-12 03:53:32 +00:00
|
|
|
MIO_INFO1 (mio, "Cannot make ICMP4 socket device - %js\n", mio_geterrmsg(mio));
|
2016-04-25 14:07:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
icmpxtn = (icmpxtn_t*)(sck + 1);
|
2018-12-12 13:15:54 +00:00
|
|
|
icmpxtn->tmout_jobidx = MIO_TMRIDX_INVALID;
|
2016-04-25 14:07:28 +00:00
|
|
|
icmpxtn->icmp_seq = 0;
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
/*TODO: mio_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-02-11 09:02:46 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
|
|
|
static void on_dnc_resolve(mio_svc_dnc_t* dnc, mio_dns_msg_t* reqmsg, mio_errnum_t status, const void* data, mio_oow_t dlen)
|
|
|
|
{
|
2020-02-13 09:31:02 +00:00
|
|
|
mio_dns_pkt_info_t* pi = MIO_NULL;
|
|
|
|
|
2020-02-14 16:55:04 +00:00
|
|
|
if (data) // status == MIO_ENOERR
|
2020-02-11 09:02:46 +00:00
|
|
|
{
|
2020-02-13 09:31:02 +00:00
|
|
|
mio_uint32_t i;
|
|
|
|
|
|
|
|
pi = mio_dns_make_packet_info(mio_svc_dnc_getmio(dnc), data, dlen);
|
2020-02-27 17:34:27 +00:00
|
|
|
if (!pi) goto no_data;
|
2020-02-12 10:06:05 +00:00
|
|
|
|
2020-02-27 17:34:27 +00:00
|
|
|
if (pi->hdr.rcode != MIO_DNS_RCODE_NOERROR) goto no_data;
|
2020-02-12 10:06:05 +00:00
|
|
|
|
2020-02-27 17:34:27 +00:00
|
|
|
if (pi->ancount < 0) goto no_data;
|
2020-02-14 10:06:29 +00:00
|
|
|
|
|
|
|
printf (">>>>>>>> RRDLEN = %d\n", (int)pi->_rrdlen);
|
|
|
|
printf (">>>>>>>> RCODE %d EDNS exist %d uplen %d version %d dnssecok %d\n", pi->hdr.rcode, pi->edns.exist, pi->edns.uplen, pi->edns.version, pi->edns.dnssecok);
|
|
|
|
|
2020-02-13 09:31:02 +00:00
|
|
|
for (i = 0; i < pi->ancount; i++)
|
|
|
|
{
|
2020-02-14 10:06:29 +00:00
|
|
|
mio_dns_brr_t* brr = &pi->rr.an[i];
|
|
|
|
switch (pi->rr.an[i].rrtype)
|
2020-02-13 09:31:02 +00:00
|
|
|
{
|
2020-02-14 10:06:29 +00:00
|
|
|
case MIO_DNS_RRT_A:
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
case MIO_DNS_RRT_AAAA:
|
|
|
|
{
|
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);
|
|
|
|
printf ("[%s]", inet_ntop(AF_INET6, &ia, buf, MIO_COUNTOF(buf)));
|
2020-02-14 10:06:29 +00:00
|
|
|
printf ("\n");
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
case MIO_DNS_RRT_CNAME:
|
|
|
|
printf ("^^^ GOT REPLY.... CNAME [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
goto done;
|
2020-02-24 16:39:29 +00:00
|
|
|
case MIO_DNS_RRT_MX:
|
|
|
|
printf ("^^^ GOT REPLY.... MX [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
goto done;
|
2020-02-20 10:07:12 +00:00
|
|
|
case MIO_DNS_RRT_NS:
|
|
|
|
printf ("^^^ GOT REPLY.... NS [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
goto done;
|
|
|
|
case MIO_DNS_RRT_PTR:
|
|
|
|
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:
|
2020-02-14 16:55:04 +00:00
|
|
|
if (status == MIO_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:
|
|
|
|
if (pi) mio_dns_free_packet_info(mio_svc_dnc_getmio(dnc), pi);
|
2020-02-11 09:02:46 +00:00
|
|
|
}
|
2016-04-25 14:07:28 +00:00
|
|
|
|
2020-02-14 10:06:29 +00:00
|
|
|
static void on_dnc_resolve_brief (mio_svc_dnc_t* dnc, mio_dns_msg_t* reqmsg, mio_errnum_t status, const void* data, mio_oow_t dlen)
|
|
|
|
{
|
2020-02-16 13:59:41 +00:00
|
|
|
if (data) /* status must be MIO_ENOERR */
|
2020-02-14 10:06:29 +00:00
|
|
|
{
|
|
|
|
mio_dns_brr_t* brr = (mio_dns_brr_t*)data;
|
|
|
|
|
|
|
|
if (brr->rrtype == MIO_DNS_RRT_AAAA)
|
|
|
|
{
|
|
|
|
struct in6_addr ia;
|
|
|
|
char buf[128];
|
|
|
|
memcpy (&ia.s6_addr, brr->dptr, brr->dlen);
|
|
|
|
printf ("^^^ SIMPLE -> GOT REPLY........................ %d ", brr->dlen);
|
|
|
|
printf ("[%s]", inet_ntop(AF_INET6, &ia, buf, MIO_COUNTOF(buf)));
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
else if (brr->rrtype == MIO_DNS_RRT_A)
|
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
else if (brr->rrtype == MIO_DNS_RRT_CNAME)
|
|
|
|
{
|
|
|
|
printf ("^^^ SIMPLE -> CNAME [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
}
|
2020-02-17 14:54:21 +00:00
|
|
|
else if (brr->rrtype == MIO_DNS_RRT_NS)
|
|
|
|
{
|
|
|
|
printf ("^^^ SIMPLE -> NS [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
}
|
2020-02-19 14:59:06 +00:00
|
|
|
else if (brr->rrtype == MIO_DNS_RRT_PTR)
|
|
|
|
{
|
|
|
|
printf ("^^^ SIMPLE -> PTR [%s] %d\n", brr->dptr, (int)brr->dlen);
|
|
|
|
}
|
2020-02-17 14:54:21 +00:00
|
|
|
else if (brr->rrtype == MIO_DNS_RRT_SOA)
|
|
|
|
{
|
|
|
|
mio_dns_brrd_soa_t* soa = brr->dptr;
|
|
|
|
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
|
|
|
|
{
|
2020-02-14 16:55:04 +00:00
|
|
|
if (status == MIO_ETMOUT) printf ("QQQQQQQQQQQQQQQQQQQ TIMED OUT QQQQQQQQQQQQQQQQQ\n");
|
2020-02-27 17:34:27 +00:00
|
|
|
else printf ("QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ NO REPLY DATA QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQq - %d\n", mio_geterrnum(mio_svc_dnc_getmio(dnc)));
|
2020-02-14 10:06:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 13:54:15 +00:00
|
|
|
/* ========================================================================= */
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
static mio_t* g_mio;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
|
|
|
static void handle_signal (int sig)
|
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
if (g_mio) mio_stop (g_mio, MIO_STOPREQ_TERMINATION);
|
2016-01-26 16:02:20 +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
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_t* mio;
|
|
|
|
mio_dev_sck_t* tcp[3];
|
2016-04-02 15:25:35 +00:00
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
struct sigaction sigact;
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_sck_connect_t tcp_conn;
|
|
|
|
mio_dev_sck_listen_t tcp_lstn;
|
|
|
|
mio_dev_sck_bind_t tcp_bind;
|
|
|
|
mio_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
|
|
|
|
|
2019-01-30 10:18:58 +00:00
|
|
|
mio = mio_open(&mmgr, 0, MIO_NULL, 512, MIO_NULL);
|
2018-12-12 13:15:54 +00:00
|
|
|
if (!mio)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
printf ("Cannot open mio\n");
|
2016-01-26 16:02:20 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
g_mio = mio;
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&sigact, 0, MIO_SIZEOF(sigact));
|
2016-01-26 16:02:20 +00:00
|
|
|
sigact.sa_flags = SA_RESTART;
|
|
|
|
sigact.sa_handler = handle_signal;
|
2018-12-12 13:15:54 +00:00
|
|
|
sigaction (SIGINT, &sigact, MIO_NULL);
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&sigact, 0, MIO_SIZEOF(sigact));
|
2016-02-02 07:51:17 +00:00
|
|
|
sigact.sa_handler = SIG_IGN;
|
2018-12-12 13:15:54 +00:00
|
|
|
sigaction (SIGPIPE, &sigact, MIO_NULL);
|
2016-01-28 17:32:58 +00:00
|
|
|
|
2016-04-16 16:05:57 +00:00
|
|
|
/*
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&sigact, 0, MIO_SIZEOF(sigact));
|
2016-04-16 16:05:57 +00:00
|
|
|
sigact.sa_handler = SIG_IGN;
|
2018-12-12 13:15:54 +00:00
|
|
|
sigaction (SIGCHLD, &sigact, MIO_NULL);
|
2016-04-16 16:05:57 +00:00
|
|
|
*/
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
/*memset (&sin, 0, MIO_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
|
|
|
/*
|
2019-02-22 08:44:51 +00:00
|
|
|
udp = (mio_dev_udp_t*)mio_dev_make(mio, MIO_SIZEOF(*udp), &udp_mth, &udp_evcb, &sin);
|
2016-01-26 16:02:20 +00:00
|
|
|
if (!udp)
|
|
|
|
{
|
|
|
|
printf ("Cannot make udp\n");
|
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2019-04-10 09:25:34 +00:00
|
|
|
memset (&tcp_make, 0, MIO_SIZEOF(tcp_make));
|
2018-12-12 13:15:54 +00:00
|
|
|
tcp_make.type = MIO_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;
|
2019-01-11 07:35:43 +00:00
|
|
|
tcp[0] = mio_dev_sck_make(mio, MIO_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
|
|
|
{
|
2019-04-12 02:45:35 +00:00
|
|
|
MIO_INFO1 (mio, "Cannot make tcp - %js\n", mio_geterrmsg(mio));
|
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
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn));
|
2019-01-30 10:18:58 +00:00
|
|
|
/* openssl s_server -accept 9999 -key localhost.key -cert localhost.crt */
|
2020-02-20 06:21:53 +00:00
|
|
|
mio_bcstrtoskad(mio, "127.0.0.1:9999", &tcp_conn.remoteaddr);
|
2016-04-21 13:26:14 +00:00
|
|
|
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0);
|
2018-12-12 13:15:54 +00:00
|
|
|
tcp_conn.options = MIO_DEV_SCK_CONNECT_SSL;
|
2019-01-17 08:09:19 +00:00
|
|
|
if (mio_dev_sck_connect(tcp[0], &tcp_conn) <= -1)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2019-04-12 02:45:35 +00:00
|
|
|
MIO_INFO1 (mio, "tcp[0] mio_dev_sck_connect() failed - %js\n", mio_geterrmsg(mio));
|
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
|
|
|
/* -------------------------------------------------------------- */
|
2019-04-10 09:25:34 +00:00
|
|
|
memset (&tcp_make, 0, MIO_SIZEOF(tcp_make));
|
2018-12-12 13:15:54 +00:00
|
|
|
tcp_make.type = MIO_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
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
tcp[1] = mio_dev_sck_make(mio, MIO_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
|
|
|
{
|
2019-04-12 02:45:35 +00:00
|
|
|
MIO_INFO1 (mio, "Cannot make tcp[1] - %js\n", mio_geterrmsg(mio));
|
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
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&tcp_bind, 0, MIO_SIZEOF(tcp_bind));
|
2020-02-20 06:21:53 +00:00
|
|
|
mio_bcstrtoskad(mio, "0.0.0.0:1234", &tcp_bind.localaddr);
|
2018-12-12 13:15:54 +00:00
|
|
|
tcp_bind.options = MIO_DEV_SCK_BIND_REUSEADDR;
|
2016-04-17 17:00:58 +00:00
|
|
|
|
2019-01-17 08:09:19 +00:00
|
|
|
if (mio_dev_sck_bind(tcp[1],&tcp_bind) <= -1)
|
2016-04-17 17:00:58 +00:00
|
|
|
{
|
2019-04-12 02:45:35 +00:00
|
|
|
MIO_INFO1 (mio, "tcp[1] mio_dev_sck_bind() failed - %js\n", mio_geterrmsg(mio));
|
2016-04-17 17:00:58 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tcp_lstn.backlogs = 100;
|
2019-01-17 08:09:19 +00:00
|
|
|
if (mio_dev_sck_listen(tcp[1], &tcp_lstn) <= -1)
|
2016-04-17 17:00:58 +00:00
|
|
|
{
|
2019-04-12 02:45:35 +00:00
|
|
|
MIO_INFO1 (mio, "tcp[1] mio_dev_sck_listen() failed - %js\n", mio_geterrmsg(mio));
|
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
|
|
|
/* -------------------------------------------------------------- */
|
2019-04-10 09:25:34 +00:00
|
|
|
memset (&tcp_make, 0, MIO_SIZEOF(tcp_make));
|
2018-12-12 13:15:54 +00:00
|
|
|
tcp_make.type = MIO_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
|
|
|
|
2019-01-17 08:09:19 +00:00
|
|
|
tcp[2] = mio_dev_sck_make(mio, MIO_SIZEOF(tcp_server_t), &tcp_make);
|
2016-04-17 17:00:58 +00:00
|
|
|
if (!tcp[2])
|
|
|
|
{
|
2019-04-12 02:45:35 +00:00
|
|
|
MIO_INFO1 (mio, "Cannot make tcp[2] - %js\n", mio_geterrmsg(mio));
|
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
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&tcp_bind, 0, MIO_SIZEOF(tcp_bind));
|
2020-02-20 06:21:53 +00:00
|
|
|
mio_bcstrtoskad(mio, "0.0.0.0:1235", &tcp_bind.localaddr);
|
2019-04-12 03:13:07 +00:00
|
|
|
tcp_bind.options = MIO_DEV_SCK_BIND_REUSEADDR /*| MIO_DEV_SCK_BIND_REUSEPORT |*/;
|
|
|
|
#if defined(USE_SSL)
|
|
|
|
tcp_bind.options |= MIO_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
|
2019-01-27 02:09:22 +00:00
|
|
|
MIO_INIT_NTIME (&tcp_bind.accept_tmout, 5, 1);
|
2016-03-30 16:31:56 +00:00
|
|
|
|
2019-01-11 07:35:43 +00:00
|
|
|
if (mio_dev_sck_bind(tcp[2], &tcp_bind) <= -1)
|
2016-03-30 16:31:56 +00:00
|
|
|
{
|
2019-02-22 10:13:35 +00:00
|
|
|
MIO_INFO1 (mio, "tcp[2] mio_dev_sck_bind() failed - %js\n", mio_geterrmsg(mio));
|
2016-03-30 16:31:56 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
|
|
|
|
2016-01-26 16:02:20 +00:00
|
|
|
tcp_lstn.backlogs = 100;
|
2019-01-11 07:35:43 +00:00
|
|
|
if (mio_dev_sck_listen(tcp[2], &tcp_lstn) <= -1)
|
2016-01-26 16:02:20 +00:00
|
|
|
{
|
2019-02-22 10:13:35 +00:00
|
|
|
MIO_INFO1 (mio, "tcp[2] mio_dev_sck_listen() failed - %js\n", mio_geterrmsg(mio));
|
2016-01-26 16:02:20 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2016-01-26 16:07:52 +00:00
|
|
|
|
2020-02-11 09:02:46 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
//mio_dev_sck_sendfile (tcp[2], fd, offset, count);
|
2016-03-23 13:54:15 +00:00
|
|
|
|
2019-04-12 03:53:32 +00:00
|
|
|
setup_arp_tester(mio);
|
|
|
|
setup_ping4_tester(mio);
|
2016-03-29 15:02:01 +00:00
|
|
|
|
2019-01-30 10:18:58 +00:00
|
|
|
#if 1
|
2016-04-17 17:00:58 +00:00
|
|
|
for (i = 0; i < 5; i++)
|
2016-04-12 13:56:59 +00:00
|
|
|
{
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_pro_t* pro;
|
|
|
|
mio_dev_pro_make_t pro_make;
|
2016-04-12 13:56:59 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
memset (&pro_make, 0, MIO_SIZEOF(pro_make));
|
|
|
|
pro_make.flags = MIO_DEV_PRO_READOUT | MIO_DEV_PRO_READERR | MIO_DEV_PRO_WRITEIN /*| MIO_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
|
|
|
|
2019-01-17 08:09:19 +00:00
|
|
|
pro = mio_dev_pro_make(mio, 0, &pro_make);
|
2016-04-12 13:56:59 +00:00
|
|
|
if (!pro)
|
|
|
|
{
|
2019-01-30 10:18:58 +00:00
|
|
|
MIO_INFO1 (mio, "CANNOT CREATE PROCESS PIPE - %js\n", mio_geterrmsg(mio));
|
2016-04-12 13:56:59 +00:00
|
|
|
goto oops;
|
|
|
|
}
|
2019-01-31 09:16:44 +00:00
|
|
|
mio_dev_pro_read (pro, MIO_DEV_PRO_OUT, 0);
|
|
|
|
//mio_dev_pro_read (pro, MIO_DEV_PRO_ERR, 0);
|
2016-04-16 16:05:57 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_dev_pro_write (pro, "MY MIO LIBRARY\n", 16, MIO_NULL);
|
|
|
|
//mio_dev_pro_killchild (pro);
|
|
|
|
//mio_dev_pro_close (pro, MIO_DEV_PRO_IN);
|
|
|
|
//mio_dev_pro_close (pro, MIO_DEV_PRO_OUT);
|
|
|
|
//mio_dev_pro_close (pro, MIO_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
|
|
|
|
|
|
|
{
|
2020-02-11 09:02:46 +00:00
|
|
|
mio_svc_dnc_t* dnc;
|
2020-05-05 15:12:08 +00:00
|
|
|
mio_svc_htts_t* htts;
|
2020-02-16 11:03:25 +00:00
|
|
|
mio_ntime_t send_tmout, reply_tmout;
|
2020-02-20 06:00:53 +00:00
|
|
|
mio_skad_t servaddr;
|
2020-05-05 15:12:08 +00:00
|
|
|
mio_skad_t htts_bind_addr;
|
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;
|
|
|
|
|
2020-02-24 17:14:21 +00:00
|
|
|
mio_bcstrtoskad (mio, "8.8.8.8:53", &servaddr);
|
2020-02-21 09:42:10 +00:00
|
|
|
//mio_bcstrtoskad (mio, "[fe80::c7e2:bd6e:1209:ac1b]:1153", &servaddr);
|
2020-02-24 17:14:21 +00:00
|
|
|
//mio_bcstrtoskad (mio, "[fe80::c7e2:bd6e:1209:ac1b%eno1]:1153", &servaddr);
|
2020-05-05 15:12:08 +00:00
|
|
|
|
|
|
|
//mio_bcstrtoskad (mio, "[::]:9988", &htts_bind_addr);
|
|
|
|
mio_bcstrtoskad (mio, "127.0.0.1:9988", &htts_bind_addr);
|
|
|
|
|
|
|
|
dnc = mio_svc_dnc_start(mio, &servaddr, MIO_NULL, &send_tmout, &reply_tmout, 2); /* option - send to all, send one by one */
|
|
|
|
htts = mio_svc_htts_start(mio, &htts_bind_addr);
|
2020-02-20 10:07:12 +00:00
|
|
|
|
2020-03-02 04:58:22 +00:00
|
|
|
#if 1
|
2019-02-13 10:41:33 +00:00
|
|
|
{
|
2019-02-16 18:22:32 +00:00
|
|
|
mio_dns_bqr_t qrs[] =
|
2019-02-14 10:08:32 +00:00
|
|
|
{
|
2020-02-13 09:31:02 +00:00
|
|
|
{ "code.miflux.com", MIO_DNS_RRT_A, MIO_DNS_RRC_IN },
|
|
|
|
{ "code.miflux.com", MIO_DNS_RRT_AAAA, MIO_DNS_RRC_IN },
|
|
|
|
{ "code.abiyo.net", MIO_DNS_RRT_A, MIO_DNS_RRC_IN },
|
|
|
|
{ "code6.abiyo.net", MIO_DNS_RRT_AAAA, MIO_DNS_RRC_IN },
|
|
|
|
{ "abiyo.net", MIO_DNS_RRT_MX, MIO_DNS_RRC_IN }
|
2019-02-14 10:08:32 +00:00
|
|
|
};
|
2020-02-25 07:09:20 +00:00
|
|
|
|
|
|
|
mio_ip4ad_t rrdata_a = { { 4, 3, 2, 1 } };
|
|
|
|
mio_ip6ad_t rrdata_aaaa = { { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, }};
|
|
|
|
|
2020-03-02 04:58:22 +00:00
|
|
|
mio_dns_brrd_soa_t miflux_soa_data =
|
|
|
|
{
|
|
|
|
"ns9.dnszi.com", "root.dnszi.com", 2019091905, 43200, 3600, 1209600, 1100
|
|
|
|
};
|
|
|
|
|
2019-02-16 18:22:32 +00:00
|
|
|
mio_dns_brr_t rrs[] =
|
2019-02-14 10:08:32 +00:00
|
|
|
{
|
2020-02-25 07:09:20 +00:00
|
|
|
{ MIO_DNS_RR_PART_ANSWER, "code.miflux.com", MIO_DNS_RRT_A, MIO_DNS_RRC_IN, 86400, MIO_SIZEOF(rrdata_a), &rrdata_a },
|
|
|
|
{ MIO_DNS_RR_PART_ANSWER, "code.miflux.com", MIO_DNS_RRT_AAAA, MIO_DNS_RRC_IN, 86400, MIO_SIZEOF(rrdata_aaaa), &rrdata_aaaa },
|
2020-02-24 16:39:29 +00:00
|
|
|
{ MIO_DNS_RR_PART_ANSWER, "miflux.com", MIO_DNS_RRT_NS, MIO_DNS_RRC_IN, 86400, 0, "ns1.miflux.com" },
|
2020-03-02 04:58:22 +00:00
|
|
|
{ MIO_DNS_RR_PART_ANSWER, "miflux.com", MIO_DNS_RRT_NS, MIO_DNS_RRC_IN, 86400, 0, "ns2.miflux.com" },
|
|
|
|
{ MIO_DNS_RR_PART_AUTHORITY, "miflux.com", MIO_DNS_RRT_SOA, MIO_DNS_RRC_IN, 86400, MIO_SIZEOF(miflux_soa_data), &miflux_soa_data }, //,
|
2020-02-13 09:31:02 +00:00
|
|
|
//{ MIO_DNS_RR_PART_ANSERT, "www.miflux.com", MIO_DNS_RRT_CNAME, MIO_DNS_RRC_IN, 60, 15, "code.miflux.com" }
|
2019-02-14 10:08:32 +00:00
|
|
|
};
|
|
|
|
|
2019-04-10 09:05:56 +00:00
|
|
|
mio_dns_beopt_t beopt[] =
|
|
|
|
{
|
|
|
|
{ MIO_DNS_EOPT_COOKIE, 8, "\x01\x02\x03\x04\0x05\x06\0x07\0x08" },
|
|
|
|
{ MIO_DNS_EOPT_NSID, 0, MIO_NULL }
|
|
|
|
};
|
|
|
|
|
|
|
|
mio_dns_bedns_t qedns =
|
|
|
|
{
|
|
|
|
4096, /* uplen */
|
|
|
|
|
|
|
|
0, /* edns version */
|
|
|
|
0, /* dnssec ok */
|
|
|
|
|
|
|
|
MIO_COUNTOF(beopt), /* number of edns options */
|
|
|
|
beopt
|
|
|
|
};
|
|
|
|
|
2020-02-13 09:31:02 +00:00
|
|
|
mio_dns_bhdr_t qhdr =
|
2019-04-10 09:05:56 +00:00
|
|
|
{
|
|
|
|
-1, /* id */
|
|
|
|
0, /* qr */
|
|
|
|
MIO_DNS_OPCODE_QUERY, /* opcode */
|
|
|
|
0, /* aa */
|
|
|
|
0, /* tc */
|
|
|
|
1, /* rd */
|
|
|
|
0, /* ra */
|
|
|
|
0, /* ad */
|
|
|
|
0, /* cd */
|
|
|
|
MIO_DNS_RCODE_NOERROR /* rcode */
|
|
|
|
};
|
|
|
|
|
2020-02-13 09:31:02 +00:00
|
|
|
mio_dns_bhdr_t rhdr =
|
2019-04-10 09:05:56 +00:00
|
|
|
{
|
|
|
|
0x1234, /* id */
|
|
|
|
1, /* qr */
|
|
|
|
MIO_DNS_OPCODE_QUERY, /* opcode */
|
|
|
|
|
|
|
|
0, /* aa */
|
|
|
|
0, /* tc */
|
|
|
|
0, /* rd */
|
|
|
|
1, /* ra */
|
|
|
|
0, /* ad */
|
|
|
|
0, /* cd */
|
|
|
|
MIO_DNS_RCODE_BADCOOKIE /* rcode */
|
|
|
|
};
|
|
|
|
|
2020-03-01 17:43:42 +00:00
|
|
|
mio_svc_dnc_sendreq (dnc, &qhdr, &qrs[0], &qedns, 0, MIO_NULL, 0);
|
|
|
|
mio_svc_dnc_sendmsg (dnc, &rhdr, qrs, MIO_COUNTOF(qrs), rrs, MIO_COUNTOF(rrs), &qedns, 0, MIO_NULL, 0);
|
2019-02-13 10:41:33 +00:00
|
|
|
}
|
2020-02-20 10:07:12 +00:00
|
|
|
#endif
|
|
|
|
|
2020-02-25 07:09:20 +00:00
|
|
|
|
2020-03-01 17:43:42 +00:00
|
|
|
if (!mio_svc_dnc_resolve(dnc, "b.wild.com", MIO_DNS_RRT_A, MIO_SVC_DNC_RESOLVE_FLAG_PREFER_TCP, on_dnc_resolve, 0))
|
2020-02-20 10:07:12 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> a.wild.com\n");
|
|
|
|
}
|
2019-02-13 10:41:33 +00:00
|
|
|
|
2020-02-14 10:06:29 +00:00
|
|
|
if (!mio_svc_dnc_resolve(dnc, "www.microsoft.com", MIO_DNS_RRT_CNAME, 0, on_dnc_resolve, 0))
|
|
|
|
{
|
2020-02-27 17:34:27 +00:00
|
|
|
printf ("resolve attempt failure ---> www.microsoft.com\n");
|
2020-02-14 10:06:29 +00:00
|
|
|
}
|
2020-02-27 17:34:27 +00:00
|
|
|
|
2020-03-01 17:43:42 +00:00
|
|
|
|
2020-02-14 10:06:29 +00:00
|
|
|
//if (!mio_svc_dnc_resolve(dnc, "www.microsoft.com", MIO_DNS_RRT_A, MIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
2020-03-01 17:43:42 +00:00
|
|
|
if (!mio_svc_dnc_resolve(dnc, "code.miflux.com", MIO_DNS_RRT_A, MIO_SVC_DNC_RESOLVE_FLAG_BRIEF | MIO_SVC_DNC_RESOLVE_FLAG_PREFER_TCP, on_dnc_resolve_brief, 0))
|
2020-02-14 10:06:29 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> code.miflux.com\n");
|
|
|
|
}
|
|
|
|
|
2020-03-02 05:16:37 +00:00
|
|
|
if (!mio_svc_dnc_resolve(dnc, "45.77.246.105.in-addr.arpa", MIO_DNS_RRT_PTR, MIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> 45.77.246.105.in-addr.arpa.\n");
|
|
|
|
}
|
|
|
|
|
2020-03-01 17:43:42 +00:00
|
|
|
#if 0
|
2020-02-19 14:59:06 +00:00
|
|
|
if (!mio_svc_dnc_resolve(dnc, "1.1.1.1.in-addr.arpa", MIO_DNS_RRT_PTR, MIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> 1.1.1.1.in-addr.arpa\n");
|
|
|
|
}
|
|
|
|
|
2020-02-17 14:54:21 +00:00
|
|
|
//if (!mio_svc_dnc_resolve(dnc, "ipv6.google.com", MIO_DNS_RRT_AAAA, MIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
|
|
|
if (!mio_svc_dnc_resolve(dnc, "google.com", MIO_DNS_RRT_SOA, MIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
|
|
|
//if (!mio_svc_dnc_resolve(dnc, "google.com", MIO_DNS_RRT_NS, MIO_SVC_DNC_RESOLVE_FLAG_BRIEF, on_dnc_resolve_brief, 0))
|
2020-02-11 09:02:46 +00:00
|
|
|
{
|
|
|
|
printf ("resolve attempt failure ---> code.miflux.com\n");
|
|
|
|
}
|
2020-02-20 10:07:12 +00:00
|
|
|
#endif
|
2020-02-11 09:02:46 +00:00
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
mio_loop (mio);
|
2016-01-26 16:02:20 +00:00
|
|
|
|
2020-02-11 09:02:46 +00:00
|
|
|
/* TODO: let mio close it ... dnc is svc. sck is dev. */
|
2020-05-05 15:12:08 +00:00
|
|
|
if (htts) mio_svc_htts_stop (htts);
|
2020-02-11 09:02:46 +00:00
|
|
|
mio_svc_dnc_stop (dnc);
|
2019-02-13 10:41:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-12 13:15:54 +00:00
|
|
|
g_mio = MIO_NULL;
|
|
|
|
mio_close (mio);
|
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:
|
2018-12-12 13:15:54 +00:00
|
|
|
g_mio = MIO_NULL;
|
|
|
|
mio_close (mio);
|
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
|
|
|
|