]> andersk Git - openssh.git/blobdiff - misc.c
- guenther@cvs.openbsd.org 2009/12/20 07:28:36
[openssh.git] / misc.c
diff --git a/misc.c b/misc.c
index 143dbf0e2d019aa097e642d9854e60c5274a080e..21db00a137533402b68544f1f20e549cf6be01e1 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.71 2009/02/21 19:32:04 tobias Exp $ */
+/* $OpenBSD: misc.c,v 1.73 2009/11/20 03:24:07 djm Exp $ */
 /*
  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
  * Copyright (c) 2005,2006 Damien Miller.  All rights reserved.
@@ -151,6 +151,43 @@ set_nodelay(int fd)
                error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
 }
 
+/* open a socket in the specified routing domain */
+int
+socket_rdomain(int domain, int type, int protocol, int rdomain)
+{
+       int sock, ipproto = IPPROTO_IP;
+
+       if ((sock = socket(domain, type, protocol)) == -1)
+               return (-1);
+
+       if (rdomain == -1)
+               return (sock);
+       
+       switch (domain) {
+       case AF_INET6:
+               ipproto = IPPROTO_IPV6;
+               /* FALLTHROUGH */
+       case AF_INET:
+               debug2("socket %d af %d setting rdomain %d",
+                   sock, domain, rdomain);
+               if (setsockopt(sock, ipproto, SO_RDOMAIN, &rdomain,
+                   sizeof(rdomain)) == -1) {
+                       debug("setsockopt SO_RDOMAIN: %.100s",
+                           strerror(errno));
+                       close(sock);
+                       return (-1);
+               }
+               break;
+       default:
+               debug("socket %d af %d does not support rdomain %d",
+                   sock, domain, rdomain);
+               close(sock);
+               return (-1);
+       }
+
+       return (sock);
+}
+
 /* Characters considered whitespace in strsep calls. */
 #define WHITESPACE " \t\r\n"
 #define QUOTE  "\""
@@ -560,11 +597,11 @@ char *
 percent_expand(const char *string, ...)
 {
 #define EXPAND_MAX_KEYS        16
+       u_int num_keys, i, j;
        struct {
                const char *key;
                const char *repl;
        } keys[EXPAND_MAX_KEYS];
-       u_int num_keys, i, j;
        char buf[4096];
        va_list ap;
 
@@ -576,13 +613,12 @@ percent_expand(const char *string, ...)
                        break;
                keys[num_keys].repl = va_arg(ap, char *);
                if (keys[num_keys].repl == NULL)
-                       fatal("percent_expand: NULL replacement");
+                       fatal("%s: NULL replacement", __func__);
        }
+       if (num_keys == EXPAND_MAX_KEYS && va_arg(ap, char *) != NULL)
+               fatal("%s: too many keys", __func__);
        va_end(ap);
 
-       if (num_keys >= EXPAND_MAX_KEYS)
-               fatal("percent_expand: too many keys");
-
        /* Expand string */
        *buf = '\0';
        for (i = 0; *string != '\0'; string++) {
@@ -590,23 +626,24 @@ percent_expand(const char *string, ...)
  append:
                        buf[i++] = *string;
                        if (i >= sizeof(buf))
-                               fatal("percent_expand: string too long");
+                               fatal("%s: string too long", __func__);
                        buf[i] = '\0';
                        continue;
                }
                string++;
+               /* %% case */
                if (*string == '%')
                        goto append;
                for (j = 0; j < num_keys; j++) {
                        if (strchr(keys[j].key, *string) != NULL) {
                                i = strlcat(buf, keys[j].repl, sizeof(buf));
                                if (i >= sizeof(buf))
-                                       fatal("percent_expand: string too long");
+                                       fatal("%s: string too long", __func__);
                                break;
                        }
                }
                if (j >= num_keys)
-                       fatal("percent_expand: unknown key %%%c", *string);
+                       fatal("%s: unknown key %%%c", __func__, *string);
        }
        return (xstrdup(buf));
 #undef EXPAND_MAX_KEYS
@@ -849,3 +886,14 @@ ms_to_timeval(struct timeval *tv, int ms)
        tv->tv_usec = (ms % 1000) * 1000;
 }
 
+void
+sock_set_v6only(int s)
+{
+#ifdef IPV6_V6ONLY
+       int on = 1;
+
+       debug3("%s: set socket %d IPV6_V6ONLY", __func__, s);
+       if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on)) == -1)
+               error("setsockopt IPV6_V6ONLY: %s", strerror(errno));
+#endif
+}
This page took 0.039543 seconds and 4 git commands to generate.