Subversion Repositories SvarDOS

Rev

Rev 712 | Rev 875 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
207 mateuszvis 1
/*
2
 * pkgnet - pulls SvarDOS packages from the project's online repository
3
 *
4
 * PUBLISHED UNDER THE TERMS OF THE MIT LICENSE
5
 *
558 mateuszvis 6
 * COPYRIGHT (C) 2016-2022 MATEUSZ VISTE, ALL RIGHTS RESERVED.
207 mateuszvis 7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a
9
 * copy of this software and associated documentation files (the "Software"),
10
 * to deal in the Software without restriction, including without limitation
11
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
 * and/or sell copies of the Software, and to permit persons to whom the
13
 * Software is furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24
 * DEALINGS IN THE SOFTWARE.
25
 *
712 mateusz.vi 26
 * http://svardos.org
207 mateuszvis 27
 */
28
 
344 mateuszvis 29
#include <direct.h> /* opendir() and friends */
207 mateuszvis 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"
342 mateuszvis 36
#include "unchunk.h"
207 mateuszvis 37
 
712 mateusz.vi 38
#include "svarlang.lib\svarlang.h"
39
 
633 mateusz.vi 40
#include "../../pkg/trunk/lsm.h"
344 mateuszvis 41
 
42
 
725 mateusz.vi 43
#define PVER "20220216"
558 mateuszvis 44
#define PDATE "2021-2022"
209 mateuszvis 45
 
633 mateusz.vi 46
#define HOSTADDR "svardos.org"
207 mateuszvis 47
 
209 mateuszvis 48
 
712 mateusz.vi 49
/* convenience define that outputs nls strings to screen (followed by CR/LF) */
50
#define putsnls(x,y) puts(svarlang_strid((x << 8) | y))
51
 
52
 
338 mateuszvis 53
/* returns length of all http headers, or 0 if uncomplete yet */
54
static unsigned short detecthttpheadersend(const unsigned char *buff) {
55
  char lastbyteislf = 0;
56
  unsigned short i;
57
  for (i = 0; buff[i] != 0; i++) {
207 mateuszvis 58
    if (buff[i] == '\r') continue; /* ignore CR characters */
59
    if (buff[i] != '\n') {
60
      lastbyteislf = 0;
61
      continue;
62
    }
63
    /* cur byte is LF -> if last one was also LF then this is an empty line, meaning headers are over */
64
    if (lastbyteislf == 0) {
65
      lastbyteislf = 1;
66
      continue;
67
    }
338 mateuszvis 68
    /* end of headers! return length of headers */
69
    return(i + 1); /* add 1 to skip the current \n character */
207 mateuszvis 70
  }
71
  return(0);
72
}
73
 
74
 
209 mateuszvis 75
static void help(void) {
76
  puts("pkgnet ver " PVER " -- Copyright (C) " PDATE " Mateusz Viste");
77
  puts("");
712 mateusz.vi 78
  putsnls(1, 0);  /* "pkgnet is the SvarDOS package downloader" */
209 mateuszvis 79
  puts("");
712 mateusz.vi 80
  putsnls(1, 1);  /* "usage:  pkgnet search <term>" */
81
  putsnls(1, 2);  /* "        pkgnet pull <package>" */
82
  putsnls(1, 3);  /* "        pkgnet pull <package>-<version>" */
83
  putsnls(1, 6);  /* "        pkgnet checkup" */
209 mateuszvis 84
  puts("");
712 mateusz.vi 85
  putsnls(1, 7);  /* "actions:" */
209 mateuszvis 86
  puts("");
712 mateusz.vi 87
  putsnls(1, 8);  /* "search   - asks remote repository for the list of matching packages" */
88
  putsnls(1, 9);  /* "pull     - downloads package into current directory" */
89
  putsnls(1, 10); /* "checkup  - lists updates available for your system" */
90
  puts("");
342 mateuszvis 91
  printf("Watt32 kernel: %s", net_engine());
92
  puts("");
209 mateuszvis 93
}
94
 
95
 
96
/* parses command line arguments and fills outfname and url accordingly
97
 * returns 0 on success, non-zero otherwise */
