femtoTCP/femtotcp.h

91 lines
3 KiB
C

#ifndef QUECTONET_H
#define QUECTONET_H
#include <stdint.h>
/* Types */
struct ipstack;
typedef uint32_t ip4;
/* Macros, compiler specific. */
#define PACKED __attribute__((packed))
#define ee16(x) __builtin_bswap16(x)
#define ee32(x) __builtin_bswap32(x)
#define DEBUG
#ifdef DEBUG
#include <stdio.h>
#define LOG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#define LOG(fmt, ...) do{}while(0)
#endif
/* Device driver interface */
/* Struct to contain a hw device description */
struct ll {
uint8_t mac[6];
char ifname[16];
/* poll function */
int (*poll)(struct ll *ll, void *buf, int len);
/* send function */
int (*send)(struct ll *ll, void *buf, int len);
};
/* Struct to contain an IP device configuration */
struct ipconf {
struct ll *ll;
ip4 ip;
ip4 mask;
ip4 gw;
};
/* Socket interface */
#ifndef NATIVE_POSIX_SOCKET
#define IPSTACK_SOCK_STREAM 1
#define IPSTACK_SOCK_DGRAM 2
struct ipstack_sockaddr_in {
uint16_t sin_family;
uint16_t sin_port;
struct sin_addr { uint32_t s_addr; } sin_addr;
};
struct ipstack_sockaddr { uint16_t sa_family; };
typedef uint32_t socklen_t;
#ifndef AF_INET
#define AF_INET 2
#endif
#endif
int ft_socket(struct ipstack *s, int domain, int type, int protocol);
int ft_bind(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen);
int ft_listen(struct ipstack *s, int sockfd, int backlog);
int ft_accept(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
int ft_connect(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen);
int ft_sendto(struct ipstack *s, int sockfd, const void *buf, size_t len, int flags, const struct ipstack_sockaddr *dest_addr, socklen_t addrlen);
int ft_recvfrom(struct ipstack *s, int sockfd, void *buf, size_t len, int flags, struct ipstack_sockaddr *src_addr, socklen_t *addrlen);
int ft_close(struct ipstack *s, int sockfd);
int ft_getpeername(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
int ft_getsockname(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
int dhcp_client_init(struct ipstack *s);
int dhcp_bound(struct ipstack *s);
/* IP stack interface */
void ipstack_init(struct ipstack *s);
void ipstack_init_static(struct ipstack **s);
int ipstack_poll(struct ipstack *s, uint64_t now);
void ipstack_ipconfig_set(struct ipstack *s, ip4 ip, ip4 mask, ip4 gw);
void ipstack_ipconfig_get(struct ipstack *s, ip4 *ip, ip4 *mask, ip4 *gw);
struct ll *ipstack_getdev(struct ipstack *s);
ip4 atoip4(const char *ip);
/* Callback flags */
#define CB_EVENT_READABLE 0x01 /* Accepted connection or data available */
#define CB_EVENT_WRITABLE 0x02 /* Connected or space available to send */
#define CB_EVENT_CLOSED 0x04 /* Connection closed by peer */
#define CB_EVENT_TIMEOUT 0x08 /* Timeout */
void ipstack_register_callback(struct ipstack *s, int sock_fd, void (*cb)(int sock_fd, uint16_t events, void *arg), void *arg);
#endif