| 1 |
sysadm |
1.1 |
/*
|
| 2 |
|
|
* Copyright (C) 1999,2000 Ross Combs (rocombs@cs.nmsu.edu)
|
| 3 |
|
|
*
|
| 4 |
|
|
* This program is free software; you can redistribute it and/or
|
| 5 |
|
|
* modify it under the terms of the GNU General Public License
|
| 6 |
|
|
* as published by the Free Software Foundation; either version 2
|
| 7 |
|
|
* of the License, or (at your option) any later version.
|
| 8 |
|
|
*
|
| 9 |
|
|
* This program is distributed in the hope that it will be useful,
|
| 10 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
|
|
* GNU General Public License for more details.
|
| 13 |
|
|
*
|
| 14 |
|
|
* You should have received a copy of the GNU General Public License
|
| 15 |
|
|
* along with this program; if not, write to the Free Software
|
| 16 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 17 |
|
|
*/
|
| 18 |
|
|
#include "common/setup_before.h"
|
| 19 |
|
|
#include <stdio.h>
|
| 20 |
|
|
#ifdef HAVE_STDDEF_H
|
| 21 |
|
|
# include <stddef.h>
|
| 22 |
|
|
#else
|
| 23 |
|
|
# ifndef NULL
|
| 24 |
|
|
# define NULL ((void *)0)
|
| 25 |
|
|
# endif
|
| 26 |
|
|
#endif
|
| 27 |
|
|
#ifdef HAVE_STRING_H
|
| 28 |
|
|
# include <string.h>
|
| 29 |
|
|
#endif
|
| 30 |
|
|
#ifdef HAVE_MEMORY_H
|
| 31 |
|
|
# include <memory.h>
|
| 32 |
|
|
#endif
|
| 33 |
|
|
#include "compat/memset.h"
|
| 34 |
|
|
#include <errno.h>
|
| 35 |
|
|
#include "compat/strerror.h"
|
| 36 |
|
|
#ifdef TIME_WITH_SYS_TIME
|
| 37 |
|
|
# include <sys/time.h>
|
| 38 |
|
|
# include <time.h>
|
| 39 |
|
|
#else
|
| 40 |
|
|
# ifdef HAVE_SYS_TIME_H
|
| 41 |
|
|
# include <sys/time.h>
|
| 42 |
|
|
# else
|
| 43 |
|
|
# include <time.h>
|
| 44 |
|
|
# endif
|
| 45 |
|
|
#endif
|
| 46 |
|
|
#ifdef HAVE_UNISTD_H
|
| 47 |
|
|
# include <unistd.h>
|
| 48 |
|
|
#endif
|
| 49 |
|
|
#ifdef HAVE_FCNTL_H
|
| 50 |
|
|
# include <fcntl.h>
|
| 51 |
|
|
#else
|
| 52 |
|
|
# ifdef HAVE_SYS_FILE_H
|
| 53 |
|
|
# include <sys/file.h>
|
| 54 |
|
|
# endif
|
| 55 |
|
|
#endif
|
| 56 |
|
|
#ifdef HAVE_SYS_TYPES_H
|
| 57 |
|
|
# include <sys/types.h>
|
| 58 |
|
|
#endif
|
| 59 |
|
|
#ifdef HAVE_SYS_SOCKET_H
|
| 60 |
|
|
# include <sys/socket.h>
|
| 61 |
|
|
#endif
|
| 62 |
|
|
#include "compat/socket.h"
|
| 63 |
|
|
#include "compat/recv.h"
|
| 64 |
|
|
#include "compat/send.h"
|
| 65 |
|
|
#ifdef HAVE_SYS_PARAM_H
|
| 66 |
|
|
# include <sys/param.h>
|
| 67 |
|
|
#endif
|
| 68 |
|
|
#ifdef HAVE_NETINET_IN_H
|
| 69 |
|
|
# include <netinet/in.h>
|
| 70 |
|
|
#endif
|
| 71 |
|
|
#include "compat/netinet_in.h"
|
| 72 |
|
|
#include "compat/psock.h"
|
| 73 |
|
|
#include "common/packet.h"
|
| 74 |
|
|
#include "common/init_protocol.h"
|
| 75 |
|
|
#include "common/udp_protocol.h"
|
| 76 |
|
|
#include "common/tag.h"
|
| 77 |
|
|
#include "common/bn_type.h"
|
| 78 |
|
|
#include "common/field_sizes.h"
|
| 79 |
|
|
#include "common/network.h"
|
| 80 |
|
|
#include "client.h"
|
| 81 |
|
|
#include "udptest.h"
|
| 82 |
|
|
#include "common/setup_after.h"
|
| 83 |
|
|
|
| 84 |
|
|
|
| 85 |
|
|
#ifdef CLIENTDEBUG
|
| 86 |
|
|
#define dprintf printf
|
| 87 |
|
|
#else
|
| 88 |
|
|
#define dprintf if (0) printf
|
| 89 |
|
|
#endif
|
| 90 |
|
|
|
| 91 |
|
|
|
| 92 |
|
|
extern int client_udptest_setup(char const * progname, unsigned short * lsock_port_ret)
|
| 93 |
|
|
{
|
| 94 |
|
|
int lsock;
|
| 95 |
|
|
struct sockaddr_in laddr;
|
| 96 |
|
|
unsigned short lsock_port;
|
| 97 |
|
|
|
| 98 |
|
|
if (!progname)
|
| 99 |
|
|
{
|
| 100 |
|
|
fprintf(stderr,"got NULL progname\n");
|
| 101 |
|
|
return -1;
|
| 102 |
|
|
}
|
| 103 |
|
|
|
| 104 |
|
|
if ((lsock = psock_socket(PF_INET,SOCK_DGRAM,PSOCK_IPPROTO_UDP))<0)
|
| 105 |
|
|
{
|
| 106 |
|
|
fprintf(stderr,"%s: could not create UDP socket (psock_socket: %s)\n",progname,pstrerror(psock_errno()));
|
| 107 |
|
|
return -1;
|
| 108 |
|
|
}
|
| 109 |
|
|
|
| 110 |
|
|
if (psock_ctl(lsock,PSOCK_NONBLOCK)<0)
|
| 111 |
|
|
fprintf(stderr,"%s: could not set UDP socket to non-blocking mode (psock_ctl: %s)\n",progname,pstrerror(psock_errno()));
|
| 112 |
|
|
|
| 113 |
|
|
for (lsock_port=BNETD_MIN_TEST_PORT; lsock_port<=BNETD_MAX_TEST_PORT; lsock_port++)
|
| 114 |
|
|
{
|
| 115 |
|
|
memset(&laddr,0,sizeof(laddr));
|
| 116 |
|
|
laddr.sin_family = PSOCK_AF_INET;
|
| 117 |
|
|
laddr.sin_port = htons(lsock_port);
|
| 118 |
|
|
laddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
| 119 |
|
|
if (psock_bind(lsock,(struct sockaddr *)&laddr,(psock_t_socklen)sizeof(laddr))==0)
|
| 120 |
|
|
break;
|
| 121 |
|
|
|
| 122 |
|
|
if (lsock_port==BNETD_MIN_TEST_PORT)
|
| 123 |
|
|
dprintf("Could not bind to standard UDP port %hu, trying others. (psock_bind: %s)\n",BNETD_MIN_TEST_PORT,pstrerror(psock_errno()));
|
| 124 |
|
|
}
|
| 125 |
|
|
if (lsock_port>BNETD_MAX_TEST_PORT)
|
| 126 |
|
|
{
|
| 127 |
|
|
fprintf(stderr,"%s: could not bind to any UDP port %hu through %hu (psock_bind: %s)\n",progname,BNETD_MIN_TEST_PORT,BNETD_MAX_TEST_PORT,pstrerror(psock_errno()));
|
| 128 |
|
|
psock_close(lsock);
|
| 129 |
|
|
return -1;
|
| 130 |
|
|
}
|
| 131 |
|
|
|
| 132 |
|
|
if (lsock_port_ret)
|
| 133 |
|
|
*lsock_port_ret = lsock_port;
|
| 134 |
|
|
|
| 135 |
|
|
return lsock;
|
| 136 |
|
|
}
|
| 137 |
|
|
|
| 138 |
|
|
|
| 139 |
|
|
extern int client_udptest_recv(char const * progname, int lsock, unsigned short lsock_port, unsigned int timeout)
|
| 140 |
|
|
{
|
| 141 |
|
|
int len;
|
| 142 |
|
|
unsigned int count;
|
| 143 |
|
|
t_packet * rpacket;
|
| 144 |
|
|
time_t start;
|
| 145 |
|
|
|
| 146 |
|
|
if (!progname)
|
| 147 |
|
|
{
|
| 148 |
|
|
fprintf(stderr,"%s: got NULL progname\n",progname);
|
| 149 |
|
|
return -1;
|
| 150 |
|
|
}
|
| 151 |
|
|
|
| 152 |
|
|
if (!(rpacket = packet_create(packet_class_bnet)))
|
| 153 |
|
|
{
|
| 154 |
|
|
fprintf(stderr,"%s: could not create packet\n",progname);
|
| 155 |
|
|
return -1;
|
| 156 |
|
|
}
|
| 157 |
|
|
|
| 158 |
|
|
start = time(NULL);
|
| 159 |
|
|
count = 0;
|
| 160 |
|
|
while (start+(time_t)timeout>=time(NULL)) /* timeout after a few seconds from last packet */
|
| 161 |
|
|
{
|
| 162 |
|
|
if ((len = psock_recv(lsock,packet_get_raw_data_build(rpacket,0),MAX_PACKET_SIZE,0))<0)
|
| 163 |
|
|
{
|
| 164 |
|
|
if (psock_errno()!=PSOCK_EAGAIN && psock_errno()!=PSOCK_EWOULDBLOCK)
|
| 165 |
|
|
fprintf(stderr,"%s: failed to receive UDPTEST on port %hu (psock_recv: %s)\n",progname,lsock_port,pstrerror(psock_errno()));
|
| 166 |
|
|
continue;
|
| 167 |
|
|
}
|
| 168 |
|
|
packet_set_size(rpacket,len);
|
| 169 |
|
|
|
| 170 |
|
|
if (packet_get_type(rpacket)!=SERVER_UDPTEST)
|
| 171 |
|
|
{
|
| 172 |
|
|
dprintf("Got unexpected UDP packet type %u on port %hu\n",packet_get_type(rpacket),lsock_port);
|
| 173 |
|
|
continue;
|
| 174 |
|
|
}
|
| 175 |
|
|
|
| 176 |
|
|
if (bn_int_tag_eq(rpacket->u.server_udptest.bnettag,BNETTAG)<0)
|
| 177 |
|
|
{
|
| 178 |
|
|
fprintf(stderr,"%s: got bad UDPTEST packet on port %hu\n",progname,lsock_port);
|
| 179 |
|
|
continue;
|
| 180 |
|
|
}
|
| 181 |
|
|
|
| 182 |
|
|
count++;
|
| 183 |
|
|
if (count>=2)
|
| 184 |
|
|
break;
|
| 185 |
|
|
|
| 186 |
|
|
start = time(NULL);
|
| 187 |
|
|
}
|
| 188 |
|
|
|
| 189 |
|
|
packet_destroy(rpacket);
|
| 190 |
|
|
|
| 191 |
|
|
if (count<2)
|
| 192 |
|
|
{
|
| 193 |
|
|
printf("Only received %d UDP packets on port %hu. Connection may be slow or firewalled.\n",count,lsock_port);
|
| 194 |
|
|
return -1;
|
| 195 |
|
|
}
|
| 196 |
|
|
|
| 197 |
|
|
return 0;
|
| 198 |
|
|
}
|