Subversion Repositories SvarDOS

Rev

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