]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/ip/gping.c
merge
[plan9front.git] / sys / src / cmd / ip / gping.c
index ad5d89172352aa06dcad42e5831aad080d6fb85f..705bc98889879aa34d735d8f4c2074907cfb2ee6 100644 (file)
@@ -47,7 +47,6 @@ struct Req
 {
        int     seq;    /* sequence number */
        vlong   time;   /* time sent */
-//     int     rtt;
        Req     *next;
 };
 
@@ -68,13 +67,7 @@ struct Machine
        int     unreachable;
 
        ushort  seq;
-       Req     *first;
-       Req     *last;
-       Req     *rcvd;
-
-       char    buf[1024];
-       char    *bufp;
-       char    *ebufp;
+       Req     *list;
 };
 
 enum
@@ -205,26 +198,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)
 {
@@ -456,7 +429,7 @@ pingclean(Machine *m, ushort seq, vlong now, int)
        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){
@@ -494,15 +467,11 @@ pingsend(Machine *m)
        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, 0);
+       r->next = m->list;
+       m->list = r;
        unlock(m);
        if(write(m->pingfd, buf, MSGLEN) < MSGLEN){
                errstr(err, sizeof err);
@@ -516,34 +485,31 @@ pingsend(Machine *m)
 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);
        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++)
                        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)
                        continue;
                lock(m);
-               pingclean(m, x, now, ip4->ttl);
+               pingclean(m, x, nsec(), ip4->ttl);
                unlock(m);
        }
 }
@@ -551,6 +517,7 @@ pingrcv(void *arg)
 void
 initmach(Machine *m, char *name)
 {
+       int cfd = -1;
        char *p;
 
        srand(time(0));
@@ -563,9 +530,11 @@ initmach(Machine *m, char *name)
 
        m->name = estrdup(p);
        m->nproc = 1;
-       m->pingfd = dial(netmkaddr(m->name, "icmp", "1"), 0, 0, 0);
+       m->pingfd = dial(netmkaddr(m->name, "icmp", "1"), nil, nil, &cfd);
        if(m->pingfd < 0)
                sysfatal("dialing %s: %r", m->name);
+       write(cfd, "ignoreadvice", 12);
+       close(cfd);
        startproc(pingrcv, m);
 }
 
@@ -630,16 +599,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)
 {
@@ -1001,6 +960,8 @@ main(int argc, char *argv[])
        }ARGEND
        *f = 0;
 
+       pids[npid++] = getpid();
+
        for(i=0; i<argc; i++)
                addmachine(argv[i]);
 
@@ -1028,11 +989,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();