fix audio for mac; add devtls

This commit is contained in:
Russ Cox 2006-04-04 11:38:40 +00:00
parent 8f934bc2f7
commit 7e5c23755e
8 changed files with 2375 additions and 0 deletions

View File

@ -230,6 +230,7 @@ extern int fmtinstall(int, int (*)(Fmt*));
extern char* fmtstrflush(Fmt*);
extern int runefmtstrinit(Fmt*);
extern Rune* runefmtstrflush(Fmt*);
extern int encodefmt(Fmt*);
extern int fmtstrcpy(Fmt*, char*);
extern int fmtprint(Fmt*, char*, ...);
extern int fmtvprint(Fmt*, char*, va_list);

View File

@ -21,6 +21,7 @@ OFILES=\
devpipe.$O\
devroot.$O\
devssl.$O\
devtls.$O\
devtab.$O\
error.$O\
parse.$O\

View File

@ -21,6 +21,20 @@ audiodevclose(void)
error("no audio support");
}
int
audiodevread(void *a, int n)
{
error("no audio support");
return -1;
}
int
audiodevwrite(void *a, int n)
{
error("no audio support");
return -1;
}
void
audiodevsetvol(int what, int left, int right)
{

View File

@ -8,6 +8,7 @@ extern Dev consdevtab;
extern Dev rootdevtab;
extern Dev pipedevtab;
extern Dev ssldevtab;
extern Dev tlsdevtab;
extern Dev mousedevtab;
extern Dev drawdevtab;
extern Dev ipdevtab;
@ -21,6 +22,7 @@ Dev *devtab[] = {
&consdevtab,
&pipedevtab,
&ssldevtab,
&tlsdevtab,
&mousedevtab,
&drawdevtab,
&ipdevtab,

2179
kern/devtls.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -18,6 +18,7 @@ OFILES=\
dirwstat.$O\
dofmt.$O\
dorfmt.$O\
encodefmt.$O\
fcallfmt.$O\
fltfmt.$O\
fmt.$O\
@ -39,6 +40,7 @@ OFILES=\
nsec.$O\
pow10.$O\
pushssl.$O\
pushtls.$O\
read9pmsg.$O\
readn.$O\
rune.$O\

77
libc/encodefmt.c Normal file
View File

@ -0,0 +1,77 @@
#include <u.h>
#include <libc.h>
#include <ctype.h>
int
encodefmt(Fmt *f)
{
char *out;
char *buf;
int len;
int ilen;
int rv;
uchar *b;
char *p;
char obuf[64]; // rsc optimization
if(!(f->flags&FmtPrec) || f->prec < 1)
goto error;
b = va_arg(f->args, uchar*);
if(b == 0)
return fmtstrcpy(f, "<nil>");
ilen = f->prec;
f->prec = 0;
f->flags &= ~FmtPrec;
switch(f->r){
case '<':
len = (8*ilen+4)/5 + 3;
break;
case '[':
len = (8*ilen+5)/6 + 4;
break;
case 'H':
len = 2*ilen + 1;
break;
default:
goto error;
}
if(len > sizeof(obuf)){
buf = malloc(len);
if(buf == nil)
goto error;
} else
buf = obuf;
// convert
out = buf;
switch(f->r){
case '<':
rv = enc32(out, len, b, ilen);
break;
case '[':
rv = enc64(out, len, b, ilen);
break;
case 'H':
rv = enc16(out, len, b, ilen);
if(rv >= 0 && (f->flags & FmtLong))
for(p = buf; *p; p++)
*p = tolower(*p);
break;
default:
rv = -1;
break;
}
if(rv < 0)
goto error;
fmtstrcpy(f, buf);
if(buf != obuf)
free(buf);
return 0;
error:
return fmtstrcpy(f, "<encodefmt>");
}

99
libc/pushtls.c Normal file
View File

@ -0,0 +1,99 @@
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <mp.h>
#include <libsec.h>
enum {
TLSFinishedLen = 12,
HFinished = 20,
};
static int
finished(int hand, int isclient)
{
int i, n;
uchar buf[500], buf2[500];
buf[0] = HFinished;
buf[1] = TLSFinishedLen>>16;
buf[2] = TLSFinishedLen>>8;
buf[3] = TLSFinishedLen;
n = TLSFinishedLen+4;
for(i=0; i<2; i++){
if(i==0)
memmove(buf+4, "client finished", TLSFinishedLen);
else
memmove(buf+4, "server finished", TLSFinishedLen);
if(isclient == 1-i){
if(write(hand, buf, n) != n)
return -1;
}else{
if(readn(hand, buf2, n) != n || memcmp(buf,buf2,n) != 0)
return -1;
}
}
return 1;
}
// given a plain fd and secrets established beforehand, return encrypted connection
int
pushtls(int fd, char *hashalg, char *encalg, int isclient, char *secret, char *dir)
{
char buf[8];
char dname[64];
int n, data, ctl, hand;
// open a new filter; get ctl fd
data = hand = -1;
// /net/tls uses decimal file descriptors to name channels, hence a
// user-level file server can't stand in for #a; may as well hard-code it.
ctl = open("#a/tls/clone", ORDWR);
if(ctl < 0)
goto error;
n = read(ctl, buf, sizeof(buf)-1);
if(n < 0)
goto error;
buf[n] = 0;
if(dir)
sprint(dir, "#a/tls/%s", buf);
// get application fd
sprint(dname, "#a/tls/%s/data", buf);
data = open(dname, ORDWR);
if(data < 0)
goto error;
// get handshake fd
sprint(dname, "#a/tls/%s/hand", buf);
hand = open(dname, ORDWR);
if(hand < 0)
goto error;
// speak a minimal handshake
if(fprint(ctl, "fd %d 0x301", fd) < 0 ||
fprint(ctl, "version 0x301") < 0 ||
fprint(ctl, "secret %s %s %d %s", hashalg, encalg, isclient, secret) < 0 ||
fprint(ctl, "changecipher") < 0 ||
finished(hand, isclient) < 0 ||
fprint(ctl, "opened") < 0){
close(hand);
hand = -1;
goto error;
}
close(ctl);
close(hand);
close(fd);
return data;
error:
if(data>=0)
close(data);
if(ctl>=0)
close(ctl);
if(hand>=0)
close(hand);
return -1;
}