Subversion Repositories SvarDOS

Rev

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

Rev 338 Rev 342
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
#include "unchunk.h"
36
 
37
 
37
#define PVER "20210520"
38
#define PVER "20210903"
38
#define PDATE "2021"
39
#define PDATE "2021"
39
 
40
 
40
#define HOSTADDR "svardos.osdn.io"
41
#define HOSTADDR "svardos.osdn.io"
41
 
42
 
42
 
43
 
43
/* returns length of all http headers, or 0 if uncomplete yet */
44
/* returns length of all http headers, or 0 if uncomplete yet */
44
static unsigned short detecthttpheadersend(const unsigned char *buff) {
45
static unsigned short detecthttpheadersend(const unsigned char *buff) {
45
  char lastbyteislf = 0;
46
  char lastbyteislf = 0;
46
  unsigned short i;
47
  unsigned short i;
47
  for (i = 0; buff[i] != 0; i++) {
48
  for (i = 0; buff[i] != 0; i++) {
48
    if (buff[i] == '\r') continue; /* ignore CR characters */
49
    if (buff[i] == '\r') continue; /* ignore CR characters */
49
    if (buff[i] != '\n') {
50
    if (buff[i] != '\n') {
50
      lastbyteislf = 0;
51
      lastbyteislf = 0;
51
      continue;
52
      continue;
52
    }
53
    }
53
    /* cur byte is LF -> if last one was also LF then this is an empty line, meaning headers are over */
54
    /* 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
    if (lastbyteislf == 0) {
55
      lastbyteislf = 1;
56
      lastbyteislf = 1;
56
      continue;
57
      continue;
57
    }
58
    }
58
    /* end of headers! return length of headers */
59
    /* end of headers! return length of headers */
59
    return(i + 1); /* add 1 to skip the current \n character */
60
    return(i + 1); /* add 1 to skip the current \n character */
60
  }
61
  }
61
  return(0);
62
  return(0);
62
}
63
}
63
 
64
 
64
 
65
 
65
static void help(void) {
66
static void help(void) {
66
  puts("pkgnet ver " PVER " -- Copyright (C) " PDATE " Mateusz Viste");
67
  puts("pkgnet ver " PVER " -- Copyright (C) " PDATE " Mateusz Viste");
67
  puts("");
68
  puts("");
68
  puts("pkgnet is the SvarDOS package downloader.");
69
  puts("pkgnet is the SvarDOS package downloader.");
69
  puts("");
70
  puts("");
70
  puts("usage:  pkgnet search <term>");
71
  puts("usage:  pkgnet search <term>");
71
  puts("        pkgnet pull <package>");
72
  puts("        pkgnet pull <package>");
72
  puts("        pkgnet checkup");
73
  puts("        pkgnet checkup");
73
  puts("");
74
  puts("");
74
  puts("actions:");
75
  puts("actions:");
75
  puts(" search   - asks remote repository for the list of matching packages");
76
  puts(" search   - asks remote repository for the list of matching packages");
76
  puts(" pull     - downloads package into current directory");
77
  puts(" pull     - downloads package into current directory");
77
  puts(" checkup  - lists updates available for your system");
78
  puts(" checkup  - lists updates available for your system");
78
  puts("");
79
  puts("");
-
 
80
  printf("Watt32 kernel: %s", net_engine());
-
 
81
  puts("");
-
 
82
  puts("");
79
}
83
}
80
 
84
 
81
 
85
 
82
/* parses command line arguments and fills outfname and url accordingly
86
/* parses command line arguments and fills outfname and url accordingly
83
 * returns 0 on success, non-zero otherwise */
87
 * returns 0 on success, non-zero otherwise */
