0,0 → 1,314 |
/* |
* pkgnet - pulls SvarDOS packages from the project's online repository |
* Copyright (C) 2021 Mateusz Viste |
* |
* PUBLISHED UNDER THE TERMS OF THE MIT LICENSE |
* |
* COPYRIGHT (C) 2016-2021 MATEUSZ VISTE, ALL RIGHTS RESERVED. |
* |
* Permission is hereby granted, free of charge, to any person obtaining a |
* copy of this software and associated documentation files (the "Software"), |
* to deal in the Software without restriction, including without limitation |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
* and/or sell copies of the Software, and to permit persons to whom the |
* Software is furnished to do so, subject to the following conditions: |
* |
* The above copyright notice and this permission notice shall be included in |
* all copies or substantial portions of the Software. |
* |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
* DEALINGS IN THE SOFTWARE. |
* |
* http://svardos.osdn.io |
*/ |
|
#include <stdio.h> |
#include <stdlib.h> |
#include <string.h> |
#include <time.h> |
|
#include "net.h" |
|
#define PVER "20210520" |
#define PDATE "2021" |
|
#define HOSTADDR "svardos.osdn.io" |
|
|
/* returns length of all http headers, or 0 if uncomplete yet */ |
static unsigned short detecthttpheadersend(const unsigned char *buff) { |
char lastbyteislf = 0; |
unsigned short i; |
for (i = 0; buff[i] != 0; i++) { |
if (buff[i] == '\r') continue; /* ignore CR characters */ |
if (buff[i] != '\n') { |
lastbyteislf = 0; |
continue; |
} |
/* cur byte is LF -> if last one was also LF then this is an empty line, meaning headers are over */ |
if (lastbyteislf == 0) { |
lastbyteislf = 1; |
continue; |
} |
/* end of headers! return length of headers */ |
return(i + 1); /* add 1 to skip the current \n character */ |
} |
return(0); |
} |
|
|
static void help(void) { |
puts("pkgnet ver " PVER " -- Copyright (C) " PDATE " Mateusz Viste"); |
puts(""); |
puts("pkgnet is the SvarDOS package downloader."); |
puts(""); |
puts("usage: pkgnet search <term>"); |
puts(" pkgnet pull <package>"); |
puts(" pkgnet checkup"); |
puts(""); |
puts("actions:"); |
puts(" search - asks remote repository for the list of matching packages"); |
puts(" pull - downloads package into current directory"); |
puts(" checkup - lists updates available for your system"); |
puts(""); |
} |
|
|
/* parses command line arguments and fills outfname and url accordingly |
* returns 0 on success, non-zero otherwise */ |
static int parseargv(int argc, char * const *argv, char *outfname, char *url) { |
*outfname = 0; |
*url = 0; |
if ((argc == 3) && (strcasecmp(argv[1], "search") == 0)) { |
sprintf(url, "/repo/?a=search&p=%s", argv[2]); |
} else if ((argc == 3) && (strcasecmp(argv[1], "pull") == 0)) { |
if ((strlen(argv[2]) > 8) || (argv[2][0] == 0)) { |
puts("ERROR: package name must be 8 characters maximum"); |
return(-1); |
} |
sprintf(url, "/repo/?a=pull&p=%s", argv[2]); |
sprintf(outfname, "%s.zip", argv[2]); |
} else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) { |
puts("NOT SUPPORTED YET"); |
return(-1); |
} else { |
help(); |
return(-1); |
} |
return(0); |
} |
|
|
static int htget_headers(unsigned char *buffer, size_t buffersz, struct net_tcpsocket *sock, int *httpcode) { |
unsigned char *buffptr = buffer; |
unsigned short bufflen = 0; |
int byteread; |
time_t starttime = time(NULL); |
for (;;) { |
byteread = net_recv(sock, buffptr, buffersz - (bufflen + 1)); /* -1 because I will append a NULL terminator */ |
|
if (byteread > 0) { /* got data */ |
int hdlen; |
bufflen += byteread; |
buffptr += byteread; |
buffer[bufflen] = 0; |
hdlen = detecthttpheadersend(buffer); |
if (hdlen > 0) { /* full headers - parse http code and continue processing */ |
int spc; |
/* find the first space (HTTP/1.1 200 OK) */ |
for (spc = 0; spc < 16; spc++) { |
if (buffer[spc] == ' ') break; |
if (buffer[spc] == 0) break; |
} |
if (buffer[spc] != ' ') return(-1); |
*httpcode = atoi((char *)(buffer + spc + 1)); |
/* rewind the buffer */ |
bufflen -= hdlen; |
memmove(buffer, buffer + hdlen, bufflen); |
return(bufflen); /* move to body processing now */ |
} |
|
} else if (byteread < 0) { /* abort on error */ |
return(-2); /* unexpected end of connection (while waiting for all http headers) */ |
|
} else { /* else no data received - look for timeout and release a cpu cycle */ |
if (time(NULL) - starttime > 20) return(-3); /* TIMEOUT! */ |
_asm int 28h; /* release a CPU cycle */ |
} |
} |
} |
|
|
/* fetch http data from ipaddr using url |
* write result to file outfname if not null, or print to stdout otherwise |
* fills bsum with the BSD sum of the data |
* returns the length of data obtained, or neg value on error */ |
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum) { |
struct net_tcpsocket *sock; |
unsigned char buffer[4096]; |
time_t lastactivity, lastprogressoutput = 0; |
int httpcode = -1; |
int byteread; |
long flen = 0, lastflen = 0; |
FILE *fd = NULL; |
|
sock = net_connect(ipaddr, 80); |
if (sock == NULL) { |
puts("ERROR: failed to connect to " HOSTADDR); |
goto SHITQUIT; |
} |
|
/* wait for net_connect() to actually connect */ |
for (;;) { |
int connstate = net_isconnected(sock); |
if (connstate > 0) break; |
if (connstate < 0) { |
puts("ERROR: connection error"); |
goto SHITQUIT; |
} |
_asm int 28h; /* DOS idle */ |
} |
|
/* socket is connected - send the http request (MUST be HTTP/1.0 because I do not support chunked transfers!) */ |
snprintf((char *)buffer, sizeof(buffer), "GET %s HTTP/1.0\r\nHOST: " HOSTADDR "\r\nUSER-AGENT: pkgnet\r\nConnection: close\r\n\r\n", url); |
|
if (net_send(sock, buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) { |
puts("ERROR: failed to send HTTP query to remote server"); |
goto SHITQUIT; |
} |
|
/* receive and process HTTP headers */ |
byteread = htget_headers(buffer, sizeof(buffer), sock, &httpcode); |
|
/* transmission error? */ |
if (byteread < 0) { |
printf("ERROR: communication error (%d)", byteread); |
puts(""); |
goto SHITQUIT; |
} |
|
/* open destination file if required and if no server-side error occured */ |
if ((httpcode == 200) && (*outfname != 0)) { |
fd = fopen(outfname, "wb"); |
if (fd == NULL) { |
printf("ERROR: failed to create file %s", outfname); |
puts(""); |
goto SHITQUIT; |
} |
} |
|
/* read body of the answer */ |
lastactivity = time(NULL); |
for (;; byteread = net_recv(sock, buffer, sizeof(buffer) - 1)) { /* read 1 byte less because I need to append a NULL terminator */ |
|
if (byteread > 0) { /* got data */ |
buffer[byteread] = 0; |
lastactivity = time(NULL); |
/* if downloading to file, write stuff to disk */ |
if (fd != NULL) { |
int i; |
if (fwrite(buffer, 1, byteread, fd) != byteread) { |
printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen); |
puts(""); |
break; |
} |
flen += byteread; |
/* update progress once a sec */ |
if (lastprogressoutput != lastactivity) { |
lastprogressoutput = lastactivity; |
printf("%ld KiB (%ld KiB/s) \r", flen >> 10, (flen >> 10) - (lastflen >> 10)); /* trailing spaces are meant to avoid leaving garbage on screen if speed goes from, say, 1000 KiB/s to 9 KiB/s */ |
lastflen = flen; |
fflush(stdout); /* avoid console buffering */ |
} |
/* update the bsd sum */ |
for (i = 0; i < byteread; i++) { |
/* rotr16 */ |
unsigned short bsumlsb = *bsum & 1; |
*bsum >>= 1; |
*bsum |= (bsumlsb << 15); |
*bsum += buffer[i]; |
} |
} else { /* otherwise dump to screen */ |
printf("%s", buffer); |
} |
|
} else if (byteread < 0) { /* end of connection */ |
break; |
|
} else { /* check for timeout (byteread == 0) */ |
if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */ |
puts("ERROR: Timeout while waiting for data"); |
goto SHITQUIT; |
} |
/* waiting for packets - release a CPU cycle in the meantime */ |
_asm int 28h; |
} |
} |
|
goto ALLGOOD; |
|
SHITQUIT: |
flen = -1; |
|
ALLGOOD: |
if (fd != NULL) fclose(fd); |
net_close(sock); |
return(flen); |
} |
|
|
/* checks if file exists, returns 0 if not, non-zero otherwise */ |
static int fexists(const char *fname) { |
FILE *fd = fopen(fname, "rb"); |
if (fd == NULL) return(0); |
fclose(fd); |
return(1); |
} |
|
|
int main(int argc, char **argv) { |
char ipaddr[64]; |
char url[64]; |
unsigned short bsum = 0; |
char outfname[16]; |
long flen; |
|
/* parse command line arguments */ |
if (parseargv(argc, argv, outfname, url) != 0) return(1); |
|
/* if outfname requested, make sure that file does not exist yet */ |
if (fexists(outfname)) { |
printf("ERROR: file %s already exists", outfname); |
puts(""); |
return(1); |
} |
|
/* init network stack */ |
if (net_init() != 0) { |
puts("ERROR: Network subsystem initialization failed"); |
return(1); |
} |
|
puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */ |
|
if (net_dnsresolve(ipaddr, HOSTADDR) != 0) { |
puts("ERROR: DNS resolution failed"); |
return(1); |
} |
|
flen = htget(ipaddr, url, outfname, &bsum); |
if (flen < 1) return(1); |
|
if (*outfname != 0) { |
/* print bsum, size, filename */ |
printf("Downloaded %ld KiB into %s (BSUM: %04X)", flen >> 10, outfname, bsum); |
puts(""); |
} |
|
return(0); |
} |