.BI /dev/tun nn
files, it will work.
.SP
-.BI "TUN - " tun-name " open-error " device " " ecode " " message
+.BI "TUN \- " tun-name " open-error " device " " ecode " " message
An attempt to open the tunnel device file
.I device
failed.
.BI "TUN \- unet getinfo-error " ecode " " message
Reading information about the Unet interface failed. Unet is obsolete
and shouldn't be used any more.
-.SP
-.BI "TUN \- unet ifname-too-long"
-The Unet interface's name overflowed, so we couldn't read it properly.
-Unet is obsolete and shouldn't be used any more.
.SS "USER warnings"
These are issued by administration clients using the
.B WARN
*/
void p_setifname(peer *p, const char *name)
- { if (p->ifname) xfree(p->ifname); p->ifname = xstrdup(name); }
+{
+ xfree(p->ifname);
+ p->ifname = xstrdup(name);
+ if (p->spec.tops->setifname)
+ p->spec.tops->setifname(p->t, name);
+}
/* --- @p_addr@ --- *
*
p->ifname = 0;
memset(&p->st, 0, sizeof(stats));
p->st.t_start = time(0);
- if ((p->t = spec->tops->create(p)) == 0)
+ if ((p->t = spec->tops->create(p, &p->ifname)) == 0)
goto tidy_0;
p_setkatimer(p);
if (kx_init(&p->kx, p, &p->ks))
goto tidy_1;
- p_setifname(p, spec->tops->ifname(p->t));
p->next = peers;
if (peers)
peers->prev = p;
tidy_1:
if (spec->t_ka)
sel_rmtimer(&p->tka);
+ xfree(p->ifname);
p->t->ops->destroy(p->t);
tidy_0:
xfree(p->spec.name);
p->t->ops->destroy(p->t);
if (p->spec.t_ka)
sel_rmtimer(&p->tka);
- xfree(p->spec.name);
for (pg = p->pings; pg; pg = ppg) {
ppg = pg->next;
p_pingdone(pg, PING_PEERDIED);
typedef struct tunnel_ops {
const char *name; /* Name of this tunnel driver */
void (*init)(void); /* Initializes the system */
- tunnel *(*create)(struct peer */*p*/); /* Initializes a new tunnel */
- const char *(*ifname)(tunnel */*t*/); /* Returns tunnel's interface name */
+ tunnel *(*create)(struct peer */*p*/, char **/*ifn*/);
+ /* Initializes a new tunnel */
+ void (*setifname)(tunnel */*t*/, const char */*ifn*/);
+ /* Notifies ifname change */
void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
} tunnel_ops;
unsigned n; /* Number of my tunnel device */
};
-/* --- @t_ifname@ --- *
- *
- * Arguments: @tunnel *t@ = pointer to tunnel block
- *
- * Returns: A pointer to the tunnel's interface name.
- */
-
-static const char *t_ifname(tunnel *t)
-{
- static char buf[8];
- sprintf(buf, "tun%u", t->n);
- return (buf);
-}
-
/* --- @t_read@ --- *
*
* Arguments: @int fd@ = file descriptor to read
n = read(fd, buf_i, sizeof(buf_i));
if (n < 0) {
- a_warn("TUN", "%s", t_ifname(t), "read-error", "?ERRNO", A_END);
+ a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
return;
}
IF_TRACING(T_TUNNEL, {
/* --- @t_create@ --- *
*
* Arguments: @peer *p@ = pointer to peer block
+ * @char **ifn@ = where to put the interface name
*
* Returns: A tunnel block if it worked, or null on failure.
*
* Use: Initializes a new tunnel.
*/
-static tunnel *t_create(peer *p)
+static tunnel *t_create(peer *p, char **ifn)
{
int fd;
unsigned n;
t->n = n;
sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
sel_addfile(&t->f);
+ *ifn = xstrdup(buf + 5);
T( trace(T_TUNNEL, "tun-bsd: attached interface %s to peer `%s'",
- t_ifname(t), p_name(p)); )
+ *ifn, p_name(p)); )
return (t);
}
"bsd",
t_init,
t_create,
- t_ifname,
+ 0,
t_inject,
t_destroy
};
const tunnel_ops *ops; /* Pointer to operations */
sel_file f; /* Selector for TUN/TAP device */
struct peer *p; /* Pointer to my peer */
- char ifn[IFNAMSIZ]; /* Interface name buffer */
};
/* --- @t_read@ --- *
n = read(fd, buf_i, sizeof(buf_i));
if (n < 0) {
- a_warn("TUN", "%s", t->ifn, "read-error", "?ERRNO", A_END);
+ a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
return;
}
IF_TRACING(T_TUNNEL, {
/* --- @t_create@ --- *
*
* Arguments: @peer *p@ = pointer to peer block
+ * @char **ifn@ = where to put the interface name
*
* Returns: A tunnel block if it worked, or null on failure.
*
* Use: Initializes a new tunnel.
*/
-static tunnel *t_create(peer *p)
+static tunnel *t_create(peer *p, char **ifn)
{
int fd;
int f;
sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
sel_addfile(&t->f);
iff.ifr_name[IFNAMSIZ - 1] = 0;
- strcpy(t->ifn, iff.ifr_name);
+ *ifn = xstrdup(iff.ifr_name);
T( trace(T_TUNNEL, "tun-linux: attached interface %s to peer `%s'",
- t->ifn, p_name(p)); )
+ *ifn, p_name(p)); )
return (t);
}
-/* --- @t_ifname@ --- *
- *
- * Arguments: @tunnel *t@ = pointer to tunnel block
- *
- * Returns: A pointer to the tunnel's interface name.
- */
-
-static const char *t_ifname(tunnel *t) { return (t->ifn); }
-
/* --- @t_inject@ --- *
*
* Arguments: @tunnel *t@ = pointer to tunnel block
"linux",
t_init,
t_create,
- t_ifname,
+ 0,
t_inject,
t_destroy
};
#endif
errno == EAGAIN)
return;
- a_warn("TUN", "%s", t->sl->name, "read-error", "?ERRNO", A_END);
+ a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
return;
}
if (!n) {
- a_warn("TUN", "%s", t->sl->name, "slip", "eof", A_END);
+ a_warn("TUN", "%s", p_ifname(t->p), "slip", "eof", A_END);
t->st = ST_EOF;
sel_rmfile(&t->f);
return;
if (st & ST_BAD)
;
else if (st & ST_ESC)
- a_warn("TUN", "%s", t->sl->name, "slip", "escape-end", A_END);
+ a_warn("TUN", "%s", p_ifname(t->p), "slip", "escape-end", A_END);
else if (q == t->buf) {
T( trace(T_TUNNEL, "tun-slip: empty packet"); )
} else {
break;
case SL_ESC:
if ((st & ST_ESC) && !(st & ST_BAD)) {
- a_warn("TUN", "%s", t->sl->name, "slip", "bad-escape", A_END);
+ a_warn("TUN", "%s", p_ifname(t->p), "slip", "bad-escape", A_END);
st |= ST_BAD;
} else
st |= ST_ESC;
goto emit;
default:
if ((st & ST_ESC) && !(st & ST_BAD)) {
- a_warn("TUN", "%s", t->sl->name, "slip", "bad-escape", A_END);
+ a_warn("TUN", "%s", p_ifname(t->p), "slip", "bad-escape", A_END);
st |= ST_BAD;
}
emit:
if (q < ll)
*q++ = o;
else {
- a_warn("TUN", "%s", t->sl->name, "slip", "overflow", A_END);
+ a_warn("TUN", "%s", p_ifname(t->p), "slip", "overflow", A_END);
st |= ST_BAD;
}
}
/* --- @t_create@ --- *
*
* Arguments: @peer *p@ = pointer to peer block
+ * @char **ifn@ = where to put the interface name
*
* Returns: A tunnel block if it worked, or null on failure.
*
* Use: Initializes a new tunnel.
*/
-static tunnel *t_create(peer *p)
+static tunnel *t_create(peer *p, char **ifn)
{
slipif *sl = 0;
int pin[2] = { -1, -1 }, pout[2] = { -1, -1 };
sel_initfile(&sel, &t->f, sl->ifd, SEL_READ, t_read, t);
sel_addfile(&t->f);
write(sl->ofd, end, sizeof(end));
+ *ifn = xstrdup(sl->name);
dstr_destroy(&d);
return (t);
return (0);
}
-/* --- @t_ifname@ --- *
+/* --- @t_setifname@ --- *
*
* Arguments: @tunnel *t@ = pointer to tunnel block
+ * @const char *ifn@ = new interface name
*
- * Returns: A pointer to the tunnel's interface name.
+ * Returns: ---
+ *
+ * Use: Updates the interface name of a slip interface.
*/
-static const char *t_ifname(tunnel *t) { return (t->sl->name); }
+static void t_setifname(tunnel *t, const char *ifn)
+ { xfree(t->sl->name); t->sl->name = xstrdup(ifn); }
/* --- @t_inject@ --- *
*
"slip",
t_init,
t_create,
- t_ifname,
+ t_setifname,
t_inject,
t_destroy
};
struct peer *p; /* Pointer to my peer */
};
-/* --- @t_ifname@ --- *
- *
- * Arguments: @tunnel *t@ = pointer to tunnel block
- *
- * Returns: A pointer to the tunnel's interface name.
- */
-
-static const char *t_ifname(tunnel *t)
-{
- static char b[UNET_NAMEMAX];
- struct unet_info uni;
- if (ioctl(t->f.fd, UNIOCGINFO, &uni)) {
- a_warn("TUN", "-", "unet", "getinfo-error", "?ERRNO", A_END);
- return ("<error>");
- }
- if (strlen(uni.uni_ifname) + 1 > sizeof(b)) {
- a_warn("TUN", "-", "unet", "ifname-too-long", A_END);
- return ("<error>");
- }
- strcpy(b, uni.uni_ifname);
- return (b);
-}
-
/* --- @t_read@ --- *
*
* Arguments: @int fd@ = file descriptor to read
n = read(fd, buf_i, sizeof(buf_i));
if (n < 0) {
- a_warn("TUN", "%s", t_ifname(t), "read-error", "?ERRNO", A_END);
+ a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
return;
}
IF_TRACING(T_TUNNEL, {
*
* Arguments: @tunnel *t@ = pointer to tunnel block
* @peer *p@ = pointer to peer block
+ * @char *ifn@ = where to put the interface name
*
* Returns: A tunnel block if it worked, or null on failure.
*
* Use: Initializes a new tunnel.
*/
-static tunnel *t_create(peer *p)
+static tunnel *t_create(peer *p, char **ifn)
{
int fd;
tunnel *t;
int f;
+ struct unet_info uni;
if ((fd = open("/dev/unet", O_RDWR)) < 0) {
a_warn("TUN", "-", "unet", "open-error", "/dev/unet", "?ERRNO", A_END);
t->p = p;
sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
sel_addfile(&t->f);
+
+ if (ioctl(t->f.fd, UNIOCGINFO, &uni)) {
+ a_warn("TUN", "-", "unet", "getinfo-error", "?ERRNO", A_END);
+ return ("<error>");
+ }
+ *ifn = xstrdup(uni.uni_ifname);
T( trace(T_TUNNEL, "tun-unet: attached interface %s to peer `%s'",
- t_ifname(t), p_name(p)); )
+ *ifn, p_name(p)); )
return (t);
}
"unet",
t_init,
t_create,
- t_ifname,
+ 0,
t_inject,
t_destroy
};