Subversion Repositories SvarDOS

Rev

Rev 215 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 215 Rev 218
1
/*
1
/*
2
 * pkgnet - pulls SvarDOS packages from the project's online repository
2
 * pkgnet - pulls SvarDOS packages from the project's online repository
3
 * Copyright (C) 2021 Mateusz Viste
3
 * Copyright (C) 2021 Mateusz Viste
4
 *
4
 *
5
 * PUBLISHED UNDER THE TERMS OF THE MIT LICENSE
5
 * PUBLISHED UNDER THE TERMS OF THE MIT LICENSE
6
 *
6
 *
7
 * COPYRIGHT (C) 2016-2021 MATEUSZ VISTE, ALL RIGHTS RESERVED.
7
 * COPYRIGHT (C) 2016-2021 MATEUSZ VISTE, ALL RIGHTS RESERVED.
8
 *
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a
9
 * Permission is hereby granted, free of charge, to any person obtaining a
10
 * copy of this software and associated documentation files (the "Software"),
10
 * copy of this software and associated documentation files (the "Software"),
11
 * to deal in the Software without restriction, including without limitation
11
 * to deal in the Software without restriction, including without limitation
12
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
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
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:
14
 * Software is furnished to do so, subject to the following conditions:
15
 *
15
 *
16
 * The above copyright notice and this permission notice shall be included in
16
 * The above copyright notice and this permission notice shall be included in
17
 * all copies or substantial portions of the Software.
17
 * all copies or substantial portions of the Software.
18
 *
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
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,
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
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
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
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
24
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25
 * DEALINGS IN THE SOFTWARE.
25
 * DEALINGS IN THE SOFTWARE.
26
 *
26
 *
27
 * http://svardos.osdn.io
27
 * http://svardos.osdn.io
28
 */
28
 */
29
 
29
 
30
#include <stdio.h>
30
#include <stdio.h>
31
#include <stdlib.h>
31
#include <stdlib.h>
32
#include <string.h>
32
#include <string.h>
33
#include <time.h>
33
#include <time.h>
34
 
34
 
35
#include "net.h"
35
#include "net.h"
36
 
36
 
37
#define PVER "20210121"
37
#define PVER "20210121"
38
#define PDATE "2021"
38
#define PDATE "2021"
39
 
39
 
40
#define HOSTADDR "svardos.osdn.io"
40
#define HOSTADDR "svardos.osdn.io"
41
 
41
 
42
 
42
 
43
/* strips http headers and returns new buff len */
43
/* strips http headers and returns new buff len */
44
static int detecthttpheadersend(unsigned char *buff, int len) {
44
static int detecthttpheadersend(unsigned char *buff, int len) {
45
  static char lastbyteislf = 0; /* static because I must potentially remember it for next packet/call */
45
  static char lastbyteislf = 0; /* static because I must potentially remember it for next packet/call */
46
  int i;
46
  int i;
47
  for (i = 0; i < len; i++) {
47
  for (i = 0; i < len; i++) {
48
    if (buff[i] == '\r') continue; /* ignore CR characters */
48
    if (buff[i] == '\r') continue; /* ignore CR characters */
49
    if (buff[i] != '\n') {
49
    if (buff[i] != '\n') {
50
      lastbyteislf = 0;
50
      lastbyteislf = 0;
51
      continue;
51
      continue;
52
    }
52
    }
53
    /* cur byte is LF -> if last one was also LF then this is an empty line, meaning headers are over */
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) {
54
    if (lastbyteislf == 0) {
55
      lastbyteislf = 1;
55
      lastbyteislf = 1;
56
      continue;
56
      continue;
57
    }
57
    }
58
    /* end of headers! rewind the buffer and return new len */
58
    /* end of headers! rewind the buffer and return new len */
59
    i += 1; /* add 1 to skip the current \n character */
59
    i += 1; /* add 1 to skip the current \n character */
60
    len -= i;
60
    len -= i;
61
    if (len > 0) memmove(buff, buff + i, len + 1); /* +1 so I catch the string terminator as well */
61
    if (len > 0) memmove(buff, buff + i, len + 1); /* +1 so I catch the string terminator as well */
62
    return(len);
62
    return(len);
63
  }
63
  }
64
  return(0);
64
  return(0);
65
}
65
}
66
 
66
 
67
 
67
 
