uidgen uuid.c patch for FreeBSD

Reid Linnemann <[email protected]> Tue, 01 Nov 2005 16:28:02 -0600
Newsgroups gmane.comp.web.zope.plone.collective.ticle
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------020206030408020508090401
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

I don't know if this project is still alive, but uuid.c,v1.1 does not 
compile on FreeBSD. Attached is a (hasty) fix.

Note that most likely this #elif'ed block could be used on both 
defined(__FreeBSD__) and defined(__NetBSD__) conditions.
-- 
Reid Linnemann
Senior Systems Analyst
Oklahoma Department of CareerTech
405-743-5422
[email protected]

-Ars longa, vita brevis-

--------------020206030408020508090401
Content-Type: text/plain;
 name="uuid_freebsd.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="uuid_freebsd.diff"

--- uuid_1.1.c	Tue Nov  1 16:11:25 2005
+++ uuid.c	Tue Nov  1 16:01:47 2005
@@ -172,6 +172,94 @@
     return fd;
 }
 
+
+#elif defined(__FreeBSD__)
+
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <sys/socket.h>
+#include <sys/time.h>
+#include <net/if.h>
+#include <net/if_dl.h>
+#include <net/if_types.h>
+#include <netinet/in.h>
+
+#define __int64 long long
+
+/* Ask the kernel for an ethernet card's globally unique node id. If an error
+ * occurs, or no ethernet card is found, return -1. Return 0 when successful.
+ * Source code for this function is inspired from e2fsprogs 1.33 uuidgen's.
+ */
+
+static int get_ethernet_node_id(unsigned char *node_id, size_t node_len)
+{
+    int sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
+	
+    if (sd >= 0)
+    {
+        struct ifreq ifr, *ifrp;
+        struct ifconf ifc;
+        char buf[1024];
+
+        memset(buf, 0, sizeof(buf));
+        ifc.ifc_len = sizeof(buf);
+        ifc.ifc_buf = buf;
+
+        if (ioctl(sd, SIOCGIFCONF, (char *) &ifc) >= 0)
+        {
+            int n = ifc.ifc_len;
+            int ifn;
+
+            for (ifn = 0; ifn < n; ifn += sizeof(struct ifreq)) {
+                struct ifreq *ifrp = (struct ifreq *)
+                    ((char *) ifc.ifc_buf + ifn);
+
+                strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
+
+                if (ioctl(sd, SIOCGIFADDR, &ifr) < 0)
+                    continue;
+                else
+                {
+                    unsigned char *id = (unsigned char *)
+                        &ifr.ifr_addr.sa_data;
+                    int i = -1;
+
+                    /* ensure non-zero */
+                    while (++i < node_len) if (id[i]) break;
+
+                    if (i < node_len)
+                    {
+                        if (node_id)
+                            memcpy(node_id, id, node_len);
+                        close(sd);
+
+                        return 0;
+                    }
+                }
+            }
+        }
+
+        close(sd);
+    }
+
+    return -1;
+}
+
+static int get_random_fd(void)
+{
+    static int fd = -2;
+
+    if (fd == -2)
+    {
+        fd = open("/dev/urandom", O_RDONLY);
+        if (fd == -1)
+            fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
+    }
+
+    return fd;
+}
+
+
 #elif defined(linux)
 
 #include <unistd.h>

--------------020206030408020508090401--


-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP.  Click here to play: http://sourceforge.net/geronimo.php