-
Notifications
You must be signed in to change notification settings - Fork 743
Added support to for SO_BINDTODEVICE on listening socket #494
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
Open
Mario-Klebsch
wants to merge
1
commit into
tinyproxy:master
Choose a base branch
from
Mario-Klebsch:pull_request_bindtodevice
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -35,6 +35,10 @@ | |||||
| #include "conf.h" | ||||||
| #include "loop.h" | ||||||
| #include "sblist.h" | ||||||
| #ifdef SO_BINDTODEVICE | ||||||
| #include <net/if.h> | ||||||
| #endif | ||||||
|
|
||||||
|
|
||||||
| /* | ||||||
| * Return a human readable error for getaddrinfo() and getnameinfo(). | ||||||
|
|
@@ -310,6 +314,88 @@ static int listen_on_one_socket(struct addrinfo *ad) | |||||
| return listenfd; | ||||||
| } | ||||||
|
|
||||||
| #ifdef SO_BINDTODEVICE | ||||||
| static int listen_on_interface(const char *interface, uint16_t port) | ||||||
| { | ||||||
| int listenfd; | ||||||
| int ret; | ||||||
| int ipv6=1; | ||||||
| const int on = 1; | ||||||
| const int off = 0; | ||||||
| struct sockaddr_storage addr; | ||||||
| struct ifreq ifr; | ||||||
|
|
||||||
| listenfd = socket(PF_INET6, SOCK_STREAM, 0); | ||||||
| if (listenfd == -1) { | ||||||
| listenfd = socket(PF_INET, SOCK_STREAM, 0); | ||||||
| if (listenfd == -1) { | ||||||
| log_message(LOG_ERR, "socket() failed: %s", strerror(errno)); | ||||||
| return -1; | ||||||
| } | ||||||
| ipv6=0; | ||||||
|
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.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| ret = setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)); | ||||||
| if (ret != 0) { | ||||||
| log_message(LOG_ERR, | ||||||
| "setsockopt failed to set SO_REUSEADDR: %s", | ||||||
| strerror(errno)); | ||||||
| close(listenfd); | ||||||
| return -1; | ||||||
| } | ||||||
|
|
||||||
| strncpy(ifr.ifr_name, interface, sizeof(ifr.ifr_name)); | ||||||
| if (setsockopt(listenfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) { | ||||||
| log_message(LOG_ERR, "setsockopt(SO_BINDTODEVICE,%s) failed: %s", interface, strerror (errno)); | ||||||
|
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. I have not read the surrounding tinyproxy code, but I think it should be
Suggested change
|
||||||
| close(listenfd); | ||||||
| return -1; | ||||||
| } | ||||||
|
|
||||||
| if (ipv6) { | ||||||
| struct sockaddr_in6 *addr6; | ||||||
| ret = setsockopt(listenfd, IPPROTO_IPV6, IPV6_V6ONLY, &off, | ||||||
| sizeof(off)); | ||||||
| if (ret != 0) { | ||||||
| log_message(LOG_ERR, | ||||||
| "setsockopt failed to clear IPV6_V6ONLY: %s", | ||||||
| strerror(errno)); | ||||||
| close(listenfd); | ||||||
| return -1; | ||||||
| } | ||||||
| addr6 = (struct sockaddr_in6*)&addr; | ||||||
| memset(addr6, 0, sizeof(*addr6)); | ||||||
| addr6->sin6_family = AF_INET6; | ||||||
| addr6->sin6_port = htons(port); | ||||||
| addr6->sin6_addr = in6addr_any; | ||||||
| } else { | ||||||
| struct sockaddr_in *addr4; | ||||||
| addr4 = (struct sockaddr_in*)&addr; | ||||||
| memset(addr4, 0, sizeof(*addr4)); | ||||||
| addr4->sin_family = AF_INET; | ||||||
| addr4->sin_port = htons(port); | ||||||
| addr4->sin_addr.s_addr = htonl(INADDR_ANY); | ||||||
| } | ||||||
|
|
||||||
| ret = bind(listenfd, (struct sockaddr*)&addr, sizeof(addr)); | ||||||
| if (ret != 0) { | ||||||
| log_message(LOG_ERR, "bind failed: %s", strerror (errno)); | ||||||
| close(listenfd); | ||||||
| return -1; | ||||||
| } | ||||||
|
|
||||||
| ret = listen(listenfd, MAXLISTEN); | ||||||
| if (ret != 0) { | ||||||
| log_message(LOG_ERR, "listen failed: %s", strerror(errno)); | ||||||
| close(listenfd); | ||||||
| return -1; | ||||||
| } | ||||||
|
|
||||||
| log_message(LOG_INFO, "listening on fd [%d]", listenfd); | ||||||
|
|
||||||
| return listenfd; | ||||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| /* | ||||||
| * Start listening on a socket. Create a socket with the selected port. | ||||||
| * If the provided address is NULL, we may listen on multiple sockets, | ||||||
|
|
@@ -333,6 +419,17 @@ int listen_sock (const char *addr, uint16_t port, sblist* listen_fds) | |||||
| log_message(LOG_INFO, "listen_sock called with addr = '%s'", | ||||||
| addr == NULL ? "(NULL)" : addr); | ||||||
|
|
||||||
| #ifdef SO_BINDTODEVICE | ||||||
| if (addr && if_nametoindex(addr)) { | ||||||
| int listenfd; | ||||||
| listenfd = listen_on_interface(addr, port); | ||||||
| if (listenfd >= 0) { | ||||||
| sblist_add (listen_fds, &listenfd); | ||||||
| return 0; | ||||||
| } | ||||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| memset (&hints, 0, sizeof (struct addrinfo)); | ||||||
| hints.ai_family = AF_UNSPEC; | ||||||
| hints.ai_socktype = SOCK_STREAM; | ||||||
|
|
||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@BlankEclair: please refrain on suggesting whitespace changes on random PRs you absolutely are not involved with. if i want such changes, i will ask for them myself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry--I overzealously commented on the inconsistency
It won't happen again