Subversion Repositories SvarDOS

Rev

Rev 338 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 338 Rev 342
Line 31... Line 31...
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
 
Line 74... Line 75...
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 (;;) {
Line 117... Line 122...
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
      }
Line 142... Line 165...
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
  }
Line 173... Line 212...
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);
Line 232... Line 302...
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;
Line 270... Line 341...
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 */
Line 294... Line 377...
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
}