Subversion Repositories SvarDOS

Rev

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