Subversion Repositories SvarDOS

Rev

Details | 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];
207 mateuszvis 117
  time_t lastactivity;
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;
208
        /* update the bsd sum */
209
        for (i = 0; i < byteread; i++) {
210
          /* rotr16 */
211 mateuszvis 211
          unsigned short bsumlsb = *bsum & 1;
212
          *bsum >>= 1;
213
          *bsum |= (bsumlsb << 15);
209 mateuszvis 214
          /* bsum += ch */
211 mateuszvis 215
          *bsum += buffer[i];
209 mateuszvis 216
        }
217
      } else { /* otherwise dump to screen */
218
        printf("%s", buffer);
219
      }
207 mateuszvis 220
    }
221
  }
211 mateuszvis 222
  net_close(sock);
223
  return(flen);
207 mateuszvis 224
 
211 mateuszvis 225
  SHITQUIT:
226
  net_abort(sock);
227
  return(-1);
228
}
229
 
230
 
231
/* checks if file exists, returns 0 if not, non-zero otherwise */
232
static int fexists(const char *fname) {
233
  FILE *fd = fopen(fname, "rb");
234
  if (fd == NULL) return(0);
235
  fclose(fd);
236
  return(1);
237
}
238
 
239
 
240
int main(int argc, char **argv) {
241
  char ipaddr[64];
242
  char url[64];
243
  unsigned short bsum = 0;
244
  char outfname[16];
245
  long flen;
246
 
247
  /* parse command line arguments */
248
  if (parseargv(argc, argv, outfname, url) != 0) return(1);
249
 
250
  /* if outfname requested, make sure that file does not exist yet */
251
  if (fexists(outfname)) {
252
    printf("ERROR: file %s already exists", outfname);
253
    puts("");
254
    return(1);
255
  }
256
 
257
  /* init network stack */
258
  if (net_init() != 0) {
259
    puts("ERROR: Network subsystem initialization failed");
260
    return(1);
261
  }
262
 
263
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
264
 
265
  if (net_dnsresolve(ipaddr, HOSTADDR) != 0) {
266
    puts("ERROR: DNS resolution failed");
267
    return(1);
268
  }
269
 
270
  flen = htget(ipaddr, url, outfname, &bsum);
271
  if (flen < 1) return(1);
272
 
273
  if (*outfname != 0) {
209 mateuszvis 274
    /* print bsum, size, filename */
275
    printf("Downloaded %ld KiB into %s (BSUM: %04X)", flen >> 10, outfname, bsum);
276
    puts("");
277
  }
278
 
207 mateuszvis 279
  return(0);
280
}