84
static int parseargv(int argc, char * const *argv, char *outfname, char *url) {
88
static int parseargv(int argc, char * const *argv, char *outfname, char *url, int *ispost) {
85
  *outfname = 0;
89
  *outfname = 0;
86
  *url = 0;
90
  *url = 0;
-
 
91
  *ispost = 0;
87
  if ((argc == 3) && (strcasecmp(argv[1], "search") == 0)) {
92
  if ((argc == 3) && (strcasecmp(argv[1], "search") == 0)) {
88
    sprintf(url, "/repo/?a=search&p=%s", argv[2]);
93
    sprintf(url, "/repo/?a=search&p=%s", argv[2]);
89
  } else if ((argc == 3) && (strcasecmp(argv[1], "pull") == 0)) {
94
  } else if ((argc == 3) && (strcasecmp(argv[1], "pull") == 0)) {
90
    if ((strlen(argv[2]) > 8) || (argv[2][0] == 0)) {
95
    if ((strlen(argv[2]) > 8) || (argv[2][0] == 0)) {
91
      puts("ERROR: package name must be 8 characters maximum");
96
      puts("ERROR: package name must be 8 characters maximum");
92
      return(-1);
97
      return(-1);
93
    }
98
    }
94
    sprintf(url, "/repo/?a=pull&p=%s", argv[2]);
99
    sprintf(url, "/repo/?a=pull&p=%s", argv[2]);
95
    sprintf(outfname, "%s.zip", argv[2]);
100
    sprintf(outfname, "%s.zip", argv[2]);
96
  } else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
101
  } else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
97
    puts("NOT SUPPORTED YET");
102
    sprintf(url, "/repo/?a=checkup");
98
    return(-1);
103
    *ispost = 1;
99
  } else {
104
  } else {
100
    help();
105
    help();
101
    return(-1);
106
    return(-1);
102
  }
107
  }
103
  return(0);
108
  return(0);
104
}
109
}
105
 
110
 
106
 
111
 
107
static int htget_headers(unsigned char *buffer, size_t buffersz, struct net_tcpsocket *sock, int *httpcode)  {
112
static int htget_headers(unsigned char *buffer, size_t buffersz, struct net_tcpsocket *sock, int *httpcode, int *ischunked)  {
108
  unsigned char *buffptr = buffer;
113
  unsigned char *buffptr = buffer;
109
  unsigned short bufflen = 0;
114
  unsigned short bufflen = 0;
110
  int byteread;
115
  int byteread;
111
  time_t starttime = time(NULL);
116
  time_t starttime = time(NULL);
112
  for (;;) {
117
  for (;;) {
113
    byteread = net_recv(sock, buffptr, buffersz - (bufflen + 1)); /* -1 because I will append a NULL terminator */
118
    byteread = net_recv(sock, buffptr, buffersz - (bufflen + 1)); /* -1 because I will append a NULL terminator */
114
 
119
 
115
    if (byteread > 0) { /* got data */
120
    if (byteread > 0) { /* got data */
116
      int hdlen;
121
      int hdlen;
117
      bufflen += byteread;
122
      bufflen += byteread;
118
      buffptr += byteread;
123
      buffptr += byteread;
119
      buffer[bufflen] = 0;
124
      buffer[bufflen] = 0;
120
      hdlen = detecthttpheadersend(buffer);
125
      hdlen = detecthttpheadersend(buffer);
121
      if (hdlen > 0) { /* full headers - parse http code and continue processing */
126
      if (hdlen > 0) { /* full headers - parse http code and continue processing */
122
        int spc;
127
        int i;
-
 
128
        buffer[hdlen - 1] = 0;
123
        /* find the first space (HTTP/1.1 200 OK) */
129
        /* find the first space (HTTP/1.1 200 OK) */
124
        for (spc = 0; spc < 16; spc++) {
130
        for (i = 0; i < 16; i++) {
125
          if (buffer[spc] == ' ') break;
131
          if ((buffer[i] == ' ') || (buffer[i] == 0)) break;
-
 
132
        }
126
          if (buffer[spc] == 0) break;
133
        if (buffer[i] != ' ') return(-1);
-
 
134
        *httpcode = atoi((char *)(buffer + i + 1));
-
 
135
        /* switch all headers to low-case so it is easier to parse them */
-
 
136
        for (i = 0; i < hdlen; i++) if ((buffer[i] >= 'A') && (buffer[i] <= 'Z')) buffer[i] += ('a' - 'A');
-
 
137
        /* look out for chunked transfer encoding */
-
 
138
        {
-
 
139
        char *lineptr = strstr((char *)buffer, "\ntransfer-encoding:");
-
 
140
        if (lineptr != NULL) {
-
 
141
          lineptr += 19;
-
 
142
          /* replace nearest \r, \n or 0 by 0 */
-
 
143
          for (i = 0; ; i++) {
-
 
144
            if ((lineptr[i] == '\r') || (lineptr[i] == '\n') || (lineptr[i] == 0)) {
-
 
145
              lineptr[i] = 0;
-
 
146
              break;
-
 
147
            }
-
 
148
          }
-
 
149
          /* do I see the 'chunked' word? */
-
 
150
          if (strstr((char *)lineptr, "chunked") != NULL) *ischunked = 1;
-
 
151
        }
127
        }
152
        }
128
        if (buffer[spc] != ' ') return(-1);
-
 
129
        *httpcode = atoi((char *)(buffer + spc + 1));
-
 
130
        /* rewind the buffer */
153
        /* rewind the buffer */
131
        bufflen -= hdlen;
154
        bufflen -= hdlen;
132
        memmove(buffer, buffer + hdlen, bufflen);
155
        memmove(buffer, buffer + hdlen, bufflen);
133
        return(bufflen); /* move to body processing now */
156
        return(bufflen); /* move to body processing now */
134
      }
157
      }
135
 
158
 
136
    } else if (byteread < 0) { /* abort on error */
159
    } else if (byteread < 0) { /* abort on error */
137
      return(-2); /* unexpected end of connection (while waiting for all http headers) */
160
      return(-2); /* unexpected end of connection (while waiting for all http headers) */
138
 
161
 
139
    } else { /* else no data received - look for timeout and release a cpu cycle */
162
    } else { /* else no data received - look for timeout and release a cpu cycle */
140
      if (time(NULL) - starttime > 20) return(-3); /* TIMEOUT! */
163
      if (time(NULL) - starttime > 20) return(-3); /* TIMEOUT! */
141
      _asm int 28h; /* release a CPU cycle */
164
      _asm int 28h; /* release a CPU cycle */
142
    }
165
    }
143
  }
166
  }
144
}
167
}
145
 
