lib/tsocket: add tsocket_address_is_inet() function
authorStefan Metzmacher <metze@samba.org>
Tue, 27 Apr 2010 08:34:15 +0000 (10:34 +0200)
committerStefan Metzmacher <metze@samba.org>
Tue, 27 Apr 2010 11:00:24 +0000 (13:00 +0200)
metze

lib/tsocket/tsocket.h
lib/tsocket/tsocket_bsd.c

index d983325c4540935a1fd59c3f355584a6887013d4..a008fd8232f1d6779d1a472fe0fb421fd9ac0dab 100644 (file)
@@ -101,6 +101,7 @@ struct iovec;
  *
  * @return              The address as a string representation, NULL on error.
  *
+ * @see tsocket_address_is_inet()
  * @see tsocket_address_inet_addr_string()
  * @see tsocket_address_inet_port()
  */
@@ -486,6 +487,20 @@ int tstream_disconnect_recv(struct tevent_req *req,
  * @{
  */
 
+/**
+ * @brief Find out if the tsocket_address represents an ipv4 or ipv6 endpoint.
+ *
+ * @param[in]  addr     The tsocket_address pointer
+ *
+ * @param[in]  fam      The family can be can be "ipv4", "ipv6" or "ip". With
+ *                      "ip" is autodetects "ipv4" or "ipv6" based on the
+ *                      addr.
+ *
+ * @return              true if addr represents an address of the given family,
+ *                      otherwise false.
+ */
+bool tsocket_address_is_inet(const struct tsocket_address *addr, const char *fam);
+
 #if DOXYGEN
 /**
  * @brief Create a tsocket_address for ipv4 and ipv6 endpoint addresses.
@@ -533,6 +548,8 @@ int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
  *
  * @return              A newly allocated string of the address, NULL on error
  *                      with errno set.
+ *
+ * @see tsocket_address_is_inet()
  */
 char *tsocket_address_inet_addr_string(const struct tsocket_address *addr,
                                       TALLOC_CTX *mem_ctx);
index 43defb30c809d30b6c3c00d52ee61ecf411738c0..5adf9214c8ee9d80a90ce72c4aed92b17c0057ae 100644 (file)
@@ -294,6 +294,43 @@ ssize_t tsocket_address_bsd_sockaddr(const struct tsocket_address *addr,
        return sa_socklen;
 }
 
+bool tsocket_address_is_inet(const struct tsocket_address *addr, const char *fam)
+{
+       struct tsocket_address_bsd *bsda = talloc_get_type(addr->private_data,
+                                          struct tsocket_address_bsd);
+
+       if (!bsda) {
+               return false;
+       }
+
+       switch (bsda->u.sa.sa_family) {
+       case AF_INET:
+               if (strcasecmp(fam, "ip") == 0) {
+                       return true;
+               }
+
+               if (strcasecmp(fam, "ipv4") == 0) {
+                       return true;
+               }
+
+               return false;
+#ifdef HAVE_IPV6
+       case AF_INET6:
+               if (strcasecmp(fam, "ip") == 0) {
+                       return true;
+               }
+
+               if (strcasecmp(fam, "ipv6") == 0) {
+                       return true;
+               }
+
+               return false;
+#endif
+       }
+
+       return false;
+}
+
 int _tsocket_address_inet_from_strings(TALLOC_CTX *mem_ctx,
                                       const char *fam,
                                       const char *addr,