Subversion Repositories SvarDOS

Rev

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

Rev 209 Rev 211
Line 69... Line 69...
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
 
Line 181... Line 167...
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) {
Line 208... Line 194...
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
}