2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2023-07-23 23:31:10 +00:00
|
|
|
│ Copyright 2023 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2023-07-23 23:31:10 +00:00
|
|
|
#include "libc/sock/struct/sockaddr.h"
|
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sock/sock.h"
|
2022-10-11 00:52:41 +00:00
|
|
|
#include "libc/sock/struct/sockaddr.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/sock/struct/sockaddr.internal.h"
|
2023-07-23 23:31:10 +00:00
|
|
|
#include "libc/sock/struct/sockaddr6.h"
|
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/af.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2023-07-23 23:31:10 +00:00
|
|
|
// computes byte length of socket address
|
|
|
|
uint8_t __get_sockaddr_len(const struct sockaddr_storage *addr) {
|
|
|
|
if (addr->ss_family == AF_INET) {
|
|
|
|
return sizeof(struct sockaddr_in);
|
|
|
|
} else if (addr->ss_family == AF_INET6) {
|
|
|
|
return sizeof(struct sockaddr_in6);
|
|
|
|
} else if (addr->ss_family == AF_UNIX) {
|
|
|
|
struct sockaddr_un *sun = (struct sockaddr_un *)addr;
|
|
|
|
return sizeof(sun->sun_family) +
|
|
|
|
strnlen(sun->sun_path, sizeof(sun->sun_path)) + 1;
|
2020-06-15 14:18:57 +00:00
|
|
|
} else {
|
2023-07-23 23:31:10 +00:00
|
|
|
return sizeof(struct sockaddr_storage);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2023-07-23 23:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// converts bsd sockaddr to cosmo abi
|
|
|
|
void __convert_bsd_to_sockaddr(struct sockaddr_storage *addr) {
|
|
|
|
union {
|
|
|
|
struct sockaddr cosmo;
|
|
|
|
struct sockaddr_bsd bsd;
|
|
|
|
} *pun = (void *)addr;
|
|
|
|
pun->cosmo.sa_family = pun->bsd.sa_family;
|
|
|
|
}
|
|
|
|
|
|
|
|
// converts cosmo sockaddr abi to bsd
|
|
|
|
void __convert_sockaddr_to_bsd(struct sockaddr_storage *addr) {
|
|
|
|
uint8_t len;
|
|
|
|
union {
|
|
|
|
struct sockaddr cosmo;
|
|
|
|
struct sockaddr_bsd bsd;
|
|
|
|
} *pun = (void *)addr;
|
|
|
|
len = __get_sockaddr_len(addr);
|
|
|
|
pun->bsd.sa_family = pun->cosmo.sa_family;
|
|
|
|
pun->bsd.sa_len = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copies sockaddr from internal memory to user's buffer
|
|
|
|
void __write_sockaddr(const struct sockaddr_storage *addr, void *out_addr,
|
|
|
|
uint32_t *inout_addrsize) {
|
|
|
|
if (!out_addr) return;
|
|
|
|
if (!inout_addrsize) return;
|
|
|
|
uint32_t insize = *inout_addrsize;
|
|
|
|
if (insize) bzero(out_addr, insize);
|
|
|
|
uint32_t outsize = __get_sockaddr_len(addr);
|
|
|
|
uint32_t copysize = MIN(insize, outsize);
|
|
|
|
if (copysize) memcpy(out_addr, addr, copysize);
|
|
|
|
*inout_addrsize = outsize;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|