68
static void help(void) {
68
static void help(void) {
69
  puts("pkgnet ver " PVER " -- Copyright (C) " PDATE " Mateusz Viste");
69
  puts("pkgnet ver " PVER " -- Copyright (C) " PDATE " Mateusz Viste");
70
  puts("");
70
  puts("");
71
  puts("pkgnet is the SvarDOS package downloader.");
71
  puts("pkgnet is the SvarDOS package downloader.");
72
  puts("");
72
  puts("");
73
  puts("usage:  pkgnet search <term>");
73
  puts("usage:  pkgnet search <term>");
74
  puts("        pkgnet pull <package>");
74
  puts("        pkgnet pull <package>");
75
  puts("        pkgnet checkup");
75
  puts("        pkgnet checkup");
76
  puts("");
76
  puts("");
77
  puts("actions:");
77
  puts("actions:");
78
  puts(" search   - asks remote repository for the list of matching packages");
78
  puts(" search   - asks remote repository for the list of matching packages");
79
  puts(" pull     - downloads package into current directory");
79
  puts(" pull     - downloads package into current directory");
80
  puts(" checkup  - lists updates available for your system");
80
  puts(" checkup  - lists updates available for your system");
81
  puts("");
81
  puts("");
82
}
82
}
83
 
83
 
84
 
84
 
85
/* parses command line arguments and fills outfname and url accordingly
85
/* parses command line arguments and fills outfname and url accordingly
86
 * returns 0 on success, non-zero otherwise */
86
 * returns 0 on success, non-zero otherwise */
87
static int parseargv(int argc, char * const *argv, char *outfname, char *url) {
87
static int parseargv(int argc, char * const *argv, char *outfname, char *url) {
88
  *outfname = 0;
88
  *outfname = 0;
89
  *url = 0;
89
  *url = 0;
90
  if ((argc == 3) && (strcasecmp(argv[1], "search") == 0)) {
90
  if ((argc == 3) && (strcasecmp(argv[1], "search") == 0)) {
91
    sprintf(url, "/repo/?a=search&p=%s", argv[2]);
91
    sprintf(url, "/repo/?a=search&p=%s", argv[2]);
92
  } else if ((argc == 3) && (strcasecmp(argv[1], "pull") == 0)) {
92
  } else if ((argc == 3) && (strcasecmp(argv[1], "pull") == 0)) {
93
    if ((strlen(argv[2]) > 8) || (argv[2][0] == 0)) {
93
    if ((strlen(argv[2]) > 8) || (argv[2][0] == 0)) {
94
      puts("ERROR: package name must be 8 characters maximum");
94
      puts("ERROR: package name must be 8 characters maximum");
95
      return(-1);
95
      return(-1);
96
    }
96
    }
97
    sprintf(url, "/repo/?a=pull&p=%s", argv[2]);
97
    sprintf(url, "/repo/?a=pull&p=%s", argv[2]);
98
    sprintf(outfname, "%s.zip", argv[2]);
98
    sprintf(outfname, "%s.zip", argv[2]);
99
  } else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
99
  } else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
100
    puts("NOT SUPPORTED YET");
100
    puts("NOT SUPPORTED YET");
101
    return(-1);
101
    return(-1);
102
  } else {
102
  } else {
103
    help();
103
    help();
104
    return(-1);
104
    return(-1);
105
  }
105
  }
106
  return(0);
106
  return(0);
107
}
107
}
108
 
108
 
109
 
109
 
110
/* fetch http data from ipaddr using url
110
/* fetch http data from ipaddr using url
111
 * write result to file outfname if not null, or print to stdout otherwise
111
 * write result to file outfname if not null, or print to stdout otherwise
112
 * fills bsum with the BSD sum of the data
112
 * fills bsum with the BSD sum of the data
113
 * returns the length of data obtained, or neg value on error */
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) {
114
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum) {
115
  struct net_tcpsocket *sock;
115
  struct net_tcpsocket *sock;
116
  unsigned char buffer[4096];
116
  unsigned char buffer[4096];
117
  time_t lastactivity;
117
  time_t lastactivity;
118
  int headersdone = 0;
118
  int headersdone = 0;
119
  int httpcode = -1;
119
  int httpcode = -1;
120
  long flen = 0;
120
  long flen = 0;
121
  FILE *fd = NULL;
121
  FILE *fd = NULL;
122
 
122
 
123
  sock = net_connect(ipaddr, 80);
123
  sock = net_connect(ipaddr, 80);
124
  if (sock == NULL) {
124
  if (sock == NULL) {
125
    puts("ERROR: failed to connect to " HOSTADDR);
125
    puts("ERROR: failed to connect to " HOSTADDR);
126
    goto SHITQUIT;
126
    goto SHITQUIT;
127
  }
127
  }
128
 
128
 
129
  /* wait for net_connect() to actually connect */
129
  /* wait for net_connect() to actually connect */
130
  for (;;) {
130
  for (;;) {
131
    int connstate = net_isconnected(sock, 1);
131
    int connstate = net_isconnected(sock, 1);
132
    if (connstate > 0) break;
132
    if (connstate > 0) break;
133
    if (connstate < 0) {
133
    if (connstate < 0) {
134
      puts("ERROR: connection error");
134
      puts("ERROR: connection error");
135
      goto SHITQUIT;
135
      goto SHITQUIT;
136
    }
136
    }
137
    _asm int 28h;  /* DOS idle */
137
    _asm int 28h;  /* DOS idle */
138
  }
138
  }
139
 
139
 
140
  /* socket is connected - send the http request */
140
  /* socket is connected - send the http request */
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);
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);
142
 
