1#ifndef _TCP_H_ 2#define _TCP_H_ 3 4#include <stdint.h> 5#include <netinet/in.h> 6 7struct tcp_conf { 8 int ipproto; 9 int reuseaddr; 10 int checksum; 11 unsigned short port; 12 union { 13 struct { 14 struct in_addr inet_addr; 15 } ipv4; 16 struct { 17 struct in6_addr inet_addr6; 18 int scope_id; 19 } ipv6; 20 } server; 21 union { 22 struct in_addr inet_addr; 23 struct in6_addr inet_addr6; 24 } client; 25 int sndbuf; 26 int rcvbuf; 27}; 28 29struct tcp_stats { 30 uint64_t bytes; 31 uint64_t messages; 32 uint64_t error; 33}; 34 35enum tcp_sock_state { 36 TCP_SERVER_ACCEPTING, 37 TCP_SERVER_CONNECTED, 38 TCP_CLIENT_DISCONNECTED, 39 TCP_CLIENT_CONNECTED 40}; 41 42struct tcp_sock { 43 int state; /* enum tcp_sock_state */ 44 int fd; 45 int client_fd; /* only for the server side */ 46 union { 47 struct sockaddr_in ipv4; 48 struct sockaddr_in6 ipv6; 49 } addr; 50 socklen_t sockaddr_len; 51 struct tcp_stats stats; 52 struct tcp_conf *conf; 53}; 54 55struct tcp_sock *tcp_server_create(struct tcp_conf *conf); 56void tcp_server_destroy(struct tcp_sock *m); 57 58struct tcp_sock *tcp_client_create(struct tcp_conf *conf); 59void tcp_client_destroy(struct tcp_sock *m); 60 61ssize_t tcp_send(struct tcp_sock *m, const void *data, int size); 62ssize_t tcp_recv(struct tcp_sock *m, void *data, int size); 63int tcp_accept(struct tcp_sock *m); 64 65int tcp_get_fd(struct tcp_sock *m); 66int tcp_isset(struct tcp_sock *m, fd_set *readfds); 67int tcp_accept_isset(struct tcp_sock *m, fd_set *readfds); 68 69int tcp_snprintf_stats(char *buf, size_t buflen, char *ifname, 70 struct tcp_sock *s, struct tcp_sock *r); 71 72int tcp_snprintf_stats2(char *buf, size_t buflen, const char *ifname, 73 const char *status, int active, 74 struct tcp_stats *s, struct tcp_stats *r); 75 76#endif 77