James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file re_tcp.h Interface to Transport Control Protocol |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | struct sa; |
| 7 | struct tcp_sock; |
| 8 | struct tcp_conn; |
| 9 | |
| 10 | |
| 11 | /** |
| 12 | * Defines the incoming TCP connection handler |
| 13 | * |
| 14 | * @param peer Network address of peer |
| 15 | * @param arg Handler argument |
| 16 | */ |
| 17 | typedef void (tcp_conn_h)(const struct sa *peer, void *arg); |
| 18 | |
| 19 | /** |
| 20 | * Defines the TCP connection established handler |
| 21 | * |
| 22 | * @param arg Handler argument |
| 23 | */ |
| 24 | typedef void (tcp_estab_h)(void *arg); |
| 25 | |
| 26 | /** |
| 27 | * Defines the TCP connection data send handler |
| 28 | * |
| 29 | * @param arg Handler argument |
| 30 | */ |
| 31 | typedef void (tcp_send_h)(void *arg); |
| 32 | |
| 33 | /** |
| 34 | * Defines the TCP connection data receive handler |
| 35 | * |
| 36 | * @param mb Buffer with data |
| 37 | * @param arg Handler argument |
| 38 | */ |
| 39 | typedef void (tcp_recv_h)(struct mbuf *mb, void *arg); |
| 40 | |
| 41 | /** |
| 42 | * Defines the TCP connection close handler |
| 43 | * |
| 44 | * @param err Error code |
| 45 | * @param arg Handler argument |
| 46 | */ |
| 47 | typedef void (tcp_close_h)(int err, void *arg); |
| 48 | |
| 49 | |
| 50 | /* TCP Socket */ |
| 51 | int tcp_sock_alloc(struct tcp_sock **tsp, const struct sa *local, |
| 52 | tcp_conn_h *ch, void *arg); |
| 53 | int tcp_sock_bind(struct tcp_sock *ts, const struct sa *local); |
| 54 | int tcp_sock_listen(struct tcp_sock *ts, int backlog); |
| 55 | int tcp_accept(struct tcp_conn **tcp, struct tcp_sock *ts, tcp_estab_h *eh, |
| 56 | tcp_recv_h *rh, tcp_close_h *ch, void *arg); |
| 57 | void tcp_reject(struct tcp_sock *ts); |
| 58 | int tcp_sock_local_get(const struct tcp_sock *ts, struct sa *local); |
| 59 | |
| 60 | |
| 61 | /* TCP Connection */ |
| 62 | int tcp_conn_alloc(struct tcp_conn **tcp, const struct sa *peer, |
| 63 | tcp_estab_h *eh, tcp_recv_h *rh, tcp_close_h *ch, |
| 64 | void *arg); |
| 65 | int tcp_conn_bind(struct tcp_conn *tc, const struct sa *local); |
| 66 | int tcp_conn_connect(struct tcp_conn *tc, const struct sa *peer); |
| 67 | int tcp_send(struct tcp_conn *tc, struct mbuf *mb); |
| 68 | int tcp_set_send(struct tcp_conn *tc, tcp_send_h *sendh); |
| 69 | void tcp_set_handlers(struct tcp_conn *tc, tcp_estab_h *eh, tcp_recv_h *rh, |
| 70 | tcp_close_h *ch, void *arg); |
| 71 | void tcp_conn_rxsz_set(struct tcp_conn *tc, size_t rxsz); |
| 72 | void tcp_conn_txqsz_set(struct tcp_conn *tc, size_t txqsz); |
| 73 | int tcp_conn_local_get(const struct tcp_conn *tc, struct sa *local); |
| 74 | int tcp_conn_peer_get(const struct tcp_conn *tc, struct sa *peer); |
| 75 | int tcp_conn_fd(const struct tcp_conn *tc); |
| 76 | size_t tcp_conn_txqsz(const struct tcp_conn *tc); |
| 77 | |
| 78 | |
| 79 | /* High-level API */ |
| 80 | int tcp_listen(struct tcp_sock **tsp, const struct sa *local, |
| 81 | tcp_conn_h *ch, void *arg); |
| 82 | int tcp_connect(struct tcp_conn **tcp, const struct sa *peer, |
| 83 | tcp_estab_h *eh, tcp_recv_h *rh, tcp_close_h *ch, void *arg); |
| 84 | int tcp_local_get(const struct tcp_sock *ts, struct sa *local); |
| 85 | |
| 86 | |
| 87 | /* Helper API */ |
| 88 | typedef bool (tcp_helper_estab_h)(int *err, bool active, void *arg); |
| 89 | typedef bool (tcp_helper_send_h)(int *err, struct mbuf *mb, void *arg); |
| 90 | typedef bool (tcp_helper_recv_h)(int *err, struct mbuf *mb, bool *estab, |
| 91 | void *arg); |
| 92 | |
| 93 | struct tcp_helper; |
| 94 | |
| 95 | |
| 96 | int tcp_register_helper(struct tcp_helper **thp, struct tcp_conn *tc, |
| 97 | int layer, |
| 98 | tcp_helper_estab_h *eh, tcp_helper_send_h *sh, |
| 99 | tcp_helper_recv_h *rh, void *arg); |
| 100 | int tcp_send_helper(struct tcp_conn *tc, struct mbuf *mb, |
| 101 | struct tcp_helper *th); |