168
 
146
 
169
 
-
 
170
/* provides body data of the POST query for checkup actions
-
 
171
   fills buff with data and returns data length.
-
 
172
   must be called repeateadly until zero-lengh is returned */
-
 
173
static unsigned short checkupdata(char *buff) {
-
 
174
  buff[0] = 0;
-
 
175
  return(0);
-
 
176
}
-
 
177
 
-
 
178
 
147
/* fetch http data from ipaddr using url
179
/* fetch http data from ipaddr using url
148
 * write result to file outfname if not null, or print to stdout otherwise
180
 * write result to file outfname if not null, or print to stdout otherwise
149
 * fills bsum with the BSD sum of the data
181
 * fills bsum with the BSD sum of the data
-
 
182
 * is ispost is non-zero, then the request is a POST and its body data is
-
 
183
 * obtained through repeated calls to checkupdata()
150
 * returns the length of data obtained, or neg value on error */
184
 * returns the length of data obtained, or neg value on error */
151
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum) {
185
static long htget(const char *ipaddr, const char *url, const char *outfname, unsigned short *bsum, int ispost, unsigned char *buffer, size_t buffersz) {
152
  struct net_tcpsocket *sock;
186
  struct net_tcpsocket *sock;
153
  unsigned char buffer[4096];
-
 
154
  time_t lastactivity, lastprogressoutput = 0;
187
  time_t lastactivity, lastprogressoutput = 0;
155
  int httpcode = -1;
188
  int httpcode = -1, ischunked = 0;
156
  int byteread;
189
  int byteread;
157
  long flen = 0, lastflen = 0;
190
  long flen = 0, lastflen = 0;
158
  FILE *fd = NULL;
191
  FILE *fd = NULL;
159
 
192
 
-
 
193
  /* unchunk state variable is using a little part of the supplied buffer */
-
 
194
  struct unchunk_state *unchstate = (void *)buffer;
-
 
195
  buffer += sizeof(*unchstate);
-
 
196
  buffersz -= sizeof(*unchstate);
-
 
197
  memset(unchstate, 0, sizeof(*unchstate));
-
 
198
 
160
  sock = net_connect(ipaddr, 80);
199
  sock = net_connect(ipaddr, 80);
161
  if (sock == NULL) {
200
  if (sock == NULL) {
162
    puts("ERROR: failed to connect to " HOSTADDR);
201
    puts("ERROR: failed to connect to " HOSTADDR);
163
    goto SHITQUIT;
202
    goto SHITQUIT;
164
  }
203
  }
165
 
204
 
166
  /* wait for net_connect() to actually connect */
205
  /* wait for net_connect() to actually connect */
167
  for (;;) {
206
  for (;;) {
168
    int connstate = net_isconnected(sock);
207
    int connstate = net_isconnected(sock);
169
    if (connstate > 0) break;
208
    if (connstate > 0) break;
170
    if (connstate < 0) {
209
    if (connstate < 0) {
171
      puts("ERROR: connection error");
210
      puts("ERROR: connection error");
172
      goto SHITQUIT;
211
      goto SHITQUIT;
173
    }
212
    }
174
    _asm int 28h;  /* DOS idle */
213
    _asm int 28h;  /* DOS idle */
175
  }
214
  }
176
 
215
 
177
  /* socket is connected - send the http request (MUST be HTTP/1.0 because I do not support chunked transfers!) */
216
  /* socket is connected - send the http request (MUST be HTTP/1.0 because I do not support chunked transfers!) */
-
 
217
  if (ispost) {
-
 
218
    snprintf((char *)buffer, buffersz, "POST %s HTTP/1.1\r\nHOST: " HOSTADDR "\r\nUSER-AGENT: pkgnet/" PVER "\r\nTransfer-Encoding: chunked\r\nConnection: close\r\n\r\n", url);
-
 
219
  } else {
178
  snprintf((char *)buffer, sizeof(buffer), "GET %s HTTP/1.0\r\nHOST: " HOSTADDR "\r\nUSER-AGENT: pkgnet\r\nConnection: close\r\n\r\n", url);
220
    snprintf((char *)buffer, buffersz, "GET %s HTTP/1.1\r\nHOST: " HOSTADDR "\r\nUSER-AGENT: pkgnet/" PVER "\r\nConnection: close\r\n\r\n", url);
-
 
221
  }
179
 
222
 
180
  if (net_send(sock, buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
223
  if (net_send(sock, buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
181
    puts("ERROR: failed to send HTTP query to remote server");
224
    puts("ERROR: failed to send HTTP query to remote server");
182
    goto SHITQUIT;
225
    goto SHITQUIT;
183
  }
226
  }
184
 
227
 
-
 
228
  /* send chunked data for POST queries */
-
 
229
  if (ispost) {
-
 
230
    unsigned short blen;
-
 
231
    int hlen;
-
 
232
    char *hbuf = (char *)buffer + buffersz - 16;
-
 
233
    for (;;) {
-
 
234
      blen = checkupdata((char *)buffer);
-
 
235
      if (blen == 0) { /* last item contains the message trailer */
-
 
236
        hlen = sprintf(hbuf, "0\r\n\r\n");
-
 
237
      } else {
-
 
238
        hlen = sprintf(hbuf, "%X\r\n", blen);
-
 
239
      }
-
 
240
      if (net_send(sock, hbuf, hlen) != hlen) {
-
 
241
        puts("ERROR: failed to send POST data to remote server");
-
 
242
        goto SHITQUIT;
-
 
243
      }
-
 
244
      if (blen == 0) break;
-
 
245
      if (net_send(sock, buffer, blen) != blen) {
-
 
246
        puts("ERROR: failed to send POST data to remote server");
-
 
247
        goto SHITQUIT;
-
 
248
      }
-
 
249
    }
-
 
250
  }
-
 
251
 
185
  /* receive and process HTTP headers */
252
  /* receive and process HTTP headers */
186
  byteread = htget_headers(buffer, sizeof(buffer), sock, &httpcode);
253
  byteread = htget_headers(buffer, buffersz, sock, &httpcode, &ischunked);
187
 
254
 
188
  /* transmission error? */
255
  /* transmission error? */
189
  if (byteread < 0) {
256
  if (byteread < 0) {
190
    printf("ERROR: communication error (%d)", byteread);
257
    printf("ERROR: communication error (%d)", byteread);
191
    puts("");
258
    puts("");
192
    goto SHITQUIT;
259
    goto SHITQUIT;
193
  }
260
  }
194
 
261
 
195
  /* open destination file if required and if no server-side error occured */
262
  /* open destination file if required and if no server-side error occured */
196
  if ((httpcode == 200) && (*outfname != 0)) {
263
  if ((httpcode >= 200) && (httpcode <= 299) && (*outfname != 0)) {
197
    fd = fopen(outfname, "wb");
264
    fd = fopen(outfname, "wb");
198
    if (fd == NULL) {
265
    if (fd == NULL) {
199
      printf("ERROR: failed to create file %s", outfname);
266
      printf("ERROR: failed to create file %s", outfname);
200
      puts("");
267
      puts("");
201
      goto SHITQUIT;
268
      goto SHITQUIT;
202
    }
269
    }
203
  }
270
  }
204
 
271
 
205
  /* read body of the answer */
272
  /* read body of the answer (and chunk-decode it if needed) */
206
  lastactivity = time(NULL);
273
  lastactivity = time(NULL);
207
  for (;; byteread = net_recv(sock, buffer, sizeof(buffer) - 1)) { /* read 1 byte less because I need to append a NULL terminator */
274
  for (;; byteread = net_recv(sock, buffer, buffersz - 1)) { /* read 1 byte less because I need to append a NULL terminator when printf'ing the content */
208
 
275
 
209
    if (byteread > 0) { /* got data */
276
    if (byteread > 0) { /* got data */
210
      buffer[byteread] = 0;
-
 
211
      lastactivity = time(NULL);
277
      lastactivity = time(NULL);
-
 
278
 
-
 
279
      /* unpack data if server transmits in chunked mode */
-
 
280
      if (ischunked) byteread = unchunk(buffer, byteread, unchstate);
-
 
281
 
212
      /* if downloading to file, write stuff to disk */
282
      /* if downloading to file, write stuff to disk */
213
      if (fd != NULL) {
283
      if (fd != NULL) {
214
        int i;
284
        int i;
215
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
285
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
216
          printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen);
286
          printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen);
217
          puts("");
287
          puts("");
218
          break;
288
          break;
219
        }
289
        }
220
        flen += byteread;
290
        flen += byteread;
221
        /* update progress once a sec */
291
        /* update progress once a sec */
222
        if (lastprogressoutput != lastactivity) {
292
        if (lastprogressoutput != lastactivity) {
223
          lastprogressoutput = lastactivity;
293
          lastprogressoutput = lastactivity;
224
          printf("%ld KiB (%ld KiB/s)     \r", flen >> 10, (flen >> 10) - (lastflen >> 10)); /* trailing spaces are meant to avoid leaving garbage on screen if speed goes from, say, 1000 KiB/s to 9 KiB/s */
294
          printf("%ld KiB (%ld KiB/s)     \r", flen >> 10, (flen >> 10) - (lastflen >> 10)); /* trailing spaces are meant to avoid leaving garbage on screen if speed goes from, say, 1000 KiB/s to 9 KiB/s */
225
          lastflen = flen;
295
          lastflen = flen;
226
          fflush(stdout); /* avoid console buffering */
296
          fflush(stdout); /* avoid console buffering */
227
        }
297
        }
228
        /* update the bsd sum */
298
        /* update the bsd sum */
229
        for (i = 0; i < byteread; i++) {
299
        for (i = 0; i < byteread; i++) {
230
          /* rotr16 */
300
          /* rotr16 */
231
          unsigned short bsumlsb = *bsum & 1;
301
          unsigned short bsumlsb = *bsum & 1;
232
          *bsum >>= 1;
302
          *bsum >>= 1;
233
          *bsum |= (bsumlsb << 15);
303
          *bsum |= (bsumlsb << 15);
234
          *bsum += buffer[i];
304
          *bsum += buffer[i];
235
        }
305
        }
236
      } else { /* otherwise dump to screen */
306
      } else { /* otherwise dump to screen */
-
 
307
        buffer[byteread] = 0;
237
        printf("%s", buffer);
308
        printf("%s", buffer);
238
      }
309
      }
239
 
310
 
240
    } else if (byteread < 0) { /* end of connection */
311
    } else if (byteread < 0) { /* end of connection */
241
      break;
312
      break;
242
 
313
 
243
    } else { /* check for timeout (byteread == 0) */
314
    } else { /* check for timeout (byteread == 0) */
244
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
315
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
245
        puts("ERROR: Timeout while waiting for data");
316
        puts("ERROR: Timeout while waiting for data");
246
        goto SHITQUIT;
317
        goto SHITQUIT;
247
      }
318
      }
248
      /* waiting for packets - release a CPU cycle in the meantime */
319
      /* waiting for packets - release a CPU cycle in the meantime */
249
      _asm int 28h;
320
      _asm int 28h;
250
    }
321
    }
