Debian Codegraph @

xpa/timedconn.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 #include <xpap.h>
6
7 /*
8 *----------------------------------------------------------------------------
9 *
10 *
11 * Private Routines and Data
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 * Public Routines and Data
36 *
37 *
38 *----------------------------------------------------------------------------
39 */
40
41 /*
42 *----------------------------------------------------------------------------
43 *
44 * Routine: alrmconnect
45 *
46 * Purpose: connect with alarm-based timeout
47 *
48 * Returns: status
49 *
50 * adapted from the connect_alarm() code in:
51 * W. Richard Stevens
52 * "Advanced Programming in the Unix Environment"
53 * Addison-Wesley Publishing Co, 1992
54 * p. 350
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 /* no error yet */
72 alarm_flag = 0;
73 errno = 0;
74
75 /* set up alarm */
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 /* start alarm */
87 alarm(nsec);
88 }
89
90 /* try to connect */
91 status=connect(sockfd, (struct sockaddr *)saptr, salen);
92
93 /* turn off alarm if it did not go off */
94 if( nsec )
95 alarm(0);
96
97 done:
98 /* check for alarm => we timed out */
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 * Routine: noblkconnect
114 *
115 * Purpose: non-blocking connect with select-based timeout
116 *
117 * Returns: status
118 *
119 * adapted from the connect_nonb() code in:
120 * W. Richard Stevens
121 * "Advanced Programming in the Unix Environment"
122 * Addison-Wesley Publishing Co, 1992
123 * p. 411
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 /* save state and set in non-blocking mode */
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 /* Do whatever we want while the connect is taking place. */
152 if(n == 0)
153 goto done; /* connect completed immediately */
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); /* timeout */
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); /* Solaris pending error */
172 } else{
173 errno = ETIMEDOUT;
174 }
175
176 done:
177 xfcntl_restore(sockfd, flags); /* restore file status flags */
178
179 if(error) {
180 xclose(sockfd); /* just in case */
181 errno = error;
182 return(-1);
183 }
184 return(0);
185 }
186
187 #ifdef DO_MAIN
188 /*
189 * solaris: gcc -o atest atest.c -lsocket -lnsl
190 * linux: gcc -o atest atest.c
191 * os x: gcc -o atest atest.c
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 /* process switch arguments */
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 /* this ip is bogus and normally will cause connect to hang */
226 else
227 sip ="209.1.1.1";
228
229 /* set up socket to a bogus ip and port */
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 /* if alarm_flag is 1, alarm went off and interrupted connect */
253 fprintf(stderr, "alarm_flag=%d status=%d\n", alarm_flag, status);
254 if( status != 0 )
255 perror("connect");
256 }
257
258 #endif