1 /* Copyright © Coraid, Inc. 2006. All rights reserved. */
2 #include <u.h>
3 #include <libc.h>
4 #include "cec.h"
5
6 int fd = -1;
7 int cfd = -1;
8 int efd = -1;
9
10 int
netopen0(char * e)11 netopen0(char *e)
12 {
13 char buf[128], ctl[13];
14 int n;
15
16 snprint(buf, sizeof buf, "%s/clone", e);
17 if((efd = open(buf, ORDWR)) == -1)
18 return -1;
19 memset(ctl, 0, sizeof ctl);
20 if(read(efd, ctl, sizeof ctl) < 0)
21 return -1;
22 n = atoi(ctl);
23 snprint(buf, sizeof buf, "connect %d", Etype);
24 if(write(efd, buf, strlen(buf)) != strlen(buf))
25 return -1;
26 snprint(buf, sizeof buf, "%s/%d/ctl", e, n);
27 if((cfd = open(buf, ORDWR)) < 0)
28 return -1;
29 snprint(buf, sizeof buf, "nonblocking");
30 if(write(cfd, buf, strlen(buf)) != strlen(buf))
31 return -1;
32 snprint(buf, sizeof buf, "%s/%d/data", e, n);
33 fd = open(buf, ORDWR);
34 return fd;
35 }
36
37 void
netclose(void)38 netclose(void)
39 {
40 close(efd);
41 close(cfd);
42 close(fd);
43 efd = -1;
44 cfd = -1;
45 fd = -1;
46 }
47
48 int
netopen(char * e)49 netopen(char *e)
50 {
51 int r;
52
53 if((r = netopen0(e)) >= 0)
54 return r;
55 perror("netopen");
56 netclose();
57 return -1;
58 }
59
60 /* what if len < netlen? */
61 int
netget(void * v,int len)62 netget(void *v, int len)
63 {
64 int l;
65
66 l = read(fd, v, len);
67 if(debug && l > 0){
68 fprint(2, "read %d bytes\n", l);
69 dump((uchar*)v, l);
70 }
71 if (l <= 0)
72 return 0;
73 return l;
74 }
75
76 int
netsend(void * v,int len)77 netsend(void *v, int len)
78 {
79 uchar *p;
80
81 p = v;
82 if (debug) {
83 fprint(2, "sending %d bytes\n", len);
84 dump(p, len);
85 }
86 if (len < 60)
87 len = 60; /* mintu */
88 return write(fd, p, len);
89 }
90