reorganized and enhanced socket address functions

This commit is contained in:
hyung-hwan 2020-02-20 06:00:53 +00:00
parent c5e8e902aa
commit 3d20fc7421
10 changed files with 236 additions and 348 deletions

View File

@ -161,28 +161,18 @@ static void tcp_sck_on_disconnect (mio_dev_sck_t* tcp)
static void tcp_sck_on_connect (mio_dev_sck_t* tcp)
{
mio_sckfam_t fam;
mio_scklen_t len;
mio_bch_t buf1[128], buf2[128];
memset (buf1, 0, MIO_SIZEOF(buf1));
memset (buf2, 0, MIO_SIZEOF(buf2));
mio_getsckaddrinfo (tcp->mio, &tcp->localaddr, &len, &fam);
inet_ntop (fam, tcp->localaddr.data, buf1, MIO_COUNTOF(buf1));
mio_getsckaddrinfo (tcp->mio, &tcp->remoteaddr, &len, &fam);
inet_ntop (fam, tcp->remoteaddr.data, buf2, MIO_COUNTOF(buf2));
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);
if (tcp->state & MIO_DEV_SCK_CONNECTED)
{
MIO_INFO5 (tcp->mio, "DEVICE connected to a remote server... LOCAL %hs:%d REMOTE %hs:%d SCK: %d\n",
buf1, mio_getsckaddrport(&tcp->localaddr), buf2, mio_getsckaddrport(&tcp->remoteaddr), tcp->sck);
MIO_INFO3 (tcp->mio, "DEVICE connected to a remote server... LOCAL %hs REMOTE %hs SCK: %d\n", buf1, buf2, tcp->sck);
}
else if (tcp->state & MIO_DEV_SCK_ACCEPTED)
{
MIO_INFO5 (tcp->mio, "DEVICE accepted client device... .LOCAL %hs:%d REMOTE %hs:%d\n",
buf1, mio_getsckaddrport(&tcp->localaddr), buf2, mio_getsckaddrport(&tcp->remoteaddr), tcp->sck);
MIO_INFO3 (tcp->mio, "DEVICE accepted client device... .LOCAL %hs REMOTE %hs SCK: %d\n", buf1, buf2, tcp->sck);
}
if (mio_dev_sck_write(tcp, "hello", 5, MIO_NULL, MIO_NULL) <= -1)
@ -191,7 +181,7 @@ static void tcp_sck_on_connect (mio_dev_sck_t* tcp)
}
}
static int tcp_sck_on_write (mio_dev_sck_t* tcp, mio_iolen_t wrlen, void* wrctx, const mio_sckaddr_t* dstaddr)
static int tcp_sck_on_write (mio_dev_sck_t* tcp, mio_iolen_t wrlen, void* wrctx, const mio_skad_t* dstaddr)
{
tcp_server_t* ts;
mio_ntime_t tmout;
@ -226,7 +216,7 @@ static int tcp_sck_on_write (mio_dev_sck_t* tcp, mio_iolen_t wrlen, void* wrctx,
return 0;
}
static int tcp_sck_on_read (mio_dev_sck_t* tcp, const void* buf, mio_iolen_t len, const mio_sckaddr_t* srcaddr)
static int tcp_sck_on_read (mio_dev_sck_t* tcp, const void* buf, mio_iolen_t len, const mio_skad_t* srcaddr)
{
int n;
@ -375,10 +365,7 @@ int main (int argc, char* argv[])
ts->tally = 0;
memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn));
{
in_addr_t ia = inet_addr("127.0.0.1");
mio_sckaddr_initforip4 (&tcp_conn.remoteaddr, 9999, (mio_ip4addr_t*)&ia);
}
mio_bcharstoskad(mio, "127.0.0.1:9999", 14, &tcp_conn.remoteaddr);
MIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0);
tcp_conn.options = 0;
if (mio_dev_sck_connect(tcpsvr, &tcp_conn) <= -1)

View File

