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