Skip to contentSkip to conversation

fix vl.c for OpenBSD & part 2 of prototype fixes

PATCH In review Todd T. Fries 2 participants 2 messages May 18, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fix vl.c for OpenBSD & part 2 of prototype fixes

vl.c renamed from vl.c.orig +27 -5
@@ -43,7 +43,8 @@
 #include <netdb.h>
 #ifdef _BSD
 #include <sys/stat.h>
-#ifndef __APPLE__
+#include <net/if.h>
+#if !defined(__APPLE__) && !defined(__OpenBSD__)
 #include <libutil.h>
 #endif
 #else
@@ -292,7 +293,7 @@
 
 /***********************************************************/
 
-void pstrcpy(char *buf, int buf_size, const char *str)
+void pstrcpy(char *buf, size_t buf_size, const char *str)
 {
     int c;
     char *q = buf;
@@ -310,7 +311,7 @@
 }
 
 /* strcat and truncate. */
-char *pstrcat(char *buf, int buf_size, const char *s)
+char *pstrcat(char *buf, size_t buf_size, const char *s)
 {
     int len;
     len = strlen(buf);
@@ -561,7 +562,23 @@
 }
 
 #else
-#error unsupported CPU
+# warning non-optimized CPU
+#include <sys/time.h>
+#include <time.h>
+
+int64_t cpu_get_real_ticks(void)
+{
+    struct timeval tv;
+    static int64_t i = 0;
+    int64_t j;
+
+    gettimeofday(&tv, NULL);
+    do {
+	j = (tv.tv_sec * (uint64_t) 1000000) + tv.tv_usec;
+    } while (i == j);
+    i = j;
+    return j;
+}
 #endif
 
 static int64_t cpu_ticks_prev;
@@ -2201,7 +2218,7 @@
     return 0;
 }
 
-static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
+static int get_str_sep(char *buf, size_t buf_size, const char **pp, int sep)
 {
     const char *p, *p1;
     int len;
@@ -2560,11 +2577,85 @@
     char *dev;
     struct stat s;
 
+    /* If the device was specified on the command line, use it */
+    if (ifname[0]) {
+	fd = open(ifname, O_RDWR);
+	if (fd < 0) {
+	   fprintf(stderr, "warning: could not open %s: no virtual network 

emulation\n", ifname);
+ return -1;
+ }
+ } else {
+#ifdef __OpenBSD__

+    struct ifreq ifr;
+    int i = 0, enoentcount = 0, err = 0, sock;
+    char dname[100], iname[100];
+
+    bzero(&ifr, sizeof(ifr));
+    if (ifname != NULL && ifname[0] != '\0') {
+        snprintf(dname, sizeof(dname), "/dev/%s", ifname);
+        strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
+        fd = open(dname, O_RDWR);

+ } else {
+ for (; i != -1; i++) {
+ snprintf(dname, sizeof dname, "/dev/tun%d", i);
+ bzero(&ifr.ifr_name, sizeof(ifr.ifr_name));

+	    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", i);
+	    fd = open(dname, O_RDWR);
+	    if (fd >= 0)

+ break;
+ else if (errno != ENOENT || ++enoentcount > 3) {
+ if (errno != EBUSY) {
+ err = errno;
+ break;
+ }
+ } else
+ err = errno;
+ }
+ }
+ if (fd < 0) {
+ fprintf(stderr, "warning: could not open %s (%s): no virtual "
+ "network emulation\n", dname, strerror(err));
+ return -1;
+ }
+
+ /* Set the tunnel device operation mode */
+ if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) == -1) {
+ close(fd);
+ return -1;
+ }
+
+ /* Get interface flags */
+ if (ioctl(sock, SIOCGIFFLAGS, &ifr) == -1) {

+        close(fd);
+        close(sock);
+        return -1;
+    }
+
+    /* Set interface mode */
+    ifr.ifr_flags &= ~IFF_UP;
+    ifr.ifr_flags |= IFF_LINK0;
+    if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
+        close(fd);
+        close(sock);
+        return -1;
+    }
+
+    /* Bring interface up */
+    ifr.ifr_flags |= IFF_UP;
+    if (ioctl(sock, SIOCSIFFLAGS, &ifr) == -1) {
+        close(fd);
+        close(sock);
+        return -1;
+    }
+
+#else
     fd = open("/dev/tap", O_RDWR);
     if (fd < 0) {
         fprintf(stderr, "warning: could not open /dev/tap: no virtual network 
emulation\n");
         return -1;
     }

+#endif
+ }

     fstat(fd, &s);
     dev = devname(s.st_rdev, S_IFCHR);

@@ -3065,7 +3156,8 @@

}

-static int get_param_value(char *buf, int buf_size,
+static int get_param_value(char *, size_t, const char *, const char *); +static int get_param_value(char *buf, size_t buf_size,
const char *tag, const char *str)

 {
     const char *p;
@@ -3190,17 +3282,21 @@
         char ifname[64];
         char setup_script[1024];
         int fd;
+	bzero(&ifname,sizeof(ifname));
+	bzero(&setup_script,sizeof(setup_script));
         if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
             fd = strtol(buf, NULL, 0);
             ret = -1;
             if (net_tap_fd_init(vlan, fd))
                 ret = 0;
         } else {

- get_param_value(ifname, sizeof(ifname), "ifname", p);
if (get_param_value(setup_script, sizeof(setup_script), "script",
p) == 0) {
pstrcpy(setup_script, sizeof(setup_script),

DEFAULT_NETWORK_SCRIPT);
             }

- ret = net_tap_init(vlan, ifname, setup_script);
+ if (get_param_value(ifname, sizeof(ifname), "ifname", p) == 0)
+ ret = net_tap_init(vlan, NULL, setup_script);
+ else

+            	ret = net_tap_init(vlan, ifname, setup_script);
         }
     } else
 #endif
@@ -5101,7 +5197,7 @@
         serial_devices[i][0] = '\0';
     serial_device_index = 0;

- pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc");

+    pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "null");
     for(i = 1; i < MAX_PARALLEL_PORTS; i++)
         parallel_devices[i][0] = '\0';
     parallel_device_index = 0;
Signature · 12 lines
Todd Fries .. [email protected]

 _____________________________________________
|                                             \  1.636.410.0632 (voice)
| Free Daemon Consulting, LLC                 \  1.405.227.9094 (voice)
| http://FreeDaemonConsulting.com             \  1.866.792.3418 (FAX)
| "..in support of free software solutions."  \          250797 (FWD)
|                                             \
 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
                                                 
              37E7 D3EB 74D0 8D66 A68D  B866 0326 204E 3F42 004A
                        http://todd.fries.net/pgp.txt
#1 lore raw

I don't know if your patch will be applied or not, but if you had added a description of what your patch does, it would have helped. More than the subject, i mean. ;-)

Regards.

Cesare.