| 1 |
/* netToe Version 1.5.1 |
| 2 |
* |
| 3 |
* Copyright 2000,2001 Gabriele Giorgetti <[email protected]> |
| 4 |
* 2009-2014 Mats Erik Andersson <[email protected]> |
| 5 |
* |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifdef HAVE_CONFIG_H |
| 23 |
# include <config.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include <stdio.h> |
| 27 |
#include <string.h> |
| 28 |
#include <errno.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/socket.h> |
| 33 |
#include <netinet/in.h> |
| 34 |
#include <arpa/inet.h> |
| 35 |
#include <sys/wait.h> |
| 36 |
#include <netdb.h> |
| 37 |
|
| 38 |
#include "network.h" |
| 39 |
|
| 40 |
/* Has network partner reported EOF? */ |
| 41 |
extern int has_given_eof; |
| 42 |
|
| 43 |
/* Choice of INET address family. */ |
| 44 |
extern int addrfamily; |
| 45 |
|
| 46 |
|
| 47 |
int establish_listening_socket(unsigned short port_number, char *peer_ip_address, int len) |
| 48 |
{ |
| 49 |
int sockfd, new_fd; |
| 50 |
int reuse = 1; |
| 51 |
socklen_t sin_size; |
| 52 |
int BACKLOG = 2; |
| 53 |
struct sockaddr_storage their_addr; |
| 54 |
struct addrinfo hints, *ai, *aiptr; |
| 55 |
char portstr[10]; |
| 56 |
int status; |
| 57 |
|
| 58 |
memset(&hints, 0, sizeof(hints)); |
| 59 |
hints.ai_family = addrfamily; |
| 60 |
hints.ai_socktype = SOCK_STREAM; |
| 61 |
hints.ai_flags = AI_PASSIVE; |
| 62 |
#ifdef AI_ADDRCONFIG |
| 63 |
hints.ai_flags |= AI_ADDRCONFIG; |
| 64 |
#endif |
| 65 |
|
| 66 |
sprintf(portstr, "%u", port_number); |
| 67 |
|
| 68 |
if ( (status = getaddrinfo(NULL, portstr, &hints, &aiptr)) != 0 ) { |
| 69 |
perror("getaddrinfo"); |
| 70 |
fprintf(stderr, "Address failure. Quitting...\n\n"); |
| 71 |
return -1; |
| 72 |
} |
| 73 |
|
| 74 |
/* Prefer IPv6 over IPv4. */ |
| 75 |
ai = aiptr; |
| 76 |
while (ai->ai_next && (ai->ai_family == AF_INET)) |
| 77 |
ai = ai->ai_next; |
| 78 |
|
| 79 |
if ( (sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) |
| 80 |
== -1 ) |
| 81 |
{ |
| 82 |
perror("socket"); |
| 83 |
fprintf(stderr, "Peer error: (socket). Quitting...\n\n"); |
| 84 |
exit(EXIT_FAILURE); |
| 85 |
} |
| 86 |
|
| 87 |
/* avoid "Address already in use" errors when restarting */ |
| 88 |
if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, |
| 89 |
(void *) &reuse, sizeof (reuse)) == -1 ) |
| 90 |
{ |
| 91 |
perror("SO_REUSEADDR"); |
| 92 |
fprintf(stderr, "Peer error: (SO_REUSEADDR). Quitting...\n\n"); |
| 93 |
exit(EXIT_FAILURE); |
| 94 |
} |
| 95 |
|
| 96 |
/* |
| 97 |
* When an unspecified family was asked for, and an IPv6 |
| 98 |
* result address resulted, the attempt a dual stacked |
| 99 |
* listening socket. Otherwise a lock down to a single |
| 100 |
* address family is sought. |
| 101 |
*/ |
| 102 |
if (addrfamily == AF_UNSPEC && ai->ai_family == AF_INET6) |
| 103 |
{ |
| 104 |
int data = 0; |
| 105 |
setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, |
| 106 |
&data, sizeof(data)); |
| 107 |
} |
| 108 |
else if (ai->ai_family == AF_INET6) |
| 109 |
{ |
| 110 |
int data = 1; |
| 111 |
setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, |
| 112 |
&data, sizeof(data)); |
| 113 |
} |
| 114 |
|
| 115 |
if ( bind(sockfd, ai->ai_addr, ai->ai_addrlen) == -1) |
| 116 |
{ |
| 117 |
printf("\n Network error: Address is in use,"); |
| 118 |
printf("or permission was insufficient.\n\n"); |
| 119 |
close(sockfd); |
| 120 |
return -1; |
| 121 |
} |
| 122 |
|
| 123 |
freeaddrinfo(aiptr); |
| 124 |
|
| 125 |
if ( listen(sockfd, BACKLOG) == -1 ) |
| 126 |
{ |
| 127 |
perror("listen"); |
| 128 |
fprintf(stderr, "Peer error: (bind: address already in use or " |
| 129 |
"permission is denied).\nQuitting...\n\n"); |
| 130 |
close(sockfd); |
| 131 |
return -1; |
| 132 |
} |
| 133 |
|
| 134 |
sin_size = sizeof(their_addr); |
| 135 |
|
| 136 |
if ( (new_fd = accept(sockfd, (struct sockaddr *) &their_addr, |
| 137 |
&sin_size)) == -1) |
| 138 |
{ |
| 139 |
perror("accept"); |
| 140 |
fprintf(stderr, "Peer error: (accept). continuing:\n\n"); |
| 141 |
} |
| 142 |
|
| 143 |
close(sockfd); |
| 144 |
|
| 145 |
memset(peer_ip_address, 0, len); |
| 146 |
|
| 147 |
getnameinfo((struct sockaddr *) &their_addr, sin_size, |
| 148 |
peer_ip_address, len, |
| 149 |
NULL, 0, NI_NUMERICHOST); |
| 150 |
|
| 151 |
return new_fd; |
| 152 |
} /* establish_listening_socket(unsigned short, char *, int) */ |
| 153 |
|
| 154 |
|
| 155 |
int connect_to_socket(char *host_ip_number, unsigned short port_number) |
| 156 |
{ |
| 157 |
int sockfd = -1, status; |
| 158 |
char portstr[20]; |
| 159 |
struct addrinfo hints, *ai, *aiptr; |
| 160 |
|
| 161 |
//printf("\n Calling %s.\n\n", host_ip_number); |
| 162 |
|
| 163 |
memset(&hints, 0, sizeof(hints)); |
| 164 |
hints.ai_family = addrfamily; |
| 165 |
hints.ai_socktype = SOCK_STREAM; |
| 166 |
#ifdef AI_ADDRCONFIG |
| 167 |
hints.ai_flags = AI_ADDRCONFIG; |
| 168 |
#endif |
| 169 |
|
| 170 |
sprintf(portstr, "%u", port_number); |
| 171 |
|
| 172 |
if ( (status = getaddrinfo(host_ip_number, portstr, &hints, &ai)) ) |
| 173 |
{ |
| 174 |
/* Failed host lookup. */ |
| 175 |
printf("failed\n\n"); |
| 176 |
printf(" Incorrect IP address, or failing host name.\n"); |
| 177 |
printf(" Try again with a usable destination!\n\n"); |
| 178 |
return -1; |
| 179 |
} |
| 180 |
|
| 181 |
for ( aiptr = ai; aiptr; aiptr = aiptr->ai_next) |
| 182 |
{ |
| 183 |
if ((sockfd = socket (aiptr->ai_family, SOCK_STREAM, 0)) == -1) |
| 184 |
continue; |
| 185 |
|
| 186 |
if ( connect(sockfd, aiptr->ai_addr, aiptr->ai_addrlen) == 0 ) |
| 187 |
break; |
| 188 |
|
| 189 |
close(sockfd); |
| 190 |
sockfd = -1; |
| 191 |
} /* for "aiptr" */ |
| 192 |
|
| 193 |
freeaddrinfo(ai); |
| 194 |
|
| 195 |
if ( aiptr == NULL ) |
| 196 |
{ |
| 197 |
printf("failed\n\n"); |
| 198 |
printf(" No game found at: %s\n\n", host_ip_number); |
| 199 |
printf(" Try again if the host was typed correctly.\n"); |
| 200 |
printf(" Maybe the host is not ready yet.\n\n"); |
| 201 |
return -1; |
| 202 |
} |
| 203 |
|
| 204 |
return sockfd; |
| 205 |
} /* connect_to_socket(char *, unsigned short) */ |
| 206 |
|
| 207 |
|
| 208 |
int read_from_socket(int connected_socket, char *buffer, int bytes) |
| 209 |
{ |
| 210 |
int bytescount; |
| 211 |
int bytesread; |
| 212 |
|
| 213 |
bytescount = 0; |
| 214 |
bytesread = 0; |
| 215 |
|
| 216 |
while (bytescount < bytes) |
| 217 |
{ |
| 218 |
if ( (bytesread = read(connected_socket, buffer, bytes-bytescount)) |
| 219 |
> 0 ) |
| 220 |
{ |
| 221 |
bytescount += bytesread; |
| 222 |
buffer += bytesread; |
| 223 |
continue; |
| 224 |
} |
| 225 |
else if (bytesread < 0) |
| 226 |
return -1; |
| 227 |
|
| 228 |
/* EOF from other end. */ |
| 229 |
has_given_eof = 1; |
| 230 |
break; |
| 231 |
} |
| 232 |
|
| 233 |
return bytescount; |
| 234 |
} /* read_from_socket( int, char *, int) */ |
| 235 |
|
| 236 |
|
| 237 |
int write_to_socket(int connected_socket, char *buffer, int bytes) |
| 238 |
{ |
| 239 |
int bytescount; |
| 240 |
int byteswritten; |
| 241 |
|
| 242 |
bytescount = 0; |
| 243 |
byteswritten = 0; |
| 244 |
|
| 245 |
while (bytescount < bytes) |
| 246 |
{ |
| 247 |
if ( (byteswritten = write(connected_socket, buffer, |
| 248 |
bytes - bytescount)) |
| 249 |
> 0) |
| 250 |
{ |
| 251 |
bytescount += byteswritten; |
| 252 |
buffer += byteswritten; |
| 253 |
} |
| 254 |
else if ( (byteswritten == 0) && (errno == EPIPE) ) |
| 255 |
{ |
| 256 |
has_given_eof = 1; |
| 257 |
break; |
| 258 |
} |
| 259 |
else if (byteswritten < 0) |
| 260 |
return -1; |
| 261 |
} |
| 262 |
|
| 263 |
return bytescount; |
| 264 |
} /* write_to_socket(int, char *, int) */ |
| 265 |
|
| 266 |
|
| 267 |
void give_local_IP(char *local_ip_address, int len) |
| 268 |
{ |
| 269 |
char hostname[100]; |
| 270 |
struct addrinfo hints, *ai; |
| 271 |
int status; |
| 272 |
|
| 273 |
gethostname(hostname, sizeof(hostname)); |
| 274 |
|
| 275 |
local_ip_address[0] = '\0'; |
| 276 |
|
| 277 |
memset(&hints, 0, sizeof(hints)); |
| 278 |
hints.ai_family = addrfamily; |
| 279 |
hints.ai_socktype = SOCK_STREAM; |
| 280 |
#ifdef AI_ADDRCONFIG |
| 281 |
hints.ai_flags = AI_ADDRCONFIG; |
| 282 |
#endif |
| 283 |
|
| 284 |
if ( (status = getaddrinfo(hostname, NULL, &hints, &ai)) ) { |
| 285 |
printf("Address lookup failed: %s\n", gai_strerror(status)); |
| 286 |
} else { |
| 287 |
getnameinfo(ai->ai_addr, ai->ai_addrlen, |
| 288 |
local_ip_address, len, |
| 289 |
NULL, 0, NI_NUMERICHOST); |
| 290 |
} |
| 291 |
} /* give_local_IP(char *) */ |
| 292 |
|
| 293 |
|
| 294 |
/* NOT USED */ |
| 295 |
/* ================================== |
| 296 |
const char *give_local_hostname( void ) |
| 297 |
{ |
| 298 |
char hostname[100]; |
| 299 |
struct hostent *h; |
| 300 |
|
| 301 |
gethostname ( hostname, 100); |
| 302 |
|
| 303 |
if ((h=gethostbyname(hostname)) == NULL) { |
| 304 |
herror("gethostbyname"); |
| 305 |
} |
| 306 |
strcpy(hostname, h->h_name ); |
| 307 |
return ( hostname ); |
| 308 |
} |
| 309 |
*/ |