| 1 | |
| 2 | #ifndef COMM_COMMON_H |
| 3 | #define COMM_COMMON_H |
| 4 | |
| 5 | #include "secnet.h" |
| 6 | #include "util.h" |
| 7 | |
| 8 | /*----- for all comms -----*/ |
| 9 | |
| 10 | struct comm_notify_entry { |
| 11 | comm_notify_fn *fn; |
| 12 | void *state; |
| 13 | LIST_ENTRY(comm_notify_entry) entry; |
| 14 | }; |
| 15 | LIST_HEAD(comm_notify_list, comm_notify_entry) notify; |
| 16 | |
| 17 | struct commcommon { /* must be first so that void* is comm_common* */ |
| 18 | closure_t cl; |
| 19 | struct comm_if ops; |
| 20 | struct cloc loc; |
| 21 | struct comm_notify_list notify; |
| 22 | struct buffer_if *rbuf; |
| 23 | }; |
| 24 | |
| 25 | void comm_request_notify(void *commst, void *nst, comm_notify_fn *fn); |
| 26 | void comm_release_notify(void *commst, void *nst, comm_notify_fn *fn); |
| 27 | |
| 28 | bool_t comm_notify(struct comm_notify_list *notify, struct buffer_if *buf, |
| 29 | const struct comm_addr *ca); |
| 30 | /* Either: returns True, with message delivered and buffer freed. |
| 31 | * Or: False, if no-one wanted it - buffer still allocd'd. |
| 32 | * Ie, like comm_notify_fn. */ |
| 33 | |
| 34 | void comm_apply(struct commcommon *cc, void *st); |
| 35 | |
| 36 | #define COMM_APPLY(st,cc,prefix,desc,loc) \ |
| 37 | (st)=safe_malloc(sizeof(*(st)), desc "_apply"); \ |
| 38 | (cc)->loc=loc; \ |
| 39 | (cc)->cl.description=desc; \ |
| 40 | (cc)->ops.sendmsg=prefix##sendmsg; \ |
| 41 | (cc)->ops.addr_to_string=prefix##addr_to_string; \ |
| 42 | comm_apply((cc),(st)) |
| 43 | /* void COMM_APPLY(SOMETHING *st, struct commcommon *FUNCTIONOF(st), |
| 44 | * prefix, "DESC", struct cloc loc); |
| 45 | * // Expects in scope: prefix##sendmsg, prefix##addr_to_string. |
| 46 | */ |
| 47 | |
| 48 | #define COMM_APPLY_STANDARD(st,cc,desc,args) \ |
| 49 | item_t *item=list_elem(args,0); \ |
| 50 | if (!item || item->type!=t_dict) { \ |
| 51 | cfgfatal((cc)->loc,desc,"first argument must be a dictionary\n"); \ |
| 52 | } \ |
| 53 | dict_t *d=item->data.dict; \ |
| 54 | (cc)->rbuf=find_cl_if(d,"buffer",CL_BUFFER,True,desc,(cc)->loc) |
| 55 | /* void COMM_APPLY_STANDARD(SOMETHING *st, struct commcommon *cc, |
| 56 | * const char *desc, list_t *args); |
| 57 | * // Declares: |
| 58 | * // item_t *item = <undefined>; |
| 59 | * // dict_t *dict = <construction dictionary argument>; |
| 60 | */ |
| 61 | |
| 62 | /*----- for udp-based comms -----*/ |
| 63 | |
| 64 | #define UDP_MAX_SOCKETS 3 /* 2 ought to do really */ |
| 65 | |
| 66 | #define MAX_AF MAX_RAW(AF_INET6,AF_INET) |
| 67 | |
| 68 | struct udpsock { |
| 69 | union iaddr addr; |
| 70 | int fd; |
| 71 | bool_t experienced[/*0=recv,1=send*/2][MAX_AF+1][/*success?*/2]; |
| 72 | }; |
| 73 | |
| 74 | struct udpsocks { |
| 75 | int n_socks; |
| 76 | struct udpsock socks[UDP_MAX_SOCKETS]; |
| 77 | /* private for udp_socks_* */ |
| 78 | struct udpcommon *uc; /* link to parent, for cfg, notify list, etc. */ |
| 79 | struct poll_interest *interest; |
| 80 | }; |
| 81 | |
| 82 | struct udpcommon { |
| 83 | struct commcommon cc; |
| 84 | int port; |
| 85 | string_t authbind; |
| 86 | bool_t use_proxy; |
| 87 | union iaddr proxy; |
| 88 | }; |
| 89 | |
| 90 | bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us, |
| 91 | int failmsgclass); |
| 92 | /* Fills in us->fd. Logs any errors with lg_[v]perror. */ |
| 93 | |
| 94 | void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us); |
| 95 | /* Idempotent. No errors are possible. */ |
| 96 | |
| 97 | const char *af_name(int af); |
| 98 | void udp_sock_experienced(struct log_if *lg, struct udpcommon *uc, |
| 99 | const char *socksdesc, struct udpsock *us, |
| 100 | bool_t recvsend, int af /* 0 means any */, |
| 101 | int r, int errnoval); |
| 102 | |
| 103 | void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks); |
| 104 | void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks); |
| 105 | |
| 106 | #define UDP_APPLY_STANDARD(st,uc,desc) \ |
| 107 | (uc)->use_proxy=False; \ |
| 108 | (uc)->authbind=dict_read_string(d,"authbind",False,"udp",(uc)->cc.loc); \ |
| 109 | (uc)->port=dict_read_number(d,"port",True,"udp",(uc)->cc.loc,0) |
| 110 | /* void UDP_APPLY_STANDARD(SOMETHING *st, struct udpcommon *uc, |
| 111 | * const char *desc); |
| 112 | * // Expects in scope: dict_t *d=...; as from COMM_APPLY_STANDARD |
| 113 | */ |
| 114 | |
| 115 | #endif /*COMM_COMMON_H*/ |