Subversion Repositories SvarDOS

Rev

Rev 338 | Rev 344 | 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
 * 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"
342 mateuszvis 36
#include "unchunk.h"
207 mateuszvis 37
 
342 mateuszvis 38
#define PVER "20210903"
209 mateuszvis 39
#define PDATE "2021"
40
 
207 mateuszvis 41
#define HOSTADDR "svardos.osdn.io"
42
 
209 mateuszvis 43
 
338 mateuszvis 44
/* returns length of all http headers, or 0 if uncomplete yet */
45
static unsigned short detecthttpheadersend(const unsigned char *buff) {
46
  char lastbyteislf = 0;
47
  unsigned short i;
48
  for (i = 0; buff[i] != 0; i++) {
207 mateuszvis 49
    if (buff[i] == '\r') continue; /* ignore CR characters */
50
    if (buff[i] != '\n') {
51
      lastbyteislf = 0;
52
      continue;
53
    }
54
    /* cur byte is LF -> if last one was also LF then this is an empty line, meaning headers are over */
55
    if (lastbyteislf == 0) {
56
      lastbyteislf = 1;
57
      continue;
58
    }
338 mateuszvis 59
    /* end of headers! return length of headers */
60
    return(i + 1); /* add 1 to skip the current \n character */
207 mateuszvis 61
  }
62
  return(0);
63
}
64
 
65
 
209 mateuszvis 66
static void help(void) {
67
  puts("pkgnet ver " PVER " -- Copyright (C) " PDATE " Mateusz Viste");
68
  puts("");
69
  puts("pkgnet is the SvarDOS package downloader.");
70
  puts("");
71
  puts("usage:  pkgnet search <term>");
72
  puts("        pkgnet pull <package>");
211 mateuszvis 73
  puts("        pkgnet checkup");
209 mateuszvis 74
  puts("");
75
  puts("actions:");
76
  puts(" search   - asks remote repository for the list of matching packages");
77
  puts(" pull     - downloads package into current directory");
211 mateuszvis 78
  puts(" checkup  - lists updates available for your system");
209 mateuszvis 79
  puts("");
342 mateuszvis 80
  printf("Watt32 kernel: %s", net_engine());
81
  puts("");
82
  puts("");
209 mateuszvis 83
}
84
 
85
 
86
/* parses command line arguments and fills outfname and url accordingly
87
 * returns 0 on success, non-zero otherwise */
342 mateuszvis 88
static int parseargv(int argc, char * const *argv, char *outfname, char *url, int *ispost) {
211 mateuszvis 89
  *outfname = 0;
209 mateuszvis 90
  *url = 0;
342 mateuszvis 91
  *ispost = 0;
209 mateuszvis 92
  if ((argc == 3) && (strcasecmp(argv[1], "search") == 0)) {
214 mateuszvis 93
    sprintf(url, "/repo/?a=search&p=%s", argv[2]);
209 mateuszvis 94
  } else if ((argc == 3) && (strcasecmp(argv[1], "pull") == 0)) {
211 mateuszvis 95
    if ((strlen(argv[2]) > 8) || (argv[2][0] == 0)) {
96
      puts("ERROR: package name must be 8 characters maximum");
97
      return(-1);
98
    }
214 mateuszvis 99
    sprintf(url, "/repo/?a=pull&p=%s", argv[2]);
211 mateuszvis 100
    sprintf(outfname, "%s.zip", argv[2]);
209 mateuszvis 101
  } else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
342 mateuszvis 102
    sprintf(url, "/repo/?a=checkup");
103
    *ispost = 1;
209 mateuszvis 104
  } else {
105
    help();
106
    return(-1);
107
  }
108
  return(0);
109
}
110
 
111
 
