-
Notifications
You must be signed in to change notification settings - Fork 1
/
AddressFunctions.h
88 lines (73 loc) · 3.36 KB
/
AddressFunctions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
File: AddressFunctions.h
Version: Functions to display the IP of client and server sockets
Author: Joe O'Regan
Year 4 Networked Games Assignment
Team 1:
Joe O'Regan K00203642
Samantha Marah K00200782
Jason Foley K00186690
Code adapted (unsuccessfully) from:
TCP/IP Sockets in C, Donahoo, Calvert -> PrintSocketAddr() P42
Unix Network Programming, Stevens, Fenner, Rudoff
Beej's Guide to Network Programming, Hall
*/
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <arpa/inet.h>
int get_ip_str(int ip, void *sa, char *s, size_t maxlen);
/*
Server: Display the IP address and Port the Server is running on
*/
void displayAddress(const struct sockaddr* address, FILE* stream){ // Test for address and stream
if (address == NULL || stream == NULL) return; // return from function if the struct or file is empty
void *numericAddress; // Pointer to binary address
char addressBuffer[INET6_ADDRSTRLEN]; // string large enough to hold IPv6 address
in_port_t port; // Port to display
if (address->sa_family == AF_INET) { // If the address family is IPv4
numericAddress = &((struct sockaddr_in *) address)->sin_addr; // Get the IPv4 address to display
port = ntohs(((struct sockaddr_in *) address)->sin_port); // Get the port number to display
}
else if (address->sa_family == AF_INET6) { // if the address family is IPv6
numericAddress = &((struct sockaddr_in6 *) address)->sin6_addr; // Get the IPv6 address to display
port = ntohs(((struct sockaddr_in6 *) address)->sin6_port); // Get the port number to display
}
else {
fputs("Stream error", stream); // Error with stream
return;
}
fputs("Server now running on ", stdout); // Convert binary to printable address
if(get_ip_str(address->sa_family, (void *) address, addressBuffer, INET6_ADDRSTRLEN) == 1) {
fputs("[invalid address]", stream); // Unable to convert
} else {r
printf("%s", addressBuffer);
if (port != 0) fprintf(stream, "/%u", port); // Zero not valid in socket address
}
fputc('\n', stdout); // Add new line
}
/*
Function to return the IP address for an IPv4 or IPv6 address structure.
Taken from beej website, but not implemented
*/
int get_ip_str(int ip, void *sa, char *s, size_t maxlen) {
switch(ip) {
case AF_INET: // If it's an IPv4 address set the address value in the IPv4 address structure
inet_ntop(AF_INET, &(((struct sockaddr_in *)sa)->sin_addr), s, maxlen);
break;
case AF_INET6: // If it's an IPv6 address set the address value in the IPv6 address stucture
inet_ntop(AF_INET6, &(((struct sockaddr_in6 *)sa)->sin6_addr), s, maxlen);
break;
default:
strncpy(s, "Unknown Address Family", maxlen); // If it's neither, something is wrong
return 1; // if unsuccessful
}
return 0; // successful
}
char* storeAndDisplayIP(struct sockaddr_in addr, char* ip, int port) { // Store and display the client IP and Port
if (inet_ntop(AF_INET, &addr.sin_addr.s_addr, ip, INET_ADDRSTRLEN) != NULL){ // inet_ntop() Convert IP address to human readable form
printf("Handling client %s/%d \n", ip, port); // Display the client IP address and port number
}
printf("storeAndDisplayIP: %s\n", ip);
return ip;
}