207 |
mateuszvis |
1 |
/*
|
|
|
2 |
* pkgnet - pulls SvarDOS packages from the project's online repository
|
|
|
3 |
* Copyright (C) 2021 Mateusz Viste
|
|
|
4 |
*
|
|
|
5 |
* PUBLISHED UNDER THE TERMS OF THE MIT LICENSE
|
|
|
6 |
*
|
|
|
7 |
* COPYRIGHT (C) 2016-2021 MATEUSZ VISTE, ALL RIGHTS RESERVED.
|
|
|
8 |
*
|
|
|
9 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
10 |
* copy of this software and associated documentation files (the "Software"),
|
|
|
11 |
* to deal in the Software without restriction, including without limitation
|
|
|
12 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
13 |
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
14 |
* Software is furnished to do so, subject to the following conditions:
|
|
|
15 |
*
|
|
|
16 |
* The above copyright notice and this permission notice shall be included in
|
|
|
17 |
* all copies or substantial portions of the Software.
|
|
|
18 |
*
|
|
|
19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
24 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
25 |
* DEALINGS IN THE SOFTWARE.
|
|
|
26 |
*
|
|
|
27 |
* http://svardos.osdn.io
|
|
|
28 |
*/
|
|
|
29 |
|
|
|
30 |
#include <stdio.h>
|
209 |
mateuszvis |
31 |
#include <stdlib.h>
|
207 |
mateuszvis |
32 |
#include <string.h>
|
|
|
33 |
#include <time.h>
|
|
|
34 |
|
|
|
35 |
#include "net.h"
|
|
|
36 |
|
209 |
mateuszvis |
37 |
#define PVER "20210121"
|
|
|
38 |
#define PDATE "2021"
|
|
|
39 |
|
207 |
mateuszvis |
40 |
#define HOSTADDR "svardos.osdn.io"
|
|
|
41 |
|
209 |
mateuszvis |
42 |
|
207 |
mateuszvis |
43 |
/* strips http headers and returns new buff len */
|
209 |
mateuszvis |
44 |
static int detecthttpheadersend(unsigned char *buff, int len) {
|
207 |
mateuszvis |
45 |
static char lastbyteislf = 0; /* static because I must potentially remember it for next packet/call */
|
|
|
46 |
int i;
|
|
|
47 |
for (i = 0; i < len; i++) {
|
|
|
48 |
if (buff[i] == '\r') continue; /* ignore CR characters */
|
|
|
49 |
if (buff[i] != '\n') {
|
|
|
50 |
lastbyteislf = 0;
|
|
|
51 |
continue;
|
|
|
52 |
}
|
|
|
53 |
/* cur byte is LF -> if last one was also LF then this is an empty line, meaning headers are over */
|
|
|
54 |
if (lastbyteislf == 0) {
|
|
|
55 |
lastbyteislf = 1;
|
|
|
56 |
continue;
|
|
|
57 |
}
|
|
|
58 |
/* end of headers! rewind the buffer and return new len */
|
209 |
mateuszvis |
59 |
len -= (i + 1);
|
|
|
60 |
if (len > 0) memmove(buff, buff + i + 1, len);
|
207 |
mateuszvis |
61 |
return(len);
|
|
|
62 |
}
|
|
|
63 |
return(0);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
|
209 |
mateuszvis |
67 |
static void help(void) {
|
|
|
68 |
puts("pkgnet ver " PVER " -- Copyright (C) " PDATE " Mateusz Viste");
|
|
|
69 |
puts("");
|
|
|
70 |
puts("pkgnet is the SvarDOS package downloader.");
|
|
|
71 |
puts("");
|
|
|
72 |
puts("usage: pkgnet search <term>");
|
|
|
73 |
puts(" pkgnet pull <package>");
|
|
|
74 |
puts(" pkgnet checkup [<package>]");
|
|
|
75 |
puts("");
|
|
|
76 |
puts("actions:");
|
|
|
77 |
puts(" search - asks remote repository for the list of matching packages");
|
|
|
78 |
puts(" pull - downloads package into current directory");
|
|
|
79 |
puts(" checkup - lists available updates (for all packages if no argument)");
|
|
|
80 |
puts("");
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
/* parses command line arguments and fills outfname and url accordingly
|
|
|
85 |
* returns 0 on success, non-zero otherwise */
|
|
|
86 |
static int parseargv(int argc, char * const *argv, char **outfname, char *url) {
|
|
|
87 |
*outfname = NULL;
|
|
|
88 |
*url = 0;
|
|
|
89 |
if ((argc == 3) && (strcasecmp(argv[1], "search") == 0)) {
|
|
|
90 |
sprintf(url, "/pkg.php?a=search&p=%s", argv[2]);
|
|
|
91 |
} else if ((argc == 3) && (strcasecmp(argv[1], "pull") == 0)) {
|
|
|
92 |
sprintf(url, "/pkg.php?a=pull&p=%s", argv[2]);
|
|
|
93 |
*outfname = argv[2];
|
|
|
94 |
} else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
|
|
|
95 |
puts("NOT SUPPORTED YET");
|
|
|
96 |
return(-1);
|
|
|
97 |
} else if ((argc == 3) && (strcasecmp(argv[1], "checkup") == 0)) {
|
|
|
98 |
puts("NOT SUPPORTED YET");
|
|
|
99 |
return(-1);
|
|
|
100 |
} else {
|
|
|
101 |
help();
|
|
|
102 |
return(-1);
|
|
|
103 |
}
|
|
|
104 |
return(0);
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
|
207 |
mateuszvis |
108 |
int main(int argc, char **argv) {
|
209 |
mateuszvis |
109 |
unsigned char buffer[4096];
|
207 |
mateuszvis |
110 |
char url[64];
|
|
|
111 |
struct net_tcpsocket *sock;
|
|
|
112 |
time_t lastactivity;
|
209 |
mateuszvis |
113 |
int headersdone = 0;
|
|
|
114 |
int httpcode = -1;
|
|
|
115 |
long flen = 0;
|
|
|
116 |
unsigned short bsum = 0;
|
|
|
117 |
char *outfname = NULL;
|
|
|
118 |
FILE *fd = NULL;
|
207 |
mateuszvis |
119 |
|
209 |
mateuszvis |
120 |
/* parse command line arguments */
|
|
|
121 |
if (parseargv(argc, argv, &outfname, url) != 0) return(1);
|
207 |
mateuszvis |
122 |
|
|
|
123 |
/* init network stack */
|
|
|
124 |
if (net_init() != 0) {
|
|
|
125 |
puts("ERROR: Network subsystem initialization failed");
|
|
|
126 |
return(1);
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
|
|
|
130 |
|
209 |
mateuszvis |
131 |
if (net_dnsresolve((char *)buffer, HOSTADDR) != 0) {
|
207 |
mateuszvis |
132 |
puts("ERROR: DNS resolution failed");
|
|
|
133 |
return(1);
|
|
|
134 |
}
|
|
|
135 |
|
209 |
mateuszvis |
136 |
sock = net_connect((char *)buffer, 80);
|
207 |
mateuszvis |
137 |
if (sock == NULL) {
|
|
|
138 |
puts("ERROR: failed to connect to " HOSTADDR);
|
|
|
139 |
goto SHITQUIT;
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/* wait for net_connect() to actually connect */
|
|
|
143 |
for (;;) {
|
|
|
144 |
int connstate;
|
|
|
145 |
connstate = net_isconnected(sock, 1);
|
|
|
146 |
if (connstate > 0) break;
|
|
|
147 |
if (connstate < 0) {
|
|
|
148 |
puts("ERROR: connection error");
|
|
|
149 |
goto SHITQUIT;
|
|
|
150 |
}
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
/* socket is connected - send the http request */
|
209 |
mateuszvis |
154 |
snprintf((char *)buffer, sizeof(buffer), "GET %s HTTP/1.1\r\nHOST: " HOSTADDR "\r\nUSER-AGENT: pkgnet\r\nConnection: close\r\n\r\n", url);
|
207 |
mateuszvis |
155 |
|
209 |
mateuszvis |
156 |
if (net_send(sock, (char *)buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
|
207 |
mateuszvis |
157 |
puts("ERROR: failed to send HTTP query to remote server");
|
|
|
158 |
goto SHITQUIT;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
lastactivity = time(NULL);
|
|
|
162 |
for (;;) {
|
209 |
mateuszvis |
163 |
int byteread = net_recv(sock, (char *)buffer, sizeof(buffer) - 1); /* -1 because I will append a NULL terminator */
|
207 |
mateuszvis |
164 |
|
|
|
165 |
if (byteread < 0) break; /* end of connection */
|
|
|
166 |
|
|
|
167 |
/* */
|
|
|
168 |
if (byteread == 0) {
|
|
|
169 |
if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
|
|
|
170 |
puts("ERROR: Timeout while waiting for data");
|
|
|
171 |
goto SHITQUIT;
|
|
|
172 |
}
|
|
|
173 |
/* waiting for packets - release a CPU cycle in the meantime */
|
|
|
174 |
_asm int 28h;
|
|
|
175 |
/* */
|
|
|
176 |
continue;
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
if (byteread > 0) {
|
|
|
180 |
buffer[byteread] = 0;
|
|
|
181 |
lastactivity = time(NULL);
|
209 |
mateuszvis |
182 |
/* do I know the http code yet? */
|
|
|
183 |
if (httpcode < 0) {
|
|
|
184 |
httpcode = atoi((char *)buffer);
|
|
|
185 |
/* on error, the answer should be always printed on screen */
|
|
|
186 |
if ((httpcode == 200) && (outfname != NULL)) {
|
|
|
187 |
fd = fopen(outfname, "wb");
|
|
|
188 |
if (fd == NULL) {
|
|
|
189 |
printf("ERROR: failed to create file %s", outfname);
|
|
|
190 |
puts("");
|
|
|
191 |
break;
|
|
|
192 |
}
|
|
|
193 |
}
|
|
|
194 |
}
|
207 |
mateuszvis |
195 |
/* skip headers: look for \r\n\r\n or \n\n within the stream */
|
|
|
196 |
if (headersdone == 0) {
|
|
|
197 |
byteread = detecthttpheadersend(buffer, byteread);
|
|
|
198 |
headersdone = 1;
|
|
|
199 |
if (byteread == 0) continue;
|
|
|
200 |
}
|
|
|
201 |
/* if downloading to file, write stuff to disk */
|
209 |
mateuszvis |
202 |
if (fd != NULL) {
|
|
|
203 |
int i;
|
|
|
204 |
if (fwrite(buffer, 1, byteread, fd) != byteread) {
|
|
|
205 |
printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen);
|
|
|
206 |
puts("");
|
|
|
207 |
break;
|
|
|
208 |
}
|
|
|
209 |
flen += byteread;
|
|
|
210 |
/* update the bsd sum */
|
|
|
211 |
for (i = 0; i < byteread; i++) {
|
|
|
212 |
/* rotr16 */
|
|
|
213 |
unsigned short bsumlsb = bsum & 1;
|
|
|
214 |
bsum >>= 1;
|
|
|
215 |
bsum |= (bsumlsb << 15);
|
|
|
216 |
/* bsum += ch */
|
|
|
217 |
bsum += buffer[i];
|
|
|
218 |
}
|
|
|
219 |
} else { /* otherwise dump to screen */
|
|
|
220 |
printf("%s", buffer);
|
|
|
221 |
}
|
207 |
mateuszvis |
222 |
}
|
|
|
223 |
}
|
|
|
224 |
|
209 |
mateuszvis |
225 |
if (fd != NULL) {
|
|
|
226 |
/* print bsum, size, filename */
|
|
|
227 |
printf("Downloaded %ld KiB into %s (BSUM: %04X)", flen >> 10, outfname, bsum);
|
|
|
228 |
puts("");
|
|
|
229 |
fclose(fd);
|
|
|
230 |
}
|
|
|
231 |
|
207 |
mateuszvis |
232 |
net_close(sock);
|
|
|
233 |
return(0);
|
|
|
234 |
|
|
|
235 |
SHITQUIT:
|
|
|
236 |
if (sock != NULL) net_abort(sock);
|
|
|
237 |
return(1);
|
|
|
238 |
}
|