142
 
143
  if (net_send(sock, (char *)buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
143
  if (net_send(sock, (char *)buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
144
    puts("ERROR: failed to send HTTP query to remote server");
144
    puts("ERROR: failed to send HTTP query to remote server");
145
    goto SHITQUIT;
145
    goto SHITQUIT;
146
  }
146
  }
147
 
147
 
148
  lastactivity = time(NULL);
148
  lastactivity = time(NULL);
149
  for (;;) {
149
  for (;;) {
150
    int byteread = net_recv(sock, (char *)buffer, sizeof(buffer) - 1); /* -1 because I will append a NULL terminator */
150
    int byteread = net_recv(sock, (char *)buffer, sizeof(buffer) - 1); /* -1 because I will append a NULL terminator */
151
 
151
 
152
    if (byteread < 0) break; /* end of connection */
152
    if (byteread < 0) break; /* end of connection */
153
 
153
 
154
    /*  */
154
    /*  */
155
    if (byteread == 0) {
155
    if (byteread == 0) {
156
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
156
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
157
        puts("ERROR: Timeout while waiting for data");
157
        puts("ERROR: Timeout while waiting for data");
158
        goto SHITQUIT;
158
        goto SHITQUIT;
159
      }
159
      }
160
      /* waiting for packets - release a CPU cycle in the meantime */
160
      /* waiting for packets - release a CPU cycle in the meantime */
161
      _asm int 28h;
161
      _asm int 28h;
162
      /* */
162
      /* */
163
      continue;
163
      continue;
164
    }
164
    }
165
 
165
 
166
    if (byteread > 0) {
166
    if (byteread > 0) {
167
      buffer[byteread] = 0;
167
      buffer[byteread] = 0;
168
      lastactivity = time(NULL);
168
      lastactivity = time(NULL);
169
      /* do I know the http code yet? */
169
      /* do I know the http code yet? */
170
      if (httpcode < 0) {
170
      if (httpcode < 0) {
-
 
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
        }
171
        httpcode = atoi((char *)buffer);
182
        httpcode = atoi((char *)(buffer + spc + 1));
172
        /* on error, the answer should be always printed on screen */
183
        /* on error, the answer should be always printed on screen */
173
        if ((httpcode == 200) && (*outfname != 0)) {
184
        if ((httpcode == 200) && (*outfname != 0)) {
174
          fd = fopen(outfname, "wb");
185
          fd = fopen(outfname, "wb");
175
          if (fd == NULL) {
186
          if (fd == NULL) {
176
            printf("ERROR: failed to create file %s", outfname);
187
            printf("ERROR: failed to create file %s", outfname);
177
            puts("");
188
            puts("");
178
            goto SHITQUIT;
189
            goto SHITQUIT;
179
          }
190
          }
180
        }
191
        }
181
      }
192
      }
182
      /* skip headers: look for \r\n\r\n or \n\n within the stream */
193
      /* skip headers: look for \r\n\r\n or \n\n within the stream */
183
      if (headersdone == 0) {
194
      if (headersdone == 0) {
184
        byteread = detecthttpheadersend(buffer, byteread);
195
        byteread = detecthttpheadersend(buffer, byteread);
185
        headersdone = 1;
196
        headersdone = 1;
186
        if (byteread == 0) continue;
197
        if (byteread == 0) continue;
187
      }
198
      }
188
      /* if downloading to file, write stuff to disk */
199
      /* if downloading to file, write stuff to disk */
189
      if (fd != NULL) {
200
      if (fd != NULL) {
190
        int i;
201
        int i;
191
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
202
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
192
          printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen);
203
          printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen);
193
          puts("");
204
          puts("");
194
          break;
205
          break;
195
        }
206
        }
196
        flen += byteread;
207
        flen += byteread;
197
        /* update the bsd sum */
208
        /* update the bsd sum */
198
        for (i = 0; i < byteread; i++) {
209
        for (i = 0; i < byteread; i++) {
199
          /* rotr16 */
210
          /* rotr16 */
200
          unsigned short bsumlsb = *bsum & 1;
211
          unsigned short bsumlsb = *bsum & 1;
201
          *bsum >>= 1;
212
          *bsum >>= 1;
202
          *bsum |= (bsumlsb << 15);
213
          *bsum |= (bsumlsb << 15);
203
          /* bsum += ch */
214
          /* bsum += ch */
204
          *bsum += buffer[i];
215
          *bsum += buffer[i];
205
        }
216
        }
206
      } else { /* otherwise dump to screen */
217
      } else { /* otherwise dump to screen */
207
        printf("%s", buffer);
218
        printf("%s", buffer);
208
      }
