1 /* $NetBSD: h_netget.c,v 1.2 2012/04/17 09:23:21 jruoho Exp $ */
4 * Copyright (c) 2011 Antti Kantee. All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * simple utility to fetch a webpage. we wouldn't need this
30 * if we had something like netcat in base
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: h_netget.c,v 1.2 2012/04/17 09:23:21 jruoho Exp $");
36 #include <sys/types.h>
37 #include <sys/socket.h>
39 #include <arpa/inet.h>
41 #include <netinet/in.h>
50 #define GETSTR "GET / HTTP/1.0\n\n"
53 main(int argc, char *argv[])
56 struct sockaddr_in sin;
62 fprintf(stderr, "usage: %s address port savefile\n",
67 s = socket(PF_INET, SOCK_STREAM, 0);
69 err(EXIT_FAILURE, "socket");
71 memset(&sin, 0, sizeof(sin));
72 sin.sin_len = sizeof(sin);
73 sin.sin_family = AF_INET;
74 sin.sin_port = htons(atoi(argv[2]));
75 sin.sin_addr.s_addr = inet_addr(argv[1]);
77 fd = open(argv[3], O_CREAT | O_RDWR, 0644);
79 err(EXIT_FAILURE, "open");
80 if (ftruncate(fd, 0) == -1)
81 err(EXIT_FAILURE, "ftruncate savefile");
83 if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
84 err(EXIT_FAILURE, "connect");
86 if (write(s, GETSTR, strlen(GETSTR)) != strlen(GETSTR))
87 err(EXIT_FAILURE, "socket write");
90 n = read(s, buf, sizeof(buf));
94 err(EXIT_FAILURE, "socket read");
96 if (write(fd, buf, n) != n)
97 err(EXIT_FAILURE, "write file");