Add multicast helper function#841
Open
rorschach-py wants to merge 8 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
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(andis_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_ipv6use the correct destination field;ResolveAddrnow 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 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 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 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 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 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 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 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 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)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
添加了一些组播常用的函数,原本想把ipv4和ipv6的函数统一成一个,但是没找到简单判断sockfd的ip版本的办法,参数改成hio_t的话会与其他函数不一致