1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup nettest
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /** @file
|
---|
34 | * Networking test 3.
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <async.h>
|
---|
38 | #include <stdio.h>
|
---|
39 | #include <str.h>
|
---|
40 |
|
---|
41 | #include <inet/dnsr.h>
|
---|
42 | #include <net/in.h>
|
---|
43 | #include <net/in6.h>
|
---|
44 | #include <net/inet.h>
|
---|
45 | #include <net/socket.h>
|
---|
46 |
|
---|
47 | #define BUF_SIZE 32
|
---|
48 |
|
---|
49 | static char *data;
|
---|
50 | static size_t size;
|
---|
51 |
|
---|
52 | static char buf[BUF_SIZE];
|
---|
53 |
|
---|
54 | static struct sockaddr_in addr;
|
---|
55 |
|
---|
56 | static uint16_t port;
|
---|
57 |
|
---|
58 | int main(int argc, char *argv[])
|
---|
59 | {
|
---|
60 | int rc;
|
---|
61 | int fd;
|
---|
62 | char *endptr;
|
---|
63 | dnsr_hostinfo_t *hinfo;
|
---|
64 |
|
---|
65 | port = 7;
|
---|
66 |
|
---|
67 | data = (char *) "Hello World!";
|
---|
68 | size = str_size(data);
|
---|
69 |
|
---|
70 | /* Connect to local IP address by default */
|
---|
71 | addr.sin_family = AF_INET;
|
---|
72 | addr.sin_port = htons(port);
|
---|
73 | addr.sin_addr.s_addr = htonl(0x7f000001);
|
---|
74 |
|
---|
75 | if (argc >= 2) {
|
---|
76 | printf("parsing address '%s'\n", argv[1]);
|
---|
77 | rc = inet_pton(AF_INET, argv[1], (uint8_t *)&addr.sin_addr.s_addr);
|
---|
78 | if (rc != EOK) {
|
---|
79 | /* Try interpreting as a host name */
|
---|
80 | rc = dnsr_name2host(argv[1], &hinfo, AF_INET);
|
---|
81 | if (rc != EOK) {
|
---|
82 | printf("Error resolving host '%s'.\n", argv[1]);
|
---|
83 | return rc;
|
---|
84 | }
|
---|
85 |
|
---|
86 | uint16_t af = inet_addr_sockaddr_in(&hinfo->addr, &addr, NULL);
|
---|
87 | if (af != AF_INET) {
|
---|
88 | printf("Host '%s' not resolved as IPv4 address.\n", argv[1]);
|
---|
89 | return rc;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | printf("result: rc=%d, family=%d, addr=%x\n", rc,
|
---|
93 | addr.sin_family, addr.sin_addr.s_addr);
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (argc >= 3) {
|
---|
97 | printf("parsing port '%s'\n", argv[2]);
|
---|
98 | addr.sin_port = htons(strtoul(argv[2], &endptr, 10));
|
---|
99 | if (*endptr != '\0') {
|
---|
100 | fprintf(stderr, "Error parsing port\n");
|
---|
101 | return 1;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | printf("socket()\n");
|
---|
106 | fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
107 | printf(" -> %d\n", fd);
|
---|
108 | if (fd < 0)
|
---|
109 | return 1;
|
---|
110 |
|
---|
111 | printf("connect()\n");
|
---|
112 | rc = connect(fd, (struct sockaddr *) &addr, sizeof(addr));
|
---|
113 | printf(" -> %d\n", rc);
|
---|
114 | if (rc != 0)
|
---|
115 | return 1;
|
---|
116 |
|
---|
117 | printf("send()\n");
|
---|
118 | rc = send(fd, data, size, 0);
|
---|
119 | printf(" -> %d\n", rc);
|
---|
120 | if (rc < 0)
|
---|
121 | return 1;
|
---|
122 |
|
---|
123 | do {
|
---|
124 | printf("recv()\n");
|
---|
125 | rc = recv(fd, buf, BUF_SIZE, 0);
|
---|
126 | printf(" -> %d\n", rc);
|
---|
127 | } while (rc > 0);
|
---|
128 |
|
---|
129 | async_usleep(1000 * 1000);
|
---|
130 |
|
---|
131 | printf("closesocket()\n");
|
---|
132 | rc = closesocket(fd);
|
---|
133 | printf(" -> %d\n", rc);
|
---|
134 |
|
---|
135 | return 0;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /** @}
|
---|
139 | */
|
---|