Subversion Repositories SvarDOS

Rev

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

Rev 209 Rev 211
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 [<package>]");
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 available updates (for all packages if no argument)");
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 = NULL;
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, "/pkg.php?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)) {
-
 
93
      puts("ERROR: package name must be 8 characters maximum");
-
 
94
      return(-1);
-
 
95
    }
92
    sprintf(url, "/pkg.php?a=pull&p=%s", argv[2]);
96
    sprintf(url, "/pkg.php?a=pull&p=%s", argv[2]);
93
    *outfname = argv[2];
97
    sprintf(outfname, "%s.zip", argv[2]);
94
  } else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
98
  } else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
95
    puts("NOT SUPPORTED YET");
99
    puts("NOT SUPPORTED YET");
96
    return(-1);
100
    return(-1);
97
  } else if ((argc == 3) && (strcasecmp(argv[1], "checkup") == 0)) {
-
 
98
    puts("NOT SUPPORTED YET");
-
 
99
    return(-1);
-
 
100
  } else {
101
  } else {
101
    help();
102
    help();
102
    return(-1);
103
    return(-1);
103
  }
104
  }
104
  return(0);
105
  return(0);
105
}
106
}
106
 
107
 
107
 
108
 
108
int main(int argc, char **argv) {
109
/* fetch http data from ipaddr using url
-
 
110
 * write result to file outfname if not null, or print to stdout otherwise
109
  unsigned char buffer[4096];
111
 * fills bsum with the BSD sum of the data
110
  char url[64];
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) {
111
  struct net_tcpsocket *sock;
114
  struct net_tcpsocket *sock;
-
 
115
  unsigned char buffer[4096];
112
  time_t lastactivity;
116
  time_t lastactivity;
113
  int headersdone = 0;
117
  int headersdone = 0;
114
  int httpcode = -1;
118
  int httpcode = -1;
115
  long flen = 0;
119
  long flen = 0;
116
  unsigned short bsum = 0;
-
 
117
  char *outfname = NULL;
-
 
118
  FILE *fd = NULL;
120
  FILE *fd = NULL;
119
 
121
 
120
  /* parse command line arguments */
-
 
121
  if (parseargv(argc, argv, &outfname, url) != 0) return(1);
-
 
122
 
-
 
123
  /* init network stack */
-
 
124
  if (net_init() != 0) {
-
 
125
    puts("ERROR: Network subsystem initialization failed");
-
 
126
    return(1);
-
 
127
  }
-
 
128
 
-
 
129
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
-
 
130
 
-
 
131
  if (net_dnsresolve((char *)buffer, HOSTADDR) != 0) {
-
 
132
    puts("ERROR: DNS resolution failed");
-
 
133
    return(1);
-
 
134
  }
-
 
135
 
-
 
136
  sock = net_connect((char *)buffer, 80);
122
  sock = net_connect(ipaddr, 80);
137
  if (sock == NULL) {
123
  if (sock == NULL) {
138
    puts("ERROR: failed to connect to " HOSTADDR);
124
    puts("ERROR: failed to connect to " HOSTADDR);
139
    goto SHITQUIT;
125
    goto SHITQUIT;
140
  }
126
  }
141
 
127
 
142
  /* wait for net_connect() to actually connect */
128
  /* wait for net_connect() to actually connect */
143
  for (;;) {
129
  for (;;) {
144
    int connstate;
-
 
145
    connstate = net_isconnected(sock, 1);
130
    int connstate = net_isconnected(sock, 1);
146
    if (connstate > 0) break;
131
    if (connstate > 0) break;
147
    if (connstate < 0) {
132
    if (connstate < 0) {
148
      puts("ERROR: connection error");
133
      puts("ERROR: connection error");
149
      goto SHITQUIT;
134
      goto SHITQUIT;
150
    }
135
    }
-
 
136
    _asm int 28h;  /* DOS idle */
151
  }
137
  }
152
 
138
 
153
  /* socket is connected - send the http request */
139
  /* socket is connected - send the http request */
154
  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);
155
 
141
 
156
  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)) {
157
    puts("ERROR: failed to send HTTP query to remote server");
143
    puts("ERROR: failed to send HTTP query to remote server");
158
    goto SHITQUIT;
144
    goto SHITQUIT;
159
  }
145
  }
160
 
146
 
161
  lastactivity = time(NULL);
147
  lastactivity = time(NULL);
