-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Enable NetworkAddressChange on FreeBSD #1602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stephentoub
merged 1 commit into
dotnet:master
from
am11:feature/gh-33530_network-change-on-freebsd
Jan 22, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
File renamed without changes.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,35 +10,48 @@ | |
| #include "pal_utilities.h" | ||
|
|
||
| #include <errno.h> | ||
| #include <linux/rtnetlink.h> | ||
| #include <net/if.h> | ||
| #include <sys/socket.h> | ||
| #include <sys/types.h> | ||
| #include <sys/uio.h> | ||
| #include <unistd.h> | ||
| #if HAVE_LINUX_RTNETLINK_H | ||
| #include <linux/rtnetlink.h> | ||
| #elif HAVE_RT_MSGHDR | ||
| #include <net/route.h> | ||
| #else | ||
| #error System must have linux/rtnetlink.h or net/route.h. | ||
| #endif | ||
|
|
||
| #pragma clang diagnostic ignored "-Wcast-align" // NLMSG_* macros trigger this | ||
|
|
||
| Error SystemNative_CreateNetworkChangeListenerSocket(int32_t* retSocket) | ||
| { | ||
| #if HAVE_LINUX_RTNETLINK_H | ||
| struct sockaddr_nl sa; | ||
| memset(&sa, 0, sizeof(struct sockaddr_nl)); | ||
|
|
||
| sa.nl_family = AF_NETLINK; | ||
| sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR | RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE; | ||
| int32_t sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); | ||
| #elif HAVE_RT_MSGHDR | ||
| int32_t sock = socket(PF_ROUTE, SOCK_RAW, 0); | ||
| #endif | ||
| if (sock == -1) | ||
| { | ||
| *retSocket = -1; | ||
| return (Error)(SystemNative_ConvertErrorPlatformToPal(errno)); | ||
| } | ||
|
|
||
| #if HAVE_LINUX_RTNETLINK_H | ||
| if (bind(sock, (struct sockaddr*)(&sa), sizeof(sa)) != 0) | ||
| { | ||
| *retSocket = -1; | ||
| Error palError = (Error)(SystemNative_ConvertErrorPlatformToPal(errno)); | ||
| close(sock); | ||
| return palError; | ||
| } | ||
| #endif | ||
|
|
||
| *retSocket = sock; | ||
| return Error_SUCCESS; | ||
|
|
@@ -50,6 +63,7 @@ Error SystemNative_CloseNetworkChangeListenerSocket(int32_t socket) | |
| return err == 0 || CheckInterrupted(err) ? Error_SUCCESS : (Error)(SystemNative_ConvertErrorPlatformToPal(errno)); | ||
| } | ||
|
|
||
| #if HAVE_LINUX_RTNETLINK_H | ||
| static NetworkChangeKind ReadNewLinkMessage(struct nlmsghdr* hdr) | ||
| { | ||
| assert(hdr != NULL); | ||
|
|
@@ -117,3 +131,44 @@ void SystemNative_ReadEvents(int32_t sock, NetworkChangeEvent onNetworkChange) | |
| } | ||
| } | ||
| } | ||
| #elif HAVE_RT_MSGHDR | ||
| void SystemNative_ReadEvents(int32_t sock, NetworkChangeEvent onNetworkChange) | ||
| { | ||
| char buffer[4096]; | ||
| ssize_t count = read(sock, buffer, sizeof(buffer)); | ||
| if (count < 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| struct rt_msghdr msghdr; | ||
| for (char *ptr = buffer; (ptr + sizeof(struct rt_msghdr)) <= (buffer + count); ptr += msghdr.rtm_msglen) | ||
| { | ||
| memcpy(&msghdr, ptr, sizeof(msghdr)); | ||
| if (msghdr.rtm_version != RTM_VERSION) | ||
| { | ||
| // version mismatch | ||
| return; | ||
| } | ||
|
|
||
| switch (msghdr.rtm_type) | ||
| { | ||
| case RTM_NEWADDR: | ||
| onNetworkChange(sock, AddressAdded); | ||
| break; | ||
| case RTM_DELADDR: | ||
| onNetworkChange(sock, AddressRemoved); | ||
| break; | ||
| case RTM_ADD: | ||
| case RTM_DELETE: | ||
| case RTM_REDIRECT: | ||
| { | ||
| onNetworkChange(sock, AvailabilityChanged); | ||
| return; | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: why are the braces needed here but not for the other cases?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed in #2071. |
||
| default: | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| #endif | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,6 @@ | |
|
|
||
| #include "pal_compiler.h" | ||
| #include "pal_types.h" | ||
| #include <linux/netlink.h> | ||
|
|
||
| typedef enum | ||
| { | ||
|
|
||
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
File renamed without changes.
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.