]> git.saurik.com Git - apple/xnu.git/blob - tools/tests/libMicro/getpeername.c
xnu-3248.60.10.tar.gz
[apple/xnu.git] / tools / tests / libMicro / getpeername.c
1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms
5 * of the Common Development and Distribution License
6 * (the "License"). You may not use this file except
7 * in compliance with the License.
8 *
9 * You can obtain a copy of the license at
10 * src/OPENSOLARIS.LICENSE
11 * or http://www.opensolaris.org/os/licensing.
12 * See the License for the specific language governing
13 * permissions and limitations under the License.
14 *
15 * When distributing Covered Code, include this CDDL
16 * HEADER in each file and include the License file at
17 * usr/src/OPENSOLARIS.LICENSE. If applicable,
18 * add the following below this CDDL HEADER, with the
19 * fields enclosed by brackets "[]" replaced with your
20 * own identifying information: Portions Copyright [yyyy]
21 * [name of copyright owner]
22 *
23 * CDDL HEADER END
24 */
25
26 /*
27 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
29 */
30
31 /*
32 * getpeername test
33 */
34
35
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <netinet/in.h>
39 #include <netinet/tcp.h>
40 #include <arpa/inet.h>
41 #include <netdb.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <fcntl.h>
47 #include <errno.h>
48
49 #include "libmicro.h"
50
51 #define FIRSTPORT 12345
52
53 static int sock = -1;
54
55 int
56 benchmark_init()
57 {
58 (void) sprintf(lm_usage, "notes: measures getpeername()\n");
59 lm_tsdsize = 0;
60
61 return (0);
62 }
63
64 int
65 benchmark_initrun()
66 {
67 int j;
68 int opt = 1;
69 int result;
70 socklen_t size;
71 struct hostent *host;
72 struct sockaddr_in adds;
73 int sock2, sock3;
74
75 sock2 = socket(AF_INET, SOCK_STREAM, 0);
76 if (sock2 == -1) {
77 perror("socket");
78 exit(1);
79 }
80
81 if (setsockopt(sock2, SOL_SOCKET, SO_REUSEADDR,
82 &opt, sizeof (int)) == -1) {
83 perror("setsockopt");
84 exit(1);
85 }
86
87 if ((host = gethostbyname("localhost")) == NULL) {
88 perror("gethostbyname");
89 exit(1);
90 }
91
92 j = FIRSTPORT;
93 for (;;) {
94 (void) memset(&adds, 0, sizeof (struct sockaddr_in));
95 adds.sin_family = AF_INET;
96 adds.sin_port = htons(j++);
97 (void) memcpy(&adds.sin_addr.s_addr, host->h_addr_list[0],
98 sizeof (struct in_addr));
99
100 if (bind(sock2, (struct sockaddr *)&adds,
101 sizeof (struct sockaddr_in)) == 0) {
102 break;
103 }
104
105 if (errno != EADDRINUSE) {
106 perror("bind");
107 exit(1);
108 }
109 }
110
111 if (listen(sock2, 5) == -1) {
112 perror("listen");
113 exit(1);
114 }
115
116 sock3 = socket(AF_INET, SOCK_STREAM, 0);
117 if (sock3 == -1) {
118 perror("socket");
119 exit(1);
120 }
121
122 if (fcntl(sock3, F_SETFL, O_NDELAY) == -1) {
123 perror("fcntl");
124 exit(1);
125 }
126
127 result = connect(sock3, (struct sockaddr *)&adds,
128 sizeof (struct sockaddr_in));
129 if ((result == -1) && (errno != EINPROGRESS)) {
130 perror("connect");
131 exit(1);
132 }
133
134 size = sizeof (struct sockaddr);
135 sock = accept(sock2, (struct sockaddr *)&adds, &size);
136 if (sock == -1) {
137 perror("accept");
138 exit(1);
139 }
140
141 return (0);
142 }
143
144 /*ARGSUSED*/
145 int
146 benchmark(void *tsd, result_t *res)
147 {
148 int i;
149 struct sockaddr_in adds;
150 socklen_t size;
151
152 for (i = 0; i < lm_optB; i++) {
153 size = sizeof (struct sockaddr_in);
154 if (getpeername(sock, (struct sockaddr *)&adds, &size) == -1) {
155 perror("getpeername");
156 exit(1);
157 res->re_errors++;
158 }
159 }
160 res->re_count = i;
161
162 return (0);
163 }