| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <xpap.h> |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#if HAVE_MINGW32==0 |
| 18 |
|
| 19 |
static int alarm_flag=0; |
| 20 |
|
| 21 |
#ifdef ANSI_FUNC |
| 22 |
static void AlarmFunc (int signo) |
| 23 |
#else |
| 24 |
static void AlarmFunc (signo) |
| 25 |
int signo; |
| 26 |
#endif |
| 27 |
{ |
| 28 |
alarm_flag = 1; |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
#ifdef ANSI_FUNC |
| 59 |
int alrmconnect(int sockfd, void *saptr, int salen, int nsec) |
| 60 |
#else |
| 61 |
int alrmconnect(sockfd, saptr, salen, nsec) |
| 62 |
int sockfd; |
| 63 |
void *saptr; |
| 64 |
int salen; |
| 65 |
int nsec; |
| 66 |
#endif |
| 67 |
{ |
| 68 |
int status=0; |
| 69 |
struct sigaction act1, oact1; |
| 70 |
|
| 71 |
|
| 72 |
alarm_flag = 0; |
| 73 |
errno = 0; |
| 74 |
|
| 75 |
|
| 76 |
if( nsec ){ |
| 77 |
act1.sa_handler = AlarmFunc; |
| 78 |
sigemptyset(&act1.sa_mask); |
| 79 |
act1.sa_flags = 0; |
| 80 |
#ifdef SA_INTERRUPT |
| 81 |
act1.sa_flags |= SA_INTERRUPT; |
| 82 |
#endif |
| 83 |
if( sigaction(SIGALRM, &act1, &oact1) < 0 ){ |
| 84 |
goto done; |
| 85 |
} |
| 86 |
|
| 87 |
alarm(nsec); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
status=connect(sockfd, (struct sockaddr *)saptr, salen); |
| 92 |
|
| 93 |
|
| 94 |
if( nsec ) |
| 95 |
alarm(0); |
| 96 |
|
| 97 |
done: |
| 98 |
|
| 99 |
if( alarm_flag ){ |
| 100 |
xclose(sockfd); |
| 101 |
errno = ETIMEDOUT; |
| 102 |
status = -1; |
| 103 |
} |
| 104 |
|
| 105 |
return(status); |
| 106 |
} |
| 107 |
|
| 108 |
#endif |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
#ifdef ANSI_FUNC |
| 128 |
int noblkconnect(int sockfd, void *saptr, int salen, int nsec) |
| 129 |
#else |
| 130 |
int noblkconnect(sockfd, saptr, salen, nsec) |
| 131 |
int sockfd; |
| 132 |
void *saptr; |
| 133 |
int salen; |
| 134 |
int nsec; |
| 135 |
#endif |
| 136 |
{ |
| 137 |
int flags, n, error; |
| 138 |
socklen_t len; |
| 139 |
fd_set rset, wset; |
| 140 |
struct timeval tval; |
| 141 |
|
| 142 |
|
| 143 |
xfcntl_nonblock(sockfd, flags); |
| 144 |
|
| 145 |
error = 0; |
| 146 |
if( (n = connect(sockfd, (struct sockaddr *) saptr, salen)) < 0){ |
| 147 |
if((xerrno != EINPROGRESS) && (xerrno != EWOULDBLOCK)) |
| 148 |
return(-1); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
if(n == 0) |
| 153 |
goto done; |
| 154 |
|
| 155 |
FD_ZERO(&rset); |
| 156 |
FD_SET(sockfd, &rset); |
| 157 |
wset = rset; |
| 158 |
tval.tv_sec = nsec; |
| 159 |
tval.tv_usec = 0; |
| 160 |
|
| 161 |
if( (n = xselect(sockfd+1, &rset, &wset, NULL, |
| 162 |
nsec ? &tval : NULL)) == 0) { |
| 163 |
xclose(sockfd); |
| 164 |
errno = ETIMEDOUT; |
| 165 |
return(-1); |
| 166 |
} |
| 167 |
|
| 168 |
if(FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) { |
| 169 |
len = sizeof(error); |
| 170 |
if(getsockopt(sockfd, SOL_SOCKET, SO_ERROR, (void *)&error, &len) <0) |
| 171 |
return(-1); |
| 172 |
} else{ |
| 173 |
errno = ETIMEDOUT; |
| 174 |
} |
| 175 |
|
| 176 |
done: |
| 177 |
xfcntl_restore(sockfd, flags); |
| 178 |
|
| 179 |
if(error) { |
| 180 |
xclose(sockfd); |
| 181 |
errno = error; |
| 182 |
return(-1); |
| 183 |
} |
| 184 |
return(0); |
| 185 |
} |
| 186 |
|
| 187 |
#ifdef DO_MAIN |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
#include <stdio.h> |
| 194 |
|
| 195 |
extern char *optarg; |
| 196 |
extern int optind; |
| 197 |
|
| 198 |
int main(int argc, char **argv) |
| 199 |
{ |
| 200 |
int c; |
| 201 |
int args; |
| 202 |
int status; |
| 203 |
int fd; |
| 204 |
int doalarm=0; |
| 205 |
int nsec=2; |
| 206 |
unsigned int ip; |
| 207 |
char *sip; |
| 208 |
struct sockaddr_in sock_in; |
| 209 |
|
| 210 |
|
| 211 |
while ((c = getopt(argc, argv, "ab")) != -1){ |
| 212 |
switch(c){ |
| 213 |
case 'a': |
| 214 |
doalarm = 1; |
| 215 |
break; |
| 216 |
case 'b': |
| 217 |
doalarm = 0; |
| 218 |
break; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
args = argc - optind; |
| 223 |
if( args > 0 ) |
| 224 |
sip = argv[optind]; |
| 225 |
|
| 226 |
else |
| 227 |
sip ="209.1.1.1"; |
| 228 |
|
| 229 |
|
| 230 |
if( (int)(ip = inet_addr(sip)) == -1 ){ |
| 231 |
perror("inet_addr"); |
| 232 |
exit(1); |
| 233 |
} |
| 234 |
if( (fd = xsocket(AF_INET, SOCK_STREAM, 0)) < 0 ){ |
| 235 |
perror("socket"); |
| 236 |
exit(1); |
| 237 |
} |
| 238 |
memset((char *)&sock_in, 0, sizeof(sock_in)); |
| 239 |
sock_in.sin_family = AF_INET; |
| 240 |
sock_in.sin_addr.s_addr = ip; |
| 241 |
sock_in.sin_port = htons(80); |
| 242 |
|
| 243 |
if( doalarm ){ |
| 244 |
fprintf(stderr, "alarm-based connect() ...\n"); |
| 245 |
status=alrmconnect(fd, (void *)&sock_in, sizeof(sock_in), nsec); |
| 246 |
} |
| 247 |
else{ |
| 248 |
fprintf(stderr, "non-blocking connect() ...\n"); |
| 249 |
status=noblkconnect(fd, (void *)&sock_in, sizeof(sock_in), nsec); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
fprintf(stderr, "alarm_flag=%d status=%d\n", alarm_flag, status); |
| 254 |
if( status != 0 ) |
| 255 |
perror("connect"); |
| 256 |
} |
| 257 |
|
| 258 |
#endif |