342 mateuszvis 112
static int htget_headers(unsigned char *buffer, size_t buffersz, struct net_tcpsocket *sock, int *httpcode, int *ischunked)  {
338 mateuszvis 113
  unsigned char *buffptr = buffer;
114
  unsigned short bufflen = 0;
115
  int byteread;
116
  time_t starttime = time(NULL);
117
  for (;;) {
118
    byteread = net_recv(sock, buffptr, buffersz - (bufflen + 1)); /* -1 because I will append a NULL terminator */
119
 
120
    if (byteread > 0) { /* got data */
121
      int hdlen;
122
      bufflen += byteread;
123
      buffptr += byteread;
124
      buffer[bufflen] = 0;
125
      hdlen = detecthttpheadersend(buffer);
126
      if (hdlen > 0) { /* full headers - parse http code and continue processing */
342 mateuszvis 127
        int i;
128
        buffer[hdlen - 1] = 0;
338 mateuszvis 129
        /* find the first space (HTTP/1.1 200 OK) */
342 mateuszvis 130
        for (i = 0; i < 16; i++) {
131
          if ((buffer[i] == ' ') || (buffer[i] == 0)) break;
338 mateuszvis 132
        }
342 mateuszvis 133
        if (buffer[i] != ' ') return(-1);
134
        *httpcode = atoi((char *)(buffer + i + 1));
135
        /* switch all headers to low-case so it is easier to parse them */
136
        for (i = 0; i < hdlen; i++) if ((buffer[i] >= 'A') && (buffer[i] <= 'Z')) buffer[i] += ('a' - 'A');
137
        /* look out for chunked transfer encoding */
138
        {
139
        char *lineptr = strstr((char *)buffer, "\ntransfer-encoding:");
140
        if (lineptr != NULL) {
141
          lineptr += 19;
142
          /* replace nearest \r, \n or 0 by 0 */
143
          for (i = 0; ; i++) {
144
            if ((lineptr[i] == '\r') || (lineptr[i] == '\n') || (lineptr[i] == 0)) {
145
              lineptr[i] = 0;
146
              break;
147
            }
148
          }
149
          /* do I see the 'chunked' word? */
150
          if (strstr((char *)lineptr, "chunked") != NULL) *ischunked = 1;
151
        }
152
        }
338 mateuszvis 153
        /* rewind the buffer */
154
        bufflen -= hdlen;
155
        memmove(buffer, buffer + hdlen, bufflen);
156
        return(bufflen); /* move to body processing now */
157
      }
158
 
159
    } else if (byteread < 0) { /* abort on error */
160
      return(-2); /* unexpected end of connection (while waiting for all http headers) */
161
 
162
    } else { /* else no data received - look for timeout and release a cpu cycle */
163
      if (time(NULL) - starttime > 20) return(-3); /* TIMEOUT! */
164
      _asm int 28h; /* release a CPU cycle */
165
    }
166
  }
167
}
168
 
169
 
342 mateuszvis 170
/* provides body data of the POST query for checkup actions
171
   fills buff with data and returns data length.
172
   must be called repeateadly until zero-lengh is returned */
173
static unsigned short checkupdata(char *buff) {
174
  buff[0] = 0;
175
  return(0);
176
}
177
 
178
 
211 mateuszvis 179
/* fetch http data from ipaddr using url
180
 * write result to file outfname if not null, or print to stdout otherwise
181
 * fills bsum with the BSD sum of the data
342 mateuszvis 182
 * is ispost is non-zero, then the request is a POST and its body data is
183
 * obtained through repeated calls to checkupdata()
211 mateuszvis 184
 * returns the length of data obtained, or neg value on error */
342 mateuszvis 185
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 186
  struct net_tcpsocket *sock;
327 mateuszvis 187
  time_t lastactivity, lastprogressoutput = 0;
342 mateuszvis 188
  int httpcode = -1, ischunked = 0;
338 mateuszvis 189
  int byteread;
332 mateuszvis 190
  long flen = 0, lastflen = 0;
209 mateuszvis 191
  FILE *fd = NULL;
207 mateuszvis 192
 
342 mateuszvis 193
  /* unchunk state variable is using a little part of the supplied buffer */
194
  struct unchunk_state *unchstate = (void *)buffer;
195
  buffer += sizeof(*unchstate);
196
  buffersz -= sizeof(*unchstate);
197
  memset(unchstate, 0, sizeof(*unchstate));
198
 
