lib/tsocket: add tdgram_inet_udp_broadcast_socket()
authorStefan Metzmacher <metze@samba.org>
Thu, 21 May 2015 09:39:38 +0000 (11:39 +0200)
committerStefan Metzmacher <metze@samba.org>
Fri, 12 Jun 2015 15:08:17 +0000 (17:08 +0200)
This is similar to tdgram_inet_udp_socket(), but it allows
the use of ipv4 broadcast traffic.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=11316

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/tsocket/tsocket.h
lib/tsocket/tsocket_bsd.c

index 296c7c555ae907b51fd7854eff9e9ed3b239989a..f52b7466dfb069cf662a7310122c372a0a52996e 100644 (file)
@@ -705,6 +705,8 @@ bool tdgram_bsd_optimize_recvfrom(struct tdgram_context *dgram,
  *                      communication. The function will allocate the memory.
  *
  * @return              0 on success, -1 on error with errno set.
+ *
+ * @see tdgram_inet_udp_broadcast_socket()
  */
 int tdgram_inet_udp_socket(const struct tsocket_address *local,
                            const struct tsocket_address *remote,
@@ -720,6 +722,33 @@ int _tdgram_inet_udp_socket(const struct tsocket_address *local,
        _tdgram_inet_udp_socket(local, remote, mem_ctx, dgram, __location__)
 #endif
 
+#ifdef DOXYGEN
+/**
+ * @brief Create a tdgram_context for a ipv4 UDP broadcast (and unicast) communication.
+ *
+ * @param[in]  local    An 'inet' (ipv4 only) tsocket_address for the local endpoint.
+ *
+ * @param[in]  mem_ctx  The talloc memory context to use.
+ *
+ * @param[in]  dgram    The tdgram_context pointer to setup the udp
+ *                      communication. The function will allocate the memory.
+ *
+ * @return              0 on success, -1 on error with errno set.
+ *
+ * @see tdgram_inet_udp_socket()
+ */
+int tdgram_inet_udp_broadcast_socket(const struct tsocket_address *local,
+                                    TALLOC_CTX *mem_ctx,
+                                    struct tdgram_context **dgram);
+#else
+int _tdgram_inet_udp_broadcast_socket(const struct tsocket_address *local,
+                                     TALLOC_CTX *mem_ctx,
+                                     struct tdgram_context **dgram,
+                                     const char *location);
+#define tdgram_inet_udp_broadcast_socket(local, mem_ctx, dgram) \
+       _tdgram_inet_udp_broadcast_socket(local, mem_ctx, dgram, __location__)
+#endif
+
 #ifdef DOXYGEN
 /**
  * @brief Create a tdgram_context for unix domain datagram communication.
index 9ddbc061b94bf7db9ae999068f15edc406739857..f1ece7595e0953c859116967675e3e4c3278d304 100644 (file)
@@ -1441,6 +1441,36 @@ int _tdgram_inet_udp_socket(const struct tsocket_address *local,
        return ret;
 }
 
+int _tdgram_inet_udp_broadcast_socket(const struct tsocket_address *local,
+                                     TALLOC_CTX *mem_ctx,
+                                     struct tdgram_context **dgram,
+                                     const char *location)
+{
+       struct tsocket_address_bsd *lbsda =
+               talloc_get_type_abort(local->private_data,
+               struct tsocket_address_bsd);
+       int ret;
+
+       switch (lbsda->u.sa.sa_family) {
+       case AF_INET:
+               break;
+#ifdef HAVE_IPV6
+       case AF_INET6:
+               /* only ipv4 */
+               errno = EINVAL;
+               return -1;
+#endif
+       default:
+               errno = EINVAL;
+               return -1;
+       }
+
+       ret = tdgram_bsd_dgram_socket(local, NULL, true,
+                                     mem_ctx, dgram, location);
+
+       return ret;
+}
+
 int _tdgram_unix_socket(const struct tsocket_address *local,
                        const struct tsocket_address *remote,
                        TALLOC_CTX *mem_ctx,