This commit is contained in:
@@ -112,18 +112,121 @@ void bip_set_interface(char *ifname)
|
||||
#ifdef BIP_DEBUG
|
||||
address.s_addr = htonl(bip_get_addr());
|
||||
fprintf(stderr, "Interface: %s\n", ifname);
|
||||
fprintf(stderr, "IP Address: %s\n", inet_ntoa(address));
|
||||
#endif
|
||||
/* setup local broadcast address */
|
||||
if (bip_get_broadcast_addr() == 0) {
|
||||
address.s_addr = htonl(bip_get_addr());
|
||||
set_broadcast_address(address.s_addr);
|
||||
}
|
||||
#ifdef BIP_DEBUG
|
||||
address.s_addr = htonl(bip_get_broadcast_addr());
|
||||
fprintf(stderr, "Broadcast Address: %s\n",
|
||||
inet_ntoa(address));
|
||||
#endif
|
||||
}
|
||||
|
||||
static char *winsock_error_code_text(int code)
|
||||
{
|
||||
switch (code) {
|
||||
case 10013:
|
||||
return "Permission denied.";
|
||||
case WSAEINTR:
|
||||
return "Interrupted system call.";
|
||||
case WSAEBADF:
|
||||
return "Bad file number.";
|
||||
case WSAEFAULT:
|
||||
return "Bad address.";
|
||||
case WSAEINVAL:
|
||||
return "Invalid argument.";
|
||||
case WSAEMFILE:
|
||||
return "Too many open files.";
|
||||
case WSAEWOULDBLOCK:
|
||||
return "Operation would block.";
|
||||
case WSAEINPROGRESS:
|
||||
return "Operation now in progress. This error is returned if any Windows Sockets API function is called while a blocking function is in progress.";
|
||||
case WSAENOTSOCK:
|
||||
return "Socket operation on nonsocket.";
|
||||
case WSAEDESTADDRREQ:
|
||||
return "Destination address required.";
|
||||
case WSAEMSGSIZE:
|
||||
return "Message too long.";
|
||||
case WSAEPROTOTYPE:
|
||||
return "Protocol wrong type for socket.";
|
||||
case WSAENOPROTOOPT:
|
||||
return "Protocol not available.";
|
||||
case WSAEPROTONOSUPPORT:
|
||||
return "Protocol not supported.";
|
||||
case WSAESOCKTNOSUPPORT:
|
||||
return "Socket type not supported.";
|
||||
case WSAEOPNOTSUPP:
|
||||
return "Operation not supported on socket.";
|
||||
case WSAEPFNOSUPPORT:
|
||||
return "Protocol family not supported.";
|
||||
case WSAEAFNOSUPPORT:
|
||||
return "Address family not supported by protocol family.";
|
||||
case WSAEADDRINUSE:
|
||||
return "Address already in use.";
|
||||
case WSAEADDRNOTAVAIL:
|
||||
return "Cannot assign requested address.";
|
||||
case WSAENETDOWN:
|
||||
return "Network is down. This error may be reported at any time if the Windows Sockets implementation detects an underlying failure.";
|
||||
case WSAENETUNREACH:
|
||||
return "Network is unreachable.";
|
||||
case WSAENETRESET:
|
||||
return "Network dropped connection on reset.";
|
||||
case WSAECONNABORTED:
|
||||
return "Software caused connection abort.";
|
||||
case WSAECONNRESET:
|
||||
return "Connection reset by peer.";
|
||||
case WSAENOBUFS:
|
||||
return "No buffer space available.";
|
||||
case WSAEISCONN:
|
||||
return "Socket is already connected.";
|
||||
case WSAENOTCONN:
|
||||
return "Socket is not connected.";
|
||||
case WSAESHUTDOWN:
|
||||
return "Cannot send after socket shutdown.";
|
||||
case WSAETOOMANYREFS:
|
||||
return "Too many references: cannot splice.";
|
||||
case WSAETIMEDOUT:
|
||||
return "Connection timed out.";
|
||||
case WSAECONNREFUSED:
|
||||
return "Connection refused.";
|
||||
case WSAELOOP:
|
||||
return "Too many levels of symbolic links.";
|
||||
case WSAENAMETOOLONG:
|
||||
return "File name too long.";
|
||||
case WSAEHOSTDOWN:
|
||||
return "Host is down.";
|
||||
case WSAEHOSTUNREACH:
|
||||
return "No route to host.";
|
||||
case WSASYSNOTREADY:
|
||||
return "Returned by WSAStartup(), "
|
||||
"indicating that the network subsystem is unusable.";
|
||||
case WSAVERNOTSUPPORTED:
|
||||
return "Returned by WSAStartup(), "
|
||||
"indicating that the Windows Sockets DLL cannot support "
|
||||
"this application.";
|
||||
case WSANOTINITIALISED:
|
||||
return "Winsock not initialized. "
|
||||
"This message is returned by any function except WSAStartup(), "
|
||||
"indicating that a successful WSAStartup() has not yet "
|
||||
"been performed.";
|
||||
case WSAEDISCON:
|
||||
return "Disconnect.";
|
||||
case WSAHOST_NOT_FOUND:
|
||||
return "Host not found. "
|
||||
"This message indicates that the key "
|
||||
"(name, address, and so on) was not found.";
|
||||
case WSATRY_AGAIN:
|
||||
return "Nonauthoritative host not found. "
|
||||
"This error may suggest that the name service itself "
|
||||
"is not functioning.";
|
||||
case WSANO_RECOVERY:
|
||||
return "Nonrecoverable error. "
|
||||
"This error may suggest that the name service itself "
|
||||
"is not functioning.";
|
||||
case WSANO_DATA:
|
||||
return "Valid name, no data record of requested type. "
|
||||
"This error indicates that the key "
|
||||
"(name, address, and so on) was not found.";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
bool bip_init(char *ifname)
|
||||
@@ -142,8 +245,9 @@ bool bip_init(char *ifname)
|
||||
/*Result = WSAStartup(MAKEWORD(2,2), &wd); */
|
||||
if (Result != 0) {
|
||||
Code = WSAGetLastError();
|
||||
printf("TCP/IP stack initialization failed, error code: %i\n",
|
||||
Code);
|
||||
printf("TCP/IP stack initialization failed\n"
|
||||
" error code: %i %s\n",
|
||||
Code, winsock_error_code_text(Code));
|
||||
exit(1);
|
||||
}
|
||||
atexit(cleanup);
|
||||
@@ -156,13 +260,15 @@ bool bip_init(char *ifname)
|
||||
address.s_addr = gethostaddr();
|
||||
if (address.s_addr == (unsigned) -1) {
|
||||
Code = WSAGetLastError();
|
||||
printf("Get host address failed, error code: %i\n", Code);
|
||||
printf("Get host address failed\n"
|
||||
" error code: %i %s\n",
|
||||
Code, winsock_error_code_text(Code));
|
||||
exit(1);
|
||||
}
|
||||
bip_set_addr(address.s_addr);
|
||||
}
|
||||
#ifdef BIP_DEBUG
|
||||
printf("host address: %s\n", inet_ntoa(address));
|
||||
fprintf(stderr, "IP Address: %s\n", inet_ntoa(address));
|
||||
#endif
|
||||
/* has broadcast address been set? */
|
||||
if (bip_get_broadcast_addr() == 0) {
|
||||
@@ -170,9 +276,12 @@ bool bip_init(char *ifname)
|
||||
}
|
||||
#ifdef BIP_DEBUG
|
||||
broadcast_address.s_addr = htonl(bip_get_broadcast_addr());
|
||||
printf("broadcast address: %s\n", inet_ntoa(broadcast_address));
|
||||
fprintf(stderr, "IP Broadcast Address: %s\n",
|
||||
inet_ntoa(broadcast_address));
|
||||
fprintf(stderr, "UDP Port: 0x%04X [%hu]\n",
|
||||
bip_get_port(),
|
||||
bip_get_port());
|
||||
#endif
|
||||
|
||||
/* assumes that the driver has already been initialized */
|
||||
sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
bip_set_socket(sock_fd);
|
||||
@@ -226,10 +335,7 @@ bool bip_init(char *ifname)
|
||||
to allow any one of the computer's IP addresses
|
||||
to be used for connections. Use INADDR_ANY (0L) to
|
||||
allow clients to connect using any one of the host's
|
||||
IP addresses.
|
||||
|
||||
Note: sometimes INADDR_ANY does not let me get
|
||||
any unicast messages. Not sure why... */
|
||||
IP addresses. */
|
||||
sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
#else
|
||||
/* or we could use the specific adapter address
|
||||
|
||||
Reference in New Issue
Block a user