@ -30,6 +30,7 @@
#include <mio-sck.h>
#include <mio-pro.h>
#include <mio-dns.h>
#include <mio-nwif.h>
#include <stdlib.h>
#include <string.h>
@ -39,8 +40,6 @@
#include <arpa/inet.h>
#include <signal.h>
#include <net/if.h>
#include <assert.h>
#if defined(HAVE_OPENSSL_SSL_H) && defined(HAVE_SSL)
@ -51,7 +50,7 @@
# if defined(HAVE_OPENSSL_ENGINE_H)
# include <openssl/engine.h>
# endif
# define USE_SSL
//# define USE_SSL
#endif
/* ========================================================================= */
@ -160,28 +159,18 @@ static void tcp_sck_on_disconnect (mio_dev_sck_t* tcp)
static void tcp_sck_on_connect (mio_dev_sck_t* tcp)
{
mio_sckfam_t fam;
mio_scklen_t len;
mio_bch_t buf1[128], buf2[128];
memset (buf1, 0, MIO_SIZEOF(buf1));
memset (buf2, 0, MIO_SIZEOF(buf2));
mio_getsckaddrinfo (tcp->mio, &tcp->localaddr, &len, &fam);
inet_ntop (fam, tcp->localaddr.data, buf1, MIO_COUNTOF(buf1));
mio_getsckaddrinfo (tcp->mio, &tcp->remoteaddr, &len, &fam);
inet_ntop (fam, tcp->remoteaddr.data, buf2, MIO_COUNTOF(buf2));
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);
if (tcp->state & MIO_DEV_SCK_CONNECTED)
{
MIO_INFO5 (tcp->mio, "DEVICE connected to a remote server... LOCAL %hs:%d REMOTE %hs:%d SCK: %d\n",
buf1, mio_getsckaddrport(&tcp->localaddr), buf2, mio_getsckaddrport(&tcp->remoteaddr), tcp->sck);
MIO_INFO3 (tcp->mio, "DEVICE connected to a remote server... LOCAL %hs REMOTE %hs SCK: %d\n", buf1, buf2, tcp->sck);
}
else if (tcp->state & MIO_DEV_SCK_ACCEPTED)
{
MIO_INFO5 (tcp->mio, "DEVICE accepted client device... .LOCAL %hs:%d REMOTE %hs:%d\n",
buf1, mio_getsckaddrport(&tcp->localaddr), buf2, mio_getsckaddrport(&tcp->remoteaddr), tcp->sck);
MIO_INFO3 (tcp->mio, "DEVICE accepted client device... .LOCAL %hs REMOTE %hs SCK: %d\n", buf1, buf2, tcp->sck);
}
if (mio_dev_sck_write(tcp, "hello", 5, MIO_NULL, MIO_NULL) <= -1)
@ -190,7 +179,7 @@ static void tcp_sck_on_connect (mio_dev_sck_t* tcp)
}
}
static int tcp_sck_on_write (mio_dev_sck_t* tcp, mio_iolen_t wrlen, void* wrctx, const mio_sckaddr_t* dstaddr)
static int tcp_sck_on_write (mio_dev_sck_t* tcp, mio_iolen_t wrlen, void* wrctx, const mio_skad_t* dstaddr)
{
tcp_server_t* ts;
mio_ntime_t tmout;
@ -225,7 +214,7 @@ static int tcp_sck_on_write (mio_dev_sck_t* tcp, mio_iolen_t wrlen, void* wrctx,
return 0;
}
static int tcp_sck_on_read (mio_dev_sck_t* tcp, const void* buf, mio_iolen_t len, const mio_sckaddr_t* srcaddr)
static int tcp_sck_on_read (mio_dev_sck_t* tcp, const void* buf, mio_iolen_t len, const mio_skad_t* srcaddr)
{
int n;
@ -333,7 +322,7 @@ static int pro_on_write (mio_dev_pro_t* pro, mio_iolen_t wrlen, void* wrctx)
/* ========================================================================= */
static int arp_sck_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_sckaddr_t* srcaddr)
static int arp_sck_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_skad_t* srcaddr)
{
mio_etharp_pkt_t* eap;
@ -342,7 +331,7 @@ static int arp_sck_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dl
eap = (mio_etharp_pkt_t*)data;
printf ("ARP ON IFINDEX %d OPCODE: %d", mio_getsckaddrifindex(srcaddr), ntohs(eap->arphdr.opcode));
printf ("ARP ON IFINDEX %d OPCODE: %d", mio_skad_ifindex(srcaddr), ntohs(eap->arphdr.opcode));
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]);
@ -352,7 +341,7 @@ static int arp_sck_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dl
return 0;
}
static int arp_sck_on_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_sckaddr_t* dstaddr)
static int arp_sck_on_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_skad_t* dstaddr)
{
return 0;
}
@ -369,11 +358,11 @@ printf ("SHUTTING DOWN ARP SOCKET %d...\n", dev->sck);
static int setup_arp_tester (mio_t* mio)
{
mio_sckaddr_t ethdst;
mio_skad_t ethdst;
mio_etharp_pkt_t etharp;
mio_dev_sck_make_t sck_make;
mio_dev_sck_t* sck;
unsigned int ifindex;
memset (&sck_make, 0, MIO_SIZEOF(sck_make));
sck_make.type = MIO_DEV_SCK_ARP;
@ -389,11 +378,11 @@ static int setup_arp_tester (mio_t* mio)
return -1;
}
//mio_sckaddr_initforeth (&ethdst, if_nametoindex("enp0s25.3"), (mio_ethaddr_t*)"\xFF\xFF\xFF\xFF\xFF\xFF");
//mio_sckaddr_initforeth (&ethdst, if_nametoindex("enp0s25.3"), (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
mio_sckaddr_initforeth (&ethdst, if_nametoindex("wlan0"), (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
//mio_bcstrtoifindex (mio, "enp0s25.3", &ifindex);
//mio_skad_init_for_eth (&ethdst, ifindex, (mio_ethaddr_t*)"\xFF\xFF\xFF\xFF\xFF\xFF");
//mio_skad_init_for_eth (&ethdst, ifindex, (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
mio_bcstrtoifindex (mio, "eno1", &ifindex);
mio_skad_init_for_eth (&ethdst, ifindex, (mio_ethaddr_t*)"\xAA\xBB\xFF\xCC\xDD\xFF");
memset (&etharp, 0, MIO_SIZEOF(etharp));
@ -436,13 +425,11 @@ static int schedule_icmp_wait (mio_dev_sck_t* dev);
static void send_icmp (mio_dev_sck_t* dev, mio_uint16_t seq)
{
mio_t* mio = dev->mio;
mio_sckaddr_t dstaddr;
mio_ip4addr_t ia;
mio_skad_t dstaddr;
mio_icmphdr_t* icmphdr;
mio_uint8_t buf[512];
inet_pton (AF_INET, "192.168.1.131", &ia);
mio_sckaddr_initforip4 (&dstaddr, 0, &ia);
mio_bcharstoskad (mio, "192.168.9.1", 11, &dstaddr);
memset(buf, 0, MIO_SIZEOF(buf));
icmphdr = (mio_icmphdr_t*)buf;
@ -504,7 +491,7 @@ static int schedule_icmp_wait (mio_dev_sck_t* dev)
return (mio_instmrjob(dev->mio, &tmrjob) == MIO_TMRIDX_INVALID)? -1: 0;
}
static int icmp_sck_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_sckaddr_t* srcaddr)
static int icmp_sck_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_skad_t* srcaddr)
{
icmpxtn_t* icmpxtn;
mio_iphdr_t* iphdr;
@ -548,7 +535,7 @@ static int icmp_sck_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t d
}
static int icmp_sck_on_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_sckaddr_t* dstaddr)
static int icmp_sck_on_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_skad_t* dstaddr)
{
/*icmpxtn_t* icmpxtn;
@ -801,11 +788,8 @@ int main (int argc, char* argv[])
ts->tally = 0;
memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn));
{
/* openssl s_server -accept 9999 -key localhost.key -cert localhost.crt */
in_addr_t ia = inet_addr("127.0.0.1");
mio_sckaddr_initforip4 (&tcp_conn.remoteaddr, 9999, (mio_ip4addr_t*)&ia);
}
mio_bcharstoskad(mio, "127.0.0.1:9999", 14, &tcp_conn.remoteaddr);
MIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0);
tcp_conn.options = MIO_DEV_SCK_CONNECT_SSL;
@ -833,7 +817,7 @@ int main (int argc, char* argv[])
ts->tally = 0;
memset (&tcp_bind, 0, MIO_SIZEOF(tcp_bind));
mio_sckaddr_initforip4 (&tcp_bind.localaddr, 1234, MIO_NULL);
mio_bcharstoskad(mio, "0.0.0.0:1234", 14, &tcp_bind.localaddr);
tcp_bind.options = MIO_DEV_SCK_BIND_REUSEADDR;
if (mio_dev_sck_bind(tcp[1],&tcp_bind) <= -1)
@ -870,7 +854,7 @@ int main (int argc, char* argv[])
memset (&tcp_bind, 0, MIO_SIZEOF(tcp_bind));
mio_sckaddr_initforip4 (&tcp_bind.localaddr, 1235, MIO_NULL);
mio_bcharstoskad(mio, "0.0.0.0:1235", 14, &tcp_bind.localaddr);
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;
@ -933,13 +917,16 @@ for (i = 0; i < 5; i++)
{
mio_svc_dnc_t* dnc;
mio_ntime_t send_tmout, reply_tmout;
mio_skad_t servaddr;
send_tmout.sec = 0;
send_tmout.nsec = 0;
reply_tmout.sec = 1;
reply_tmout.nsec = 0;
dnc = mio_svc_dnc_start (mio/*, "8.8.8.8:53,1.1.1.1:53"*/, &send_tmout, &reply_tmout, 2); /* option - send to all, send one by one */
mio_bcharstoskad (mio, "1.1.1.1:53", 10, &servaddr);
dnc = mio_svc_dnc_start (mio, &servaddr, &send_tmout, &reply_tmout, 2); /* option - send to all, send one by one */
{
mio_dns_bqr_t qrs[] =
{
@ -1086,10 +1073,8 @@ int main (int argc, char* argv[])
ts->tally = 0;
memset (&tcp_conn, 0, MIO_SIZEOF(tcp_conn));
{
in_addr_t ia = inet_addr("127.0.0.1");
mio_sckaddr_initforip4 (&tcp_conn.remoteaddr, 9999, (mio_ip4addr_t*)&ia);
}
mio_bcharstoskad(mio, "127.0.0.1:9999", 14, &tcp_conn.remoteaddr);
MIO_INIT_NTIME (&tcp_conn.connect_tmout, 5, 0);
tcp_conn.options = 0;
if (mio_dev_sck_connect(tcpsvr, &tcp_conn) <= -1)

View File

@ -42,7 +42,7 @@ struct mio_svc_dnc_t
/*MIO_DNS_SVC_HEADERS;*/
mio_dev_sck_t* sck;
mio_sckaddr_t serveraddr;
mio_skad_t serv_addr;
mio_ntime_t send_tmout;
mio_ntime_t reply_tmout; /* default reply timeout */
@ -213,7 +213,7 @@ MIO_DEBUG1 (mio, "releasing dns msg %d\n", (int)mio_ntoh16(mio_dns_msg_to_pkt(ms
}
static int dnc_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_sckaddr_t* srcaddr)
static int dnc_on_read (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_skad_t* srcaddr)
{
mio_t* mio = dev->mio;
mio_svc_dnc_t* dnc = ((dnc_sck_xtn_t*)mio_dev_sck_getxtn(dev))->dnc;
@ -275,13 +275,13 @@ MIO_DEBUG0 (mio, "unable to receive dns response in time...\n");
mio_ntime_t* tmout;
tmout = MIO_IS_POS_NTIME(&msgxtn->wtmout)? &msgxtn->wtmout: MIO_NULL;
if (mio_dev_sck_timedwrite(dnc->sck, mio_dns_msg_to_pkt(reqmsg), reqmsg->pktlen, tmout, reqmsg, &dnc->serveraddr) <= -1)
if (mio_dev_sck_timedwrite(dnc->sck, mio_dns_msg_to_pkt(reqmsg), reqmsg->pktlen, tmout, reqmsg, &dnc->serv_addr) <= -1)
{
if (MIO_LIKELY(msgxtn->on_reply))
msgxtn->on_reply (dnc, reqmsg, MIO_ETMOUT, MIO_NULL, 0);
release_dns_msg (dnc, reqmsg);
return MIO_NULL;
return;
}
printf (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RESENT REQUEST >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \n");
@ -295,7 +295,7 @@ printf (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> RESENT REQUEST >>>>>>>>>>>>>>>>
}
static int dnc_on_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_sckaddr_t* dstaddr)
static int dnc_on_write (mio_dev_sck_t* dev, mio_iolen_t wrlen, void* wrctx, const mio_skad_t* dstaddr)
{
mio_t* mio = dev->mio;
mio_dns_msg_t* msg = (mio_dns_msg_t*)wrctx;
@ -366,7 +366,7 @@ static void dnc_on_disconnect (mio_dev_sck_t* dev)
{
}
mio_svc_dnc_t* mio_svc_dnc_start (mio_t* mio, const mio_ntime_t* send_tmout, const mio_ntime_t* reply_tmout, mio_oow_t reply_tmout_max_tries)
mio_svc_dnc_t* mio_svc_dnc_start (mio_t* mio, const mio_skad_t* serv_addr, const mio_ntime_t* send_tmout, const mio_ntime_t* reply_tmout, mio_oow_t reply_tmout_max_tries)
{
mio_svc_dnc_t* dnc = MIO_NULL;
mio_dev_sck_make_t mkinfo;
@ -377,6 +377,7 @@ mio_svc_dnc_t* mio_svc_dnc_start (mio_t* mio, const mio_ntime_t* send_tmout, con
dnc->mio = mio;
dnc->stop = mio_svc_dnc_stop;
dnc->serv_addr = *serv_addr;
dnc->send_tmout = *send_tmout;
dnc->reply_tmout = *reply_tmout;
dnc->reply_tmout_max_tries = reply_tmout_max_tries;
@ -395,10 +396,6 @@ mio_svc_dnc_t* mio_svc_dnc_start (mio_t* mio, const mio_ntime_t* send_tmout, con
/* TODO: bind if requested */
/*if (mio_dev_sck_bind(dev, ....) <= -1) goto oops;*/
{
mio_uint32_t ia = 0x01010101; /* 1.1.1.1 */ /* TODO: accept as parameter ... */
mio_sckaddr_initforip4 (&dnc->serveraddr, 53, (mio_ip4addr_t*)&ia);
}
MIO_SVC_REGISTER (mio, (mio_svc_t*)dnc);
return dnc;
@ -439,9 +436,9 @@ mio_dns_msg_t* mio_svc_dnc_sendmsg (mio_svc_dnc_t* dnc, mio_dns_bhdr_t* bdns, mi
msgxtn->rmaxtries = dnc->reply_tmout_max_tries;
msgxtn->rtries = 0;
/* TODO: optionally, override dnc->serveraddr and use the target address passed as a parameter */
/* TODO: optionally, override dnc->serv_addr and use the target address passed as a parameter */
tmout = MIO_IS_POS_NTIME(&msgxtn->wtmout)? &msgxtn->wtmout: MIO_NULL;
if (mio_dev_sck_timedwrite(dnc->sck, mio_dns_msg_to_pkt(msg), msg->pktlen, tmout, msg, &dnc->serveraddr) <= -1)
if (mio_dev_sck_timedwrite(dnc->sck, mio_dns_msg_to_pkt(msg), msg->pktlen, tmout, msg, &dnc->serv_addr) <= -1)
{
release_dns_msg (dnc, msg);
return MIO_NULL;

View File

@ -910,6 +910,13 @@ struct mio_cmgr_t
# define MIO_UNLIKELY(x) (x)
#endif
#if defined(__GNUC__)
# define MIO_PACKED __attribute__((__packed__))
#else
# define MIO_PACKED
#endif
/* =========================================================================
* STATIC ASSERTION
* =========================================================================*/

View File

@ -28,6 +28,7 @@
#define _MIO_DNS_H_
#include <mio.h>
#include <mio-skad.h>
#define MIO_DNS_PORT (53)
@ -443,6 +444,7 @@ extern "C" {
MIO_EXPORT mio_svc_dnc_t* mio_svc_dnc_start (
mio_t* mio,
const mio_skad_t* serv_addr,
const mio_ntime_t* send_tmout,
const mio_ntime_t* reply_tmout,
mio_oow_t reply_tmout_max_tries

View File

@ -28,6 +28,7 @@
#define _MIO_SCK_H_
#include <mio.h>
#include <mio-skad.h>
/* ========================================================================= */
/* TOOD: move these to a separte file */
@ -44,46 +45,7 @@
#define MIO_ARPHDR_HTYPE_ETH 0x0001
#define MIO_ARPHDR_PTYPE_IP4 0x0800
#define MIO_ETHADDR_LEN 6
#define MIO_IP4ADDR_LEN 4
#define MIO_IP6ADDR_LEN 16
#if defined(__GNUC__)
# define MIO_PACKED __attribute__((__packed__))
#else
# define MIO_PACKED
# MIO_PACK_PUSH pack(push)
# MIO_PACK_PUSH pack(push)
# MIO_PACK(x) pack(x)
#endif
#if defined(__GNUC__)
/* nothing */
#else
#pragma pack(push)
#pragma pack(1)
#endif
struct MIO_PACKED mio_ethaddr_t
{
mio_uint8_t v[MIO_ETHADDR_LEN];
};
typedef struct mio_ethaddr_t mio_ethaddr_t;
struct MIO_PACKED mio_ip4addr_t
{
mio_uint8_t v[MIO_IP4ADDR_LEN];
};
typedef struct mio_ip4addr_t mio_ip4addr_t;
struct MIO_PACKED mio_ip6addr_t
{
mio_uint8_t v[MIO_IP6ADDR_LEN];
};
typedef struct mio_ip6addr_t mio_ip6addr_t;
#include <mio-pac1.h>
struct MIO_PACKED mio_ethhdr_t
{
@ -171,11 +133,8 @@ struct MIO_PACKED mio_icmphdr_t
};
typedef struct mio_icmphdr_t mio_icmphdr_t;
#if defined(__GNUC__)
/* nothing */
#else
#pragma pack(pop)
#endif
#include <mio-upac.h>
/* ICMP types */
#define MIO_ICMP_ECHO_REPLY 0
@ -211,18 +170,6 @@ typedef struct mio_icmphdr_t mio_icmphdr_t;
/* ========================================================================= */
typedef int mio_sckfam_t;
struct mio_sckaddr_t
{
#if defined(MIO_OFFSETOF_SA_FAMILY) && (MIO_OFFSETOF_SA_FAMILY > 0)
mio_uint8_t filler[MIO_OFFSETOF_SA_FAMILY];
#endif
mio_sckfam_t family;
mio_uint8_t data[128]; /* TODO: use the actual sockaddr size */
};
typedef struct mio_sckaddr_t mio_sckaddr_t;
#if (MIO_SIZEOF_SOCKLEN_T == MIO_SIZEOF_INT)
#if defined(MIO_SOCKLEN_T_IS_SIGNED)
typedef int mio_scklen_t;
@ -305,14 +252,14 @@ typedef int (*mio_dev_sck_on_read_t) (
mio_dev_sck_t* dev,
const void* data,
mio_iolen_t dlen,
const mio_sckaddr_t* srcaddr
const mio_skad_t* srcaddr
);
typedef int (*mio_dev_sck_on_write_t) (
mio_dev_sck_t* dev,
mio_iolen_t wrlen,
void* wrctx,
const mio_sckaddr_t* dstaddr
const mio_skad_t* dstaddr
);
typedef void (*mio_dev_sck_on_disconnect_t) (
@ -374,7 +321,7 @@ typedef struct mio_dev_sck_bind_t mio_dev_sck_bind_t;
struct mio_dev_sck_bind_t
{
int options;
mio_sckaddr_t localaddr;
mio_skad_t localaddr;
/* TODO: add device name for BIND_TO_DEVICE */
const mio_bch_t* ssl_certfile;
@ -392,7 +339,7 @@ typedef struct mio_dev_sck_connect_t mio_dev_sck_connect_t;
struct mio_dev_sck_connect_t
{
int options;
mio_sckaddr_t remoteaddr;
mio_skad_t remoteaddr;
mio_ntime_t connect_tmout;
};
@ -407,7 +354,7 @@ struct mio_dev_sck_accept_t
{
mio_syshnd_t sck;
/* TODO: add timeout */
mio_sckaddr_t remoteaddr;
mio_skad_t remoteaddr;
};
struct mio_dev_sck_t
@ -429,13 +376,13 @@ struct mio_dev_sck_t
*
* also used as a placeholder to store source address for
* a stateless socket */
mio_sckaddr_t remoteaddr;
mio_skad_t remoteaddr;
/* local socket address */
mio_sckaddr_t localaddr;
mio_skad_t localaddr;
/* original destination address */
mio_sckaddr_t orgdstaddr;
mio_skad_t orgdstaddr;
mio_dev_sck_on_write_t on_write;
mio_dev_sck_on_read_t on_read;
@ -470,48 +417,6 @@ MIO_EXPORT int mio_makesckasync (
mio_sckhnd_t sck
);
MIO_EXPORT int mio_getsckaddrinfo (
mio_t* mio,
const mio_sckaddr_t* addr,
mio_scklen_t* len,
mio_sckfam_t* family
);
/*
* The mio_getsckaddrport() function returns the port number of a socket
* address in the host byte order. If the address doesn't support the port
* number, it returns 0.
*/
MIO_EXPORT mio_uint16_t mio_getsckaddrport (
const mio_sckaddr_t* addr
);
/*
* The mio_getsckaddrifindex() function returns an interface number.
* If the address doesn't support the interface number, it returns 0. */
MIO_EXPORT int mio_getsckaddrifindex (
const mio_sckaddr_t* addr
);
MIO_EXPORT void mio_sckaddr_initforip4 (
mio_sckaddr_t* sckaddr,
mio_uint16_t port,
mio_ip4addr_t* ip4addr
);
MIO_EXPORT void mio_sckaddr_initforip6 (
mio_sckaddr_t* sckaddr,
mio_uint16_t port,
mio_ip6addr_t* ip6addr
);
MIO_EXPORT void mio_sckaddr_initforeth (
mio_sckaddr_t* sckaddr,
int ifindex,
mio_ethaddr_t* ethaddr
);
/* ========================================================================= */
MIO_EXPORT mio_dev_sck_t* mio_dev_sck_make (
@ -546,7 +451,7 @@ MIO_EXPORT int mio_dev_sck_write (
const void* data,
mio_iolen_t len,
void* wrctx,
const mio_sckaddr_t* dstaddr
const mio_skad_t* dstaddr
);
MIO_EXPORT int mio_dev_sck_timedwrite (
@ -555,7 +460,7 @@ MIO_EXPORT int mio_dev_sck_timedwrite (
mio_iolen_t len,
const mio_ntime_t* tmout,
void* wrctx,
const mio_sckaddr_t* dstaddr
const mio_skad_t* dstaddr
);
#if defined(MIO_HAVE_INLINE)

View File

@ -62,6 +62,31 @@ typedef struct mio_skad_t mio_skad_t;
#define MIO_SKAD_TO_BCSTR_ADDR MIO_SKAD_TO_OOCSTR_ADDR
#define MIO_SKAD_TO_BCSTR_PORT MIO_SKAD_TO_OOCSTR_PORT
/* -------------------------------------------------------------------- */
#define MIO_ETHADDR_LEN 6
#define MIO_IP4ADDR_LEN 4
#define MIO_IP6ADDR_LEN 16
#include <mio-pac1.h>
struct MIO_PACKED mio_ethaddr_t
{
mio_uint8_t v[MIO_ETHADDR_LEN];
};
typedef struct mio_ethaddr_t mio_ethaddr_t;
struct MIO_PACKED mio_ip4addr_t
{
mio_uint8_t v[MIO_IP4ADDR_LEN];
};
typedef struct mio_ip4addr_t mio_ip4addr_t;
struct MIO_PACKED mio_ip6addr_t
{
mio_uint8_t v[MIO_IP6ADDR_LEN];
};
typedef struct mio_ip6addr_t mio_ip6addr_t;
#include <mio-upac.h>
#if defined(__cplusplus)
extern "C" {
@ -105,6 +130,25 @@ MIO_EXPORT mio_oow_t mio_skadtobcstr (
# define mio_skadtooocstr mio_skadtobcstr
#endif
MIO_EXPORT void mio_skad_init_for_ip4 (
mio_skad_t* skad,
mio_uint16_t port,
mio_ip4addr_t* ip4addr
);
MIO_EXPORT void mio_skad_init_for_ip6 (
mio_skad_t* skad,
mio_uint16_t port,
mio_ip6addr_t* ip6addr,
int scope_id
);
MIO_EXPORT void mio_skad_init_for_eth (
mio_skad_t* skad,
int ifindex,
mio_ethaddr_t* ethaddr
);
MIO_EXPORT int mio_skad_family (
const mio_skad_t* skad
);
@ -113,10 +157,23 @@ MIO_EXPORT int mio_skad_size (
const mio_skad_t* skad
);
MIO_EXPORT int mio_skad_port (
const mio_skad_t* skad
);
MIO_EXPORT int mio_skad_ifindex (
const mio_skad_t* skad
);
MIO_EXPORT void mio_clear_skad (
mio_skad_t* skad
);
MIO_EXPORT int mio_equal_skads (
const mio_skad_t* addr1,
const mio_skad_t* addr2
);
#if defined(__cplusplus)
}
#endif

View File

@ -133,157 +133,23 @@ oops:
return MIO_SCKHND_INVALID;
}
int mio_getsckaddrinfo (mio_t* mio, const mio_sckaddr_t* addr, mio_scklen_t* len, mio_sckfam_t* family)
{
struct sockaddr* saddr = (struct sockaddr*)addr;
switch (saddr->sa_family)
{
case AF_INET:
if (len) *len = MIO_SIZEOF(struct sockaddr_in);
if (family) *family = AF_INET;
return 0;
case AF_INET6:
if (len) *len = MIO_SIZEOF(struct sockaddr_in6);
if (family) *family = AF_INET6;
return 0;
#if defined(AF_PACKET) && (MIO_SIZEOF_STRUCT_SOCKADDR_LL > 0)
case AF_PACKET:
if (len) *len = MIO_SIZEOF(struct sockaddr_ll);
if (family) *family = AF_PACKET;
return 0;
#elif defined(AF_LINK) && (MIO_SIZEOF_STRUCT_SOCKADDR_DL > 0)
case AF_LINK:
if (len) *len = MIO_SIZEOF(struct sockaddr_dl);
if (family) *family = AF_LINK;
return 0;
#endif
/* TODO: more address type */
}
mio_seterrnum (mio, MIO_EINVAL);
return -1;
}
mio_uint16_t mio_getsckaddrport (const mio_sckaddr_t* addr)
{
struct sockaddr* saddr = (struct sockaddr*)addr;
switch (saddr->sa_family)
{
case AF_INET:
return mio_ntoh16(((struct sockaddr_in*)addr)->sin_port);
case AF_INET6:
return mio_ntoh16(((struct sockaddr_in6*)addr)->sin6_port);
}
return 0;
}
int mio_getsckaddrifindex (const mio_sckaddr_t* addr)
{
struct sockaddr* saddr = (struct sockaddr*)addr;
#if defined(AF_PACKET) && (MIO_SIZEOF_STRUCT_SOCKADDR_LL > 0)
if (saddr->sa_family == AF_PACKET)
{
return ((struct sockaddr_ll*)addr)->sll_ifindex;
}
#elif defined(AF_LINK) && (MIO_SIZEOF_STRUCT_SOCKADDR_DL > 0)
if (saddr->sa_family == AF_LINK)
{
return ((struct sockaddr_dl*)addr)->sdl_index;
}
#endif
return 0;
}
int mio_equalsckaddrs (mio_t* mio, const mio_sckaddr_t* addr1, const mio_sckaddr_t* addr2)
{
mio_sckfam_t fam1, fam2;
mio_scklen_t len1, len2;
mio_getsckaddrinfo (mio, addr1, &len1, &fam1);
mio_getsckaddrinfo (mio, addr2, &len2, &fam2);
return fam1 == fam2 && len1 == len2 && MIO_MEMCMP(addr1, addr2, len1) == 0;
}
/* ========================================================================= */
void mio_sckaddr_initforip4 (mio_sckaddr_t* sckaddr, mio_uint16_t port, mio_ip4addr_t* ip4addr)
{
struct sockaddr_in* sin = (struct sockaddr_in*)sckaddr;
MIO_MEMSET (sin, 0, MIO_SIZEOF(*sin));
sin->sin_family = AF_INET;
sin->sin_port = htons(port);
if (ip4addr) MIO_MEMCPY (&sin->sin_addr, ip4addr, MIO_IP4ADDR_LEN);
}
void mio_sckaddr_initforip6 (mio_sckaddr_t* sckaddr, mio_uint16_t port, mio_ip6addr_t* ip6addr)
{
struct sockaddr_in6* sin = (struct sockaddr_in6*)sckaddr;
/* TODO: include sin6_scope_id */
MIO_MEMSET (sin, 0, MIO_SIZEOF(*sin));
sin->sin6_family = AF_INET;
sin->sin6_port = htons(port);
if (ip6addr) MIO_MEMCPY (&sin->sin6_addr, ip6addr, MIO_IP6ADDR_LEN);
}
void mio_sckaddr_initforeth (mio_sckaddr_t* sckaddr, int ifindex, mio_ethaddr_t* ethaddr)
{
#if defined(AF_PACKET) && (MIO_SIZEOF_STRUCT_SOCKADDR_LL > 0)
struct sockaddr_ll* sll = (struct sockaddr_ll*)sckaddr;
MIO_MEMSET (sll, 0, MIO_SIZEOF(*sll));
sll->sll_family = AF_PACKET;
sll->sll_ifindex = ifindex;
if (ethaddr)
{
sll->sll_halen = MIO_ETHADDR_LEN;
MIO_MEMCPY (sll->sll_addr, ethaddr, MIO_ETHADDR_LEN);
}
#elif defined(AF_LINK) && (MIO_SIZEOF_STRUCT_SOCKADDR_DL > 0)
struct sockaddr_dl* sll = (struct sockaddr_dl*)sckaddr;
MIO_MEMSET (sll, 0, MIO_SIZEOF(*sll));
sll->sdl_family = AF_LINK;
sll->sdl_index = ifindex;
if (ethaddr)
{
sll->sdl_alen = MIO_ETHADDR_LEN;
MIO_MEMCPY (sll->sdl_data, ethaddr, MIO_ETHADDR_LEN);
}
#else
# error UNSUPPORTED DATALINK SOCKET ADDRESS
#endif
}
/* ========================================================================= */
static mio_devaddr_t* sckaddr_to_devaddr (mio_dev_sck_t* dev, const mio_sckaddr_t* sckaddr, mio_devaddr_t* devaddr)
static mio_devaddr_t* skad_to_devaddr (mio_dev_sck_t* dev, const mio_skad_t* sckaddr, mio_devaddr_t* devaddr)
{
if (sckaddr)
{
mio_scklen_t len;
mio_getsckaddrinfo (dev->mio, sckaddr, &len, MIO_NULL);
devaddr->ptr = (void*)sckaddr;
devaddr->len = len;
devaddr->len = mio_skad_size(sckaddr);
return devaddr;
}
return MIO_NULL;
}
static MIO_INLINE mio_sckaddr_t* devaddr_to_sckaddr (mio_dev_sck_t* dev, const mio_devaddr_t* devaddr, mio_sckaddr_t* sckaddr)
static MIO_INLINE mio_skad_t* devaddr_to_skad (mio_dev_sck_t* dev, const mio_devaddr_t* devaddr, mio_skad_t* sckaddr)
{
return (mio_sckaddr_t*)devaddr->ptr;
return (mio_skad_t*)devaddr->ptr;
}
/* ========================================================================= */
@ -792,9 +658,6 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg)
case MIO_DEV_SCK_BIND:
{
mio_dev_sck_bind_t* bnd = (mio_dev_sck_bind_t*)arg;
struct sockaddr* sa = (struct sockaddr*)&bnd->localaddr;
mio_scklen_t sl;
mio_sckfam_t fam;
int x;
#if defined(USE_SSL)
SSL_CTX* ssl_ctx = MIO_NULL;
@ -923,15 +786,7 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg)
#endif
}
if (mio_getsckaddrinfo(mio, &bnd->localaddr, &sl, &fam) <= -1)
{
#if defined(USE_SSL)
if (ssl_ctx) SSL_CTX_free (ssl_ctx);
#endif
return -1;
}
x = bind(rdev->sck, sa, sl);
x = bind(rdev->sck, (struct sockaddr*)&bnd->localaddr, mio_skad_size(&bnd->localaddr));
if (x == -1)
{
mio_seterrwithsyserr (mio, 0, errno);
@ -955,7 +810,7 @@ static int dev_sck_ioctl (mio_dev_t* dev, int cmd, void* arg)
mio_dev_sck_connect_t* conn = (mio_dev_sck_connect_t*)arg;
struct sockaddr* sa = (struct sockaddr*)&conn->remoteaddr;
mio_scklen_t sl;
mio_sckaddr_t localaddr;
mio_skad_t localaddr;
int x;
#if defined(USE_SSL)
SSL_CTX* ssl_ctx = MIO_NULL;
@ -1228,7 +1083,7 @@ static int harvest_outgoing_connection (mio_dev_sck_t* rdev)
}
else if (errcode == 0)
{
mio_sckaddr_t localaddr;
mio_skad_t localaddr;
mio_scklen_t addrlen;
/* connected */
@ -1308,7 +1163,7 @@ static int accept_incoming_connection (mio_dev_sck_t* rdev)
{
mio_t* mio = rdev->mio;
mio_sckhnd_t clisck;
mio_sckaddr_t remoteaddr;
mio_skad_t remoteaddr;
mio_scklen_t addrlen;
mio_dev_sck_t* clidev;
int flags;
@ -1382,11 +1237,11 @@ accept_done:
clidev->orgdstaddr = rdev->localaddr;
#endif
if (!mio_equalsckaddrs (mio, &clidev->orgdstaddr, &clidev->localaddr))
if (!mio_equal_skads(&clidev->orgdstaddr, &clidev->localaddr))
{
clidev->state |= MIO_DEV_SCK_INTERCEPTED;
}
else if (mio_getsckaddrport (&clidev->localaddr) != mio_getsckaddrport(&rdev->localaddr))
else if (mio_skad_port(&clidev->localaddr) != mio_skad_port(&rdev->localaddr))
{
/* When TPROXY is used, getsockname() and SO_ORIGNAL_DST return
* the same addresses. however, the port number may be different
@ -1734,16 +1589,16 @@ int mio_dev_sck_listen (mio_dev_sck_t* dev, mio_dev_sck_listen_t* info)
return mio_dev_ioctl((mio_dev_t*)dev, MIO_DEV_SCK_LISTEN, info);
}
int mio_dev_sck_write (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, void* wrctx, const mio_sckaddr_t* dstaddr)
int mio_dev_sck_write (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, void* wrctx, const mio_skad_t* dstaddr)
{
mio_devaddr_t devaddr;
return mio_dev_write((mio_dev_t*)dev, data, dlen, wrctx, sckaddr_to_devaddr(dev, dstaddr, &devaddr));
return mio_dev_write((mio_dev_t*)dev, data, dlen, wrctx, skad_to_devaddr(dev, dstaddr, &devaddr));
}
int mio_dev_sck_timedwrite (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_ntime_t* tmout, void* wrctx, const mio_sckaddr_t* dstaddr)
int mio_dev_sck_timedwrite (mio_dev_sck_t* dev, const void* data, mio_iolen_t dlen, const mio_ntime_t* tmout, void* wrctx, const mio_skad_t* dstaddr)
{
mio_devaddr_t devaddr;
return mio_dev_timedwrite((mio_dev_t*)dev, data, dlen, tmout, wrctx, sckaddr_to_devaddr(dev, dstaddr, &devaddr));
return mio_dev_timedwrite((mio_dev_t*)dev, data, dlen, tmout, wrctx, skad_to_devaddr(dev, dstaddr, &devaddr));
}

View File

@ -55,6 +55,9 @@ union mio_skad_alt_t
#if (MIO_SIZEOF_STRUCT_SOCKADDR_LL > 0)
struct sockaddr_ll ll;
#endif
#if (MIO_SIZEOF_STRUCT_SOCKADDR_DL > 0)
struct sockaddr_dl dl;
#endif
#if (MIO_SIZEOF_STRUCT_SOCKADDR_UN > 0)
struct sockaddr_un un;
#endif
@ -1345,6 +1348,9 @@ int mio_skad_size (const mio_skad_t* _skad)
#if defined(AF_PACKET) && (MIO_SIZEOF_STRUCT_SOCKADDR_LL > 0)
case AF_PACKET: return MIO_SIZEOF(struct sockaddr_ll);
#endif
#if defined(AF_LINK) && (MIO_SIZEOF_STRUCT_SOCKADDR_DL > 0)
case AF_LINK: return MIO_SIZEOF(struct sockaddr_dl);
#endif
#if defined(AF_UNIX) && (MIO_SIZEOF_STRUCT_SOCKADDR_UN > 0)
case AF_UNIX: return MIO_SIZEOF(struct sockaddr_un);
#endif
@ -1353,6 +1359,86 @@ int mio_skad_size (const mio_skad_t* _skad)
return 0;
}
int mio_skad_port (const mio_skad_t* _skad)
{
const mio_skad_alt_t* skad = (const mio_skad_alt_t*)_skad;
switch (skad->sa.sa_family)
{
#if defined(AF_INET) && (MIO_SIZEOF_STRUCT_SOCKADDR_IN > 0)
case AF_INET: return mio_ntoh16(((struct sockaddr_in*)skad)->sin_port);
#endif
#if defined(AF_INET6) && (MIO_SIZEOF_STRUCT_SOCKADDR_IN6 > 0)
case AF_INET6: return mio_ntoh16(((struct sockaddr_in6*)skad)->sin6_port);
#endif
}
return 0;
}
int mio_skad_ifindex (const mio_skad_t* _skad)
{
const mio_skad_alt_t* skad = (const mio_skad_alt_t*)_skad;
#if defined(AF_PACKET) && (MIO_SIZEOF_STRUCT_SOCKADDR_LL > 0)
if (skad->sa.sa_family == AF_PACKET) return ((struct sockaddr_ll*)skad)->sll_ifindex;
#elif defined(AF_LINK) && (MIO_SIZEOF_STRUCT_SOCKADDR_DL > 0)
if (skad->sa.sa_family == AF_LINK) return ((struct sockaddr_dl*)skad)->sdl_index;
#endif
return 0;
}
void mio_skad_init_for_ip4 (mio_skad_t* skad, mio_uint16_t port, mio_ip4addr_t* ip4addr)
{
struct sockaddr_in* sin = (struct sockaddr_in*)skad;
MIO_MEMSET (sin, 0, MIO_SIZEOF(*sin));
sin->sin_family = AF_INET;
sin->sin_port = htons(port);
if (ip4addr) MIO_MEMCPY (&sin->sin_addr, ip4addr, MIO_IP4ADDR_LEN);
}
void mio_skad_init_for_ip6 (mio_skad_t* skad, mio_uint16_t port, mio_ip6addr_t* ip6addr, int scope_id)
{
struct sockaddr_in6* sin = (struct sockaddr_in6*)skad;
MIO_MEMSET (sin, 0, MIO_SIZEOF(*sin));
sin->sin6_family = AF_INET;
sin->sin6_port = htons(port);
sin->sin6_scope_id = scope_id;
if (ip6addr) MIO_MEMCPY (&sin->sin6_addr, ip6addr, MIO_IP6ADDR_LEN);
}
void mio_skad_init_for_eth (mio_skad_t* skad, int ifindex, mio_ethaddr_t* ethaddr)
{
#if defined(AF_PACKET) && (MIO_SIZEOF_STRUCT_SOCKADDR_LL > 0)
struct sockaddr_ll* sll = (struct sockaddr_ll*)skad;
MIO_MEMSET (sll, 0, MIO_SIZEOF(*sll));
sll->sll_family = AF_PACKET;
sll->sll_ifindex = ifindex;
if (ethaddr)
{
sll->sll_halen = MIO_ETHADDR_LEN;
MIO_MEMCPY (sll->sll_addr, ethaddr, MIO_ETHADDR_LEN);
}
#elif defined(AF_LINK) && (MIO_SIZEOF_STRUCT_SOCKADDR_DL > 0)
struct sockaddr_dl* sll = (struct sockaddr_dl*)skad;
MIO_MEMSET (sll, 0, MIO_SIZEOF(*sll));
sll->sdl_family = AF_LINK;
sll->sdl_index = ifindex;
if (ethaddr)
{
sll->sdl_alen = MIO_ETHADDR_LEN;
MIO_MEMCPY (sll->sdl_data, ethaddr, MIO_ETHADDR_LEN);
}
#else
# error UNSUPPORTED DATALINK SOCKET ADDRESS
#endif
}
void mio_clear_skad (mio_skad_t* _skad)
{
mio_skad_alt_t* skad = (mio_skad_alt_t*)_skad;
@ -1360,3 +1446,10 @@ void mio_clear_skad (mio_skad_t* _skad)
MIO_MEMSET (skad, 0, MIO_SIZEOF(*skad));
skad->sa.sa_family = MIO_AF_UNSPEC;
}
int mio_equal_skads (const mio_skad_t* addr1, const mio_skad_t* addr2)
{
return mio_skad_family(addr1) == mio_skad_family(addr2) &&
mio_skad_size(addr1) == mio_skad_size(addr2) &&
MIO_MEMCMP(addr1, addr2, mio_skad_size(addr1)) == 0;
}