Subversion Repositories SvarDOS

Rev

Rev 336 | Rev 342 | 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"
36
 
338 mateuszvis 37
#define PVER "20210520"
209 mateuszvis 38
#define PDATE "2021"
39
 
207 mateuszvis 40
#define HOSTADDR "svardos.osdn.io"
41
 
209 mateuszvis 42
 
338 mateuszvis 43
/* returns length of all http headers, or 0 if uncomplete yet */
44
static unsigned short detecthttpheadersend(const unsigned char *buff) {
45
  char lastbyteislf = 0;
46
  unsigned short i;
47
  for (i = 0; buff[i] != 0; i++) {
207 mateuszvis 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
    }
338 mateuszvis 58
    /* end of headers! return length of headers */
59
    return(i + 1); /* add 1 to skip the current \n character */
207 mateuszvis 60
  }
61
  return(0);
62
}
63
 
64
 
209 mateuszvis 65
static void help(void) {
66
  puts("pkgnet ver " PVER " -- Copyright (C) " PDATE " Mateusz Viste");
67
  puts("");
68
  puts("pkgnet is the SvarDOS package downloader.");
69
  puts("");
70
  puts("usage:  pkgnet search <term>");
71
  puts("        pkgnet pull <package>");
211 mateuszvis 72
  puts("        pkgnet checkup");
209 mateuszvis 73
  puts("");
74
  puts("actions:");
75
  puts(" search   - asks remote repository for the list of matching packages");
76
  puts(" pull     - downloads package into current directory");
211 mateuszvis 77
  puts(" checkup  - lists updates available for your system");
209 mateuszvis 78
  puts("");
79
}
80
 
81
 
82
/* parses command line arguments and fills outfname and url accordingly
83
 * returns 0 on success, non-zero otherwise */
211 mateuszvis 84
static int parseargv(int argc, char * const *argv, char *outfname, char *url) {
85
  *outfname = 0;
209 mateuszvis 86
  *url = 0;
87
  if ((argc == 3) && (strcasecmp(argv[1], "search") == 0)) {
214 mateuszvis 88
    sprintf(url, "/repo/?a=search&p=%s", argv[2]);
209 mateuszvis 89
  } else if ((argc == 3) && (strcasecmp(argv[1], "pull") == 0)) {
211 mateuszvis 90
    if ((strlen(argv[2]) > 8) || (argv[2][0] == 0)) {
91
      puts("ERROR: package name must be 8 characters maximum");
92
      return(-1);
93
    }
214 mateuszvis 94
    sprintf(url, "/repo/?a=pull&p=%s", argv[2]);
211 mateuszvis 95
    sprintf(outfname, "%s.zip", argv[2]);
209 mateuszvis 96
  } else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
97
    puts("NOT SUPPORTED YET");
98
    return(-1);
99
  } else {
100
    help();
101
    return(-1);
102
  }
103
  return(0);
104
}
105
 
106
 
338 mateuszvis 107
static int htget_headers(unsigned char *buffer, size_t buffersz, struct net_tcpsocket *sock, int *httpcode)  {
108
  unsigned char *buffptr = buffer;
109
  unsigned short bufflen = 0;
110
  int byteread;
111
  time_t starttime = time(NULL);
112
  for (;;) {
113
    byteread = net_recv(sock, buffptr, buffersz - (bufflen + 1)); /* -1 because I will append a NULL terminator */
114
 
115
    if (byteread > 0) { /* got data */
116
      int hdlen;
117
      bufflen += byteread;
118
      buffptr += byteread;
119
      buffer[bufflen] = 0;
120
      hdlen = detecthttpheadersend(buffer);
121
      if (hdlen > 0) { /* full headers - parse http code and continue processing */
122
        int spc;
123
        /* find the first space (HTTP/1.1 200 OK) */
124
        for (spc = 0; spc < 16; spc++) {
125
          if (buffer[spc] == ' ') break;
126
          if (buffer[spc] == 0) break;
127
        }
128
        if (buffer[spc] != ' ') return(-1);
129
        *httpcode = atoi((char *)(buffer + spc + 1));
130
        /* rewind the buffer */
131
        bufflen -= hdlen;
132
        memmove(buffer, buffer + hdlen, bufflen);
133
        return(bufflen); /* move to body processing now */
134
      }
135
 
136
    } else if (byteread < 0) { /* abort on error */
137
      return(-2); /* unexpected end of connection (while waiting for all http headers) */
138
 
139
    } else { /* else no data received - look for timeout and release a cpu cycle */
140
      if (time(NULL) - starttime > 20) return(-3); /* TIMEOUT! */
141
      _asm int 28h; /* release a CPU cycle */
142
    }
143
  }
144
}
145
 
146
 
211 mateuszvis 147
/* fetch http data from ipaddr using url
148
 * write result to file outfname if not null, or print to stdout otherwise
149
 * fills bsum with the BSD sum of the data
150
 * returns the length of data obtained, or neg value on error */
151
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum) {
152
  struct net_tcpsocket *sock;
209 mateuszvis 153
  unsigned char buffer[4096];
327 mateuszvis 154
  time_t lastactivity, lastprogressoutput = 0;
209 mateuszvis 155
  int httpcode = -1;
338 mateuszvis 156
  int byteread;
332 mateuszvis 157
  long flen = 0, lastflen = 0;
209 mateuszvis 158
  FILE *fd = NULL;
207 mateuszvis 159
 
211 mateuszvis 160
  sock = net_connect(ipaddr, 80);
207 mateuszvis 161
  if (sock == NULL) {
162
    puts("ERROR: failed to connect to " HOSTADDR);
163
    goto SHITQUIT;
164
  }
165
 
166
  /* wait for net_connect() to actually connect */
167
  for (;;) {
329 mateuszvis 168
    int connstate = net_isconnected(sock);
207 mateuszvis 169
    if (connstate > 0) break;
170
    if (connstate < 0) {
171
      puts("ERROR: connection error");
172
      goto SHITQUIT;
173
    }
211 mateuszvis 174
    _asm int 28h;  /* DOS idle */
207 mateuszvis 175
  }
176
 
330 mateuszvis 177
  /* socket is connected - send the http request (MUST be HTTP/1.0 because I do not support chunked transfers!) */
178
  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);
