-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpd-modoki.c
340 lines (282 loc) · 5.66 KB
/
lpd-modoki.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// SPDX-License-Identifier: WTFPL
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netdb.h>
extern char *optarg;
static int port = 515;
static char *queue = NULL;
static char *file = NULL;
static int multi = 0;
static int debug = 0;
static int stream = 0;
#define BUFSIZE 16384
static char buf[BUFSIZE];
#define send_ack(d) send_response(d, 0)
#define send_nak(d) send_response(d, 1)
#define min(a, b) (((a) < (b)) ? (a) : (b))
static int send_response(int d, int nak)
{
unsigned char rsp = nak;
return (write(d, &rsp, sizeof(rsp)) >= sizeof(rsp)) ? 0 : -1;
}
static int recv_cmd(int d)
{
unsigned char cmd;
return (read(d, &cmd, sizeof(cmd)) >= sizeof(cmd)) ? cmd : -1;
}
static int recv_until_lf(int d)
{
int i = 0;
unsigned char c;
while (1) {
if (read(d, &c, sizeof(c)) < sizeof(c))
return -1;
if (c == 0x0a)
break;
if (i < BUFSIZE -1)
buf[i++] = c;
}
buf[i++] = 0;
return i;
}
static int recv_file(int d, FILE *fp, long long count, int disp)
{
int len, s, rv = -1;
long long c, remain;
s = count < 0;
for (c = 0; s || c < count; c += len) {
remain = s ? BUFSIZE : min(BUFSIZE, count - c);
if ((len = read(d, buf, remain)) < 1) {
if (s) {
break;
} else {
fprintf(stderr, "recv_file: read\n");
goto fin0;
}
}
if (debug && disp) {
buf[min(remain, BUFSIZE - 1)] = '\0';
fprintf(stderr, "%s", buf);
}
if (fp != NULL)
fwrite(buf, len, 1, fp);
}
if (!s) {
/* check transfer complete */
if (recv_cmd(d)) {
fprintf(stderr, "recv_file: recv_cmd\n");
goto fin0;
}
send_ack(d);
}
if (debug) {
fprintf(stderr, "%lld bytes %s\n",
c, (fp == NULL) ? "discarded" : "received");
}
rv = 0;
fin0:
return rv;
}
static int create_socket(struct sockaddr_in *addr, char *hostname, int port)
{
int s;
struct hostent *h;
struct in_addr *a;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
goto fin0;
memset(addr, 0, sizeof(*addr));
if (hostname == NULL)
addr->sin_addr.s_addr = INADDR_ANY;
else if ((h = gethostbyname(hostname)) != NULL &&
h->h_addrtype == AF_INET &&
(a = (struct in_addr *)h->h_addr) != NULL)
addr->sin_addr.s_addr = a->s_addr;
else
addr->sin_addr.s_addr = INADDR_NONE;
if (addr->sin_addr.s_addr == INADDR_NONE) {
close(s);
s = -1;
goto fin0;
}
addr->sin_port = htons(port);
addr->sin_family = AF_INET;
fin0:
return s;
}
static void do_command2_loop(int d)
{
int subcmd, len, once = 1;
long long count;
FILE *fp;
fp = (file != NULL) ? fopen(file, "w") : stdout;
if (fp == NULL) {
fprintf(stderr, "do_command2_loop: fopen NULL\n");
goto fin0;
}
while (1) {
if ((subcmd = recv_cmd(d)) < 0)
goto fin1;
if ((len = recv_until_lf(d)) < 0)
goto fin1;
if (debug) {
fprintf(stderr,
"subcmd=%d len=%d arg=%s\n", subcmd, len, buf);
}
count = atoll(buf);
/* process subcommands */
switch (subcmd) {
case 0x01:
send_ack(d);
/* do nothing */
break;
case 0x02:
send_ack(d);
if (recv_file(d, NULL, count, 1) < 0)
goto fin1;
break;
case 0x03:
if (stream ? (count < 0) : (count <= 0)) {
send_nak(d);
goto fin1;
} else {
send_ack(d);
if (recv_file(d, (multi || once) ? fp : NULL,
(stream && !count) ? -1 : count,
0) < 0)
goto fin1;
once = 0;
}
break;
default:
send_nak(d);
break;
}
}
fin1:
if (file != NULL)
fclose(fp);
fin0:
return;
}
static int is_invalid_queue(void)
{
char *p;
if ((p = strchr(buf, ' ')) != NULL)
*p = 0;
return (queue == NULL) ? 0 : strcmp(buf, queue);
}
static int do_command_loop(int fd)
{
int cmd, len, d;
struct sockaddr_in peer;
socklen_t peer_len;
peer_len = sizeof(peer);
if ((d = accept(fd, (struct sockaddr *)&peer, &peer_len)) < 0) {
fprintf(stderr, "do_main: accept\n");
return -1;
}
if (debug) {
fprintf(stderr, "connected from %s port %d\n",
inet_ntoa(peer.sin_addr), ntohs(peer.sin_port));
}
while (1) {
if ((cmd = recv_cmd(d)) < 0)
goto fin0;
if ((len = recv_until_lf(d)) < 0)
goto fin0;
if (debug) {
fprintf(stderr,
"cmd=%d len=%d arg=%s\n", cmd, len, buf);
}
if (is_invalid_queue()) {
send_nak(d);
continue;
}
/* only accept command 02, "Receive a printer job" */
switch (cmd) {
default:
send_nak(d);
break;
case 0x02:
send_ack(d);
do_command2_loop(d);
goto fin0;
}
}
fin0:
close(d);
return 0;
}
static int do_main(char *ipstr)
{
int fd, en = 1, rv = -1;
struct sockaddr_in addr;
/* create socket */
if ((fd = create_socket(&addr, ipstr, port)) < 0) {
fprintf(stderr, "do_main: create_socket\n");
goto fin0;
}
/* wait for connect */
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &en, sizeof(en));
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
fprintf(stderr, "do_main: bind\n");
goto fin1;
}
if (listen(fd, 1) < 0) {
fprintf(stderr, "do_main: listen\n");
goto fin1;
}
rv = do_command_loop(fd);
fin1:
close(fd);
fin0:
return rv;
}
int main(int argc, char *argv[])
{
int ch, help = 0;
char *ipstr = NULL;
char *appname = argv[0];
while ((ch = getopt(argc, argv, "p:a:q:f:mdsh")) != -1) {
switch (ch) {
case 'p':
port = atoi(optarg);
break;
case 'a':
ipstr = optarg;
break;
case 'q':
queue = optarg;
break;
case 'f':
file = optarg;
break;
case 'm':
multi = 1;
stream = 0;
break;
case 'd':
debug = 1;
break;
case 's':
multi = 0;
stream = 1;
break;
case 'h':
default:
help = 1;
break;
}
}
if (help) {
fprintf(stderr, "usage: %s -a [ip address] -p [portnum] "
"-q [queue] -f [filename]\n", appname);
return -1;
}
return do_main(ipstr);
}