219
      }
209
    }
220
    }
210
  }
221
  }
211
  net_close(sock);
222
  net_close(sock);
212
  return(flen);
223
  return(flen);
213
 
224
 
214
  SHITQUIT:
225
  SHITQUIT:
215
  net_abort(sock);
226
  net_abort(sock);
216
  return(-1);
227
  return(-1);
217
}
228
}
218
 
229
 
219
 
230
 
220
/* checks if file exists, returns 0 if not, non-zero otherwise */
231
/* checks if file exists, returns 0 if not, non-zero otherwise */
221
static int fexists(const char *fname) {
232
static int fexists(const char *fname) {
222
  FILE *fd = fopen(fname, "rb");
233
  FILE *fd = fopen(fname, "rb");
223
  if (fd == NULL) return(0);
234
  if (fd == NULL) return(0);
224
  fclose(fd);
235
  fclose(fd);
225
  return(1);
236
  return(1);
226
}
237
}
227
 
238
 
228
 
239
 
229
int main(int argc, char **argv) {
240
int main(int argc, char **argv) {
230
  char ipaddr[64];
241
  char ipaddr[64];
231
  char url[64];
242
  char url[64];
232
  unsigned short bsum = 0;
243
  unsigned short bsum = 0;
233
  char outfname[16];
244
  char outfname[16];
234
  long flen;
245
  long flen;
235
 
246
 
236
  /* parse command line arguments */
247
  /* parse command line arguments */
237
  if (parseargv(argc, argv, outfname, url) != 0) return(1);
248
  if (parseargv(argc, argv, outfname, url) != 0) return(1);
238
 
249
 
239
  /* if outfname requested, make sure that file does not exist yet */
250
  /* if outfname requested, make sure that file does not exist yet */
240
  if (fexists(outfname)) {
251
  if (fexists(outfname)) {
241
    printf("ERROR: file %s already exists", outfname);
252
    printf("ERROR: file %s already exists", outfname);
242
    puts("");
253
    puts("");
243
    return(1);
254
    return(1);
244
  }
255
  }
245
 
256
 
246
  /* init network stack */
257
  /* init network stack */
247
  if (net_init() != 0) {
258
  if (net_init() != 0) {
248
    puts("ERROR: Network subsystem initialization failed");
259
    puts("ERROR: Network subsystem initialization failed");
249
    return(1);
260
    return(1);
250
  }
261
  }
251
 
262
 
252
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
263
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
253
 
264
 
254
  if (net_dnsresolve(ipaddr, HOSTADDR) != 0) {
265
  if (net_dnsresolve(ipaddr, HOSTADDR) != 0) {
255
    puts("ERROR: DNS resolution failed");
266
    puts("ERROR: DNS resolution failed");
256
    return(1);
267
    return(1);
257
  }
268
  }
258
 
269
 
259
  flen = htget(ipaddr, url, outfname, &bsum);
270
  flen = htget(ipaddr, url, outfname, &bsum);
260
  if (flen < 1) return(1);
271
  if (flen < 1) return(1);
261
 
272
 
262
  if (*outfname != 0) {
273
  if (*outfname != 0) {
263
    /* print bsum, size, filename */
274
    /* print bsum, size, filename */
264
    printf("Downloaded %ld KiB into %s (BSUM: %04X)", flen >> 10, outfname, bsum);
275
    printf("Downloaded %ld KiB into %s (BSUM: %04X)", flen >> 10, outfname, bsum);
265
    puts("");
276
    puts("");
266
  }
277
  }
267
 
278
 
268
  return(0);
279
  return(0);
269
}
280
}
270
 
281