From 93a7073e48e2d8324d312c5e69f70194b04cde50 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 16 Jul 2021 02:30:25 +0000 Subject: [PATCH] added mio_svc_htts_getsockaddr(), mio_dev_sck_getsockaddr(), mio_dev_sck_getpeeraddr() --- mio/lib/http-svr.c | 10 ++++++++-- mio/lib/mio-http.h | 5 +++++ mio/lib/mio-sck.h | 11 +++++++++++ mio/lib/sck.c | 32 ++++++++++++++++++++++++++++++-- 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/mio/lib/http-svr.c b/mio/lib/http-svr.c index 46913ba..fb248ba 100644 --- a/mio/lib/http-svr.c +++ b/mio/lib/http-svr.c @@ -360,7 +360,7 @@ mio_svc_htts_t* mio_svc_htts_start (mio_t* mio, mio_dev_sck_bind_t* sck_bind, mi htts->lsck = mio_dev_sck_make(mio, MIO_SIZEOF(*cli), &info.m); if (!htts->lsck) goto oops; - /* the name 'cli' for the listening socket is awkard. + /* the name 'cli' for the listening socket is awkward. * the listening socket will use the htts and sck fields for tracking only. * each accepted client socket gets the extension size for this size as well. * most of other fields are used for client management */ @@ -373,7 +373,7 @@ mio_svc_htts_t* mio_svc_htts_start (mio_t* mio, mio_dev_sck_bind_t* sck_bind, mi if (mio_dev_sck_bind(htts->lsck, sck_bind) <= -1) goto oops; MIO_MEMSET (&info, 0, MIO_SIZEOF(info)); - info.l.backlogs = 4096; + info.l.backlogs = 4096; /* TODO: use configuration? */ MIO_INIT_NTIME (&info.l.accept_tmout, 5, 1); /* usedd for ssl accept */ if (mio_dev_sck_listen(htts->lsck, &info.l) <= -1) goto oops; } @@ -454,6 +454,12 @@ int mio_svc_htts_setservernamewithbcstr (mio_svc_htts_t* htts, const mio_bch_t* return 0; } +int mio_svc_htts_getsockaddr (mio_svc_htts_t* htts, mio_skad_t* skad) +{ + /* return the socket address of the listening socket. */ + return mio_dev_sck_getsockaddr(htts->lsck, skad); +} + /* ----------------------------------------------------------------- */ mio_svc_htts_rsrc_t* mio_svc_htts_rsrc_make (mio_svc_htts_t* htts, mio_oow_t rsrc_size, mio_svc_htts_rsrc_on_kill_t on_kill) diff --git a/mio/lib/mio-http.h b/mio/lib/mio-http.h index 699eeaf..1f38f59 100644 --- a/mio/lib/mio-http.h +++ b/mio/lib/mio-http.h @@ -275,6 +275,11 @@ MIO_EXPORT int mio_svc_htts_setservernamewithbcstr ( const mio_bch_t* server_name ); +MIO_EXPORT int mio_svc_htts_getsockaddr ( + mio_svc_htts_t* htts, + mio_skad_t* skad +); + MIO_EXPORT int mio_svc_htts_docgi ( mio_svc_htts_t* htts, mio_dev_sck_t* csck, diff --git a/mio/lib/mio-sck.h b/mio/lib/mio-sck.h index b675041..08b8cc2 100644 --- a/mio/lib/mio-sck.h +++ b/mio/lib/mio-sck.h @@ -576,6 +576,17 @@ MIO_EXPORT int mio_dev_sck_getsockopt ( mio_scklen_t* optlen ); + +MIO_EXPORT int mio_dev_sck_getsockaddr ( + mio_dev_sck_t* dev, + mio_skad_t* skad +); + +MIO_EXPORT int mio_dev_sck_getpeeraddr ( + mio_dev_sck_t* dev, + mio_skad_t* skad +); + MIO_EXPORT int mio_dev_sck_shutdown ( mio_dev_sck_t* dev, int how /* bitwise-ORed of mio_dev_sck_shutdown_how_t enumerators */ diff --git a/mio/lib/sck.c b/mio/lib/sck.c index 06e665f..0c4ad31 100644 --- a/mio/lib/sck.c +++ b/mio/lib/sck.c @@ -1538,7 +1538,7 @@ static int make_accepted_client_connection (mio_dev_sck_t* rdev, mio_syshnd_t cl { /* this is a special optional callback. If you don't want a client socket device * to be created upon accept, you may implement the on_raw_accept() handler. - * the socket handle is delated to the callback. */ + * the socket handle is delegated to the callback. */ rdev->on_raw_accept (rdev, clisck, remoteaddr); return 0; } @@ -2163,6 +2163,28 @@ int mio_dev_sck_getsockopt (mio_dev_sck_t* dev, int level, int optname, void* op return getsockopt(dev->hnd, level, optname, optval, optlen); } +int mio_dev_sck_getsockaddr (mio_dev_sck_t* dev, mio_skad_t* skad) +{ + mio_scklen_t addrlen = MIO_SIZEOF(*skad); + if (getsockname(dev->hnd, (struct sockaddr*)skad, &addrlen) <= -1) + { + mio_seterrwithsyserr (dev->mio, 0, errno); + return -1; + } + return 0; +} + +int mio_dev_sck_getpeeraddr (mio_dev_sck_t* dev, mio_skad_t* skad) +{ + mio_scklen_t addrlen = MIO_SIZEOF(*skad); + if (getpeername(dev->hnd, (struct sockaddr*)skad, &addrlen) <= -1) + { + mio_seterrwithsyserr (dev->mio, 0, errno); + return -1; + } + return 0; +} + int mio_dev_sck_shutdown (mio_dev_sck_t* dev, int how) { switch (how & (MIO_DEV_SCK_SHUTDOWN_READ | MIO_DEV_SCK_SHUTDOWN_WRITE)) @@ -2184,7 +2206,13 @@ int mio_dev_sck_shutdown (mio_dev_sck_t* dev, int how) return -1; } - return shutdown(dev->hnd, how); + if (shutdown(dev->hnd, how) <= -1) + { + mio_seterrwithsyserr (dev->mio, 0, errno); + return -1; + } + + return 0; } int mio_dev_sck_sendfileok (mio_dev_sck_t* dev)