aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/smugfs/tcp.c
blob: a203ece937e37e8a341a53b7997ed9cc5198fb51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "a.h"

struct Pfd
{
	int fd;
};

static Pfd*
httpconnect(char *host)
{
	char buf[1024];
	Pfd *pfd;
	int fd;

	snprint(buf, sizeof buf, "tcp!%s!http", host);
	if((fd = dial(buf, nil, nil, nil)) < 0)
		return nil;
	pfd = emalloc(sizeof *pfd);
	pfd->fd = fd;
	return pfd;
}

static void
httpclose(Pfd *pfd)
{
	if(pfd == nil)
		return;
	close(pfd->fd);
	free(pfd);
}

static int
httpwrite(Pfd *pfd, void *v, int n)
{
	return writen(pfd->fd, v, n);
}

static int
httpread(Pfd *pfd, void *v, int n)
{
	return read(pfd->fd, v, n);
}

Protocol http = {
	httpconnect,
	httpread,
	httpwrite,
	httpclose,
};