Subversion Repositories SvarDOS

Rev

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

Rev 207 Rev 209
Line 26... Line 26...
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 <string.h>
32
#include <string.h>
32
#include <time.h>
33
#include <time.h>
33
 
34
 
34
#include "net.h"
35
#include "net.h"
35
 
36
 
-
 
37
#define PVER "20210121"
-
 
38
#define PDATE "2021"
-
 
39
 
36
#define HOSTADDR "svardos.osdn.io"
40
#define HOSTADDR "svardos.osdn.io"
37
 
41
 
-
 
42
 
38
/* strips http headers and returns new buff len */
43
/* strips http headers and returns new buff len */
39
static int detecthttpheadersend(char *buff, int len) {
44
static int detecthttpheadersend(unsigned char *buff, int len) {
40
  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 */
41
  int i;
46
  int i;
42
  for (i = 0; i < len; i++) {
47
  for (i = 0; i < len; i++) {
43
    if (buff[i] == '\r') continue; /* ignore CR characters */
48
    if (buff[i] == '\r') continue; /* ignore CR characters */
44
    if (buff[i] != '\n') {
49
    if (buff[i] != '\n') {
Line 49... Line 54...
49
    if (lastbyteislf == 0) {
54
    if (lastbyteislf == 0) {
50
      lastbyteislf = 1;
55
      lastbyteislf = 1;
51
      continue;
56
      continue;
52
    }
57
    }
53
    /* end of headers! rewind the buffer and return new len */
58
    /* end of headers! rewind the buffer and return new len */
54
    len -= i;
59
    len -= (i + 1);
55
    if (len > 0) memmove(buff, buff + i, len);
60
    if (len > 0) memmove(buff, buff + i + 1, len);
56
    return(len);
61
    return(len);
57
  }
62
  }
58
  return(0);
63
  return(0);
59
}
64
}
60
 
65
 
61
 
66
 
-
 
67
static void help(void) {
-
 
68
  puts("pkgnet ver " PVER " -- Copyright (C) " PDATE " Mateusz Viste");
-
 
69
  puts("");
-
 
70
  puts("pkgnet is the SvarDOS package downloader.");
-
 
71
  puts("");
-
 
72
  puts("usage:  pkgnet search <term>");
-
 
73
  puts("        pkgnet pull <package>");
-
 
74
  puts("        pkgnet checkup [<package>]");
-
 
75
  puts("");
-
 
76
  puts("actions:");
-
 
77
  puts(" search   - asks remote repository for the list of matching packages");
-
 
78
  puts(" pull     - downloads package into current directory");
-
 
79
  puts(" checkup  - lists available updates (for all packages if no argument)");
-
 
80
  puts("");
-
 
81
}
-
 
82
 
-
 
83
 
-
 
84
/* parses command line arguments and fills outfname and url accordingly
-
 
85
 * returns 0 on success, non-zero otherwise */
-
 
86
static int parseargv(int argc, char * const *argv, char **outfname, char *url) {
-
 
87
  *outfname = NULL;
-
 
88
  *url = 0;
-
 
89
  if ((argc == 3) && (strcasecmp(argv[1], "search") == 0)) {
-
 
90
    sprintf(url, "/pkg.php?a=search&p=%s", argv[2]);
-
 
91
  } else if ((argc == 3) && (strcasecmp(argv[1], "pull") == 0)) {
-
 
92
    sprintf(url, "/pkg.php?a=pull&p=%s", argv[2]);
-
 
93
    *outfname = argv[2];
-
 
94
  } else if ((argc == 2) && (strcasecmp(argv[1], "checkup") == 0)) {
-
 
95
    puts("NOT SUPPORTED YET");
-
 
96
    return(-1);
-
 
97
  } else if ((argc == 3) && (strcasecmp(argv[1], "checkup") == 0)) {
-
 
98
    puts("NOT SUPPORTED YET");
-
 
99
    return(-1);
-
 
100
  } else {
-
 
101
    help();
-
 
102
    return(-1);
-
 
103
  }
-
 
104
  return(0);
-
 
105
}
-
 
106
 
-
 
107
 
62
int main(int argc, char **argv) {
108
int main(int argc, char **argv) {
63
  char buffer[4096];
109
  unsigned char buffer[4096];
64
  char url[64];
110
  char url[64];
65
  struct net_tcpsocket *sock;
111
  struct net_tcpsocket *sock;
66
  time_t lastactivity;
112
  time_t lastactivity;
67
  int headersdone;
113
  int headersdone = 0;
-
 
114
  int httpcode = -1;
-
 
115
  long flen = 0;
-
 
116
  unsigned short bsum = 0;
-
 
117
  char *outfname = NULL;
-
 
118
  FILE *fd = NULL;
68
 
119
 
69
  /* prepare the query */
120
  /* parse command line arguments */
70
  snprintf(url, sizeof(url), "/pkgnet.php?action=xxx");
121
  if (parseargv(argc, argv, &outfname, url) != 0) return(1);
71
 
122
 
72
  /* init network stack */
123
  /* init network stack */
73
  if (net_init() != 0) {
124
  if (net_init() != 0) {
74
    puts("ERROR: Network subsystem initialization failed");
125
    puts("ERROR: Network subsystem initialization failed");
75
    return(1);
126
    return(1);
76
  }
127
  }
77
 
128
 
78
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
129
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
79
 
130
 
80
  if (net_dnsresolve(buffer, HOSTADDR) != 0) {
131
  if (net_dnsresolve((char *)buffer, HOSTADDR) != 0) {
81
    puts("ERROR: DNS resolution failed");
132
    puts("ERROR: DNS resolution failed");
82
    return(1);
133
    return(1);
83
  }
134
  }
84
 
135
 
85
  sock = net_connect(buffer, 80);
136
  sock = net_connect((char *)buffer, 80);
86
  if (sock == NULL) {
137
  if (sock == NULL) {
87
    puts("ERROR: failed to connect to " HOSTADDR);
138
    puts("ERROR: failed to connect to " HOSTADDR);
88
    goto SHITQUIT;
139
    goto SHITQUIT;
89
  }
140
  }
90
 
141
 
Line 98... Line 149...
98
      goto SHITQUIT;
149
      goto SHITQUIT;
99
    }
150
    }
100
  }
151
  }
