source: mainline/uspace/app/nettest3/nettest3.c@ 10ef47ba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 10ef47ba was 26de91a, checked in by Jiri Svoboda <jiri@…>, 12 years ago

IPv4 and v6 should not need separate handling by a simple client that is just connecting to a host/address. Add IPv6/DNS support in applications where missing.

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[92e717c]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>
[26de91a]39#include <stdlib.h>
[92e717c]40#include <str.h>
41
[287d729]42#include <inet/dnsr.h>
[92e717c]43#include <net/in.h>
44#include <net/in6.h>
45#include <net/inet.h>
46#include <net/socket.h>
47
48#define BUF_SIZE 32
49
50static char *data;
51static size_t size;
52
53static char buf[BUF_SIZE];
54
[26de91a]55static struct sockaddr *address;
56static socklen_t addrlen;
[92e717c]57
58static uint16_t port;
59
60int main(int argc, char *argv[])
61{
62 int rc;
63 int fd;
[453f9e33]64 char *endptr;
[287d729]65 dnsr_hostinfo_t *hinfo;
[26de91a]66 inet_addr_t addr;
67 char *addr_s;
[92e717c]68
[453f9e33]69 port = 7;
[92e717c]70
[af076b4]71 data = (char *) "Hello World!";
[92e717c]72 size = str_size(data);
73
[453f9e33]74 /* Connect to local IP address by default */
[26de91a]75 inet_addr(&addr, 127, 0, 0, 1);
[92e717c]76
[453f9e33]77 if (argc >= 2) {
78 printf("parsing address '%s'\n", argv[1]);
[26de91a]79 rc = inet_addr_parse(argv[1], &addr);
[453f9e33]80 if (rc != EOK) {
[287d729]81 /* Try interpreting as a host name */
[e948fde]82 rc = dnsr_name2host(argv[1], &hinfo, ip_v4);
[287d729]83 if (rc != EOK) {
84 printf("Error resolving host '%s'.\n", argv[1]);
85 return rc;
86 }
[e948fde]87
[26de91a]88 addr = hinfo->addr;
89 }
90 rc = inet_addr_format(&addr, &addr_s);
91 if (rc != EOK) {
92 assert(rc == ENOMEM);
93 printf("Out of memory.\n");
94 return rc;
[453f9e33]95 }
[26de91a]96 printf("result: rc=%d, ver=%d, addr=%s\n", rc,
97 addr.version, addr_s);
98 free(addr_s);
[453f9e33]99 }
100
101 if (argc >= 3) {
102 printf("parsing port '%s'\n", argv[2]);
[26de91a]103 port = htons(strtoul(argv[2], &endptr, 10));
[453f9e33]104 if (*endptr != '\0') {
105 fprintf(stderr, "Error parsing port\n");
106 return 1;
107 }
108 }
109
[26de91a]110 rc = inet_addr_sockaddr(&hinfo->addr, port, &address, &addrlen);
111 if (rc != EOK) {
112 printf("Out of memory.\n");
113 return rc;
114 }
115
[92e717c]116 printf("socket()\n");
[26de91a]117 fd = socket(address->sa_family, SOCK_STREAM, IPPROTO_TCP);
[92e717c]118 printf(" -> %d\n", fd);
119 if (fd < 0)
120 return 1;
121
122 printf("connect()\n");
[26de91a]123 rc = connect(fd, address, addrlen);
[92e717c]124 printf(" -> %d\n", rc);
125 if (rc != 0)
126 return 1;
127
128 printf("send()\n");
129 rc = send(fd, data, size, 0);
130 printf(" -> %d\n", rc);
131 if (rc < 0)
132 return 1;
133
134 do {
135 printf("recv()\n");
136 rc = recv(fd, buf, BUF_SIZE, 0);
137 printf(" -> %d\n", rc);
138 } while (rc > 0);
139
[af076b4]140 async_usleep(1000 * 1000);
[92e717c]141
142 printf("closesocket()\n");
143 rc = closesocket(fd);
144 printf(" -> %d\n", rc);
145
[26de91a]146 free(address);
147
[92e717c]148 return 0;
149}
150
151/** @}
152 */
Note: See TracBrowser for help on using the repository browser.