211 mateuszvis 199
  sock = net_connect(ipaddr, 80);
207 mateuszvis 200
  if (sock == NULL) {
201
    puts("ERROR: failed to connect to " HOSTADDR);
202
    goto SHITQUIT;
203
  }
204
 
205
  /* wait for net_connect() to actually connect */
206
  for (;;) {
329 mateuszvis 207
    int connstate = net_isconnected(sock);
207 mateuszvis 208
    if (connstate > 0) break;
209
    if (connstate < 0) {
210
      puts("ERROR: connection error");
211
      goto SHITQUIT;
212
    }
211 mateuszvis 213
    _asm int 28h;  /* DOS idle */
207 mateuszvis 214
  }
215
 
330 mateuszvis 216
  /* socket is connected - send the http request (MUST be HTTP/1.0 because I do not support chunked transfers!) */
342 mateuszvis 217
  if (ispost) {
218
    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);
219
  } else {
220
    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);
221
  }
207 mateuszvis 222
 
329 mateuszvis 223
  if (net_send(sock, buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
207 mateuszvis 224
    puts("ERROR: failed to send HTTP query to remote server");
225
    goto SHITQUIT;
226
  }
227
 
342 mateuszvis 228
  /* send chunked data for POST queries */
229
  if (ispost) {
230
    unsigned short blen;
231
    int hlen;
232
    char *hbuf = (char *)buffer + buffersz - 16;
233
    for (;;) {
234
      blen = checkupdata((char *)buffer);
235
      if (blen == 0) { /* last item contains the message trailer */
236
        hlen = sprintf(hbuf, "0\r\n\r\n");
237
      } else {
238
        hlen = sprintf(hbuf, "%X\r\n", blen);
239
      }
240
      if (net_send(sock, hbuf, hlen) != hlen) {
241
        puts("ERROR: failed to send POST data to remote server");
242
        goto SHITQUIT;
243
      }
244
      if (blen == 0) break;
245
      if (net_send(sock, buffer, blen) != blen) {
246
        puts("ERROR: failed to send POST data to remote server");
247
        goto SHITQUIT;
248
      }
249
    }
250
  }
251
 
338 mateuszvis 252
  /* receive and process HTTP headers */
342 mateuszvis 253
  byteread = htget_headers(buffer, buffersz, sock, &httpcode, &ischunked);
207 mateuszvis 254
 
338 mateuszvis 255
  /* transmission error? */
256
  if (byteread < 0) {
257
    printf("ERROR: communication error (%d)", byteread);
258
    puts("");
259
    goto SHITQUIT;
260
  }
207 mateuszvis 261
 
338 mateuszvis 262
  /* open destination file if required and if no server-side error occured */
342 mateuszvis 263
  if ((httpcode >= 200) && (httpcode <= 299) && (*outfname != 0)) {
338 mateuszvis 264
    fd = fopen(outfname, "wb");
265
    if (fd == NULL) {
266
      printf("ERROR: failed to create file %s", outfname);
267
      puts("");
268
      goto SHITQUIT;
207 mateuszvis 269
    }
338 mateuszvis 270
  }
207 mateuszvis 271
 
342 mateuszvis 272
  /* read body of the answer (and chunk-decode it if needed) */
338 mateuszvis 273
  lastactivity = time(NULL);
342 mateuszvis 274
  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 275
 
276
    if (byteread > 0) { /* got data */
207 mateuszvis 277
      lastactivity = time(NULL);
342 mateuszvis 278
 
279
      /* unpack data if server transmits in chunked mode */
280
      if (ischunked) byteread = unchunk(buffer, byteread, unchstate);
281
 
207 mateuszvis 282
      /* if downloading to file, write stuff to disk */
209 mateuszvis 283
      if (fd != NULL) {
284
        int i;
285
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
286
          printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen);
287
          puts("");
288
          break;
289
        }
290
        flen += byteread;
327 mateuszvis 291
        /* update progress once a sec */
292
        if (lastprogressoutput != lastactivity) {
293
          lastprogressoutput = lastactivity;
333 mateuszvis 294
          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 295
          lastflen = flen;
296
          fflush(stdout); /* avoid console buffering */
327 mateuszvis 297
        }
209 mateuszvis 298
        /* update the bsd sum */
299
        for (i = 0; i < byteread; i++) {
300
          /* rotr16 */
211 mateuszvis 301
          unsigned short bsumlsb = *bsum & 1;
302
          *bsum >>= 1;
303
          *bsum |= (bsumlsb << 15);
304
          *bsum += buffer[i];
209 mateuszvis 305
        }
306
      } else { /* otherwise dump to screen */
342 mateuszvis 307
        buffer[byteread] = 0;
209 mateuszvis 308
        printf("%s", buffer);
309
      }
