aboutsummaryrefslogtreecommitdiff
path: root/lib/update.c
blob: a599180763457e8505ed0b6fcf2037fcd0c51d3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* $Id$ */
/*
 * Copyright (c) 2004 demon <demon@vhost.dyndns.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <err.h>
#include <unistd.h>
#include <strings.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <sys/types.h>
#include <time.h>
#include <zlib.h>
#include "unzip.h"
#include <tgebup.h>

#define DATFILE "tarife"

const char *TGebURL = "www.billiger-telefonieren.de";
const char *TGebGET = "GET /downloads/tgeb/tarife_%.4i%.2i%.2i.zip HTTP/1.0\n\n";

static void unzip(void);

void
tgeb_update(struct tm * tm)
{
	struct hostent *he;
	struct sockaddr_in sin;
	int     sockfd;
	char   *get;
	char   *buf;
	FILE   *fd;
	char   *file;
	int     filelen;
	int     offset = 0;

	if (!(he = gethostbyname(TGebURL)))
		err(1, "gethostbyname");

	if ((sockfd = socket(AF_INET, SOCK_STREAM, NULL)) == -1)
		err(1, "socket");
	memset(&sin, '\0', sizeof(struct sockaddr_in));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(80);
	sin.sin_addr = *((struct in_addr *) he->h_addr);

	if ((connect(sockfd, (struct sockaddr *) & sin,
		     sizeof(struct sockaddr))) == -1)
		err(1, "connect");

	if (!(get = malloc(sizeof(char) * strlen(TGebGET))))
		err(1, "malloc");
	sprintf(get, TGebGET, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
	if ((send(sockfd, get, strlen(get), NULL)) == -1)
		err(1, "send");
	free(get);

	if (!(buf = malloc(sizeof(char) * 1024)))
		err(1, "malloc");

	for (;;) {
		int     block;
		if ((block = recv(sockfd, &buf[offset], 1024, NULL)) == -1)
			err(1, "recv");
		if (block == 0)
			break;
		offset += block;
		if (!(buf = realloc(buf, offset + 1024)))
			err(1, "malloc");
	}
	close(sockfd);
	file = strstr(buf, "\r\n\r\n") + 4;
	filelen = offset + buf - file;

	if (!(fd = fopen(DATFILE, "wb")))
		err(1, "fopen %s", DATFILE);
	fwrite(file, 1, filelen, fd);
	fclose(fd);
	free(buf);
	unzip();
	return;
}

void
unzip(void)
{
	FILE   *fd;
	unzFile *unzfd;
	unz_global_info gi;
	unz_file_info fi;
	char    FileName[256];
	char    buf[1024];
	int     i, len;

	if (!(unzfd = unzOpen(DATFILE)))
		errx(1, "unzOpen %s", DATFILE);
	unzGetGlobalInfo(unzfd, &gi);
	for (i = 0; i < gi.number_entry; i++) {
		unzGetCurrentFileInfo(unzfd, &fi, FileName, sizeof(FileName),
				      NULL, 0, NULL, 0);
		unzOpenCurrentFile(unzfd);
		if (!(fd = fopen(FileName, "wb")))
			err(1, "fopen %s", FileName);
		while ((len = unzReadCurrentFile(unzfd, &buf, 1024))) {
			fwrite(buf, len, 1, fd);
		}
		fclose(fd);
		unzGoToNextFile(unzfd);
	}
	unzClose(unzfd);
	unlink(DATFILE);
	return;
}