1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 01:37:19 +00:00

Cleanup code a bit

Inconsistent indentation and such fixed, superfluous newlines removed,
documentation of new functions.
This commit is contained in:
llmII 2021-09-06 18:30:54 -05:00
parent 8c938ceff9
commit 1597ca0de5
No known key found for this signature in database
GPG Key ID: E3AD2E259F58A9A0

View File

@ -576,23 +576,18 @@ JANET_CORE_FN(cfun_net_listen,
#define SO_MIN(a, b) (((a) < (b))? (a) : (b)) #define SO_MIN(a, b) (((a) < (b))? (a) : (b))
#define SA_ADDRSTRLEN SO_MAX(INET6_ADDRSTRLEN, (sizeof ((struct sockaddr_un *)0)->sun_path) + 1) #define SA_ADDRSTRLEN SO_MAX(INET6_ADDRSTRLEN, (sizeof ((struct sockaddr_un *)0)->sun_path) + 1)
#define sa_ntoa(sa) sa_ntoa_((char [SA_ADDRSTRLEN]){ 0 }, SA_ADDRSTRLEN, (sa)) #define sa_ntoa(sa) sa_ntoa_((char [SA_ADDRSTRLEN]){ 0 }, SA_ADDRSTRLEN, (sa))
#define sa_aton(str) sa_aton_(&(struct sockaddr_storage){ 0 }, sizeof (struct sockaddr_storage), (str))
#define sa_family(...) sa_family(__VA_ARGS__) #define sa_family(...) sa_family(__VA_ARGS__)
#define sa_port(...) sa_port(__VA_ARGS__) #define sa_port(...) sa_port(__VA_ARGS__)
union sockaddr_arg { union sockaddr_arg {
struct sockaddr *sa; struct sockaddr *sa;
const struct sockaddr *c_sa; const struct sockaddr *c_sa;
struct sockaddr_storage *ss; struct sockaddr_storage *ss;
struct sockaddr_storage *c_ss; struct sockaddr_storage *c_ss;
struct sockaddr_in *sin; struct sockaddr_in *sin;
struct sockaddr_in *c_sin; struct sockaddr_in *c_sin;
struct sockaddr_in6 *sin6; struct sockaddr_in6 *sin6;
struct sockaddr_in6 *c_sin6; struct sockaddr_in6 *c_sin6;
struct sockaddr_un *sun; struct sockaddr_un *sun;
struct sockaddr_un *c_sun; struct sockaddr_un *c_sun;
union sockaddr_any *any; union sockaddr_any *any;
@ -610,15 +605,17 @@ union sockaddr_any {
struct sockaddr_un sun; struct sockaddr_un sun;
}; };
static inline union sockaddr_arg sockaddr_ref(void* arg) { static inline union sockaddr_arg sockaddr_ref(void *arg) {
return (union sockaddr_arg){ arg }; return (union sockaddr_arg) {
arg
};
} }
static inline sa_family_t *(sa_family)(void* arg) { static inline sa_family_t *(sa_family)(void *arg) {
return &sockaddr_ref(arg).sa->sa_family; return &sockaddr_ref(arg).sa->sa_family;
} }
static inline in_port_t *(sa_port)(void* arg, const in_port_t *def, int *error) { static inline in_port_t *(sa_port)(void *arg, const in_port_t *def, int *error) {
switch (*sa_family(arg)) { switch (*sa_family(arg)) {
case AF_INET: case AF_INET:
return &sockaddr_ref(arg).sin->sin_port; return &sockaddr_ref(arg).sin->sin_port;
@ -703,29 +700,23 @@ error:
* NOTE: Always write something in case caller ignores errors, such * NOTE: Always write something in case caller ignores errors, such
* as when caller is using the sa_ntoa() macro. * as when caller is using the sa_ntoa() macro.
*/ */
safe_memcpy(dst, (def)? def : unspec, lim); safe_memcpy(dst, (def) ? def : unspec, lim);
return (char *)def; return (char *)def;
} }
void *sa_pton(void *, size_t, const char *, const void *, int *);
static inline char *sa_ntoa_(char *dst, size_t lim, const void *src) { static inline char *sa_ntoa_(char *dst, size_t lim, const void *src) {
return sa_ntop(dst, lim, src, NULL, &(int){ 0 }), dst; return sa_ntop(dst, lim, src, NULL, &(int) {
0
}), dst;
} }
/*
static inline void *sa_aton_(void *dst, size_t lim, const char *src) {
return sa_pton(dst, lim, src, NULL, &(int){ 0 }), dst;
}
*/
static Janet janet_so_getname(const struct sockaddr_storage *ss, socklen_t slen) { static Janet janet_so_getname(const struct sockaddr_storage *ss, socklen_t slen) {
uint8_t *hn = NULL; uint8_t *hn = NULL;
uint16_t hp = 0; uint16_t hp = 0;
size_t plen = SA_ADDRSTRLEN; size_t plen = SA_ADDRSTRLEN;
switch(ss->ss_family) { switch (ss->ss_family) {
case AF_INET: case AF_INET:
/* fall through */ /* fall through */
case AF_INET6: case AF_INET6:
@ -737,7 +728,7 @@ static Janet janet_so_getname(const struct sockaddr_storage *ss, socklen_t slen)
/* support nameless sockets, linux-ism */ /* support nameless sockets, linux-ism */
if (slen > offsetof(struct sockaddr_un, sun_path)) { if (slen > offsetof(struct sockaddr_un, sun_path)) {
struct sockaddr_un *sun = (struct sockaddr_un *)ss; struct sockaddr_un *sun = (struct sockaddr_un *)ss;
char *pe = (char *)sun + SO_MIN(sizeof *sun, slen); char *pe = (char *)sun + SO_MIN(sizeof * sun, slen);
size_t plen; size_t plen;
while (pe > sun->sun_path && pe[-1] == '\0') while (pe > sun->sun_path && pe[-1] == '\0')
@ -773,7 +764,7 @@ static Janet janet_so_getname(const struct sockaddr_storage *ss, socklen_t slen)
JANET_CORE_FN(cfun_net_getsockname, JANET_CORE_FN(cfun_net_getsockname,
"(net/localname stream)", "(net/localname stream)",
"Document me!") { "Gets the local address and port in a tuple in that order.") {
janet_arity(argc, 1, 1); janet_arity(argc, 1, 1);
JanetStream *js = janet_getabstract(argv, 0, &janet_stream_type); JanetStream *js = janet_getabstract(argv, 0, &janet_stream_type);
struct sockaddr_storage ss; struct sockaddr_storage ss;
@ -781,7 +772,7 @@ JANET_CORE_FN(cfun_net_getsockname,
memset(&ss, 0, slen); memset(&ss, 0, slen);
int error; int error;
if(0 != (error = getsockname(js->handle, (struct sockaddr *) &ss, &slen))) if (0 != (error = getsockname(js->handle, (struct sockaddr *) &ss, &slen)))
janet_panicf("Failed to get peername on fd %d, error: %s", js->handle, janet_ev_lasterr()); janet_panicf("Failed to get peername on fd %d, error: %s", js->handle, janet_ev_lasterr());
return janet_so_getname(&ss, slen); return janet_so_getname(&ss, slen);
@ -790,7 +781,7 @@ JANET_CORE_FN(cfun_net_getsockname,
JANET_CORE_FN(cfun_net_getpeername, JANET_CORE_FN(cfun_net_getpeername,
"(net/peername stream)", "(net/peername stream)",
"Document me!") { "Gets the remote peer's address and port in a tuple in that order.") {
janet_arity(argc, 1, 1); janet_arity(argc, 1, 1);
JanetStream *js = janet_getabstract(argv, 0, &janet_stream_type); JanetStream *js = janet_getabstract(argv, 0, &janet_stream_type);
struct sockaddr_storage ss; struct sockaddr_storage ss;