From 4544da52005bf0af3bb05a73f4e02ce35117aae0 Mon Sep 17 00:00:00 2001 From: David du Colombier <0intro@gmail.com> Date: Tue, 8 Nov 2011 09:02:31 -0500 Subject: [PATCH] libventi: add functions reconn and redial for use with fossil R=rsc CC=plan9port.codebot http://codereview.appspot.com/5325046 --- include/venti.h | 2 ++ man/man3/venti-conn.3 | 6 ++++++ src/libventi/conn.c | 22 ++++++++++++++++++++++ src/libventi/dial.c | 18 ++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/include/venti.h b/include/venti.h index 2861feee..632746db 100644 --- a/include/venti.h +++ b/include/venti.h @@ -335,7 +335,9 @@ struct VtConn }; VtConn* vtconn(int infd, int outfd); +int vtreconn(VtConn*, int, int); VtConn* vtdial(char*); +int vtredial(VtConn*, char *); void vtfreeconn(VtConn*); int vtsend(VtConn*, Packet*); Packet* vtrecv(VtConn*); diff --git a/man/man3/venti-conn.3 b/man/man3/venti-conn.3 index 293777c2..ea597de3 100644 --- a/man/man3/venti-conn.3 +++ b/man/man3/venti-conn.3 @@ -28,9 +28,15 @@ typedef struct VtConn { VtConn* vtconn(int infd, int outfd) .PP .B +int vtreconn(VtConn *z, int infd, int outfd) +.PP +.B VtConn* vtdial(char *addr) .PP .B +int vtredial(VtConn *z, char *addr) +.PP +.B int vtversion(VtConn *z) .PP .B diff --git a/src/libventi/conn.c b/src/libventi/conn.c index e488c6db..2a8f67b9 100644 --- a/src/libventi/conn.c +++ b/src/libventi/conn.c @@ -27,6 +27,28 @@ vtconn(int infd, int outfd) return z; } +int +vtreconn(VtConn *z, int infd, int outfd) +{ + NetConnInfo *nci; + + z->state = VtStateAlloc; + if(z->infd >= 0) + close(z->infd); + z->infd = infd; + if(z->outfd >= 0) + close(z->outfd); + z->outfd = outfd; + nci = getnetconninfo(nil, infd); + if(nci == nil) + snprint(z->addr, sizeof z->addr, "/dev/fd/%d", infd); + else{ + strecpy(z->addr, z->addr+sizeof z->addr, nci->raddr); + freenetconninfo(nci); + } + return 0; +} + void vtfreeconn(VtConn *z) { diff --git a/src/libventi/dial.c b/src/libventi/dial.c index 060b93f6..00482473 100644 --- a/src/libventi/dial.c +++ b/src/libventi/dial.c @@ -23,3 +23,21 @@ vtdial(char *addr) strecpy(z->addr, z->addr+sizeof z->addr, na); return z; } + +int +vtredial(VtConn *z, char *addr) +{ + char *na; + int fd; + + if(addr == nil) + addr = getenv("venti"); + if(addr == nil) + addr = "$venti"; + + na = netmkaddr(addr, "tcp", "venti"); + if((fd = dial(na, nil, nil, nil)) < 0) + return fd; + + return vtreconn(z, fd, fd); +}