]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/ip/gping.c
merge
[plan9front.git] / sys / src / cmd / ip / gping.c
old mode 100755 (executable)
new mode 100644 (file)
index 194f1f5..0bbee59
@@ -47,7 +47,6 @@ struct Req
 {
        int     seq;    /* sequence number */
        vlong   time;   /* time sent */
-//     int     rtt;
        Req     *next;
 };
 
@@ -55,6 +54,7 @@ struct Machine
 {
        Lock;
        char    *name;
+       int     version;
        int     pingfd;
        int     nproc;
 
@@ -68,13 +68,7 @@ struct Machine
        int     unreachable;
 
        ushort  seq;
-       Req     *first;
-       Req     *last;
-       Req     *rcvd;
-
-       char    buf[1024];
-       char    *bufp;
-       char    *ebufp;
+       Req     *list;
 };
 
 enum
@@ -115,7 +109,6 @@ void                (*newvaluefn[Nmenu2])(Machine*, long*, long*, long*) = {
 Image          *cols[Ncolor][3];
 Graph          *graph;
 Machine                mach[NMACH];
-Font           *mediumfont;
 int            pids[NPROC];
 int            npid;
 int            parity; /* toggled to avoid patterns in textured background */
@@ -190,10 +183,6 @@ mkcol(int i, int c0, int c1, int c2)
 void
 colinit(void)
 {
-       mediumfont = openfont(display, "/lib/font/bit/pelm/latin1.8.font");
-       if(mediumfont == nil)
-               mediumfont = font;
-
        /* Peach */
        mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
        /* Aqua */
@@ -210,26 +199,6 @@ colinit(void)
        cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x888888FF);
 }
 
-int
-loadbuf(Machine *m, int *fd)
-{
-       int n;
-
-
-       if(*fd < 0)
-               return 0;
-       seek(*fd, 0, 0);
-       n = read(*fd, m->buf, sizeof m->buf);
-       if(n <= 0){
-               close(*fd);
-               *fd = -1;
-               return 0;
-       }
-       m->bufp = m->buf;
-       m->ebufp = m->buf+n;
-       return 1;
-}
-
 void
 label(Point p, int dy, char *text)
 {
@@ -242,15 +211,15 @@ label(Point p, int dy, char *text)
        maxw = 0;
        r[1] = '\0';
        for(s=text; *s; ){
-               if(p.y+mediumfont->height-Ysqueeze > maxy)
+               if(p.y+font->height-Ysqueeze > maxy)
                        break;
                w = chartorune(r, s);
                s += w;
-               w = runestringwidth(mediumfont, r);
+               w = runestringwidth(font, r);
                if(w > maxw)
                        maxw = w;
-               runestring(screen, p, display->black, ZP, mediumfont, r);
-               p.y += mediumfont->height-Ysqueeze;
+               runestring(screen, p, display->black, ZP, font, r);
+               p.y += font->height-Ysqueeze;
        }
 }
 
@@ -263,9 +232,9 @@ hashmark(Point p, int dy, long v, long vmax, char *label)
        x = p.x + Labspace;
        y = p.y + (dy*(vmax-v))/vmax;
        draw(screen, Rect(p.x, y-1, p.x+Labspace, y+1), display->black, nil, ZP);
-       if(dy > 5*mediumfont->height)
-               string(screen, Pt(x, y-mediumfont->height/2),
-                       display->black, ZP, mediumfont, label);
+       if(dy > 5*font->height)
+               string(screen, Pt(x, y-font->height/2),
+                       display->black, ZP, font, label);
 }
 
 void
@@ -368,7 +337,7 @@ drawmsg(Graph *g, char *msg)
        /* draw message */
        if(strlen(msg) > g->overtmplen)
                msg[g->overtmplen] = 0;
-       string(screen, g->overtmp->r.min, display->black, ZP, mediumfont, msg);
+       string(screen, g->overtmp->r.min, display->black, ZP, font, msg);
 }
 
 void
@@ -455,13 +424,13 @@ pingreply(Machine *m, Req *r)
 
 
 void