251
  }
322
  }
252
 
323
 
253
  goto ALLGOOD;
324
  goto ALLGOOD;
254
 
325
 
255
  SHITQUIT:
326
  SHITQUIT:
256
  flen = -1;
327
  flen = -1;
257
 
328
 
258
  ALLGOOD:
329
  ALLGOOD:
259
  if (fd != NULL) fclose(fd);
330
  if (fd != NULL) fclose(fd);
260
  net_close(sock);
331
  net_close(sock);
261
  return(flen);
332
  return(flen);
262
}
333
}
263
 
334
 
264
 
335
 
265
/* checks if file exists, returns 0 if not, non-zero otherwise */
336
/* checks if file exists, returns 0 if not, non-zero otherwise */
266
static int fexists(const char *fname) {
337
static int fexists(const char *fname) {
267
  FILE *fd = fopen(fname, "rb");
338
  FILE *fd = fopen(fname, "rb");
268
  if (fd == NULL) return(0);
339
  if (fd == NULL) return(0);
269
  fclose(fd);
340
  fclose(fd);
270
  return(1);
341
  return(1);
271
}
342
}
272
 
343
 
273
 
344
 
274
int main(int argc, char **argv) {
345
int main(int argc, char **argv) {
275
  char ipaddr[64];
-
 
276
  char url[64];
-
 
277
  unsigned short bsum = 0;
346
  unsigned short bsum = 0;
278
  char outfname[16];
-
 
279
  long flen;
347
  long flen;
-
 
348
  int ispost; /* is the request a POST? */
-
 
349
 
-
 
350
  struct {
-
 
351
    unsigned char buffer[5000];
-
 
352
    char ipaddr[64];
-
 
353
    char url[64];
-
 
354
    char outfname[16];
-
 
355
  } *mem;
-
 
356
 
-
 
357
  /* allocate memory */
-
 
358
  mem = malloc(sizeof(*mem));
-
 
359
  if (mem == NULL) {
-
 
360
    puts("ERROR: out of memory");
-
 
361
    return(1);
-
 
362
  }
280
 
363
 
281
  /* parse command line arguments */
364
  /* parse command line arguments */
282
  if (parseargv(argc, argv, outfname, url) != 0) return(1);
365
  if (parseargv(argc, argv, mem->outfname, mem->url, &ispost) != 0) return(1);
283
 
366
 
284
  /* if outfname requested, make sure that file does not exist yet */
367
  /* if outfname requested, make sure that file does not exist yet */
285
  if (fexists(outfname)) {
368
  if ((mem->outfname[0] != 0) && (fexists(mem->outfname))) {
286
    printf("ERROR: file %s already exists", outfname);
369
    printf("ERROR: file %s already exists", mem->outfname);
287
    puts("");
370
    puts("");
288
    return(1);
371
    return(1);
289
  }
372
  }
290
 
373
 
291
  /* init network stack */
374
  /* init network stack */
292
  if (net_init() != 0) {
375
  if (net_init() != 0) {
293
    puts("ERROR: Network subsystem initialization failed");
376
    puts("ERROR: Network subsystem initialization failed");
294
    return(1);
377
    return(1);
295
  }
378
  }
296
 
379
 
297
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
380
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
298
 
381
 
299
  if (net_dnsresolve(ipaddr, HOSTADDR) != 0) {
382
  if (net_dnsresolve(mem->ipaddr, HOSTADDR) != 0) {
300
    puts("ERROR: DNS resolution failed");
383
    puts("ERROR: DNS resolution failed");
301
    return(1);
384
    return(1);
302
  }
385
  }
303
 
386
 
304
  flen = htget(ipaddr, url, outfname, &bsum);
387
  flen = htget(mem->ipaddr, mem->url, mem->outfname, &bsum, ispost, mem->buffer, sizeof(mem->buffer));
305
  if (flen < 1) return(1);
388
  if (flen < 1) return(1);
306
 
389
 
307
  if (*outfname != 0) {
390
  if (mem->outfname[0] != 0) {
308
    /* print bsum, size, filename */
391
    /* print bsum, size, filename */
309
    printf("Downloaded %ld KiB into %s (BSUM: %04X)", flen >> 10, outfname, bsum);
392
    printf("Downloaded %ld KiB into %s (BSUM: %04X)", flen >> 10, mem->outfname, bsum);
310
    puts("");
393
    puts("");
311
  }
394
  }
312
 
395
 
313
  return(0);
396
  return(0);
314
}
397
}
315
 
398