Subversion Repositories SvarDOS

Rev

Rev 209 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
207 mateuszvis 1
/*
2
 * pkgnet - pulls SvarDOS packages from the project's online repository
3
 * Copyright (C) 2021 Mateusz Viste
4
 *
5
 * PUBLISHED UNDER THE TERMS OF THE MIT LICENSE
6
 *
7
 * COPYRIGHT (C) 2016-2021 MATEUSZ VISTE, ALL RIGHTS RESERVED.
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a
10
 * copy of this software and associated documentation files (the "Software"),
11
 * to deal in the Software without restriction, including without limitation
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
14
 * Software is furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in
17
 * all copies or substantial portions of the Software.
18
 *
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,
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
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
25
 * DEALINGS IN THE SOFTWARE.
26
 *
27
 * http://svardos.osdn.io
28
 */
29
 
30
#include <stdio.h>
31
#include <string.h>
32
#include <time.h>
33
 
34
#include "net.h"
35
 
36
#define HOSTADDR "svardos.osdn.io"
37
 
38
/* strips http headers and returns new buff len */
39
static int detecthttpheadersend(char *buff, int len) {
40
  static char lastbyteislf = 0; /* static because I must potentially remember it for next packet/call */
41
  int i;
42
  for (i = 0; i < len; i++) {
43
    if (buff[i] == '\r') continue; /* ignore CR characters */
44
    if (buff[i] != '\n') {
45
      lastbyteislf = 0;
46
      continue;
47
    }
48
    /* cur byte is LF -> if last one was also LF then this is an empty line, meaning headers are over */
49
    if (lastbyteislf == 0) {
50
      lastbyteislf = 1;
51
      continue;
52
    }
53
    /* end of headers! rewind the buffer and return new len */
54
    len -= i;
55
    if (len > 0) memmove(buff, buff + i, len);
56
    return(len);
57
  }
58
  return(0);
59
}
60
 
61
 
62
int main(int argc, char **argv) {
63
  char buffer[4096];
64
  char url[64];
65
  struct net_tcpsocket *sock;
66
  time_t lastactivity;
67
  int headersdone;
68
 
69
  /* prepare the query */
70
  snprintf(url, sizeof(url), "/pkgnet.php?action=xxx");
71
 
72
  /* init network stack */
73
  if (net_init() != 0) {
74
    puts("ERROR: Network subsystem initialization failed");
75
    return(1);
76
  }
77
 
78
  puts(""); /* required because watt-32 likes to print out garbage sometimes ("configuring through DHCP...") */
79
 
80
  if (net_dnsresolve(buffer, HOSTADDR) != 0) {
81
    puts("ERROR: DNS resolution failed");
82
    return(1);
83
  }
84
 
85
  sock = net_connect(buffer, 80);
86
  if (sock == NULL) {
87
    puts("ERROR: failed to connect to " HOSTADDR);
88
    goto SHITQUIT;
89
  }
90
 
91
  /* wait for net_connect() to actually connect */
92
  for (;;) {
93
    int connstate;
94
    connstate = net_isconnected(sock, 1);
95
    if (connstate > 0) break;
96
    if (connstate < 0) {
97
      puts("ERROR: connection error");
98
      goto SHITQUIT;
99
    }
100
  }
101
 
102
  /* 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);
104
 
105
  if (net_send(sock, buffer, strlen(buffer)) != (int)strlen(buffer)) {
106
    puts("ERROR: failed to send HTTP query to remote server");
107
    goto SHITQUIT;
108
  }
109
 
110
  lastactivity = time(NULL);
111
  headersdone = 0;
112
  for (;;) {
113
    int byteread = net_recv(sock, buffer, sizeof(buffer) - 1);
114
 
115
    if (byteread < 0) break; /* end of connection */
116
 
117
    /*  */
118
    if (byteread == 0) {
119
      if (time(NULL) - lastactivity > 20) { /* TIMEOUT! */
120
        puts("ERROR: Timeout while waiting for data");
121
        goto SHITQUIT;
122
      }
123
      /* waiting for packets - release a CPU cycle in the meantime */
124
      _asm int 28h;
125
      /* */
126
      continue;
127
    }
128
 
129
    if (byteread > 0) {
130
      buffer[byteread] = 0;
131
      lastactivity = time(NULL);
132
      /* skip headers: look for \r\n\r\n or \n\n within the stream */
133
      if (headersdone == 0) {
134
        byteread = detecthttpheadersend(buffer, byteread);
135
        headersdone = 1;
136
        if (byteread == 0) continue;
137
      }
138
      /* if downloading to file, write stuff to disk */
139
      printf("%s", buffer);
140
    }
141
  }
142
 
143
  net_close(sock);
144
  return(0);
145
 
146
  SHITQUIT:
147
  if (sock != NULL) net_abort(sock);
148
  return(1);
149
}