femtoTCP/femtotcp.h
Daniele Lacamera 35351bece6 Initial import
2024-10-27 13:27:18 +01:00

84 lines
2.6 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];
uint8_t 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 posix_socket(struct ipstack *s, int domain, int type, int protocol);
int posix_bind(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen);
int posix_listen(struct ipstack *s, int sockfd, int backlog);
int posix_accept(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
int posix_connect(struct ipstack *s, int sockfd, const struct ipstack_sockaddr *addr, socklen_t addrlen);
int posix_sendto(struct ipstack *s, int sockfd, const void *buf, size_t len, int flags, const struct ipstack_sockaddr *dest_addr, socklen_t addrlen);
int posix_recvfrom(struct ipstack *s, int sockfd, void *buf, size_t len, int flags, struct ipstack_sockaddr *src_addr, socklen_t *addrlen);
int posix_close(struct ipstack *s, int sockfd);
int posix_getpeername(struct ipstack *s, int sockfd, struct ipstack_sockaddr *addr, socklen_t *addrlen);
int posix_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);
#endif