diff --git a/stio/lib/main.c b/stio/lib/main.c index 55a8ad5..054ea55 100644 --- a/stio/lib/main.c +++ b/stio/lib/main.c @@ -93,7 +93,7 @@ struct tcp_server_t }; typedef struct tcp_server_t tcp_server_t; -static void tcp_on_disconnect (stio_dev_tcp_t* tcp) +static void tcp_on_disconnect (stio_dev_sck_t* tcp) { if (tcp->state & STIO_DEV_TCP_CONNECTING) { @@ -116,7 +116,7 @@ static void tcp_on_disconnect (stio_dev_tcp_t* tcp) printf ("TCP DISCONNECTED - THIS MUST NOT HAPPEN (%d - %x)\n", (int)tcp->sck, (unsigned int)tcp->state); } } -static int tcp_on_connect (stio_dev_tcp_t* tcp) +static int tcp_on_connect (stio_dev_sck_t* tcp) { if (tcp->state & STIO_DEV_TCP_CONNECTED) @@ -128,18 +128,17 @@ printf ("device connected to a remote server... .asdfjkasdfkljasdlfkjasdj...\n") printf ("device accepted client device... .asdfjkasdfkljasdlfkjasdj...\n"); } - return stio_dev_tcp_write (tcp, "hello", 5, STIO_NULL); + return stio_dev_sck_write (tcp, "hello", 5, STIO_NULL, STIO_NULL); } - -static int tcp_on_write (stio_dev_tcp_t* tcp, stio_len_t wrlen, void* wrctx) +static int tcp_on_write (stio_dev_sck_t* tcp, stio_len_t wrlen, void* wrctx, const stio_sckadr_t* dstadr) { tcp_server_t* ts; if (wrlen <= -1) { printf ("SEDING TIMED OUT...........\n"); - stio_dev_tcp_halt(tcp); + stio_dev_sck_halt(tcp); } else { @@ -147,21 +146,21 @@ else printf (">>> SENT MESSAGE %d of length %ld\n", ts->tally, (long int)wrlen); ts->tally++; -// if (ts->tally >= 2) stio_dev_tcp_halt (tcp); +// if (ts->tally >= 2) stio_dev_sck_halt (tcp); printf ("ENABLING READING..............................\n"); - stio_dev_tcp_read (tcp, 1); + stio_dev_sck_read (tcp, 1); } return 0; } -static int tcp_on_read (stio_dev_tcp_t* tcp, const void* buf, stio_len_t len) +static int tcp_on_read (stio_dev_sck_t* tcp, const void* buf, stio_len_t len, const stio_sckadr_t* srcadr) { if (len <= 0) { printf ("STREAM DEVICE: EOF RECEIVED...\n"); /* no outstanding request. but EOF */ - stio_dev_tcp_halt (tcp); + stio_dev_sck_halt (tcp); return 0; } @@ -172,19 +171,19 @@ int n; static char a ='A'; char* xxx = malloc (1000000); memset (xxx, a++ ,1000000); - //return stio_dev_tcp_write (tcp, "HELLO", 5, STIO_NULL); + //return stio_dev_sck_write (tcp, "HELLO", 5, STIO_NULL); stio_inittime (&tmout, 1, 0); - n = stio_dev_tcp_timedwrite (tcp, xxx, 1000000, &tmout, STIO_NULL); + n = stio_dev_sck_timedwrite (tcp, xxx, 1000000, &tmout, STIO_NULL, STIO_NULL); free (xxx); if (n <= -1) return -1; /* post the write finisher */ - n = stio_dev_tcp_write (tcp, STIO_NULL, 0, STIO_NULL); + n = stio_dev_sck_write (tcp, STIO_NULL, 0, STIO_NULL, STIO_NULL); if (n <= -1) return -1; printf ("DISABLING READING..............................\n"); - stio_dev_tcp_read (tcp, 0); + stio_dev_sck_read (tcp, 0); return 0; /* return 1; let the main loop to read more greedily without consulting the multiplexer */ @@ -192,14 +191,15 @@ printf ("DISABLING READING..............................\n"); /* ========================================================================= */ -static int arp_sck_on_read (stio_dev_sck_t* dev, const void* data, stio_len_t dlen, const stio_adr_t* srcadr) +static int arp_sck_on_read (stio_dev_sck_t* dev, const void* data, stio_len_t dlen, const stio_sckadr_t* srcadr) { stio_etharp_pkt_t* eap; + struct sockaddr_ll* sll = (struct sockaddr_ll*)srcadr; if (dlen < STIO_SIZEOF(*eap)) return 0; /* drop */ eap = (stio_etharp_pkt_t*)data; - printf ("ARP OPCODE: %d", ntohs(eap->arphdr.opcode)); + printf ("ARP ON IFINDEX %d OPCODE: %d", sll->sll_ifindex, 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]); 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]); @@ -227,14 +227,15 @@ int main () { stio_t* stio; - stio_dev_udp_t* udp; stio_dev_sck_t* sck; - stio_dev_tcp_t* tcp[2]; + stio_dev_sck_t* tcp[2]; struct sockaddr_in sin; struct sigaction sigact; - stio_dev_tcp_connect_t tcp_conn; - stio_dev_tcp_listen_t tcp_lstn; - stio_dev_tcp_make_t tcp_make; + stio_dev_sck_connect_t tcp_conn; + stio_dev_sck_listen_t tcp_lstn; + stio_dev_sck_bind_t tcp_bind; + stio_dev_sck_make_t tcp_make; + stio_dev_sck_make_t sck_make; tcp_server_t* ts; @@ -268,13 +269,11 @@ int main () } */ - memset (&sin, 0, STIO_SIZEOF(sin)); - sin.sin_family = AF_INET; memset (&tcp_make, 0, STIO_SIZEOF(&tcp_make)); - memcpy (&tcp_make.addr, &sin, STIO_SIZEOF(sin)); + tcp_make.type = STIO_DEV_SCK_TCP4; tcp_make.on_write = tcp_on_write; tcp_make.on_read = tcp_on_read; - tcp[0] = stio_dev_tcp_make (stio, STIO_SIZEOF(tcp_server_t), &tcp_make); + tcp[0] = stio_dev_sck_make (stio, STIO_SIZEOF(tcp_server_t), &tcp_make); if (!tcp[0]) { printf ("Cannot make tcp\n"); @@ -287,7 +286,7 @@ int main () memset (&sin, 0, STIO_SIZEOF(sin)); sin.sin_family = AF_INET; sin.sin_port = htons(9999); - inet_pton (sin.sin_family, "192.168.1.4", &sin.sin_addr); + inet_pton (sin.sin_family, "192.168.1.119", &sin.sin_addr); //inet_pton (sin.sin_family, "127.0.0.1", &sin.sin_addr); memset (&tcp_conn, 0, STIO_SIZEOF(tcp_conn)); @@ -295,21 +294,17 @@ int main () tcp_conn.tmout.sec = 5; tcp_conn.on_connect = tcp_on_connect; tcp_conn.on_disconnect = tcp_on_disconnect; - if (stio_dev_tcp_connect (tcp[0], &tcp_conn) <= -1) + if (stio_dev_sck_connect (tcp[0], &tcp_conn) <= -1) { - printf ("stio_dev_tcp_connect() failed....\n"); + printf ("stio_dev_sck_connect() failed....\n"); goto oops; } - memset (&sin, 0, STIO_SIZEOF(sin)); - sin.sin_family = AF_INET; - sin.sin_port = htons(1234); memset (&tcp_make, 0, STIO_SIZEOF(&tcp_make)); - memcpy (&tcp_make.addr, &sin, STIO_SIZEOF(sin)); tcp_make.on_write = tcp_on_write; tcp_make.on_read = tcp_on_read; - tcp[1] = stio_dev_tcp_make (stio, STIO_SIZEOF(tcp_server_t), &tcp_make); + tcp[1] = stio_dev_sck_make (stio, STIO_SIZEOF(tcp_server_t), &tcp_make); if (!tcp[1]) { printf ("Cannot make tcp\n"); @@ -318,12 +313,23 @@ int main () ts = (tcp_server_t*)(tcp[1] + 1); ts->tally = 0; + memset (&tcp_bind, 0, STIO_SIZEOF(tcp_bind)); + //memcpy (&tcp_bind.addr, &sin, STIO_SIZEOF(sin)); + ((struct sockaddr_in*)&tcp_bind.addr)->sin_family = AF_INET; + ((struct sockaddr_in*)&tcp_bind.addr)->sin_port = htons(1234); + + if (stio_dev_sck_bind (tcp[1],&tcp_bind) <= -1) + { + printf ("stio_dev_sck_bind() failed....\n"); + goto oops; + } + tcp_lstn.backlogs = 100; tcp_lstn.on_connect = tcp_on_connect; tcp_lstn.on_disconnect = tcp_on_disconnect; - if (stio_dev_tcp_listen (tcp[1], &tcp_lstn) <= -1) + if (stio_dev_sck_listen (tcp[1], &tcp_lstn) <= -1) { - printf ("stio_dev_tcp_listen() failed....\n"); + printf ("stio_dev_sck_listen() failed....\n"); goto oops; } @@ -344,11 +350,8 @@ int main () { stio_etharp_pkt_t etharp; struct sockaddr_ll sll; - stio_adr_t adr; memset (&sll, 0, sizeof(sll)); - adr.ptr = &sll; - adr.len = sizeof(sll); sll.sll_family = AF_PACKET; // sll.sll_protocol = STIO_CONST_HTON16(STIO_ETHHDR_PROTO_ARP); // sll.sll_hatype = STIO_CONST_HTON16(STIO_ARPHDR_HTYPE_ETH); @@ -359,7 +362,6 @@ int main () /* if unicast ... */ //sll.sll_pkttype = PACKET_OTHERHOST; // sll.sll_pkttype = PACKET_BROADCAST; - memset (ðarp, 0, sizeof(etharp)); memcpy (etharp.ethhdr.source, "\xB8\x6B\x23\x9C\x10\x76", STIO_ETHADR_LEN); @@ -374,7 +376,7 @@ int main () memcpy (etharp.arppld.sha, "\xB8\x6B\x23\x9C\x10\x76", STIO_ETHADR_LEN); - if (stio_dev_sck_write (sck, ðarp, sizeof(etharp), NULL, &adr) <= -1) + if (stio_dev_sck_write (sck, ðarp, sizeof(etharp), NULL, &sll) <= -1) //if (stio_dev_sck_write (sck, ðarp.arphdr, sizeof(etharp) - sizeof(etharp.ethhdr), NULL, &adr) <= -1) { printf ("CANNOT WRITE ARP...\n"); diff --git a/stio/lib/stio-arp.h b/stio/lib/stio-arp.h index eab8eb9..0f16438 100644 --- a/stio/lib/stio-arp.h +++ b/stio/lib/stio-arp.h @@ -41,9 +41,31 @@ #define STIO_ETHADR_LEN 6 #define STIO_IP4ADR_LEN 4 +#define STIO_IP6ADR_LEN 16 #pragma pack(push) #pragma pack(1) + + +struct stio_ethadr_t +{ + stio_uint8_t v[STIO_ETHADR_LEN]; +}; +typedef struct stio_ethadr_t stio_ethadr_t; + +struct stio_ip4adr_t +{ + stio_uint8_t v[STIO_IP4ADR_LEN]; +}; +typedef struct stio_ip4adr_t stio_ip4adr_t; + +struct stio_ip6adr_t +{ + stio_uint8_t v[STIO_IP6ADR_LEN]; +}; +typedef struct stio_ip6adr_t stio_ip6adr_t; + + struct stio_ethhdr_t { stio_uint8_t dest[STIO_ETHADR_LEN]; diff --git a/stio/lib/stio-pro.c b/stio/lib/stio-pro.c index e2fb117..292bcd1 100644 --- a/stio/lib/stio-pro.c +++ b/stio/lib/stio-pro.c @@ -205,13 +205,13 @@ printf ("PRO READY...%p\n", dev); return 1; /* the device is ok. carry on reading or writing */ } -static int pro_on_read (stio_dev_t* dev, const void* data, stio_len_t len, const stio_adr_t* srcadr) +static int pro_on_read (stio_dev_t* dev, const void* data, stio_len_t len, const stio_devadr_t* srcadr) { stio_dev_pro_t* pro = (stio_dev_pro_t*)dev; return pro->on_read (pro, data, len); } -static int pro_on_write (stio_dev_t* dev, void* wrctx, const stio_adr_t* dstadr) +static int pro_on_write (stio_dev_t* dev, void* wrctx, const stio_devadr_t* dstadr) { stio_dev_pro_t* pro = (stio_dev_pro_t*)dev; return pro->on_write (pro, wrctx); diff --git a/stio/lib/stio-sck.c b/stio/lib/stio-sck.c index 48f42c4..7c4f7e8 100644 --- a/stio/lib/stio-sck.c +++ b/stio/lib/stio-sck.c @@ -32,8 +32,10 @@ #include #include #include + #include #include +#include /* ========================================================================= */ void stio_closeasyncsck (stio_t* stio, stio_sckhnd_t sck) @@ -92,24 +94,80 @@ int stio_getsckadrinfo (stio_t* stio, const stio_sckadr_t* addr, stio_scklen_t* { struct sockaddr* saddr = (struct sockaddr*)addr; - if (saddr->sa_family == AF_INET) + switch (saddr->sa_family) { - if (len) *len = STIO_SIZEOF(struct sockaddr_in); - if (family) *family = AF_INET; - return 0; - } - else if (saddr->sa_family == AF_INET6) - { - if (len) *len = STIO_SIZEOF(struct sockaddr_in6); - if (family) *family = AF_INET6; - return 0; + case AF_INET: + if (len) *len = STIO_SIZEOF(struct sockaddr_in); + if (family) *family = AF_INET; + return 0; + + case AF_INET6: + if (len) *len = STIO_SIZEOF(struct sockaddr_in6); + if (family) *family = AF_INET6; + return 0; + + case AF_PACKET: + if (len) *len = STIO_SIZEOF(struct sockaddr_ll); + if (family) *family = AF_INET6; + return 0; } stio->errnum = STIO_EINVAL; return -1; } +#if 0 +static void stio_initsckadrforip4 (stio_sckadr_t* sckadr, stio_uint16_t port, stio_ip4adr_t* ip4adr) +{ + struct sockaddr_in* sin = (struct sockaddr_in*)sckadr; + STIO_MEMSET (sin, 0, STIO_SIZEOF(*sin)); + sin->sin_family = AF_INET; + sin->sin_port = htons(port); + STIO_MEMSET (&sin->sin_addr, ip4adr, STIO_IP4ADR_LEN); +} + +static void stio_initsckadrforip6 (stio_sckadr_t* sckadr, stio_uint16_t port, stio_ip6adr_t* ip6adr) +{ + struct sockaddr_in6* sin = (struct sockaddr_in6*)sckadr; + +/* TODO: include sin6_scope_id */ + STIO_MEMSET (sin, 0, STIO_SIZEOF(*sin)); + sin->sin6_family = AF_INET; + sin->sin6_port = htons(port); + STIO_MEMSET (&sin->sin_addr, ip6adr, STIO_IP6ADR_LEN); +} + +stio void stio_intisckadrforeth (stio_sckadr_t* sckadr, int ifindex, stio_ethadr_t* ethadr) +{ + struct sockaddr_ll* sll = (struct sockaddr_in*)sckadr; + STIO_MEMSET (sll, 0, STIO_SIZEOF(*sll)); + sll->sll_family = AF_PACKET; + sll->sll_ifindex = ifindex; + sll->sll_halen = STIO_ETHADR_LEN; + STIO_MEMSET (sll->sll_addr, ethadr, STIO_ETHADR_LEN); +} +#endif + +static stio_devadr_t* sckadr_to_devadr (stio_dev_sck_t* dev, const stio_sckadr_t* sckadr, stio_devadr_t* devadr) +{ + if (sckadr) + { + stio_scklen_t len; + + stio_getsckadrinfo (dev->stio, sckadr, &len, STIO_NULL); + devadr->ptr = (void*)sckadr; + devadr->len = len; + return devadr; + } + + return STIO_NULL; +} + +static STIO_INLINE stio_sckadr_t* devadr_to_sckadr (stio_dev_sck_t* dev, const stio_devadr_t* devadr, stio_sckadr_t* sckadr) +{ + return (stio_sckadr_t*)devadr->ptr; +} /* ========================================================================= */ #define IS_STATEFUL(sck) ((sck)->dev_capa & STIO_DEV_CAPA_STREAM) @@ -120,7 +178,6 @@ struct sck_type_map_t int type; int proto; int extra_dev_capa; - int sck_capa; }; static struct sck_type_map_t sck_type_map[] = @@ -131,7 +188,9 @@ static struct sck_type_map_t sck_type_map[] = { AF_INET6, SOCK_DGRAM, 0, 0 }, { AF_PACKET, SOCK_RAW, STIO_CONST_HTON16(0x0806), 0 }, - { AF_PACKET, SOCK_DGRAM, STIO_CONST_HTON16(0x0806), 0 } + { AF_PACKET, SOCK_DGRAM, STIO_CONST_HTON16(0x0806), 0 }, + + { AF_INET, SOCK_RAW, IPPROTO_ICMP, 0, } }; /* ======================================================================== */ @@ -229,7 +288,7 @@ static stio_syshnd_t dev_sck_getsyshnd (stio_dev_t* dev) return (stio_syshnd_t)rdev->sck; } -static int dev_sck_read_stateful (stio_dev_t* dev, void* buf, stio_len_t* len, stio_adr_t* srcadr) +static int dev_sck_read_stateful (stio_dev_t* dev, void* buf, stio_len_t* len, stio_devadr_t* srcadr) { stio_dev_sck_t* tcp = (stio_dev_sck_t*)dev; ssize_t x; @@ -247,14 +306,14 @@ static int dev_sck_read_stateful (stio_dev_t* dev, void* buf, stio_len_t* len, s return 1; } -static int dev_sck_read_stateless (stio_dev_t* dev, void* buf, stio_len_t* len, stio_adr_t* srcadr) +static int dev_sck_read_stateless (stio_dev_t* dev, void* buf, stio_len_t* len, stio_devadr_t* srcadr) { stio_dev_sck_t* rdev = (stio_dev_sck_t*)dev; stio_scklen_t srcadrlen; ssize_t x; - srcadrlen = srcadr->len; - x = recvfrom (rdev->sck, buf, *len, 0, srcadr->ptr, &srcadrlen); + srcadrlen = STIO_SIZEOF(rdev->peeradr); + x = recvfrom (rdev->sck, buf, *len, 0, (struct sockaddr*)&rdev->peeradr, &srcadrlen); if (x <= -1) { if (errno == EINPROGRESS || errno == EWOULDBLOCK) return 0; /* no data available */ @@ -263,13 +322,15 @@ static int dev_sck_read_stateless (stio_dev_t* dev, void* buf, stio_len_t* len, return -1; } + srcadr->ptr = &rdev->peeradr; srcadr->len = srcadrlen; + *len = x; return 1; } -static int dev_sck_write_stateful (stio_dev_t* dev, const void* data, stio_len_t* len, const stio_adr_t* dstadr) +static int dev_sck_write_stateful (stio_dev_t* dev, const void* data, stio_len_t* len, const stio_devadr_t* dstadr) { stio_dev_sck_t* rdev = (stio_dev_sck_t*)dev; ssize_t x; @@ -305,7 +366,7 @@ static int dev_sck_write_stateful (stio_dev_t* dev, const void* data, stio_len_t return 1; } -static int dev_sck_write_stateless (stio_dev_t* dev, const void* data, stio_len_t* len, const stio_adr_t* dstadr) +static int dev_sck_write_stateless (stio_dev_t* dev, const void* data, stio_len_t* len, const stio_devadr_t* dstadr) { stio_dev_sck_t* rdev = (stio_dev_sck_t*)dev; ssize_t x; @@ -402,7 +463,7 @@ static int dev_sck_ioctl (stio_dev_t* dev, int cmd, void* arg) } rdev->state |= STIO_DEV_SCK_CONNECTING; - rdev->peer = conn->addr; + rdev->peeradr = conn->addr; rdev->on_connect = conn->on_connect; rdev->on_disconnect = conn->on_disconnect; return 0; @@ -415,7 +476,7 @@ static int dev_sck_ioctl (stio_dev_t* dev, int cmd, void* arg) /* connected immediately */ rdev->state |= STIO_DEV_SCK_CONNECTED; - rdev->peer = conn->addr; + rdev->peeradr = conn->addr; rdev->on_connect = conn->on_connect; rdev->on_disconnect = conn->on_disconnect; return 0; @@ -616,7 +677,7 @@ static int dev_evcb_sck_ready_stateful (stio_dev_t* dev, int events) clidev->dev_capa |= STIO_DEV_CAPA_IN | STIO_DEV_CAPA_OUT | STIO_DEV_CAPA_STREAM; clidev->state |= STIO_DEV_SCK_ACCEPTED; - clidev->peer = peer; + clidev->peeradr = peer; /*clidev->parent = sck;*/ /* inherit some event handlers from the parent. @@ -680,52 +741,63 @@ static int dev_evcb_sck_ready_stateless (stio_dev_t* dev, int events) return 1; /* the device is ok. carry on reading or writing */ } -static int dev_evcb_sck_on_read (stio_dev_t* dev, const void* data, stio_len_t dlen, const stio_adr_t* adr) +static int dev_evcb_sck_on_read_stateful (stio_dev_t* dev, const void* data, stio_len_t dlen, const stio_devadr_t* srcadr) { stio_dev_sck_t* rdev = (stio_dev_sck_t*)dev; - return rdev->on_read (rdev, data, dlen, adr); + return rdev->on_read (rdev, data, dlen, STIO_NULL); } -static int dev_evcb_sck_on_write (stio_dev_t* dev, stio_len_t wrlen, void* wrctx, const stio_adr_t* adr) +static int dev_evcb_sck_on_write_stateful (stio_dev_t* dev, stio_len_t wrlen, void* wrctx, const stio_devadr_t* dstadr) { stio_dev_sck_t* rdev = (stio_dev_sck_t*)dev; - return rdev->on_write (rdev, wrlen, wrctx); + return rdev->on_write (rdev, wrlen, wrctx, STIO_NULL); +} + +static int dev_evcb_sck_on_read_stateless (stio_dev_t* dev, const void* data, stio_len_t dlen, const stio_devadr_t* srcadr) +{ + stio_dev_sck_t* rdev = (stio_dev_sck_t*)dev; + return rdev->on_read (rdev, data, dlen, srcadr->ptr); +} + +static int dev_evcb_sck_on_write_stateless (stio_dev_t* dev, stio_len_t wrlen, void* wrctx, const stio_devadr_t* dstadr) +{ + stio_dev_sck_t* rdev = (stio_dev_sck_t*)dev; + return rdev->on_write (rdev, wrlen, wrctx, dstadr->ptr); } static stio_dev_evcb_t dev_evcb_sck_stateful = { dev_evcb_sck_ready_stateful, - dev_evcb_sck_on_read, - dev_evcb_sck_on_write + dev_evcb_sck_on_read_stateful, + dev_evcb_sck_on_write_stateful }; static stio_dev_evcb_t dev_evcb_sck_stateless = { dev_evcb_sck_ready_stateless, - dev_evcb_sck_on_read, - dev_evcb_sck_on_write + dev_evcb_sck_on_read_stateless, + dev_evcb_sck_on_write_stateless }; /* ========================================================================= */ -stio_dev_sck_t* stio_dev_sck_make (stio_t* stio, stio_size_t xtnsize, const stio_dev_sck_make_t* mkinf) +stio_dev_sck_t* stio_dev_sck_make (stio_t* stio, stio_size_t xtnsize, const stio_dev_sck_make_t* info) { stio_dev_sck_t* rdev; - - if (mkinf->type < 0 && mkinf->type >= STIO_COUNTOF(sck_type_map)) + if (info->type < 0 && info->type >= STIO_COUNTOF(sck_type_map)) { stio->errnum = STIO_EINVAL; return STIO_NULL; } - if (sck_type_map[mkinf->type].extra_dev_capa & STIO_DEV_CAPA_STREAM) /* can't use the IS_STATEFUL() macro yet */ + if (sck_type_map[info->type].extra_dev_capa & STIO_DEV_CAPA_STREAM) /* can't use the IS_STATEFUL() macro yet */ { - rdev = (stio_dev_sck_t*)stio_makedev (stio, STIO_SIZEOF(stio_dev_sck_t) + xtnsize, &dev_mth_sck_stateful, &dev_evcb_sck_stateful, (void*)mkinf); + rdev = (stio_dev_sck_t*)stio_makedev (stio, STIO_SIZEOF(stio_dev_sck_t) + xtnsize, &dev_mth_sck_stateful, &dev_evcb_sck_stateful, (void*)info); } else { - rdev = (stio_dev_sck_t*)stio_makedev (stio, STIO_SIZEOF(stio_dev_sck_t) + xtnsize, &dev_mth_sck_stateless, &dev_evcb_sck_stateless, (void*)mkinf); + rdev = (stio_dev_sck_t*)stio_makedev (stio, STIO_SIZEOF(stio_dev_sck_t) + xtnsize, &dev_mth_sck_stateless, &dev_evcb_sck_stateless, (void*)info); } return rdev; @@ -746,12 +818,14 @@ int stio_dev_sck_listen (stio_dev_sck_t* dev, stio_dev_sck_listen_t* info) return stio_dev_ioctl ((stio_dev_t*)dev, STIO_DEV_SCK_LISTEN, info); } -int stio_dev_sck_write (stio_dev_sck_t* dev, const void* data, stio_len_t dlen, void* wrctx, const stio_adr_t* dstadr) +int stio_dev_sck_write (stio_dev_sck_t* dev, const void* data, stio_len_t dlen, void* wrctx, const stio_sckadr_t* dstadr) { - return stio_dev_write ((stio_dev_t*)dev, data, dlen, wrctx, dstadr); + stio_devadr_t devadr; + return stio_dev_write ((stio_dev_t*)dev, data, dlen, wrctx, sckadr_to_devadr(dev, dstadr, &devadr)); } -int stio_dev_sck_timedwrite (stio_dev_sck_t* dev, const void* data, stio_len_t dlen, const stio_ntime_t* tmout, void* wrctx, const stio_adr_t* dstadr) +int stio_dev_sck_timedwrite (stio_dev_sck_t* dev, const void* data, stio_len_t dlen, const stio_ntime_t* tmout, void* wrctx, const stio_sckadr_t* dstadr) { - return stio_dev_write ((stio_dev_t*)dev, data, dlen, wrctx, dstadr); + stio_devadr_t devadr; + return stio_dev_timedwrite ((stio_dev_t*)dev, data, dlen, tmout, wrctx, sckadr_to_devadr(dev, dstadr, &devadr)); } diff --git a/stio/lib/stio-sck.h b/stio/lib/stio-sck.h index 186df3f..226e579 100644 --- a/stio/lib/stio-sck.h +++ b/stio/lib/stio-sck.h @@ -101,15 +101,16 @@ typedef enum stio_dev_sck_state_t stio_dev_sck_state_t; typedef struct stio_dev_sck_t stio_dev_sck_t; typedef int (*stio_dev_sck_on_read_t) ( - stio_dev_sck_t* dev, - const void* data, - stio_len_t dlen, - const stio_adr_t* srcadr + stio_dev_sck_t* dev, + const void* data, + stio_len_t dlen, + const stio_sckadr_t* srcadr ); typedef int (*stio_dev_sck_on_write_t) ( - stio_dev_sck_t* dev, - stio_len_t wrlen, - void* wrctx + stio_dev_sck_t* dev, + stio_len_t wrlen, + void* wrctx, + const stio_sckadr_t* dstadr ); typedef int (*stio_dev_sck_on_connect_t) (stio_dev_sck_t* dev); @@ -123,7 +124,13 @@ enum stio_dev_sck_type_t STIO_DEV_SCK_UDP6, STIO_DEV_SCK_ARP, - STIO_DEV_SCK_ARP_DGRAM + STIO_DEV_SCK_ARP_DGRAM, + + STIO_DEV_SCK_ICMP4 + +#if 0 + STIO_DEV_SCK_RAW, /* raw L2-level packet */ +#endif }; typedef enum stio_dev_sck_type_t stio_dev_sck_type_t; @@ -165,11 +172,9 @@ struct stio_dev_sck_accept_t { stio_syshnd_t sck; /* TODO: add timeout */ - stio_sckadr_t peer; + stio_sckadr_t peeradr; }; - - struct stio_dev_sck_t { STIO_DEV_HEADERS; @@ -186,7 +191,16 @@ struct stio_dev_sck_t stio_dev_sck_on_connect_t on_connect; stio_dev_sck_on_disconnect_t on_disconnect; stio_tmridx_t tmridx_connect; - stio_sckadr_t peer; + + /* peer address for a stateful stream socket. valid if one of the + * followings is set in state: + * STIO_DEV_TCP_ACCEPTED + * STIO_DEV_TCP_CONNECTED + * STIO_DEV_TCP_CONNECTING + * + * also used as a placeholder to store source address for + * a stateless socket */ + stio_sckadr_t peeradr; }; #ifdef __cplusplus @@ -245,7 +259,7 @@ STIO_EXPORT int stio_dev_sck_write ( const void* data, stio_len_t len, void* wrctx, - const stio_adr_t* dstadr + const stio_sckadr_t* dstadr ); STIO_EXPORT int stio_dev_sck_timedwrite ( @@ -254,7 +268,7 @@ STIO_EXPORT int stio_dev_sck_timedwrite ( stio_len_t len, const stio_ntime_t* tmout, void* wrctx, - const stio_adr_t* dstadr + const stio_sckadr_t* dstadr ); #if defined(STIO_HAVE_INLINE) @@ -271,7 +285,7 @@ static STIO_INLINE int stio_dev_sck_read (stio_dev_sck_t* sck, int enabled) } /* -static STIO_INLINE int stio_dev_sck_write (stio_dev_sck_t* sck, const void* data, stio_len_t len, void* wrctx, const stio_adr_t* dstadr) +static STIO_INLINE int stio_dev_sck_write (stio_dev_sck_t* sck, const void* data, stio_len_t len, void* wrctx, const stio_devadr_t* dstadr) { return stio_dev_write ((stio_dev_t*)sck, data, len, wrctx, STIO_NULL); } diff --git a/stio/lib/stio-tcp.c b/stio/lib/stio-tcp.c index ea3954d..a47a8f7 100644 --- a/stio/lib/stio-tcp.c +++ b/stio/lib/stio-tcp.c @@ -264,7 +264,7 @@ static int tcp_ioctl (stio_dev_t* dev, int cmd, void* arg) } tcp->state |= STIO_DEV_TCP_CONNECTING; - tcp->peer = conn->addr; + tcp->peeradr = conn->addr; tcp->on_connect = conn->on_connect; tcp->on_disconnect = conn->on_disconnect; return 0; @@ -277,7 +277,7 @@ static int tcp_ioctl (stio_dev_t* dev, int cmd, void* arg) /* connected immediately */ tcp->state |= STIO_DEV_TCP_CONNECTED; - tcp->peer = conn->addr; + tcp->peeradr = conn->addr; tcp->on_connect = conn->on_connect; tcp->on_disconnect = conn->on_disconnect; return 0; @@ -465,7 +465,7 @@ printf ("TCP READY...%p\n", dev); clitcp->dev_capa |= STIO_DEV_CAPA_IN | STIO_DEV_CAPA_OUT | STIO_DEV_CAPA_STREAM; clitcp->state |= STIO_DEV_TCP_ACCEPTED; - clitcp->peer = peer; + clitcp->peeradr = peer; /*clitcp->parent = tcp;*/ /* inherit some event handlers from the parent. diff --git a/stio/lib/stio-tcp.h b/stio/lib/stio-tcp.h index 2582e2e..3c2eb7c 100644 --- a/stio/lib/stio-tcp.h +++ b/stio/lib/stio-tcp.h @@ -68,7 +68,7 @@ struct stio_dev_tcp_t * STIO_DEV_TCP_ACCEPTED * STIO_DEV_TCP_CONNECTED * STIO_DEV_TCP_CONNECTING */ - stio_sckadr_t peer; + stio_sckadr_t peeradr; /** return 0 on succes, -1 on failure/ * called on a new tcp device for an accepted client or @@ -121,7 +121,7 @@ struct stio_dev_tcp_accept_t { stio_syshnd_t sck; /* TODO: add timeout */ - stio_sckadr_t peer; + stio_sckadr_t peeradr; }; #ifdef __cplusplus diff --git a/stio/lib/stio-udp.c b/stio/lib/stio-udp.c index 47ff500..f240fa6 100644 --- a/stio/lib/stio-udp.c +++ b/stio/lib/stio-udp.c @@ -25,6 +25,7 @@ */ +#if 0 #include "stio-udp.h" #include "stio-prv.h" @@ -82,7 +83,7 @@ static stio_syshnd_t udp_getsyshnd (stio_dev_t* dev) return (stio_syshnd_t)udp->sck; } -static int udp_read (stio_dev_t* dev, void* buf, stio_len_t* len, stio_adr_t* adr) +static int udp_read (stio_dev_t* dev, void* buf, stio_len_t* len, stio_devadr_t* adr) { stio_dev_udp_t* udp = (stio_dev_udp_t*)dev; stio_scklen_t addrlen; @@ -90,8 +91,8 @@ static int udp_read (stio_dev_t* dev, void* buf, stio_len_t* len, stio_adr_t* ad /* TODO: udp_read need source address ... have to extend the send callback to accept the source address */ printf ("UDP RECVFROM...\n"); - addrlen = STIO_SIZEOF(udp->peer); - x = recvfrom (udp->sck, buf, *len, 0, (struct sockaddr*)&udp->peer, &addrlen); + addrlen = STIO_SIZEOF(udp->peeradr); + x = recvfrom (udp->sck, buf, *len, 0, (struct sockaddr*)&udp->peeradr, &addrlen); if (x <= -1) { if (errno == EINPROGRESS || errno == EWOULDBLOCK) return 0; /* no data available */ @@ -104,7 +105,7 @@ printf ("UDP RECVFROM...\n"); return 1; } -static int udp_write (stio_dev_t* dev, const void* data, stio_len_t* len, const stio_adr_t* adr) +static int udp_write (stio_dev_t* dev, const void* data, stio_len_t* len, const stio_devadr_t* adr) { stio_dev_udp_t* udp = (stio_dev_udp_t*)udp; ssize_t x; @@ -184,3 +185,5 @@ stio_dev_udp_t* stio_dev_udp_make (stio_t* stio, stio_size_t xtnsize, const stio { return (stio_dev_udp_t*)stio_makedev (stio, STIO_SIZEOF(stio_dev_udp_t) + xtnsize, &udp_mth, &udp_evcb, (void*)data); } + +#endif diff --git a/stio/lib/stio-udp.h b/stio/lib/stio-udp.h index e45b7ce..c087806 100644 --- a/stio/lib/stio-udp.h +++ b/stio/lib/stio-udp.h @@ -74,12 +74,12 @@ static STIO_INLINE int stio_dev_udp_read (stio_dev_udp_t* udp, int enabled) return stio_dev_read ((stio_dev_t*)udp, enabled); } -static STIO_INLINE int stio_dev_udp_write (stio_dev_udp_t* udp, const void* data, stio_len_t len, void* wrctx, const stio_adr_t* dstadr) +static STIO_INLINE int stio_dev_udp_write (stio_dev_udp_t* udp, const void* data, stio_len_t len, void* wrctx, const stio_devadr_t* dstadr) { return stio_dev_write ((stio_dev_t*)udp, data, len, wrctx, dstadr); } -static STIO_INLINE int stio_dev_udp_timedwrite (stio_dev_udp_t* udp, const void* data, stio_len_t len, const stio_ntime_t* tmout, void* wrctx, const stio_adr_t* dstadr) +static STIO_INLINE int stio_dev_udp_timedwrite (stio_dev_udp_t* udp, const void* data, stio_len_t len, const stio_ntime_t* tmout, void* wrctx, const stio_devadr_t* dstadr) { return stio_dev_timedwrite ((stio_dev_t*)udp, data, len, tmout, wrctx, dstadr); } diff --git a/stio/lib/stio.c b/stio/lib/stio.c index a03ac9f..ec92598 100644 --- a/stio/lib/stio.c +++ b/stio/lib/stio.c @@ -266,7 +266,7 @@ printf ("has urgent data...\n"); if (dev && stio->revs[i].events & EPOLLIN) { - stio_adr_t srcadr; + stio_devadr_t srcadr; stio_len_t len; int x; @@ -750,7 +750,7 @@ static void on_write_timeout (stio_t* stio, const stio_ntime_t* now, stio_tmrjob if (x <= -1) stio_dev_halt (dev); } -static int __dev_write (stio_dev_t* dev, const void* data, stio_len_t len, const stio_ntime_t* tmout, void* wrctx, const stio_adr_t* dstadr) +static int __dev_write (stio_dev_t* dev, const void* data, stio_len_t len, const stio_ntime_t* tmout, void* wrctx, const stio_devadr_t* dstadr) { const stio_uint8_t* uptr; stio_len_t urem, ulen; @@ -894,12 +894,12 @@ enqueue_data: return 0; /* request pused to a write queue. */ } -int stio_dev_write (stio_dev_t* dev, const void* data, stio_len_t len, void* wrctx, const stio_adr_t* dstadr) +int stio_dev_write (stio_dev_t* dev, const void* data, stio_len_t len, void* wrctx, const stio_devadr_t* dstadr) { return __dev_write (dev, data, len, STIO_NULL, wrctx, dstadr); } -int stio_dev_timedwrite (stio_dev_t* dev, const void* data, stio_len_t len, const stio_ntime_t* tmout, void* wrctx, const stio_adr_t* dstadr) +int stio_dev_timedwrite (stio_dev_t* dev, const void* data, stio_len_t len, const stio_ntime_t* tmout, void* wrctx, const stio_devadr_t* dstadr) { return __dev_write (dev, data, len, tmout, wrctx, dstadr); } diff --git a/stio/lib/stio.h b/stio/lib/stio.h index 1373610..564b040 100644 --- a/stio/lib/stio.h +++ b/stio/lib/stio.h @@ -48,8 +48,8 @@ struct stio_ntime_t #define STIO_SYSHND_INVALID (-1) #endif -typedef struct stio_adr_t stio_adr_t; -struct stio_adr_t +typedef struct stio_devadr_t stio_devadr_t; +struct stio_devadr_t { int len; void* ptr; @@ -143,10 +143,10 @@ struct stio_dev_mth_t /* return -1 on failure, 0 if no data is availble, 1 otherwise. * when returning 1, *len must be sent to the length of data read. * if *len is set to 0, it's treated as EOF. */ - int (*read) (stio_dev_t* dev, void* data, stio_len_t* len, stio_adr_t* srcadr); + int (*read) (stio_dev_t* dev, void* data, stio_len_t* len, stio_devadr_t* srcadr); /* ------------------------------------------------------------------ */ - int (*write) (stio_dev_t* dev, const void* data, stio_len_t* len, const stio_adr_t* dstadr); + int (*write) (stio_dev_t* dev, const void* data, stio_len_t* len, const stio_devadr_t* dstadr); /* ------------------------------------------------------------------ */ int (*ioctl) (stio_dev_t* dev, int cmd, void* arg); @@ -163,13 +163,13 @@ struct stio_dev_evcb_t /* return -1 on failure, 0 or 1 on success. * when 0 is returned, the main loop stops the attempt to read more data. * when 1 is returned, the main loop attempts to read more data without*/ - int (*on_read) (stio_dev_t* dev, const void* data, stio_len_t len, const stio_adr_t* srcadr); + int (*on_read) (stio_dev_t* dev, const void* data, stio_len_t len, const stio_devadr_t* srcadr); /* return -1 on failure, 0 on success. * wrlen is the length of data written. it is the length of the originally * posted writing request for a stream device. For a non stream device, it * may be shorter than the originally posted length. */ - int (*on_write) (stio_dev_t* dev, stio_len_t wrlen, void* wrctx, const stio_adr_t* dstadr); + int (*on_write) (stio_dev_t* dev, stio_len_t wrlen, void* wrctx, const stio_devadr_t* dstadr); }; struct stio_wq_t @@ -184,7 +184,7 @@ struct stio_wq_t stio_dev_t* dev; /* back-pointer to the device */ stio_tmridx_t tmridx; - stio_adr_t dstadr; + stio_devadr_t dstadr; }; #define STIO_WQ_INIT(wq) ((wq)->next = (wq)->prev = (wq)) @@ -368,7 +368,7 @@ STIO_EXPORT int stio_dev_write ( const void* data, stio_len_t len, void* wrctx, - const stio_adr_t* dstadr + const stio_devadr_t* dstadr ); @@ -378,7 +378,7 @@ STIO_EXPORT int stio_dev_timedwrite ( stio_len_t len, const stio_ntime_t* tmout, void* wrctx, - const stio_adr_t* dstadr + const stio_devadr_t* dstadr ); STIO_EXPORT void stio_dev_halt (