Subversion Repositories SvarDOS

Rev

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

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