162
  for (;;) {
148
  for (;;) {
163
    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 */
164
 
150
 
165
    if (byteread < 0) break; /* end of connection */
151
    if (byteread < 0) break; /* end of connection */
166
 
152
 
167
    /*  */
153
    /*  */
168
    if (byteread == 0) {
154
    if (byteread == 0) {
169
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
155
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
170
        puts("ERROR: Timeout while waiting for data");
156
        puts("ERROR: Timeout while waiting for data");
171
        goto SHITQUIT;
157
        goto SHITQUIT;
172
      }
158
      }
173
      /* waiting for packets - release a CPU cycle in the meantime */
159
      /* waiting for packets - release a CPU cycle in the meantime */
174
      _asm int 28h;
160
      _asm int 28h;
175
      /* */
161
      /* */
176
      continue;
162
      continue;
177
    }
163
    }
178
 
164
 
179
    if (byteread > 0) {
165
    if (byteread > 0) {
180
      buffer[byteread] = 0;
166
      buffer[byteread] = 0;
181
      lastactivity = time(NULL);
167
      lastactivity = time(NULL);
182
      /* do I know the http code yet? */
168
      /* do I know the http code yet? */
183
      if (httpcode < 0) {
169
      if (httpcode < 0) {
184
        httpcode = atoi((char *)buffer);
170
        httpcode = atoi((char *)buffer);
185
        /* on error, the answer should be always printed on screen */
171
        /* on error, the answer should be always printed on screen */
186
        if ((httpcode == 200) && (outfname != NULL)) {
172
        if ((httpcode == 200) && (*outfname != 0)) {
187
          fd = fopen(outfname, "wb");
173
          fd = fopen(outfname, "wb");
188
          if (fd == NULL) {
174
          if (fd == NULL) {
189
            printf("ERROR: failed to create file %s", outfname);
175
            printf("ERROR: failed to create file %s", outfname);
190
            puts("");
176
            puts("");
191
            break;
177
            goto SHITQUIT;
192
          }
178
          }
193
        }
179
        }
194
      }
180
      }
195
      /* 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 */
196
      if (headersdone == 0) {
182
      if (headersdone == 0) {
197
        byteread = detecthttpheadersend(buffer, byteread);
183
        byteread = detecthttpheadersend(buffer, byteread);
198
        headersdone = 1;
184
        headersdone = 1;
199
        if (byteread == 0) continue;
185
        if (byteread == 0) continue;
200
      }
186
      }
201
      /* if downloading to file, write stuff to disk */
187
      /* if downloading to file, write stuff to disk */
202
      if (fd != NULL) {
188
      if (fd != NULL) {
203
        int i;
189
        int i;
204
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
190
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
205
          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);
206
          puts("");
192
          puts("");
207
          break;
193
          break;
208
        }
194
        }
209
        flen += byteread;
195
        flen += byteread;
210
        /* update the bsd sum */
196
        /* update the bsd sum */
211
        for (i = 0; i < byteread; i++) {
197
        for (i = 0; i < byteread; i++) {
212
          /* rotr16 */
198
          /* rotr16 */
213
          unsigned short bsumlsb = bsum & 1;
199
          unsigned short bsumlsb = *bsum & 1;
214
          bsum >>= 1;
200
          *bsum >>= 1;
215
          bsum |= (bsumlsb << 15);
201
          *bsum |= (bsumlsb << 15);
216
          /* bsum += ch */
202
          /* bsum += ch */
217
          bsum += buffer[i];
203
          *bsum += buffer[i];
218
        }
204
        }
219
      } else { /* otherwise dump to screen */
205
      } else { /* otherwise dump to screen */
220
        printf("%s", buffer);
206
        printf("%s", buffer);
221
      }
207
      }
222
    }
208
    }
223
  }
209
  }
-
 
210
  net_close(sock);
-
 
211
  return(flen);
-
 
212
 
-
 
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
  }
224
 
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
 
225
  if (fd != NULL) {
261
  if (*outfname != 0) {
226
    /* print bsum, size, filename */
262
    /* print bsum, size, filename */
227
    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);
228
    puts("");
264
    puts("");
229
    fclose(fd);
-
 
230
  }
265
  }
231
 
266
 
232
  net_close(sock);
-
 
233
  return(0);
267
  return(0);
234
 
-
 
235
  SHITQUIT:
-
 
236
  if (sock != NULL) net_abort(sock);
-
 
237
  return(1);
-
 
238
}
268
}
239
 
269