342 mateuszvis 98
static int parseargv(int argc, char * const *argv, char *outfname, char *url, int *ispost) {
725 mateusz.vi 99
  const char *lang = getenv("LANG");
100
  if (lang == NULL) lang = "";
211 mateuszvis 101
  *outfname = 0;
209 mateuszvis 102
  *url = 0;
342 mateuszvis 103
  *ispost = 0;
209 mateuszvis 104
  if ((argc == 3) && (strcasecmp(argv[1], "search") == 0)) {
725 mateusz.vi 105
    sprintf(url, "/repo/?a=search&p=%s&lang=%s", argv[2], lang);
209 mateuszvis 106
  } else if ((argc == 3) && (strcasecmp(argv[1], "pull") == 0)) {
558 mateuszvis 107
    unsigned short i;
725 mateusz.vi 108
    sprintf(url, "/repo/?a=pull&p=%s&lang=%s", argv[2], lang);
558 mateuszvis 109
    /* copy argv[2] into outfname, but stop at first '-' or null terminator
110
     * this trims any '-version' part in filename to respect 8+3 */
111
    for (i = 0; (argv[2][i] != 0) && (argv[2][i] != '-') && (i < 8); i++) {
112
      outfname[i] = argv[2][i];
211 mateuszvis 113
    }
675 mateusz.vi 114
    /* add the svp extension to filename */
115
    strcpy(outfname + i, ".svp");
209 mateuszvis 116
  } else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
725 mateusz.vi 117
    sprintf(url, "/repo/?a=checkup&lang=%s", lang);
342 mateuszvis 118
    *ispost = 1;
209 mateuszvis 119
  } else {
120
    help();
121
    return(-1);
122
  }
123
  return(0);
124
}
125
 
126
 
342 mateuszvis 127
static int htget_headers(unsigned char *buffer, size_t buffersz, struct net_tcpsocket *sock, int *httpcode, int *ischunked)  {
338 mateuszvis 128
  unsigned char *buffptr = buffer;
129
  unsigned short bufflen = 0;
130
  int byteread;
131
  time_t starttime = time(NULL);
132
  for (;;) {
133
    byteread = net_recv(sock, buffptr, buffersz - (bufflen + 1)); /* -1 because I will append a NULL terminator */
134
 
135
    if (byteread > 0) { /* got data */
136
      int hdlen;
137
      bufflen += byteread;
138
      buffptr += byteread;
139
      buffer[bufflen] = 0;
140
      hdlen = detecthttpheadersend(buffer);
141
      if (hdlen > 0) { /* full headers - parse http code and continue processing */
342 mateuszvis 142
        int i;
143
        buffer[hdlen - 1] = 0;
338 mateuszvis 144
        /* find the first space (HTTP/1.1 200 OK) */
342 mateuszvis 145
        for (i = 0; i < 16; i++) {
146
          if ((buffer[i] == ' ') || (buffer[i] == 0)) break;
338 mateuszvis 147
        }
342 mateuszvis 148
        if (buffer[i] != ' ') return(-1);
149
        *httpcode = atoi((char *)(buffer + i + 1));
150
        /* switch all headers to low-case so it is easier to parse them */
151
        for (i = 0; i < hdlen; i++) if ((buffer[i] >= 'A') && (buffer[i] <= 'Z')) buffer[i] += ('a' - 'A');
152
        /* look out for chunked transfer encoding */
153
        {
154
        char *lineptr = strstr((char *)buffer, "\ntransfer-encoding:");
155
        if (lineptr != NULL) {
156
          lineptr += 19;
157
          /* replace nearest \r, \n or 0 by 0 */
158
          for (i = 0; ; i++) {
159
            if ((lineptr[i] == '\r') || (lineptr[i] == '\n') || (lineptr[i] == 0)) {
160
              lineptr[i] = 0;
161
              break;
162
            }
163
          }
164
          /* do I see the 'chunked' word? */
165
          if (strstr((char *)lineptr, "chunked") != NULL) *ischunked = 1;
166
        }
167
        }
338 mateuszvis 168
        /* rewind the buffer */
169
        bufflen -= hdlen;
170
        memmove(buffer, buffer + hdlen, bufflen);
171
        return(bufflen); /* move to body processing now */
172
      }
173
 
174
    } else if (byteread < 0) { /* abort on error */
175
      return(-2); /* unexpected end of connection (while waiting for all http headers) */
176
 
177
    } else { /* else no data received - look for timeout and release a cpu cycle */
178
      if (time(NULL) - starttime > 20) return(-3); /* TIMEOUT! */
179
      _asm int 28h; /* release a CPU cycle */
180
    }
181
  }
182
}
183
 
184
 
342 mateuszvis 185
/* provides body data of the POST query for checkup actions
186
   fills buff with data and returns data length.
187
   must be called repeateadly until zero-lengh is returned */
