xref: /freebsd/contrib/netbsd-tests/lib/librumphijack/h_netget.c (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*      $NetBSD: h_netget.c,v 1.2 2012/04/17 09:23:21 jruoho Exp $	*/
2 
3 /*-
4  * Copyright (c) 2011 Antti Kantee.  All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * simple utility to fetch a webpage.  we wouldn't need this
30  * if we had something like netcat in base
31  */
32 
33 #include <sys/cdefs.h>
34 __RCSID("$NetBSD: h_netget.c,v 1.2 2012/04/17 09:23:21 jruoho Exp $");
35 
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 
39 #include <arpa/inet.h>
40 
41 #include <netinet/in.h>
42 
43 #include <err.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 
50 #define GETSTR "GET / HTTP/1.0\n\n"
51 
52 int
53 main(int argc, char *argv[])
54 {
55 	char buf[8192];
56 	struct sockaddr_in sin;
57 	ssize_t n;
58 	int s, fd;
59 
60 	setprogname(argv[0]);
61 	if (argc != 4) {
62 		fprintf(stderr, "usage: %s address port savefile\n",
63 		    getprogname());
64 		return EXIT_FAILURE;
65 	}
66 
67 	s = socket(PF_INET, SOCK_STREAM, 0);
68 	if (s == -1)
69 		err(EXIT_FAILURE, "socket");
70 
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]);
76 
77 	fd = open(argv[3], O_CREAT | O_RDWR, 0644);
78 	if (fd == -1)
79 		err(EXIT_FAILURE, "open");
80 	if (ftruncate(fd, 0) == -1)
81 		err(EXIT_FAILURE, "ftruncate savefile");
82 
83 	if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) == -1)
84 		err(EXIT_FAILURE, "connect");
85 
86 	if (write(s, GETSTR, strlen(GETSTR)) != strlen(GETSTR))
87 		err(EXIT_FAILURE, "socket write");
88 
89 	for (;;) {
90 		n = read(s, buf, sizeof(buf));
91 		if (n == 0)
92 			break;
93 		if (n == -1)
94 			err(EXIT_FAILURE, "socket read");
95 
96 		if (write(fd, buf, n) != n)
97 			err(EXIT_FAILURE, "write file");
98 	}
99 
100 	return EXIT_SUCCESS;
101 }
102