207 mateuszvis 179
 
329 mateuszvis 180
  if (net_send(sock, buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
207 mateuszvis 181
    puts("ERROR: failed to send HTTP query to remote server");
182
    goto SHITQUIT;
183
  }
184
 
338 mateuszvis 185
  /* receive and process HTTP headers */
186
  byteread = htget_headers(buffer, sizeof(buffer), sock, &httpcode);
207 mateuszvis 187
 
338 mateuszvis 188
  /* transmission error? */
189
  if (byteread < 0) {
190
    printf("ERROR: communication error (%d)", byteread);
191
    puts("");
192
    goto SHITQUIT;
193
  }
207 mateuszvis 194
 
338 mateuszvis 195
  /* open destination file if required and if no server-side error occured */
196
  if ((httpcode == 200) && (*outfname != 0)) {
197
    fd = fopen(outfname, "wb");
198
    if (fd == NULL) {
199
      printf("ERROR: failed to create file %s", outfname);
200
      puts("");
201
      goto SHITQUIT;
207 mateuszvis 202
    }
338 mateuszvis 203
  }
207 mateuszvis 204
 
338 mateuszvis 205
  /* read body of the answer */
206
  lastactivity = time(NULL);
207
  for (;; byteread = net_recv(sock, buffer, sizeof(buffer) - 1)) { /* read 1 byte less because I need to append a NULL terminator */
208
 
209
    if (byteread > 0) { /* got data */
207 mateuszvis 210
      buffer[byteread] = 0;
211
      lastactivity = time(NULL);
212
      /* if downloading to file, write stuff to disk */
209 mateuszvis 213
      if (fd != NULL) {
214
        int i;
215
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
216
          printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen);
217
          puts("");
218
          break;
219
        }
220
        flen += byteread;
327 mateuszvis 221
        /* update progress once a sec */
222
        if (lastprogressoutput != lastactivity) {
223
          lastprogressoutput = lastactivity;
333 mateuszvis 224
          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 225
          lastflen = flen;
226
          fflush(stdout); /* avoid console buffering */
327 mateuszvis 227
        }
209 mateuszvis 228
        /* update the bsd sum */
229
        for (i = 0; i < byteread; i++) {
230
          /* rotr16 */
211 mateuszvis 231
          unsigned short bsumlsb = *bsum & 1;
232
          *bsum >>= 1;
233
          *bsum |= (bsumlsb << 15);
234
          *bsum += buffer[i];
209 mateuszvis 235
        }
236
      } else { /* otherwise dump to screen */
237
        printf("%s", buffer);
238
      }
338 mateuszvis 239
 
240
    } else if (byteread < 0) { /* end of connection */
241
      break;
242
 
243
    } else { /* check for timeout (byteread == 0) */
244
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
245
        puts("ERROR: Timeout while waiting for data");
246
        goto SHITQUIT;
247
      }
248
      /* waiting for packets - release a CPU cycle in the meantime */
249
      _asm int 28h;
207 mateuszvis 250
    }
251
  }
252
 
338 mateuszvis 253
  goto ALLGOOD;
254
 
211 mateuszvis 255
  SHITQUIT:
338 mateuszvis 256
  flen = -1;
257
 
258
  ALLGOOD:
259
  if (fd != NULL) fclose(fd);
333 mateuszvis 260
  net_close(sock);
338 mateuszvis 261
  return(flen);
211 mateuszvis 262
}
263
 
264
 
265
/* checks if file exists, returns 0 if not, non-zero otherwise */
266
static int fexists(const char *fname) {
267
  FILE *fd = fopen(fname, "rb");
268
  if (fd == NULL) return(0);
269
  fclose(fd);
270
  return(1);
271
}
272
 
273
 
274
int main(int argc, char **argv) {
275
  char ipaddr[64];
276
  char url[64];
277
  unsigned short bsum = 0;
278
  char outfname[16];
279
  long flen;
280
 
281
  /* parse command line arguments */
282
  if (parseargv(argc, argv, outfname, url) != 0) return(1);
283
 
284
  /* if outfname requested, make sure that file does not exist yet */
285
  if (fexists(outfname)) {
286
    printf("ERROR: file %s already exists", outfname);
287
    puts("");
288
    return(1);
289
  }
290
 
291
  /* init network stack */
292
  if (net_init() != 0) {
293
    puts("ERROR: Network subsystem initialization failed");
294
    return(1);
295
  }
296
 
297
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
298
 
299
  if (net_dnsresolve(ipaddr, HOSTADDR) != 0) {
300
    puts("ERROR: DNS resolution failed");
301
    return(1);
302
  }
303
 
304
  flen = htget(ipaddr, url, outfname, &bsum);
305
  if (flen < 1) return(1);
306
 
307
  if (*outfname != 0) {
209 mateuszvis 308
    /* print bsum, size, filename */
309
    printf("Downloaded %ld KiB into %s (BSUM: %04X)", flen >> 10, outfname, bsum);
310
    puts("");
311
  }
312
 
207 mateuszvis 313
  return(0);
314
}