188
static unsigned short checkupdata(char *buff) {
344 mateuszvis 189
  static char *dosdir = NULL;
190
  static DIR *dp;
191
  static struct dirent *ep;
192
 
193
  /* make sure I know %DOSDIR% */
194
  if (dosdir == NULL) {
195
    dosdir = getenv("DOSDIR");
196
    if ((dosdir == NULL) || (dosdir[0] == 0)) {
712 mateusz.vi 197
      putsnls(9, 0); /* "ERROR: %DOSDIR% not set" */
344 mateuszvis 198
      return(0);
199
    }
200
  }
201
 
202
  /* if first call, open the package directory */
203
  if (dp == NULL) {
204
    sprintf(buff, "%s\\packages", dosdir);
205
    dp = opendir(buff);
206
    if (dp == NULL) {
712 mateusz.vi 207
      putsnls(9, 1); /* "ERROR: Could not access %DOSDIR%\\packages directory" */
344 mateuszvis 208
      return(0);
209
    }
210
  }
211
 
212
  for (;;) {
213
    int tlen;
347 mateuszvis 214
    char ver[20];
344 mateuszvis 215
    ep = readdir(dp);   /* readdir() result must never be freed (statically allocated) */
216
    if (ep == NULL) {   /* no more entries */
217
      closedir(dp);
218
      return(0);
219
    }
220
 
221
    tlen = strlen(ep->d_name);
222
    if (tlen < 4) continue; /* files must be at least 5 bytes long ("x.lst") */
223
    if (strcasecmp(ep->d_name + tlen - 4, ".lst") != 0) continue;  /* if not an .lst file, skip it silently */
224
    ep->d_name[tlen - 4] = 0; /* trim out the ".lst" suffix */
225
 
226
    /* load the metadata from %DOSDIR\APPINFO\*.lsm */
227
    sprintf(buff, "%s\\appinfo\\%s.lsm", dosdir, ep->d_name);
228
    readlsm(buff, ver, sizeof(ver));
229
 
346 mateuszvis 230
    return(sprintf(buff, "%s\t%s\n", ep->d_name, ver));
344 mateuszvis 231
  }
342 mateuszvis 232
}
233
 
234
 
211 mateuszvis 235
/* fetch http data from ipaddr using url
236
 * write result to file outfname if not null, or print to stdout otherwise
237
 * fills bsum with the BSD sum of the data
342 mateuszvis 238
 * is ispost is non-zero, then the request is a POST and its body data is
239
 * obtained through repeated calls to checkupdata()
211 mateuszvis 240
 * returns the length of data obtained, or neg value on error */
