-
-
Notifications
You must be signed in to change notification settings - Fork 140
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
options/posix: Add support for IPv6 addresses in inet_pton #1202
Open
Dennisbonke
wants to merge
1
commit into
managarm:master
Choose a base branch
from
Dennisbonke:lfs-systemd6
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 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 |
---|---|---|
|
@@ -181,6 +181,7 @@ const char *inet_ntop(int af, const void *__restrict src, char *__restrict dst, | |
errno = ENOSPC; | ||
return NULL; | ||
} | ||
|
||
int inet_pton(int af, const char *__restrict src, void *__restrict dst) { | ||
switch (af) { | ||
case AF_INET: { | ||
|
@@ -200,9 +201,89 @@ int inet_pton(int af, const char *__restrict src, void *__restrict dst) { | |
addr->s_addr = htonl(ip); | ||
break; | ||
} | ||
case AF_INET6: | ||
mlibc::infoLogger() << "inet_pton: ipv6 is not implemented!" << frg::endlog; | ||
/* fallthrough */ | ||
case AF_INET6: { | ||
if(strchr(src, '.') != nullptr) { | ||
mlibc::infoLogger() << "inet_pton: ipv4 in ipv6 address is not supported" << frg::endlog; | ||
return 0; | ||
} | ||
|
||
const char *orig_src = src; | ||
char *abbrev = strstr(src, "::"); | ||
if(abbrev && strstr(abbrev + 2, "::") != nullptr) { | ||
return 0; | ||
} | ||
|
||
uint16_t addr[8] = {}; | ||
int len = abbrev ? abbrev - src : strlen(src); | ||
|
||
int addr_len = 0; | ||
while(*src && (src - orig_src) < len) { | ||
char *end; | ||
long int value = strtol(src, &end, 16); | ||
|
||
if(value > 0xffff) { | ||
return 0; | ||
} | ||
if(*end != '\0' && *end != ':') { | ||
return 0; | ||
} | ||
|
||
src = end + 1; | ||
|
||
if(addr_len >= 8) { | ||
return 0; | ||
} | ||
|
||
addr[addr_len++] = htons(value); | ||
} | ||
|
||
if(abbrev) { | ||
uint16_t array[8] = {}; | ||
int array_len = 0; | ||
|
||
src = abbrev + 2; | ||
while(*src) { | ||
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. Why is this case basically copy pasted from the above one? Can they not be merged? |
||
char *end; | ||
long int value = strtol(src, &end, 16); | ||
|
||
if(value > 0xffff) { | ||
return 0; | ||
} | ||
if(*end != '\0' && *end != ':') { | ||
return 0; | ||
} | ||
|
||
src = end + 1; | ||
|
||
if(*end && !*src) { | ||
return 0; | ||
} | ||
if(addr_len + array_len >= 8) { | ||
return 0; | ||
} | ||
|
||
array[array_len++] = htons(value); | ||
} | ||
|
||
for(int i = 0; i < array_len; i++) { | ||
addr[8 - array_len + i] = array[i]; | ||
} | ||
} else if(addr_len != 8) { | ||
return 0; | ||
} | ||
|
||
if(*src) { | ||
return 0; | ||
} | ||
|
||
auto addr6 = reinterpret_cast<struct in6_addr*>(dst); | ||
|
||
for(int i = 0; i < 8; i++) { | ||
addr6->s6_addr16[i] = addr[i]; | ||
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. Can we not just work directly into this array? |
||
} | ||
|
||
break; | ||
} | ||
default: | ||
errno = EAFNOSUPPORT; | ||
return -1; | ||
|
Oops, something went wrong.
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.
This seems broken, the array this block writes into goes out of scope before we can write it into the user provided argument.