338 mateuszvis 310
 
311
    } else if (byteread < 0) { /* end of connection */
312
      break;
313
 
314
    } else { /* check for timeout (byteread == 0) */
315
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
316
        puts("ERROR: Timeout while waiting for data");
317
        goto SHITQUIT;
318
      }
319
      /* waiting for packets - release a CPU cycle in the meantime */
320
      _asm int 28h;
207 mateuszvis 321
    }
322
  }
323
 
338 mateuszvis 324
  goto ALLGOOD;
325
 
211 mateuszvis 326
  SHITQUIT:
338 mateuszvis 327
  flen = -1;
328
 
329
  ALLGOOD:
330
  if (fd != NULL) fclose(fd);
333 mateuszvis 331
  net_close(sock);
338 mateuszvis 332
  return(flen);
211 mateuszvis 333
}
334
 
335
 
336
/* checks if file exists, returns 0 if not, non-zero otherwise */
337
static int fexists(const char *fname) {
338
  FILE *fd = fopen(fname, "rb");
339
  if (fd == NULL) return(0);
340
  fclose(fd);
341
  return(1);
342
}
343
 
344
 
345
int main(int argc, char **argv) {
346
  unsigned short bsum = 0;
347
  long flen;
342 mateuszvis 348
  int ispost; /* is the request a POST? */
211 mateuszvis 349
 
342 mateuszvis 350
  struct {
351
    unsigned char buffer[5000];
352
    char ipaddr[64];
353
    char url[64];
354
    char outfname[16];
355
  } *mem;
356
 
357
  /* allocate memory */
358
  mem = malloc(sizeof(*mem));
359
  if (mem == NULL) {
360
    puts("ERROR: out of memory");
361
    return(1);
362
  }
363
 
211 mateuszvis 364
  /* parse command line arguments */
342 mateuszvis 365
  if (parseargv(argc, argv, mem->outfname, mem->url, &ispost) != 0) return(1);
211 mateuszvis 366
 
367
  /* if outfname requested, make sure that file does not exist yet */
342 mateuszvis 368
  if ((mem->outfname[0] != 0) && (fexists(mem->outfname))) {
369
    printf("ERROR: file %s already exists", mem->outfname);
211 mateuszvis 370
    puts("");
371
    return(1);
372
  }
373
 
374
  /* init network stack */
375
  if (net_init() != 0) {
376
    puts("ERROR: Network subsystem initialization failed");
377
    return(1);
378
  }
379
 
380
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
381
 
342 mateuszvis 382
  if (net_dnsresolve(mem->ipaddr, HOSTADDR) != 0) {
211 mateuszvis 383
    puts("ERROR: DNS resolution failed");
384
    return(1);
385
  }
386
 
342 mateuszvis 387
  flen = htget(mem->ipaddr, mem->url, mem->outfname, &bsum, ispost, mem->buffer, sizeof(mem->buffer));
211 mateuszvis 388
  if (flen < 1) return(1);
389
 
342 mateuszvis 390
  if (mem->outfname[0] != 0) {
209 mateuszvis 391
    /* print bsum, size, filename */
342 mateuszvis 392
    printf("Downloaded %ld KiB into %s (BSUM: %04X)", flen >> 10, mem->outfname, bsum);
209 mateuszvis 393
    puts("");
394
  }
395
 
207 mateuszvis 396
  return(0);
397
}