Re: [libuv] determine client ipv4 or ipv6
using second AF_INETXx seems more robust approach, thank you. On 9/19/16, Ben Noordhuis wrote: > On Mon, Sep 19, 2016 at 4:46 AM, kang joni wrote: >> how to check if client is connected using ipv4 or ipv6 in libuv? >> I have this code, >> >> struct sockaddr_storage cp; >> int ilen=sizeof(cp); >> uv_tcp_getpeername((uv_tcp_t*)client, (struct sockaddr*)&cp,&ilen); >> int ret=uv_ip6_name((const struct >> sockaddr_in6*)&cp,(char*)ipname,sizeof(ipname)); >> //I don't think this is correct, but I haven't any clues >> if (retip<0 || std::string("::")==ipname) >> { >> retip=uv_ip4_name((const struct sockaddr_in*)&cp,(char*)ipname, >> sizeof(ipname)); >> >> } >> >> any corrections ? thanks > > You can simply check if ilen == sizeof(struct sockaddr_in) or ilen == > sizeof(struct sockaddr_in6) but a more thorough solution is to check > the address family: > > struct sockaddr *sa = (struct sockaddr *) &cp; > switch (sa->sa_family) { > case AF_INET: /* ... */; break; > case AF_INET6: /* ... */; break; > } > > -- > You received this message because you are subscribed to a topic in the > Google Groups "libuv" group. > To unsubscribe from this topic, visit > https://groups.google.com/d/topic/libuv/npXnJIDdKMo/unsubscribe. > To unsubscribe from this group and all its topics, send an email to > libuv+unsubscr...@googlegroups.com. > To post to this group, send email to libuv@googlegroups.com. > Visit this group at https://groups.google.com/group/libuv. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "libuv" group. To unsubscribe from this group and stop receiving emails from it, send an email to libuv+unsubscr...@googlegroups.com. To post to this group, send email to libuv@googlegroups.com. Visit this group at https://groups.google.com/group/libuv. For more options, visit https://groups.google.com/d/optout.
Re: [libuv] determine client ipv4 or ipv6
On Mon, Sep 19, 2016 at 4:46 AM, kang joni wrote: > how to check if client is connected using ipv4 or ipv6 in libuv? > I have this code, > > struct sockaddr_storage cp; > int ilen=sizeof(cp); > uv_tcp_getpeername((uv_tcp_t*)client, (struct sockaddr*)&cp,&ilen); > int ret=uv_ip6_name((const struct > sockaddr_in6*)&cp,(char*)ipname,sizeof(ipname)); > //I don't think this is correct, but I haven't any clues > if (retip<0 || std::string("::")==ipname) > { > retip=uv_ip4_name((const struct sockaddr_in*)&cp,(char*)ipname, > sizeof(ipname)); > > } > > any corrections ? thanks You can simply check if ilen == sizeof(struct sockaddr_in) or ilen == sizeof(struct sockaddr_in6) but a more thorough solution is to check the address family: struct sockaddr *sa = (struct sockaddr *) &cp; switch (sa->sa_family) { case AF_INET: /* ... */; break; case AF_INET6: /* ... */; break; } -- You received this message because you are subscribed to the Google Groups "libuv" group. To unsubscribe from this group and stop receiving emails from it, send an email to libuv+unsubscr...@googlegroups.com. To post to this group, send email to libuv@googlegroups.com. Visit this group at https://groups.google.com/group/libuv. For more options, visit https://groups.google.com/d/optout.
[libuv] determine client ipv4 or ipv6
how to check if client is connected using ipv4 or ipv6 in libuv? I have this code, struct sockaddr_storage cp; int ilen=sizeof(cp); uv_tcp_getpeername((uv_tcp_t*)client, (struct sockaddr*)&cp,&ilen); int ret=uv_ip6_name((const struct sockaddr_in6*)&cp,(char*)ipname,sizeof(ipname)); //I don't think this is correct, but I haven't any clues if (retip<0 || std::string("::")==ipname) { retip=uv_ip4_name((const struct sockaddr_in*)&cp,(char*)ipname, sizeof(ipname)); } any corrections ? thanks -- You received this message because you are subscribed to the Google Groups "libuv" group. To unsubscribe from this group and stop receiving emails from it, send an email to libuv+unsubscr...@googlegroups.com. To post to this group, send email to libuv@googlegroups.com. Visit this group at https://groups.google.com/group/libuv. For more options, visit https://groups.google.com/d/optout.