101
 
152
 
102
  /* socket is connected - send the http request */
153
  /* socket is connected - send the http request */
103
  snprintf(buffer, sizeof(buffer), "GET %s HTTP/1.1\r\nHOST: " HOSTADDR "\r\nUSER-AGENT: pkgnet\r\nConnection: close\r\n\r\n", url);
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);
104
 
155
 
105
  if (net_send(sock, buffer, strlen(buffer)) != (int)strlen(buffer)) {
156
  if (net_send(sock, (char *)buffer, strlen((char *)buffer)) != (int)strlen((char *)buffer)) {
106
    puts("ERROR: failed to send HTTP query to remote server");
157
    puts("ERROR: failed to send HTTP query to remote server");
107
    goto SHITQUIT;
158
    goto SHITQUIT;
108
  }
159
  }
109
 
160
 
110
  lastactivity = time(NULL);
161
  lastactivity = time(NULL);
111
  headersdone = 0;
-
 
112
  for (;;) {
162
  for (;;) {
113
    int byteread = net_recv(sock, buffer, sizeof(buffer) - 1);
163
    int byteread = net_recv(sock, (char *)buffer, sizeof(buffer) - 1); /* -1 because I will append a NULL terminator */
114
 
164
 
115
    if (byteread < 0) break; /* end of connection */
165
    if (byteread < 0) break; /* end of connection */
116
 
166
 
117
    /*  */
167
    /*  */
118
    if (byteread == 0) {
168
    if (byteread == 0) {
Line 127... Line 177...
127
    }
177
    }
128
 
178
 
129
    if (byteread > 0) {
179
    if (byteread > 0) {
130
      buffer[byteread] = 0;
180
      buffer[byteread] = 0;
131
      lastactivity = time(NULL);
181
      lastactivity = time(NULL);
-
 
182
      /* do I know the http code yet? */
-
 
183
      if (httpcode < 0) {
-
 
184
        httpcode = atoi((char *)buffer);
-
 
185
        /* on error, the answer should be always printed on screen */
-
 
186
        if ((httpcode == 200) && (outfname != NULL)) {
-
 
187
          fd = fopen(outfname, "wb");
-
 
188
          if (fd == NULL) {
-
 
189
            printf("ERROR: failed to create file %s", outfname);
-
 
190
            puts("");
-
 
191
            break;
-
 
192
          }
-
 
193
        }
-
 
194
      }
132
      /* skip headers: look for \r\n\r\n or \n\n within the stream */
195
      /* skip headers: look for \r\n\r\n or \n\n within the stream */
133
      if (headersdone == 0) {
196
      if (headersdone == 0) {
134
        byteread = detecthttpheadersend(buffer, byteread);
197
        byteread = detecthttpheadersend(buffer, byteread);
135
        headersdone = 1;
198
        headersdone = 1;
136
        if (byteread == 0) continue;
199
        if (byteread == 0) continue;
137
      }
200
      }
138
      /* if downloading to file, write stuff to disk */
201
      /* if downloading to file, write stuff to disk */
-
 
202
      if (fd != NULL) {
-
 
203
        int i;
-
 
204
        if (fwrite(buffer, 1, byteread, fd) != byteread) {
-
 
205
          printf("ERROR: failed to write data to file %s after %ld bytes", outfname, flen);
-
 
206
          puts("");
-
 
207
          break;
-
 
208
        }
-
 
209
        flen += byteread;
-
 
210
        /* update the bsd sum */
-
 
211
        for (i = 0; i < byteread; i++) {
-
 
212
          /* rotr16 */
-
 
213
          unsigned short bsumlsb = bsum & 1;
-
 
214
          bsum >>= 1;
-
 
215
          bsum |= (bsumlsb << 15);
-
 
216
          /* bsum += ch */
-
 
217
          bsum += buffer[i];
-
 
218
        }
-
 
219
      } else { /* otherwise dump to screen */
139
      printf("%s", buffer);
220
        printf("%s", buffer);
-
 
221
      }
140
    }
222
    }
141
  }
223
  }
142
 
224
 
-
 
225
  if (fd != NULL) {
-
 
226
    /* print bsum, size, filename */
-
 
227
    printf("Downloaded %ld KiB into %s (BSUM: %04X)", flen >> 10, outfname, bsum);
-
 
228
    puts("");
-
 
229
    fclose(fd);
-
 
230
  }
-
 
231
 
143
  net_close(sock);
232
  net_close(sock);
144
  return(0);
233
  return(0);
145
 
234
 
146
  SHITQUIT:
235
  SHITQUIT:
147
  if (sock != NULL) net_abort(sock);
236
  if (sock != NULL) net_abort(sock);