Idris2/support/c/idris_net.h

124 lines
3.3 KiB
C
Raw Normal View History

#pragma once
2020-05-18 15:59:07 +03:00
#include <stdio.h>
#include <sys/stat.h>
2020-05-18 15:59:07 +03:00
// Includes used by the idris-file.
#ifdef _WIN32
#include <Ws2tcpip.h>
#include <io.h>
2020-05-18 15:59:07 +03:00
#include <winsock2.h>
#else
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
2020-05-18 15:59:07 +03:00
#endif
struct sockaddr_storage;
struct addrinfo;
typedef struct idrnet_recv_result {
int result;
void *payload;
2020-05-18 15:59:07 +03:00
} idrnet_recv_result;
// Same type of thing as idrnet_recv_result, but for UDP, so stores some
// address information
typedef struct idrnet_recvfrom_result {
int result;
void *payload;
struct sockaddr_storage *remote_addr;
2020-05-18 15:59:07 +03:00
} idrnet_recvfrom_result;
#ifdef _WIN32
// mingw-w64 is currently missing the afunix.h header even though windows
// supports unix sockets so we have to define the unix sockaddr structure
// ourselves
struct sockaddr_un {
u_short sun_family;
char sun_path[108];
};
#endif
2020-05-18 15:59:07 +03:00
unsigned int idrnet_peek(void *ptr, unsigned int offset);
void idrnet_poke(void *ptr, unsigned int offset, char val);
// Gets value of errno
int idrnet_errno();
int idrnet_socket(int domain, int type, int protocol);
int idrnet_close(int fd);
2020-05-18 15:59:07 +03:00
// Address family accessors
int idrnet_af_unspec(void);
int idrnet_af_unix(void);
int idrnet_af_inet(void);
int idrnet_af_inet6(void);
2020-05-18 15:59:07 +03:00
// Bind
int idrnet_bind(int sockfd, int family, int socket_type, char *host, int port);
2020-05-18 15:59:07 +03:00
// Retrieve information about socket
int idrnet_getsockname(int sockfd, void *address, void *len);
// Connect
int idrnet_connect(int sockfd, int family, int socket_type, char *host,
int port);
2020-05-18 15:59:07 +03:00
// Listen
int idrnet_listen(int socket, int backlog);
FILE *idrnet_fdopen(int fd, const char *mode);
2020-05-18 15:59:07 +03:00
// Accessor functions for struct_sockaddr
int idrnet_sockaddr_family(void *sockaddr);
char *idrnet_sockaddr_ipv4(void *sockaddr);
int idrnet_sockaddr_ipv4_port(void *sockaddr);
char *idrnet_sockaddr_unix(void *sockaddr);
void *idrnet_create_sockaddr();
2020-05-18 15:59:07 +03:00
int idrnet_sockaddr_port(int sockfd);
// Accept
int idrnet_accept(int sockfd, void *sockaddr);
2020-05-18 15:59:07 +03:00
// Send
int idrnet_send(int sockfd, char *data);
int idrnet_send_buf(int sockfd, void *data, int len);
int idrnet_send_bytes(int sockfd, void *data, int len);
2020-05-18 15:59:07 +03:00
// Receive
// Creates a result structure containing result and payload
void *idrnet_recv(int sockfd, int len);
2020-05-18 15:59:07 +03:00
// Receives directly into a buffer
int idrnet_recv_buf(int sockfd, void *buf, int len);
int idrnet_recv_bytes(int sockfd, void *buf, int len);
2020-05-18 15:59:07 +03:00
// UDP Send
int idrnet_sendto(int sockfd, char *data, char *host, int port, int family);
int idrnet_sendto_buf(int sockfd, void *buf, int buf_len, char *host, int port,
int family);
2020-05-18 15:59:07 +03:00
// UDP Receive
void *idrnet_recvfrom(int sockfd, int len);
void *idrnet_recvfrom_buf(int sockfd, void *buf, int len);
2020-05-18 15:59:07 +03:00
// Receive structure accessors
int idrnet_get_recv_res(void *res_struct);
char *idrnet_get_recv_payload(void *res_struct);
void idrnet_free_recv_struct(void *res_struct);
2020-05-18 15:59:07 +03:00
// Recvfrom structure accessors
int idrnet_get_recvfrom_res(void *res_struct);
char *idrnet_get_recvfrom_payload(void *res_struct);
void *idrnet_get_recvfrom_sockaddr(void *res_struct);
void idrnet_free_recvfrom_struct(void *res_struct);
2020-05-18 15:59:07 +03:00
int idrnet_getaddrinfo(struct addrinfo **address_res, char *host, int port,
int family, int socket_type);
2020-05-18 15:59:07 +03:00
int idrnet_geteagain();
int isNull(void *ptr);