1*f95573c2Sjruoho /* $NetBSD: h_netget.c,v 1.2 2012/04/17 09:23:21 jruoho Exp $ */
2fbc821a0Spooka
3fbc821a0Spooka /*-
4fbc821a0Spooka * Copyright (c) 2011 Antti Kantee. All Rights Reserved.
5fbc821a0Spooka *
6fbc821a0Spooka * Redistribution and use in source and binary forms, with or without
7fbc821a0Spooka * modification, are permitted provided that the following conditions
8fbc821a0Spooka * are met:
9fbc821a0Spooka * 1. Redistributions of source code must retain the above copyright
10fbc821a0Spooka * notice, this list of conditions and the following disclaimer.
11fbc821a0Spooka * 2. Redistributions in binary form must reproduce the above copyright
12fbc821a0Spooka * notice, this list of conditions and the following disclaimer in the
13fbc821a0Spooka * documentation and/or other materials provided with the distribution.
14fbc821a0Spooka *
15fbc821a0Spooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16fbc821a0Spooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17fbc821a0Spooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18fbc821a0Spooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19fbc821a0Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20fbc821a0Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21fbc821a0Spooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22fbc821a0Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23fbc821a0Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24fbc821a0Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25fbc821a0Spooka * SUCH DAMAGE.
26fbc821a0Spooka */
27fbc821a0Spooka
28fbc821a0Spooka /*
29fbc821a0Spooka * simple utility to fetch a webpage. we wouldn't need this
30fbc821a0Spooka * if we had something like netcat in base
31fbc821a0Spooka */
32fbc821a0Spooka
33fbc821a0Spooka #include <sys/cdefs.h>
34*f95573c2Sjruoho __RCSID("$NetBSD: h_netget.c,v 1.2 2012/04/17 09:23:21 jruoho Exp $");
35fbc821a0Spooka
36fbc821a0Spooka #include <sys/types.h>
37fbc821a0Spooka #include <sys/socket.h>
38fbc821a0Spooka
39fbc821a0Spooka #include <arpa/inet.h>
40fbc821a0Spooka
41fbc821a0Spooka #include <netinet/in.h>
42fbc821a0Spooka
43fbc821a0Spooka #include <err.h>
44fbc821a0Spooka #include <fcntl.h>
45fbc821a0Spooka #include <stdio.h>
46fbc821a0Spooka #include <stdlib.h>
47fbc821a0Spooka #include <string.h>
48fbc821a0Spooka #include <unistd.h>
49fbc821a0Spooka
50fbc821a0Spooka #define GETSTR "GET / HTTP/1.0\n\n"
51fbc821a0Spooka
52fbc821a0Spooka int
main(int argc,char * argv[])53fbc821a0Spooka main(int argc, char *argv[])
54fbc821a0Spooka {
55fbc821a0Spooka char buf[8192];
56fbc821a0Spooka struct sockaddr_in sin;
57fbc821a0Spooka ssize_t n;
58fbc821a0Spooka int s, fd;
59fbc821a0Spooka
60fbc821a0Spooka setprogname(argv[0]);
61fbc821a0Spooka if (argc != 4) {
62fbc821a0Spooka fprintf(stderr, "usage: %s address port savefile\n",
63fbc821a0Spooka getprogname());
64*f95573c2Sjruoho return EXIT_FAILURE;
65fbc821a0Spooka }
66fbc821a0Spooka
67fbc821a0Spooka s = socket(PF_INET, SOCK_STREAM, 0);
68fbc821a0Spooka if (s == -1)
69*f95573c2Sjruoho err(EXIT_FAILURE, "socket");
70fbc821a0Spooka
71fbc821a0Spooka memset(&sin, 0, sizeof(sin));
72fbc821a0Spooka sin.sin_len = sizeof(sin);
73fbc821a0Spooka sin.sin_family = AF_INET;
74fbc821a0Spooka sin.sin_port = htons(atoi(argv[2]));
75fbc821a0Spooka sin.sin_addr.s_addr = inet_addr(argv[1]);
76fbc821a0Spooka
77fbc821a0Spooka fd = open(argv[3], O_CREAT | O_RDWR, 0644);
78fbc821a0Spooka if (fd == -1)
79*f95573c2Sjruoho err(EXIT_FAILURE, "open");
80fbc821a0Spooka if (ftruncate(fd, 0) == -1)
81*f95573c2Sjruoho err(EXIT_FAILURE, "ftruncate savefile");
82fbc821a0Spooka
83fbc821a0Spooka if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
84*f95573c2Sjruoho err(EXIT_FAILURE, "connect");
85fbc821a0Spooka
86fbc821a0Spooka if (write(s, GETSTR, strlen(GETSTR)) != strlen(GETSTR))
87*f95573c2Sjruoho err(EXIT_FAILURE, "socket write");
88fbc821a0Spooka
89fbc821a0Spooka for (;;) {
90fbc821a0Spooka n = read(s, buf, sizeof(buf));
91fbc821a0Spooka if (n == 0)
92fbc821a0Spooka break;
93fbc821a0Spooka if (n == -1)
94*f95573c2Sjruoho err(EXIT_FAILURE, "socket read");
95fbc821a0Spooka
96fbc821a0Spooka if (write(fd, buf, n) != n)
97*f95573c2Sjruoho err(EXIT_FAILURE, "write file");
98fbc821a0Spooka }
99fbc821a0Spooka
100*f95573c2Sjruoho return EXIT_SUCCESS;
101fbc821a0Spooka }
102