342 mateuszvis 241
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum, int ispost, unsigned char *buffer, size_t buffersz) {
211 mateuszvis 242
  struct net_tcpsocket *sock;
327 mateuszvis 243
  time_t lastactivity, lastprogressoutput = 0;
342 mateuszvis 244
  int httpcode = -1, ischunked = 0;
338 mateuszvis 245
  int byteread;
332 mateuszvis 246
  long flen = 0, lastflen = 0;
209 mateuszvis 247
  FILE *fd = NULL;
207 mateuszvis 248
 
342 mateuszvis 249
  /* unchunk state variable is using a little part of the supplied buffer */
250
  struct unchunk_state *unchstate = (void *)buffer;
251
  buffer += sizeof(*unchstate);
252
  buffersz -= sizeof(*unchstate);
253
  memset(unchstate, 0, sizeof(*unchstate));
254
 
211 mateuszvis 255
  sock = net_connect(ipaddr, 80);
207 mateuszvis 256
  if (sock == NULL) {
712 mateusz.vi 257
    printf(svarlang_strid(0x0902), HOSTADDR); /* "ERROR: failed to connect to " HOSTADDR */
258
    puts("");
207 mateuszvis 259
    goto SHITQUIT;
260
  }
261
 
262
  /* wait for net_connect() to actually connect */
263
  for (;;) {
329 mateuszvis 264
    int connstate = net_isconnected(sock);
207 mateuszvis 265
    if (connstate > 0) break;
266
    if (connstate < 0) {
712 mateusz.vi 267
      printf(svarlang_strid(0x0902), HOSTADDR); /* "ERROR: failed to connect to " HOSTADDR */
268
      puts("");
207 mateuszvis 269
      goto SHITQUIT;
270
    }
211 mateuszvis 271
    _asm int 28h;  /* DOS idle */
207 mateuszvis 272
  }
273
 
330 mateuszvis 274
  /* socket is connected - send the http request (MUST be HTTP/1.0 because I do not support chunked transfers!) */
342 mateuszvis 275
  if (ispost) {
276
    snprintf((char *)buffer, buffersz, "POST %s HTTP/1.1\r\nHOST: " HOSTADDR "\r\nUSER-AGENT: pkgnet/" PVER "\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n", url);
277
  } else {
278
    snprintf((char *)buffer, buffersz, "GET %s HTTP/1.1\r\nHOST: " HOSTADDR "\r\nUSER-AGENT: pkgnet/" PVER "\r\nConnection: close\r\n\r\n", url);
279
  }
207 mateuszvis 280
 
329 mateuszvis 281
  if (net_send(sock, buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
712 mateusz.vi 282
    putsnls(9, 3); /* "ERROR: failed to send a HTTP query to remote server" */
207 mateuszvis 283
    goto SHITQUIT;
284
  }
285
 
342 mateuszvis 286
  /* send chunked data for POST queries */
287
  if (ispost) {
288
    unsigned short blen;
289
    int hlen;
290
    char *hbuf = (char *)buffer + buffersz - 16;
344 mateuszvis 291
    do {
342 mateuszvis 292
      blen = checkupdata((char *)buffer);
293
      if (blen == 0) { /* last item contains the message trailer */
344 mateuszvis 294
        hlen = sprintf(hbuf, "0\r\n");
342 mateuszvis 295
      } else {
296
        hlen = sprintf(hbuf, "%X\r\n", blen);
297
      }
298
      if (net_send(sock, hbuf, hlen) != hlen) {
712 mateusz.vi 299
        putsnls(9, 4); /* "ERROR: failed to send POST data to remote server" */
342 mateuszvis 300
        goto SHITQUIT;
301
      }
344 mateuszvis 302
      /* add trailing CR/LF to buffer as required by chunked mode */
303
      buffer[blen++] = '\r';
304
      buffer[blen++] = '\n';
342 mateuszvis 305
      if (net_send(sock, buffer, blen) != blen) {
712 mateusz.vi 306
        putsnls(9, 4); /* "ERROR: failed to send POST data to remote server" */
342 mateuszvis 307
        goto SHITQUIT;
308
      }
344 mateuszvis 309
    } while (blen != 2);
342 mateuszvis 310
  }
311
 
338 mateuszvis 312
  /* receive and process HTTP headers */
342 mateuszvis 313
  byteread = htget_headers(buffer, buffersz, sock, &httpcode, &ischunked);
207 mateuszvis 314
 
338 mateuszvis 315
  /* transmission error? */
316
  if (byteread < 0) {
712 mateusz.vi 317
    printf(svarlang_strid(0x0905), byteread); /* "ERROR: TCP communication error #%d" */
338 mateuszvis 318
    puts("");
319
    goto SHITQUIT;
320
  }
207 mateuszvis 321
 
338 mateuszvis 322
  /* open destination file if required and if no server-side error occured */
342 mateuszvis 323
  if ((httpcode >= 200) && (httpcode <= 299) && (*outfname != 0)) {
338 mateuszvis 324
    fd = fopen(outfname, "wb");
325
    if (fd == NULL) {
712 mateusz.vi 326
      printf(svarlang_strid(0x0906), outfname); /* "ERROR: failed to create file %s" */
338 mateuszvis 327
      puts("");
328
      goto SHITQUIT;
207 mateuszvis 329
    }
338 mateuszvis 330
  }
207 mateuszvis 331
 
342 mateuszvis 332
  /* read body of the answer (and chunk-decode it if needed) */
338 mateuszvis 333
  lastactivity = time(NULL);
342 mateuszvis 334
  for (;; byteread = net_recv(sock, buffer, buffersz - 1)) { /* read 1 byte less because I need to append a NULL terminator when printf'ing the content */
338 mateuszvis 335
 
336
    if (byteread > 0) { /* got data */
207 mateuszvis 337
      lastactivity = time(NULL);
342 mateuszvis 338
 
339
      /* unpack data if server transmits in chunked mode */
340
      if (ischunked) byteread = unchunk(buffer, byteread, unchstate);
341
 
207 mateuszvis 342
      /* if downloading to file, write stuff to disk */
209 mateuszvis 343
      if (fd != NULL) {
344
        int i;
345
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
712 mateusz.vi 346
          printf(svarlang_strid(0x0907), outfname, flen); /* "ERROR: failed to write data to file %s after %ld bytes" */
209 mateuszvis 347
          puts("");
348
          break;
349
        }
350
        flen += byteread;
327 mateuszvis 351
        /* update progress once a sec */
352
        if (lastprogressoutput != lastactivity) {
353
          lastprogressoutput = lastactivity;
333 mateuszvis 354
          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 */
332 mateuszvis 355
          lastflen = flen;
356
          fflush(stdout); /* avoid console buffering */
327 mateuszvis 357
        }
209 mateuszvis 358
        /* update the bsd sum */
359
        for (i = 0; i < byteread; i++) {
360
          /* rotr16 */
211 mateuszvis 361
          unsigned short bsumlsb = *bsum & 1;
362
          *bsum >>= 1;
363
          *bsum |= (bsumlsb << 15);
364
          *bsum += buffer[i];
209 mateuszvis 365
        }
366
      } else { /* otherwise dump to screen */
342 mateuszvis 367
        buffer[byteread] = 0;
209 mateuszvis 368
        printf("%s", buffer);
369
      }
338 mateuszvis 370
 
371
    } else if (byteread < 0) { /* end of connection */
372
      break;
373
 
374
    } else { /* check for timeout (byteread == 0) */
375
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
712 mateusz.vi 376
        putsnls(9, 8); /* "ERROR: Timeout while waiting for data" */
338 mateuszvis 377
        goto SHITQUIT;
378
      }
379
      /* waiting for packets - release a CPU cycle in the meantime */
380
      _asm int 28h;
207 mateuszvis 381
    }
382
  }
