-
Notifications
You must be signed in to change notification settings - Fork 1
/
kasa.c
134 lines (122 loc) · 3 KB
/
kasa.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdint.h>
#include <stdio.h>
#include <err.h>
#include <netdb.h>
#include <sysexits.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/uio.h>
static void xor171_enc(unsigned char buf[], size_t n) {
int key = 171;
for (unsigned char *p = buf, *e = buf + n; p != e; ++p) {
*p = (key ^= *p);
}
}
static void xor171_dec(unsigned char buf[], size_t n) {
int key = 171;
for (unsigned char *p = buf, *e = buf + n; p != e; ++p) {
*p ^= key;
key ^= *p;
}
}
static void read_fully(int fd, void *buf, size_t n) {
while (n) {
ssize_t r = read(fd, buf, n);
if (r < 0) {
err(EX_IOERR, "read");
}
buf = (unsigned char *) buf + r;
n -= r;
}
}
static void writev_fully(int fd, struct iovec iov[], int iovcnt) {
for (struct iovec *end = iov + iovcnt; iov != end;) {
ssize_t w = writev(fd, iov, (int) (end - iov));
if (w < 0) {
err(EX_IOERR, "write");
}
for (; w > 0; ++iov) {
if (iov->iov_len > (size_t) w) {
iov->iov_base = (unsigned char *) iov->iov_base + w;
iov->iov_len -= w;
break;
}
w -= iov->iov_len;
iov->iov_base = (unsigned char *) iov->iov_base + iov->iov_len;
iov->iov_len = 0;
}
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: %s <host>\n", argc > 0 ? argv[0] : "kasa");
return EX_USAGE;
}
struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
}, *info, *pai;
int e;
if ((e = getaddrinfo(argv[1], NULL, &hints, &info)) != 0) {
if (e == EAI_SYSTEM) {
err(EX_OSERR, "getaddrinfo: %s", argv[1]);
}
fprintf(stderr, "%s: %s: %s: %s\n", argv[0], "getaddrinfo", argv[1], gai_strerror(e));
return EX_NOHOST;
}
int sock = -1;
for (pai = info; pai != NULL; pai = pai->ai_next) {
if ((sock = socket(pai->ai_family, pai->ai_socktype, pai->ai_protocol)) < 0) {
err(EX_OSERR, "socket");
}
((struct sockaddr_in *) pai->ai_addr)->sin_port = htons(9999);
if (connect(sock, pai->ai_addr, pai->ai_addrlen) == 0) {
goto connected;
}
if (close(sock) < 0) {
err(EX_OSERR, "close");
}
}
err(EX_UNAVAILABLE, "connect");
connected:
freeaddrinfo(info);
struct timeval timeout = {
.tv_sec = 15,
};
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout) < 0) {
err(EX_OSERR, "setsockopt");
}
unsigned char buf[4096], *p = buf;
for (;;) {
int c = getchar();
if (c == EOF) {
return p == buf ? EX_OK : EX_IOERR;
}
if (c != '\n') {
if (p == buf + sizeof buf) {
return EX_DATAERR;
}
*p++ = (unsigned char) c;
}
else if (p != buf) {
size_t n = p - buf;
uint32_t len_be = htonl((uint32_t) n);
xor171_enc(buf, n);
struct iovec iov[2] = {
{ .iov_base = &len_be, .iov_len = sizeof len_be },
{ .iov_base = buf, .iov_len = n }
};
writev_fully(sock, iov, sizeof iov / sizeof *iov);
read_fully(sock, &len_be, sizeof len_be);
n = ntohl(len_be);
if (n > sizeof buf) {
return EX_PROTOCOL;
}
read_fully(sock, buf, n);
xor171_dec(buf, n);
fwrite(buf, 1, n, stdout);
putchar('\n');
p = buf;
}
}
}