Make improvements

- Clean up sigaction() code
- Add a port scanner example
- Introduce a ParseCidr() API
- Clean up our futex abstraction code
- Fix a harmless integer overflow in ParseIp()
- Use kernel semaphores on NetBSD to make threads much faster
This commit is contained in:
Justine Tunney 2022-11-07 02:22:09 -08:00
parent 539bddce8c
commit c995838e5c
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
107 changed files with 1085 additions and 492 deletions

View file

@ -17,27 +17,33 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/str/str.h"
#include "net/http/http.h"
#include "net/http/ip.h"
/**
* Parse IPv4 address.
* Parse IPv4 host address.
*
* @param n if -1 implies strlen
* @return -1 on failure, otherwise 32-bit host-order unsigned integer
* @see ParseCidr()
*/
int64_t ParseIp(const char *s, size_t n) {
int c, j;
size_t i;
uint32_t x;
int b, c, j;
unsigned b, x;
bool dotted = false;
if (n == -1) n = s ? strlen(s) : 0;
if (!n) return -1;
for (b = x = j = i = 0; i < n; ++i) {
c = s[i] & 255;
if (isdigit(c)) {
b *= 10;
b += c - '0';
if (__builtin_mul_overflow(b, 10, &b) || //
__builtin_add_overflow(b, c - '0', &b) || //
(b > 255 && dotted)) {
return -1;
}
} else if (c == '.') {
if (b > 255) return -1;
dotted = true;
x <<= 8;
x |= b;
b = 0;