Subversion Repositories SvarDOS

Rev

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