2012-09-22 04:14:37 +01:00
|
|
|
#include <afsconfig.h>
|
|
|
|
#include <afs/param.h>
|
2012-10-10 14:42:21 +01:00
|
|
|
|
|
|
|
#include <roken.h>
|
|
|
|
|
2012-09-22 04:14:37 +01:00
|
|
|
#include <afs/cellconfig.h>
|
|
|
|
#include <rx/rx.h>
|
|
|
|
|
|
|
|
#include <tests/tap/basic.h>
|
|
|
|
#include "common.h"
|
|
|
|
|
2021-04-24 22:08:38 +01:00
|
|
|
static afs_uint32
|
|
|
|
gethostaddr(void)
|
|
|
|
{
|
|
|
|
char hostname[256];
|
|
|
|
struct hostent *host;
|
|
|
|
static afs_uint32 addr = 0;
|
|
|
|
|
|
|
|
if (addr != 0) {
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(hostname, 0, sizeof(hostname));
|
|
|
|
|
|
|
|
gethostname(hostname, sizeof(hostname));
|
|
|
|
host = gethostbyname(hostname);
|
|
|
|
if (host == NULL) {
|
|
|
|
diag("gethostbyname(%s) error: %d", hostname, h_errno);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&addr, host->h_addr, sizeof(addr));
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2012-10-10 14:45:03 +01:00
|
|
|
/*!
|
|
|
|
* Check if the current machine's hostname resolves to the loopback
|
2012-09-22 04:14:37 +01:00
|
|
|
* network.
|
|
|
|
*/
|
|
|
|
int
|
2012-10-10 14:45:03 +01:00
|
|
|
afstest_IsLoopbackNetworkDefault(void)
|
|
|
|
{
|
2021-04-24 22:08:38 +01:00
|
|
|
afs_uint32 addr = gethostaddr();
|
|
|
|
if (addr == 0) {
|
|
|
|
skip_all("Can't resolve hostname");
|
2013-02-17 18:29:38 +00:00
|
|
|
}
|
2012-09-22 04:14:37 +01:00
|
|
|
|
2012-10-10 14:45:03 +01:00
|
|
|
return(rx_IsLoopbackAddr(ntohl(addr)));
|
2012-09-22 04:14:37 +01:00
|
|
|
}
|
|
|
|
|
2012-10-10 14:45:03 +01:00
|
|
|
/*!
|
|
|
|
* Skips all TAP tests if the current machine's hostname resolves to the
|
2012-09-22 04:14:37 +01:00
|
|
|
* loopback network.
|
|
|
|
*/
|
|
|
|
int
|
2012-10-10 14:45:03 +01:00
|
|
|
afstest_SkipTestsIfLoopbackNetIsDefault(void)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = afstest_IsLoopbackNetworkDefault();
|
|
|
|
if (retval == 1) {
|
|
|
|
skip_all("Default IP address is on the loopback network!\n");
|
|
|
|
}
|
|
|
|
return retval;
|
2012-09-22 04:14:37 +01:00
|
|
|
}
|
2013-02-17 18:29:38 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Skips all TAP tests if the current machine's hostname can't be resolved
|
|
|
|
* to any IP address.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
afstest_SkipTestsIfBadHostname(void)
|
|
|
|
{
|
2021-04-24 22:08:38 +01:00
|
|
|
if (gethostaddr() == 0)
|
|
|
|
skip_all("Can't resolve hostname");
|
2013-02-17 18:29:38 +00:00
|
|
|
}
|
2020-01-10 15:54:20 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Skips all TAP tests if a server is already running on this system.
|
|
|
|
*
|
|
|
|
* \param name[in] IANA service name, e.g. "afs3-vlserver"
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
afstest_SkipTestsIfServerRunning(char *name)
|
|
|
|
{
|
|
|
|
afs_int32 code;
|
|
|
|
osi_socket sock;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
afs_int32 service;
|
|
|
|
|
|
|
|
service = afsconf_FindService(name);
|
|
|
|
if (service == -1) {
|
|
|
|
fprintf(stderr, "Unknown service name: %s\n", name);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
if (sock == OSI_NULLSOCKET) {
|
|
|
|
fprintf(stderr, "Failed to get socket file descriptor.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
addr.sin_port = service; /* Already in network byte order. */
|
|
|
|
#ifdef STRUCT_SOCKADDR_HAS_SA_LEN
|
|
|
|
addr.sin_len = sizeof(addr);
|
|
|
|
#endif
|
|
|
|
code = bind(sock, (struct sockaddr *)&addr, sizeof(addr));
|
|
|
|
if (code < 0) {
|
|
|
|
if (errno == EADDRINUSE) {
|
|
|
|
skip_all("Service %s is already running.\n", name);
|
|
|
|
} else {
|
|
|
|
perror("bind");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(sock);
|
|
|
|
}
|
2021-04-24 22:08:38 +01:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* Get the IP address of the local machine, for use with mock CellServDBs, etc.
|
|
|
|
*
|
|
|
|
* @return ipv4 address in net-byte order
|
|
|
|
*/
|
|
|
|
afs_uint32
|
|
|
|
afstest_MyHostAddr(void)
|
|
|
|
{
|
|
|
|
afs_uint32 addr = gethostaddr();
|
|
|
|
if (addr == 0) {
|
|
|
|
bail("cannot resolve hostname");
|
|
|
|
}
|
|
|
|
return addr;
|
|
|
|
}
|