Mercurial > hg > quicktun
comparison src/common.c @ 57:4c083d81f5a8
Fixes for OpenBSD (thanks to Pi <pi-quicktun@mrtheplague.net> and SeekingFor)
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Fri, 31 Jan 2014 23:58:53 +0100 |
parents | 5685fad38195 |
children | 2f4d333f7500 |
comparison
equal
deleted
inserted
replaced
56:377e7d4fbc10 | 57:4c083d81f5a8 |
---|---|
178 if (ai_remote->ai_addrlen > sizeof(sockaddr_any)) return errorexit("Resolved REMOTE_ADDRESS is too big"); | 178 if (ai_remote->ai_addrlen > sizeof(sockaddr_any)) return errorexit("Resolved REMOTE_ADDRESS is too big"); |
179 if (af && af != ai_remote->ai_family) return errorexit("Address families do not match"); | 179 if (af && af != ai_remote->ai_family) return errorexit("Address families do not match"); |
180 af = ai_remote->ai_family; | 180 af = ai_remote->ai_family; |
181 } | 181 } |
182 if (!af) af = AF_INET; | 182 if (!af) af = AF_INET; |
183 int sa_size = sizeof(sockaddr_any); | |
184 if (af == AF_INET) sa_size = sizeof(struct sockaddr_in); | |
185 else if (af == AF_INET6) sa_size = sizeof(struct sockaddr_in6); | |
183 int sfd = socket(af, SOCK_DGRAM, IPPROTO_UDP); | 186 int sfd = socket(af, SOCK_DGRAM, IPPROTO_UDP); |
184 if (sfd < 0) return errorexitp("Could not create UDP socket"); | 187 if (sfd < 0) return errorexitp("Could not create UDP socket"); |
185 sockaddr_any udpaddr; | 188 sockaddr_any udpaddr; |
186 memset(&udpaddr, 0, sizeof(udpaddr)); | 189 memset(&udpaddr, 0, sizeof(udpaddr)); |
187 udpaddr.any.sa_family = af; | 190 udpaddr.any.sa_family = af; |
188 if (ai_local) memcpy(&udpaddr, ai_local->ai_addr, ai_local->ai_addrlen); | 191 if (ai_local) memcpy(&udpaddr, ai_local->ai_addr, ai_local->ai_addrlen); |
189 int port = 2998; | 192 int port = 2998; |
190 if ((envval = getconf("LOCAL_PORT"))) port = atoi(envval); | 193 if ((envval = getconf("LOCAL_PORT"))) port = atoi(envval); |
191 if (sockaddr_set_port(&udpaddr, port)) return -1; | 194 if (sockaddr_set_port(&udpaddr, port)) return -1; |
192 if (bind(sfd, (struct sockaddr*)&udpaddr, sizeof(udpaddr))) return errorexitp("Could not bind socket"); | 195 if (bind(sfd, &udpaddr.any, sa_size)) return errorexitp("Could not bind socket"); |
193 memset(&udpaddr, 0, sizeof(udpaddr)); | 196 memset(&udpaddr, 0, sizeof(udpaddr)); |
194 udpaddr.any.sa_family = af; | 197 udpaddr.any.sa_family = af; |
195 if (ai_remote) memcpy(&udpaddr, ai_remote->ai_addr, ai_remote->ai_addrlen); | 198 if (ai_remote) memcpy(&udpaddr, ai_remote->ai_addr, ai_remote->ai_addrlen); |
196 if (!ai_remote || sockaddr_is_zero_address(&udpaddr)) { | 199 if (!ai_remote || sockaddr_is_zero_address(&udpaddr)) { |
197 session->remote_float = 1; | 200 session->remote_float = 1; |
202 if (sockaddr_set_port(&udpaddr, port)) return -1; | 205 if (sockaddr_set_port(&udpaddr, port)) return -1; |
203 session->remote_addr = udpaddr; | 206 session->remote_addr = udpaddr; |
204 if (session->remote_float) { | 207 if (session->remote_float) { |
205 session->remote_float = 2; | 208 session->remote_float = 2; |
206 } else { | 209 } else { |
207 if (connect(sfd, (struct sockaddr*)&udpaddr, sizeof(udpaddr))) return errorexitp("Could not connect socket"); | 210 if (connect(sfd, &udpaddr.any, sa_size)) return errorexitp("Could not connect socket"); |
208 } | 211 } |
209 } | 212 } |
210 if (ai_local) freeaddrinfo(ai_local); | 213 if (ai_local) freeaddrinfo(ai_local); |
211 if (ai_remote) freeaddrinfo(ai_remote); | 214 if (ai_remote) freeaddrinfo(ai_remote); |
212 session->fd_socket = sfd; | 215 session->fd_socket = sfd; |
316 if (init_tuntap(&session) < 0) return -1; | 319 if (init_tuntap(&session) < 0) return -1; |
317 int ttfd = session.fd_dev; | 320 int ttfd = session.fd_dev; |
318 | 321 |
319 char protocol_data[p->protocol_data_size]; | 322 char protocol_data[p->protocol_data_size]; |
320 memset(protocol_data, 0, p->protocol_data_size); | 323 memset(protocol_data, 0, p->protocol_data_size); |
321 session.protocol_data = &protocol_data; | 324 session.protocol_data = protocol_data; |
322 if (p->init && p->init(&session) < 0) return -1; | 325 if (p->init && p->init(&session) < 0) return -1; |
323 | 326 |
324 if (drop_privileges() < 0) return -1; | 327 if (drop_privileges() < 0) return -1; |
325 | 328 |
326 fprintf(stderr, "The tunnel is now operational!\n"); | 329 fprintf(stderr, "The tunnel is now operational!\n"); |