2011-04-20 20:33:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2009-2011, The Regents of the University of California,
|
|
|
|
* through Lawrence Berkeley National Laboratory (subject to receipt of any
|
|
|
|
* required approvals from the U.S. Dept. of Energy). All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is distributed under a BSD style license, see the LICENSE file
|
|
|
|
* for complete information.
|
|
|
|
*/
|
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
/* iperf_util.c
|
|
|
|
*
|
|
|
|
* Iperf utility functions
|
|
|
|
*
|
|
|
|
*/
|
2009-12-10 14:20:48 +00:00
|
|
|
|
2009-07-23 18:22:24 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2010-07-22 18:57:08 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
/* XXX: this code is not portable: not all versions of linux install libuuidgen
|
|
|
|
* by default
|
|
|
|
*
|
|
|
|
* if not installed, may need to do something like this:
|
|
|
|
* yum install libuuid-devel
|
|
|
|
* apt-get install uuid-dev
|
|
|
|
*/
|
2009-12-10 14:20:48 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_UUID_H)
|
|
|
|
#warning DOING SOMETHING
|
2009-07-23 18:22:24 +00:00
|
|
|
#include <uuid.h>
|
2009-12-10 14:20:48 +00:00
|
|
|
#elif defined(HAVE_UUID_UUID_H)
|
2009-07-23 18:22:24 +00:00
|
|
|
#include <uuid/uuid.h>
|
2009-12-10 14:20:48 +00:00
|
|
|
#else
|
|
|
|
#error No uuid header file specified
|
2009-07-23 18:22:24 +00:00
|
|
|
#endif
|
|
|
|
|
2009-12-10 14:20:48 +00:00
|
|
|
|
2009-10-24 20:43:06 +00:00
|
|
|
|
2010-07-22 18:57:08 +00:00
|
|
|
/* get_uuid
|
|
|
|
*
|
|
|
|
* Generate and return a UUID string
|
|
|
|
*
|
|
|
|
* Iperf uses this function to create test "cookies" which
|
|
|
|
* server as unique test identifiers. These cookies are also
|
|
|
|
* used for the authentication of stream connections.
|
|
|
|
*/
|
2009-10-24 20:43:06 +00:00
|
|
|
|
2009-07-24 18:21:50 +00:00
|
|
|
void
|
|
|
|
get_uuid(char *temp)
|
2009-07-23 18:22:24 +00:00
|
|
|
{
|
2009-10-19 22:35:18 +00:00
|
|
|
char *s;
|
|
|
|
uuid_t uu;
|
2009-07-23 18:22:24 +00:00
|
|
|
|
2009-12-10 14:20:48 +00:00
|
|
|
#if defined(HAVE_UUID_CREATE)
|
2009-07-23 18:22:24 +00:00
|
|
|
uuid_create(&uu, NULL);
|
|
|
|
uuid_to_string(&uu, &s, 0);
|
2009-12-10 14:20:48 +00:00
|
|
|
#elif defined(HAVE_UUID_GENERATE)
|
2009-11-06 01:43:50 +00:00
|
|
|
s = (char *) malloc(37);
|
2009-07-23 18:22:24 +00:00
|
|
|
uuid_generate(uu);
|
2009-10-19 22:35:18 +00:00
|
|
|
uuid_unparse(uu, s);
|
2009-12-10 14:20:48 +00:00
|
|
|
#else
|
|
|
|
#error No uuid function specified
|
2009-07-23 18:22:24 +00:00
|
|
|
#endif
|
2009-10-19 22:35:18 +00:00
|
|
|
memcpy(temp, s, 37);
|
2010-06-30 15:58:16 +00:00
|
|
|
|
|
|
|
// XXX: Freeing s only works if you HAVE_UUID_GENERATE
|
|
|
|
// Otherwise use rpc_string_free (?)
|
|
|
|
free(s);
|
2009-07-23 18:22:24 +00:00
|
|
|
}
|
2010-07-22 18:57:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* is_closed
|
|
|
|
*
|
|
|
|
* Test if the file descriptor fd is closed.
|
|
|
|
*
|
|
|
|
* Iperf uses this function to test whether a TCP stream socket
|
|
|
|
* is closed, because accepting and denying an invalid connection
|
|
|
|
* in iperf_tcp_accept is not considered an error.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
is_closed(int fd)
|
|
|
|
{
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set readset;
|
|
|
|
|
|
|
|
FD_ZERO(&readset);
|
|
|
|
FD_SET(fd, &readset);
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
if (select(fd+1, &readset, NULL, NULL, &tv) < 0) {
|
|
|
|
if (errno == EBADF)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|