comparison src/libident/id_open.c @ 10:26e34ae9a3e3

changed indention and line wrapping to a more consistent style
author meillo@marmaro.de
date Mon, 27 Oct 2008 16:23:10 +0100
parents 08114f7dcc23
children
comparison
equal deleted inserted replaced
9:31cc8a89cb74 10:26e34ae9a3e3
37 # include <sys/select.h> 37 # include <sys/select.h>
38 #endif 38 #endif
39 39
40 40
41 /* 41 /*
42 ident_t *id_open __P3(struct in_addr *, laddr, 42 ident_t *id_open __P3(struct in_addr *, laddr, struct in_addr *, faddr, struct timeval *, timeout)
43 struct in_addr *, faddr,
44 struct timeval *, timeout)
45 */ 43 */
46 44
47 ident_t *id_open __P(( __STRUCT_IN_ADDR_P laddr, 45 ident_t*
48 __STRUCT_IN_ADDR_P faddr, 46 id_open __P((__STRUCT_IN_ADDR_P laddr, __STRUCT_IN_ADDR_P faddr, __STRUCT_TIMEVAL_P timeout))
49 __STRUCT_TIMEVAL_P timeout))
50 { 47 {
51 ident_t *id; 48 ident_t *id;
52 int res, tmperrno; 49 int res, tmperrno;
53 struct sockaddr_in sin_laddr, sin_faddr; 50 struct sockaddr_in sin_laddr, sin_faddr;
54 fd_set rs, ws, es; 51 fd_set rs, ws, es;
55 #ifndef OLD_SETSOCKOPT 52 #ifndef OLD_SETSOCKOPT
56 int on = 1; 53 int on = 1;
57 struct linger linger; 54 struct linger linger;
58 #endif 55 #endif
59 56
60 if ((id = (ident_t *) malloc(sizeof(*id))) == 0) 57 if ((id = (ident_t *) malloc(sizeof(*id))) == 0)
61 return 0; 58 return 0;
62 59
63 if ((id->fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) 60 if ((id->fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
64 { 61 free(id);
65 free(id); 62 return 0;
66 return 0; 63 }
67 } 64
68 65 if (timeout) {
69 if (timeout) 66 if ((res = fcntl(id->fd, F_GETFL, 0)) < 0)
70 { 67 goto ERROR;
71 if ((res = fcntl(id->fd, F_GETFL, 0)) < 0)
72 goto ERROR;
73 68
74 #ifndef VMS 69 #ifndef VMS
75 if (fcntl(id->fd, F_SETFL, res | FNDELAY) < 0) 70 if (fcntl(id->fd, F_SETFL, res | FNDELAY) < 0)
76 goto ERROR; 71 goto ERROR;
77 #endif 72 #endif
78 } 73 }
79 74
80 /* We silently ignore errors if we can't change LINGER */ 75 /* We silently ignore errors if we can't change LINGER */
81 #ifdef OLD_SETSOCKOPT 76 #ifdef OLD_SETSOCKOPT
82 /* Old style setsockopt() */ 77 /* Old style setsockopt() */
83 (void) setsockopt(id->fd, SOL_SOCKET, SO_DONTLINGER); 78 (void) setsockopt(id->fd, SOL_SOCKET, SO_DONTLINGER);
84 (void) setsockopt(id->fd, SOL_SOCKET, SO_REUSEADDR); 79 (void) setsockopt(id->fd, SOL_SOCKET, SO_REUSEADDR);
85 #else 80 #else
86 /* New style setsockopt() */ 81 /* New style setsockopt() */
87 linger.l_onoff = 0; 82 linger.l_onoff = 0;
88 linger.l_linger = 0; 83 linger.l_linger = 0;
89 84
90 (void) setsockopt(id->fd, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger)); 85 (void) setsockopt(id->fd, SOL_SOCKET, SO_LINGER, (void *) &linger, sizeof(linger));
91 (void) setsockopt(id->fd, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof(on)); 86 (void) setsockopt(id->fd, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof(on));
92 #endif 87 #endif
93 88
94 id->buf[0] = '\0'; 89 id->buf[0] = '\0';
95 90
96 bzero((char *)&sin_laddr, sizeof(sin_laddr)); 91 bzero((char *) &sin_laddr, sizeof(sin_laddr));
97 sin_laddr.sin_family = AF_INET; 92 sin_laddr.sin_family = AF_INET;
98 sin_laddr.sin_addr = *laddr; 93 sin_laddr.sin_addr = *laddr;
99 sin_laddr.sin_port = 0; 94 sin_laddr.sin_port = 0;
100 95
101 if (bind(id->fd, (struct sockaddr *) &sin_laddr, sizeof(sin_laddr)) < 0) 96 if (bind(id->fd, (struct sockaddr *) &sin_laddr, sizeof(sin_laddr)) < 0) {
102 {
103 #ifdef DEBUG 97 #ifdef DEBUG
104 perror("libident: bind"); 98 perror("libident: bind");
105 #endif 99 #endif
106 goto ERROR; 100 goto ERROR;
107 } 101 }
108
109 bzero((char *)&sin_faddr, sizeof(sin_faddr));
110 sin_faddr.sin_family = AF_INET;
111 sin_faddr.sin_addr = *faddr;
112 sin_faddr.sin_port = htons(IDPORT);
113 102
114 errno = 0; 103 bzero((char *) &sin_faddr, sizeof(sin_faddr));
115 res = connect(id->fd, (struct sockaddr *) &sin_faddr, sizeof(sin_faddr)); 104 sin_faddr.sin_family = AF_INET;
116 if (res < 0 && errno != EINPROGRESS) 105 sin_faddr.sin_addr = *faddr;
117 { 106 sin_faddr.sin_port = htons(IDPORT);
107
108 errno = 0;
109 res = connect(id->fd, (struct sockaddr *) &sin_faddr, sizeof(sin_faddr));
110 if (res < 0 && errno != EINPROGRESS) {
118 #ifdef DEBUG 111 #ifdef DEBUG
119 perror("libident: connect"); 112 perror("libident: connect");
120 #endif 113 #endif
121 goto ERROR; 114 goto ERROR;
122 } 115 }
123 116
124 if (timeout) 117 if (timeout) {
125 { 118 FD_ZERO(&rs);
126 FD_ZERO(&rs); 119 FD_ZERO(&ws);
127 FD_ZERO(&ws); 120 FD_ZERO(&es);
128 FD_ZERO(&es); 121
129 122 FD_SET(id->fd, &rs);
130 FD_SET(id->fd, &rs); 123 FD_SET(id->fd, &ws);
131 FD_SET(id->fd, &ws); 124 FD_SET(id->fd, &es);
132 FD_SET(id->fd, &es);
133 125
134 #ifdef __hpux 126 #ifdef __hpux
135 if ((res = select(FD_SETSIZE, (int *) &rs, (int *) &ws, (int *) &es, timeout)) < 0) 127 if ((res = select(FD_SETSIZE, (int *) &rs, (int *) &ws, (int *) &es, timeout)) < 0)
136 #else 128 #else
137 if ((res = select(FD_SETSIZE, &rs, &ws, &es, timeout)) < 0) 129 if ((res = select(FD_SETSIZE, &rs, &ws, &es, timeout)) < 0)
138 #endif 130 #endif
139 { 131 {
140 #ifdef DEBUG 132 #ifdef DEBUG
141 perror("libident: select"); 133 perror("libident: select");
142 #endif 134 #endif
143 goto ERROR; 135 goto ERROR;
136 }
137
138 if (res == 0) {
139 errno = ETIMEDOUT;
140 goto ERROR;
141 }
142
143 if (FD_ISSET(id->fd, &es))
144 goto ERROR;
145
146 if (!FD_ISSET(id->fd, &rs) && !FD_ISSET(id->fd, &ws))
147 goto ERROR;
144 } 148 }
145 149
146 if (res == 0) 150 return id;
147 { 151
148 errno = ETIMEDOUT;
149 goto ERROR;
150 }
151
152 if (FD_ISSET(id->fd, &es))
153 goto ERROR;
154
155 if (!FD_ISSET(id->fd, &rs) && !FD_ISSET(id->fd, &ws))
156 goto ERROR;
157 }
158
159 return id;
160
161 ERROR: 152 ERROR:
162 tmperrno = errno; /* Save, so close() won't erase it */ 153 tmperrno = errno; /* Save, so close() won't erase it */
163 close(id->fd); 154 close(id->fd);
164 free(id); 155 free(id);
165 errno = tmperrno; 156 errno = tmperrno;
166 return 0; 157 return 0;
167 } 158 }