383
 
338 mateuszvis 384
  goto ALLGOOD;
385
 
211 mateuszvis 386
  SHITQUIT:
338 mateuszvis 387
  flen = -1;
388
 
389
  ALLGOOD:
390
  if (fd != NULL) fclose(fd);
333 mateuszvis 391
  net_close(sock);
338 mateuszvis 392
  return(flen);
211 mateuszvis 393
}
394
 
395
 
396
/* checks if file exists, returns 0 if not, non-zero otherwise */
397
static int fexists(const char *fname) {
398
  FILE *fd = fopen(fname, "rb");
399
  if (fd == NULL) return(0);
400
  fclose(fd);
401
  return(1);
402
}
403
 
404
 
405
int main(int argc, char **argv) {
406
  unsigned short bsum = 0;
407
  long flen;
342 mateuszvis 408
  int ispost; /* is the request a POST? */
211 mateuszvis 409
 
342 mateuszvis 410
  struct {
411
    unsigned char buffer[5000];
412
    char ipaddr[64];
413
    char url[64];
414
    char outfname[16];
415
  } *mem;
416
 
712 mateusz.vi 417
  svarlang_autoload("PKGNET");
418
 
342 mateuszvis 419
  /* allocate memory */
420
  mem = malloc(sizeof(*mem));
421
  if (mem == NULL) {
712 mateusz.vi 422
    putsnls(9, 9); /* "ERROR: out of memory" */
342 mateuszvis 423
    return(1);
424
  }
425
 
211 mateuszvis 426
  /* parse command line arguments */
342 mateuszvis 427
  if (parseargv(argc, argv, mem->outfname, mem->url, &ispost) != 0) return(1);
211 mateuszvis 428
 
429
  /* if outfname requested, make sure that file does not exist yet */
342 mateuszvis 430
  if ((mem->outfname[0] != 0) && (fexists(mem->outfname))) {
712 mateusz.vi 431
    printf(svarlang_strid(0x090A), mem->outfname); /* "ERROR: file %s already exists" */
211 mateuszvis 432
    puts("");
433
    return(1);
434
  }
435
 
436
  /* init network stack */
437
  if (net_init() != 0) {
712 mateusz.vi 438
    putsnls(9, 11); /* "ERROR: Network subsystem initialization failed" */
211 mateuszvis 439
    return(1);
440
  }
441
 
442
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
443
 
342 mateuszvis 444
  if (net_dnsresolve(mem->ipaddr, HOSTADDR) != 0) {
712 mateusz.vi 445
    putsnls(9, 12); /* "ERROR: DNS resolution failed" */
211 mateuszvis 446
    return(1);
447
  }
448
 
342 mateuszvis 449
  flen = htget(mem->ipaddr, mem->url, mem->outfname, &bsum, ispost, mem->buffer, sizeof(mem->buffer));
211 mateuszvis 450
  if (flen < 1) return(1);
451
 
342 mateuszvis 452
  if (mem->outfname[0] != 0) {
209 mateuszvis 453
    /* print bsum, size, filename */
712 mateusz.vi 454
    printf(svarlang_strid(0x0200), flen >> 10, mem->outfname, bsum); /* Downloaded %ld KiB into %s (BSUM: %04X) */
209 mateuszvis 455
    puts("");
456
  }
457
 
207 mateuszvis 458
  return(0);
459
}