-pingclean(Machine *m, ushort seq, vlong now, int)
+pingclean(Machine *m, ushort seq, vlong now)
 {
        Req **l, *r;
        vlong x, y;
 
        y = 10LL*1000000000LL;
-       for(l = &m->first; *l; ){
+       for(l = &m->list; *l; ){
                r = *l;
                x = now - r->time;
                if(x > y || r->seq == seq){
@@ -477,37 +446,33 @@ pingclean(Machine *m, ushort seq, vlong now, int)
        }
 }
 
-/* IPv4 only */
 void
 pingsend(Machine *m)
 {
        int i;
-       char buf[128], err[ERRMAX];
+       uchar buf[128];
+       char err[ERRMAX];
        Icmphdr *ip;
        Req *r;
 
-       ip = (Icmphdr *)(buf + IPV4HDR_LEN);
+       ip = (Icmphdr *)(buf + (m->version==4? IPV4HDR_LEN: IPV6HDR_LEN));
        memset(buf, 0, sizeof buf);
        r = malloc(sizeof *r);
        if(r == nil)
                return;
 
-       for(i = 32; i < MSGLEN; i++)
+       for(i = ip->data-buf; i < MSGLEN; i++)
                buf[i] = i;
-       ip->type = EchoRequest;
+       ip->type = m->version==4? EchoRequest: EchoRequestV6;
        ip->code = 0;
        ip->seq[0] = m->seq;
        ip->seq[1] = m->seq>>8;
        r->seq = m->seq;
-       r->next = nil;
-       lock(m);
-       pingclean(m, -1, nsec(), 0);
-       if(m->first == nil)
-               m->first = r;
-       else
-               m->last->next = r;
-       m->last = r;
        r->time = nsec();
+       lock(m);
+       pingclean(m, -1, r->time);
+       r->next = m->list;
+       m->list = r;
        unlock(m);
        if(write(m->pingfd, buf, MSGLEN) < MSGLEN){
                errstr(err, sizeof err);
@@ -517,38 +482,32 @@ pingsend(Machine *m)
        m->seq++;
 }
 
-/* IPv4 only */
 void
 pingrcv(void *arg)
 {
-       int i, n, fd;
+       int i, n;
        uchar buf[512];
        ushort x;
-       vlong now;
        Icmphdr *ip;
-       Ip4hdr *ip4;
        Machine *m = arg;
 
-       ip4 = (Ip4hdr *)buf;
-       ip = (Icmphdr *)(buf + IPV4HDR_LEN);
-       fd = dup(m->pingfd, -1);
+       ip = (Icmphdr *)(buf + (m->version==4? IPV4HDR_LEN: IPV6HDR_LEN));
        for(;;){
-               n = read(fd, buf, sizeof(buf));
-               now = nsec();
+               n = read(m->pingfd, buf, sizeof(buf));
                if(n <= 0)
+                       break;
+               if(n < MSGLEN)
                        continue;
-               if(n < MSGLEN){
-                       print("bad len %d/%d\n", n, MSGLEN);
-                       continue;
-               }
-               for(i = 32; i < MSGLEN; i++)
+               for(i = ip->data-buf; i < MSGLEN; i++)
                        if(buf[i] != (i&0xff))
-                               continue;
+                               break;
+               if(i != MSGLEN)
+                       continue;
                x = (ip->seq[1]<<8) | ip->seq[0];
-               if(ip->type != EchoReply || ip->code != 0)
+               if(ip->type != (m->version==4? EchoReply: EchoReplyV6) || ip->code != 0)
                        continue;
                lock(m);
-               pingclean(m, x, now, ip4->ttl);
+               pingclean(m, x, nsec());
                unlock(m);
        }
 }
@@ -556,6 +515,7 @@ pingrcv(void *arg)
 void
 initmach(Machine *m, char *name)
 {
+       int cfd = -1;
        char *p;
 
        srand(time(0));
@@ -565,12 +525,23 @@ initmach(Machine *m, char *name)
                m->name = estrdup(p+1);
        }else
                p = name;
-
        m->name = estrdup(p);
        m->nproc = 1;
-       m->pingfd = dial(netmkaddr(m->name, "icmp", "1"), 0, 0, 0);
-       if(m->pingfd < 0)
+
+       m->version = 4;
+       if(strstr(name, "icmpv6!") != nil)
+               m->version = 6;
+again:
+       m->pingfd = dial(netmkaddr(m->name, m->version==4? "icmp": "icmpv6", "1"), nil, nil, &cfd);
+       if(m->pingfd < 0){
+               if(m->version == 4){
+                       m->version = 6;
+                       goto again;
+               }
                sysfatal("dialing %s: %r", m->name);
+       }
+       write(cfd, "ignoreadvice", 12);
+       close(cfd);
        startproc(pingrcv, m);
 }
 
@@ -635,16 +606,6 @@ lostval(Machine *m, long *v, long *vmax, long *mark)
        *vmax = 100;
 }
 
-jmp_buf catchalarm;
-
-void
-alarmed(void *a, char *s)
-{
-       if(strcmp(s, "alarm") == 0)
-               notejmp(a, catchalarm, 1);
-       noted(NDFLT);
-}
-
 void
 usage(void)
 {
@@ -773,9 +734,9 @@ resize(void)
 
        /* label left edge */
        x = screen->r.min.x;
-       y = screen->r.min.y + Labspace+mediumfont->height+Labspace;
+       y = screen->r.min.y + Labspace+font->height+Labspace;
        dy = (screen->r.max.y - y)/ngraph;
-       dx = Labspace+stringwidth(mediumfont, "0")+Labspace;
+       dx = Labspace+stringwidth(font, "0")+Labspace;
        startx = x+dx+1;
        starty = y;
        for(i=0; i<ngraph; i++,y+=dy){
@@ -786,10 +747,10 @@ resize(void)
        }
 
        /* label right edge */
-       dx = Labspace+stringwidth(mediumfont, "0.001")+Labspace;
+       dx = Labspace+stringwidth(font, "0.001")+Labspace;
        hashdx = dx;
        x = screen->r.max.x - dx;
-       y = screen->r.min.y + Labspace+mediumfont->height+Labspace;
+       y = screen->r.min.y + Labspace+font->height+Labspace;
        for(i=0; i<ngraph; i++,y+=dy){
                draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
                draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
@@ -801,7 +762,7 @@ resize(void)
        dx = (screen->r.max.x - dx - startx)/nmach;
        for(x=startx, i=0; i<nmach; i++,x+=dx){
                draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
-               j = dx/stringwidth(mediumfont, "0");
+               j = dx/stringwidth(font, "0");
                n = mach[i].nproc;
                if(n>1 && j>=1+3+(n>10)+(n>100)){       /* first char of name + (n) */
                        j -= 3+(n>10)+(n>100);
@@ -811,7 +772,7 @@ resize(void)
                }else
                        snprint(buf, sizeof buf, "%.*s", j, mach[i].name);
                string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP,
-                       mediumfont, buf);
+                       font, buf);
        }
        /* draw last vertical line */
        draw(screen,
@@ -847,12 +808,12 @@ resize(void)
                        g->overtmp = nil;
                        g->overtmplen = 0;
                        r = g->r;
-                       r.max.y = r.min.y+mediumfont->height;
-                       n = (g->r.max.x - r.min.x)/stringwidth(mediumfont, "9");
+                       r.max.y = r.min.y+font->height;
+                       n = (g->r.max.x - r.min.x)/stringwidth(font, "9");
                        if(n > 4){
                                if(n > Gmsglen)
                                        n = Gmsglen;
-                               r.max.x = r.min.x+stringwidth(mediumfont, "9")*n;
+                               r.max.x = r.min.x+stringwidth(font, "9")*n;
                                g->overtmplen = n;
                                g->overtmp = allocimage(display, r, screen->chan, 0, -1);
                        }
@@ -1006,6 +967,8 @@ main(int argc, char *argv[])
        }ARGEND
        *f = 0;
 
+       pids[npid++] = getpid();
+
        for(i=0; i<argc; i++)
                addmachine(argv[i]);
 
@@ -1033,11 +996,10 @@ main(int argc, char *argv[])
                fprint(2, "%s: initdraw failed: %r\n", argv0);
                exits("initdraw");
        }
+       display->locking = 1;   /* tell library we're using the display lock */
        colinit();
        einit(Emouse);
-       notify(nil);
        startproc(mouseproc, 0);
-       display->locking = 1;   /* tell library we're using the display lock */
 
        resize();