Home | History | Annotate | Download | only in libmicro
      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  * getsockname
     33  */
     34 
     35 #include <sys/types.h>
     36 #include <sys/socket.h>
     37 #include <netinet/in.h>
     38 #include <netinet/tcp.h>
     39 #include <arpa/inet.h>
     40 #include <netdb.h>
     41 #include <string.h>
     42 #include <unistd.h>
     43 #include <stdlib.h>
     44 #include <stdio.h>
     45 #include <fcntl.h>
     46 #include <errno.h>
     47 
     48 #include "libmicro.h"
     49 
     50 #define	FIRSTPORT		12345
     51 
     52 static struct sockaddr_in	adds;
     53 static int			sock = -1;
     54 
     55 int
     56 benchmark_init()
     57 {
     58 	(void) sprintf(lm_usage, "notes: measures getsockname()()\n");
     59 	lm_tsdsize = 0;
     60 	return (0);
     61 }
     62 
     63 int
     64 benchmark_initrun()
     65 {
     66 	int			j;
     67 	int			opt = 1;
     68 	struct hostent	*host;
     69 
     70 	sock = socket(AF_INET, SOCK_STREAM, 0);
     71 	if (sock == -1) {
     72 		perror("socket");
     73 		exit(1);
     74 	}
     75 
     76 	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
     77 	    &opt, sizeof (int)) == -1) {
     78 		perror("setsockopt");
     79 		exit(1);
     80 	}
     81 
     82 	if ((host = gethostbyname("localhost")) == NULL) {
     83 		perror("gethostbyname");
     84 		exit(1);
     85 	}
     86 
     87 	j = FIRSTPORT;
     88 	for (;;) {
     89 		(void) memset(&adds, 0, sizeof (struct sockaddr_in));
     90 		adds.sin_family = AF_INET;
     91 		adds.sin_port = htons(j++);
     92 		(void) memcpy(&adds.sin_addr.s_addr, host->h_addr_list[0],
     93 		    sizeof (struct in_addr));
     94 
     95 		if (bind(sock, (struct sockaddr *)&adds,
     96 		    sizeof (struct sockaddr_in)) == 0) {
     97 			break;
     98 		}
     99 
    100 		if (errno != EADDRINUSE) {
    101 			perror("bind");
    102 			exit(1);
    103 		}
    104 	}
    105 
    106 	return (0);
    107 }
    108 
    109 /*ARGSUSED*/
    110 int
    111 benchmark(void *tsd, result_t *res)
    112 {
    113 	int			i;
    114 	struct sockaddr_in	adds;
    115 	socklen_t		size;
    116 
    117 	for (i = 0; i < lm_optB; i++) {
    118 		size = sizeof (struct sockaddr_in);
    119 		if (getsockname(sock, (struct sockaddr *)&adds, &size) == -1)
    120 			res->re_errors++;
    121 	}
    122 	res->re_count = i;
    123 
    124 	return (0);
    125 }
    126