2018-05-04 06:40:18 +03:00
|
|
|
/*
|
|
|
|
* socket methods (mostly unimplemented)
|
|
|
|
*/
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <errno.h>
|
2018-05-04 06:40:18 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <syscall.h>
|
|
|
|
#include <syscall_nums.h>
|
2018-05-04 06:40:18 +03:00
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
DEFN_SYSCALL3(socket, SYS_SOCKET, int, int, int);
|
|
|
|
DEFN_SYSCALL5(setsockopt, SYS_SETSOCKOPT, int,int,int,const void*,size_t);
|
|
|
|
DEFN_SYSCALL3(bind, SYS_BIND, int,const void*,size_t);
|
|
|
|
DEFN_SYSCALL4(accept, SYS_ACCEPT, int,void*,size_t*,int);
|
|
|
|
DEFN_SYSCALL2(listen, SYS_LISTEN, int,int);
|
|
|
|
DEFN_SYSCALL3(connect, SYS_CONNECT, int,const void*,size_t);
|
|
|
|
DEFN_SYSCALL5(getsockopt, SYS_GETSOCKOPT, int,int,int,void*,size_t*);
|
|
|
|
DEFN_SYSCALL3(recv, SYS_RECV, int,void*,int);
|
|
|
|
DEFN_SYSCALL3(send, SYS_SEND, int,const void*,int);
|
|
|
|
DEFN_SYSCALL2(shutdown, SYS_SHUTDOWN, int, int);
|
2018-05-04 06:40:18 +03:00
|
|
|
|
|
|
|
int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
|
2021-05-31 04:47:02 +03:00
|
|
|
__sets_errno(syscall_connect(sockfd,addr,addrlen));
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* All of these should just be reads. */
|
|
|
|
ssize_t recv(int sockfd, void *buf, size_t len, int flags) {
|
2021-05-31 04:47:02 +03:00
|
|
|
struct iovec _iovec = {
|
|
|
|
buf, len
|
|
|
|
};
|
|
|
|
struct msghdr _header = {
|
|
|
|
.msg_name = NULL,
|
|
|
|
.msg_namelen = 0,
|
|
|
|
.msg_iov = &_iovec,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
.msg_control = NULL,
|
|
|
|
.msg_controllen = 0,
|
|
|
|
.msg_flags = 0,
|
|
|
|
};
|
|
|
|
return recvmsg(sockfd, &_header, flags);
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
ssize_t recvfrom(int sockfd, void *buf, size_t len, int flags, struct sockaddr *src_addr, socklen_t *addrlen) {
|
2021-05-31 04:47:02 +03:00
|
|
|
struct iovec _iovec = {
|
|
|
|
buf, len
|
|
|
|
};
|
|
|
|
struct msghdr _header = {
|
|
|
|
.msg_name = src_addr,
|
|
|
|
.msg_namelen = addrlen ? *addrlen : 0,
|
|
|
|
.msg_iov = &_iovec,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
.msg_control = NULL,
|
|
|
|
.msg_controllen = 0,
|
|
|
|
.msg_flags = 0,
|
|
|
|
};
|
|
|
|
ssize_t result = recvmsg(sockfd, &_header, flags);
|
|
|
|
if (addrlen) *addrlen = _header.msg_namelen;
|
|
|
|
return result;
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags) {
|
2021-05-31 04:47:02 +03:00
|
|
|
__sets_errno(syscall_recv(sockfd,msg,flags));
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t send(int sockfd, const void *buf, size_t len, int flags) {
|
2021-05-31 04:47:02 +03:00
|
|
|
struct iovec _iovec = {
|
|
|
|
(void*)buf, len
|
|
|
|
};
|
|
|
|
struct msghdr _header = {
|
|
|
|
.msg_name = NULL,
|
|
|
|
.msg_namelen = 0,
|
|
|
|
.msg_iov = &_iovec,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
.msg_control = NULL,
|
|
|
|
.msg_controllen = 0,
|
|
|
|
.msg_flags = 0,
|
|
|
|
};
|
|
|
|
return sendmsg(sockfd, &_header, flags);
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
ssize_t sendto(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) {
|
2021-05-31 04:47:02 +03:00
|
|
|
struct iovec _iovec = {
|
|
|
|
(void*)buf, len
|
|
|
|
};
|
|
|
|
struct msghdr _header = {
|
|
|
|
.msg_name = (void*)dest_addr,
|
|
|
|
.msg_namelen = addrlen,
|
|
|
|
.msg_iov = &_iovec,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
.msg_control = NULL,
|
|
|
|
.msg_controllen = 0,
|
|
|
|
.msg_flags = 0,
|
|
|
|
};
|
|
|
|
return sendmsg(sockfd, &_header, flags);
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags) {
|
2021-05-31 04:47:02 +03:00
|
|
|
__sets_errno(syscall_send(sockfd,msg,flags));
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int socket(int domain, int type, int protocol) {
|
2021-05-31 04:47:02 +03:00
|
|
|
/* Thin wrapper around a new system call, I guess. */
|
|
|
|
__sets_errno(syscall_socket(domain,type,protocol));
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t htonl(uint32_t hostlong) {
|
|
|
|
return ( (((hostlong) & 0xFF) << 24) | (((hostlong) & 0xFF00) << 8) | (((hostlong) & 0xFF0000) >> 8) | (((hostlong) & 0xFF000000) >> 24));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t htons(uint16_t hostshort) {
|
|
|
|
return ( (((hostshort) & 0xFF) << 8) | (((hostshort) & 0xFF00) >> 8) );
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ntohl(uint32_t netlong) {
|
|
|
|
return htonl(netlong);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t ntohs(uint16_t netshort) {
|
|
|
|
return htons(netshort);
|
|
|
|
}
|
|
|
|
|
|
|
|
int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
|
2021-05-31 04:47:02 +03:00
|
|
|
__sets_errno(syscall_bind(sockfd,addr,addrlen));
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int accept(int sockfd, struct sockaddr * addr, socklen_t * addrlen) {
|
2021-05-31 04:47:02 +03:00
|
|
|
__sets_errno(syscall_accept(sockfd,addr,addrlen,0));
|
|
|
|
}
|
|
|
|
|
|
|
|
int accept4(int sockfd, struct sockaddr * addr, socklen_t * addrlen, int flags) {
|
|
|
|
__sets_errno(syscall_accept(sockfd,addr,addrlen,flags));
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int listen(int sockfd, int backlog) {
|
2021-05-31 04:47:02 +03:00
|
|
|
__sets_errno(syscall_listen(sockfd,backlog));
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
int getsockopt(int sockfd, int level, int optname, void *optval, socklen_t *optlen) {
|
|
|
|
__sets_errno(syscall_getsockopt(sockfd,level,optname,optval,optlen));
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen) {
|
|
|
|
__sets_errno(syscall_setsockopt(sockfd,level,optname,optval,optlen));
|
2018-05-04 06:40:18 +03:00
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
int shutdown(int sockfd, int how) {
|
|
|
|
__sets_errno(syscall_shutdown(sockfd,how));
|
|
|
|
}
|
|
|
|
|
|
|
|
#define UNIMPLEMENTED fprintf(stderr, "[libnetwork] Unimplemented: %s\n", __FUNCTION__)
|
|
|
|
|
|
|
|
int getsockname(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
|
2018-05-04 06:40:18 +03:00
|
|
|
UNIMPLEMENTED;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
int getpeername(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {
|
2018-05-04 06:40:18 +03:00
|
|
|
UNIMPLEMENTED;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
struct hostent * gethostbyname(const char * name) {
|
|
|
|
/* This formerly called into the kernel network device to perform
|
|
|
|
* DNS lookups, but we're going to resolve directly with a UDP DNS
|
|
|
|
* client with timeouts and everything, right here in the libc... */
|
2021-03-25 09:52:19 +03:00
|
|
|
UNIMPLEMENTED;
|
2021-05-31 04:47:02 +03:00
|
|
|
return NULL;
|
2021-03-25 09:52:19 +03:00
|
|
|
}
|
2021-05-31 04:47:02 +03:00
|
|
|
|