Open mode checks, don't use == for OREAD
[akaros.git] / kern / src / net / iprouter.c
1 // INFERNO
2 #include <vfs.h>
3 #include <kfs.h>
4 #include <slab.h>
5 #include <kmalloc.h>
6 #include <kref.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <assert.h>
10 #include <error.h>
11 #include <cpio.h>
12 #include <pmap.h>
13 #include <smp.h>
14 #include <ip.h>
15
16 struct IProuter iprouter;
17
18 /*
19  *  User level routing.  Ip packets we don't know what to do with
20  *  come here.
21  */
22 void
23 useriprouter(struct Fs *f, struct Ipifc *ifc, struct block *bp)
24 {
25         qlock(&(&f->iprouter)->qlock);
26         if(f->iprouter.q != NULL){
27                 bp = padblock(bp, IPaddrlen);
28                 if(bp == NULL)
29                         return;
30                 ipmove(bp->rp, ifc->lifc->local);
31                 qpass(f->iprouter.q, bp);
32         }else
33                 freeb(bp);
34         qunlock(&(&f->iprouter)->qlock);
35 }
36
37 void
38 iprouteropen(struct Fs *f)
39 {
40         qlock(&(&f->iprouter)->qlock);
41         f->iprouter.opens++;
42         if(f->iprouter.q == NULL)
43                 f->iprouter.q = qopen(64*1024, 0, 0, 0);
44         else if(f->iprouter.opens == 1)
45                 qreopen(f->iprouter.q);
46         qunlock(&(&f->iprouter)->qlock);
47 }
48
49 void
50 iprouterclose(struct Fs *f)
51 {
52         qlock(&(&f->iprouter)->qlock);
53         f->iprouter.opens--;
54         if(f->iprouter.opens == 0)
55                 qclose(f->iprouter.q);
56         qunlock(&(&f->iprouter)->qlock);
57 }
58
59 long
60 iprouterread(struct Fs *f, void *a, int n)
61 {
62         return qread(f->iprouter.q, a, n);
63 }