chiark / gitweb /
string_item_to_iaddr: Actually set port if !CONFIG_IPV6
[secnet.git] / comm-common.h
index 77ac840899c5ee2e21379456c632aac14c9b1348..b1cd9c0e123a36f7deb90c1f38e2761e76ceb7b6 100644 (file)
@@ -3,6 +3,9 @@
 #define COMM_COMMON_H
 
 #include "secnet.h"
+#include "util.h"
+
+/*----- for all comms -----*/
 
 struct comm_notify_entry {
     comm_notify_fn *fn;
@@ -31,7 +34,7 @@ bool_t comm_notify(struct comm_notify_list *notify, struct buffer_if *buf,
 void comm_apply(struct commcommon *cc, void *st);
 
 #define COMM_APPLY(st,cc,prefix,desc,loc)              \
-    (st)=safe_malloc(sizeof(*(st)), desc "_apply");    \
+    NEW(st);                                           \
     (cc)->loc=loc;                                     \
     (cc)->cl.description=desc;                         \
     (cc)->ops.sendmsg=prefix##sendmsg;                 \
@@ -56,4 +59,65 @@ void comm_apply(struct commcommon *cc, void *st);
      *   //    dict_t *dict = <construction dictionary argument>;
      */
 
+/*----- for udp-based comms -----*/
+
+#define UDP_MAX_SOCKETS 3 /* 2 ought to do really */
+
+#define MAX_AF MAX_RAW(AF_INET6,AF_INET)
+
+struct udpsock {
+    union iaddr addr;
+    int fd;
+    bool_t experienced[/*0=recv,1=send*/2][MAX_AF+1][/*success?*/2];
+};
+
+struct udpsocks {
+    int n_socks;
+    struct udpsock socks[UDP_MAX_SOCKETS];
+    /* private for udp_socks_* */
+    struct udpcommon *uc; /* link to parent, for cfg, notify list, etc. */
+    struct poll_interest *interest;
+    const char *desc;
+};
+
+struct udpcommon {
+    struct commcommon cc;
+    int port;
+    string_t authbind;
+    bool_t use_proxy;
+    union iaddr proxy;
+};
+
+bool_t udp_make_socket(struct udpcommon *uc, struct udpsock *us,
+                      int failmsgclass);
+  /* Caller should have filled in ->addr.  Fills in us->fd,
+     ->experienced; updates ->addr.  Logs any errors with lg_[v]perror. */
+bool_t udp_import_socket(struct udpcommon *uc, struct udpsock *us,
+                        int failmsgclass, int fd);
+  /* Like udp_make_socket, but caller provides fd.  fd is not closed
+     on error */
+
+void udp_destroy_socket(struct udpcommon *uc, struct udpsock *us);
+  /* Idempotent.  No errors are possible. */
+
+const char *af_name(int af);
+void udp_sock_experienced(struct log_if *lg, struct udpcommon *uc,
+                         struct udpsocks *socks, struct udpsock *us,
+                         bool_t recvsend, int af /* 0 means any */,
+                         int r, int errnoval);
+
+void udp_socks_register(struct udpcommon *uc, struct udpsocks *socks,
+                       const char *desc);
+void udp_socks_deregister(struct udpcommon *uc, struct udpsocks *socks);
+void udp_socks_childpersist(struct udpcommon *uc, struct udpsocks *socks);
+
+#define UDP_APPLY_STANDARD(st,uc,desc)                                 \
+    (uc)->use_proxy=False;                                             \
+    (uc)->authbind=dict_read_string(d,"authbind",False,"udp",(uc)->cc.loc); \
+    (uc)->port=dict_read_number(d,"port",False,"udp",(uc)->cc.loc,0)
+    /* void UDP_APPLY_STANDARD(SOMETHING *st, struct udpcommon *uc,
+     *                         const char *desc);
+     *   // Expects in scope:  dict_t *d=...;   as from COMM_APPLY_STANDARD
+     */
+
 #endif /*COMM_COMMON_H*/