Skip to content

Add multicast helper function#841

Open
rorschach-py wants to merge 8 commits into
ithewei:masterfrom
rorschach-py:master
Open

Add multicast helper function#841
rorschach-py wants to merge 8 commits into
ithewei:masterfrom
rorschach-py:master

Conversation

@rorschach-py

Copy link
Copy Markdown

添加了一些组播常用的函数,原本想把ipv4和ipv6的函数统一成一个,但是没找到简单判断sockfd的ip版本的办法,参数改成hio_t的话会与其他函数不一致

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a set of multicast-related helper APIs to base/hsocket.h / base/hsocket.c, expanding libhv’s low-level socket utilities with common UDP multicast configuration and group join/leave helpers for both IPv4 and IPv6.

Changes:

  • Added is_multicast_ipv4/is_multicast_ipv6 (and is_multicast_ip) helpers.
  • Added UDP multicast socket option helpers (loopback, TTL/hops) and join/leave group helpers for IPv4/IPv6.
  • Minor fix/improvement in address parsing (is_ipv4/is_ipv6 use the correct destination field; ResolveAddr now returns immediately for IPv6 literals).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.

File Description
base/hsocket.h Adds inline UDP multicast option + join/leave helper APIs and multicast IP predicate wrapper.
base/hsocket.c Implements multicast IP checks and fixes/optimizes some IP parsing behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread base/hsocket.c
Comment on lines +59 to +65
bool is_multicast_ipv4(const char* host) {
struct sockaddr_in addr;
if(inet_pton(AF_INET, host, &addr.sin_addr)){
return addr.sin_addr.s_net >> 4 == 0xE;
}
return 0;
}
Comment thread base/hsocket.c
Comment on lines +67 to 73
bool is_multicast_ipv6(const char* host) {
struct sockaddr_in6 addr;
if(inet_pton(AF_INET6, host, &addr.sin6_addr)){
return addr.sin6_addr.s6_addr[0] == 0xFF;
}
return 0;
}
Comment thread base/hsocket.h
Comment on lines +292 to +299
HV_INLINE int udp_multicast_loop(int sockfd, int value DEFAULT(1))
{
return setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_LOOP, (const char*)&value, sizeof(int));
}
HV_INLINE int udp_multicast_loop6(int sockfd, int value DEFAULT(1))
{
return setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, (const char*)&value, sizeof(int));
}
Comment thread base/hsocket.h
Comment on lines +301 to +308
HV_INLINE int udp_multicast_ttl(int sockfd, int value)
{
return setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, (const char*)&value, sizeof(int));
}
HV_INLINE int udp_multicast_ttl6(int sockfd, int value)
{
return setsockopt(sockfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (const char*)&value, sizeof(int));
}
Comment thread base/hsocket.h
Comment on lines +310 to +316
HV_INLINE int udp_joingroup(int sockfd, const char* group_ip, const char* intf_ip DEFAULT(ANYADDR))
{
struct ip_mreq mreq;
inet_pton(AF_INET, group_ip, &mreq.imr_multiaddr);
inet_pton(AF_INET, intf_ip, &mreq.imr_interface);
return setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char*)&mreq, sizeof(mreq));
}
Comment thread base/hsocket.h
Comment on lines +318 to +324
HV_INLINE int udp_leavegroup(int sockfd, const char* group_ip, const char* intf_ip DEFAULT(ANYADDR))
{
struct ip_mreq mreq;
inet_pton(AF_INET, group_ip, &mreq.imr_multiaddr);
inet_pton(AF_INET, intf_ip, &mreq.imr_interface);
return setsockopt(sockfd, IPPROTO_IP, IP_DROP_MEMBERSHIP, (const char*)&mreq, sizeof(mreq));
}
Comment thread base/hsocket.h
Comment on lines +327 to +333
HV_INLINE int udp_joingroup6(int sockfd, const char* group_ip, const char* intf_idx DEFAULT("0"))
{
struct ipv6_mreq mreq;
inet_pton(AF_INET6, group_ip, &mreq.ipv6mr_multiaddr);
mreq.ipv6mr_interface = atoi(intf_idx);
return setsockopt(sockfd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (const char*)&mreq, sizeof(mreq));
}
Comment thread base/hsocket.h
Comment on lines +335 to +341
HV_INLINE int udp_leavegroup6(int sockfd, const char* group_ip, const char* intf_idx DEFAULT("0"))
{
struct ipv6_mreq mreq;
inet_pton(AF_INET6, group_ip, &mreq.ipv6mr_multiaddr);
mreq.ipv6mr_interface = atoi(intf_idx);
return setsockopt(sockfd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, (const char*)&mreq, sizeof(mreq));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants