source: mainline/uspace/app/nettest3/nettest3.c@ b49d872

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

Make dnsr client initialization implicit (to allow for multiple consumers in the same program).

  • Property mode set to 100644
File size: 3.4 KB
Line 
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
49static char *data;
50static size_t size;
51
52static char buf[BUF_SIZE];
53
54static struct sockaddr_in addr;
55
56static uint16_t port;
57
58int 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);
81 if (rc != EOK) {
82 printf("Error resolving host '%s'.\n", argv[1]);
83 return rc;
84 }
85
86 addr.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4);
87 addr.sin_family = AF_INET;
88 }
89 printf("result: rc=%d, family=%d, addr=%x\n", rc,
90 addr.sin_family, addr.sin_addr.s_addr);
91 }
92
93 if (argc >= 3) {
94 printf("parsing port '%s'\n", argv[2]);
95 addr.sin_port = htons(strtoul(argv[2], &endptr, 10));
96 if (*endptr != '\0') {
97 fprintf(stderr, "Error parsing port\n");
98 return 1;
99 }
100 }
101
102 printf("socket()\n");
103 fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
104 printf(" -> %d\n", fd);
105 if (fd < 0)
106 return 1;
107
108 printf("connect()\n");
109 rc = connect(fd, (struct sockaddr *) &addr, sizeof(addr));
110 printf(" -> %d\n", rc);
111 if (rc != 0)
112 return 1;
113
114 printf("send()\n");
115 rc = send(fd, data, size, 0);
116 printf(" -> %d\n", rc);
117 if (rc < 0)
118 return 1;
119
120 do {
121 printf("recv()\n");
122 rc = recv(fd, buf, BUF_SIZE, 0);
123 printf(" -> %d\n", rc);
124 } while (rc > 0);
125
126 async_usleep(1000 * 1000);
127
128 printf("closesocket()\n");
129 rc = closesocket(fd);
130 printf(" -> %d\n", rc);
131
132 return 0;
133}
134
135/** @}
136 */
Note: See TracBrowser for help on using the repository browser.