Merge branch 'master' of gitlab.ibr.cs.tu-bs.de:tschuber/netcalc
This commit is contained in:
commit
4d7d96872a
1 changed files with 20 additions and 4 deletions
24
netcalc.c
24
netcalc.c
|
@ -16,6 +16,8 @@ int SOCKFD;
|
||||||
|
|
||||||
int CLIENT;
|
int CLIENT;
|
||||||
|
|
||||||
|
/// closes socket, signal handler
|
||||||
|
/// @param signum
|
||||||
void cleanup(int signum) {
|
void cleanup(int signum) {
|
||||||
if (close(SOCKFD) != 0) {
|
if (close(SOCKFD) != 0) {
|
||||||
perror("close");
|
perror("close");
|
||||||
|
@ -23,6 +25,13 @@ void cleanup(int signum) {
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Fills addrinfo with given params
|
||||||
|
/// @param host
|
||||||
|
/// @param port to bind
|
||||||
|
/// @param ainfo the addrinfo struct to fill
|
||||||
|
/// @param afamily specifies protocol family
|
||||||
|
/// @param flags
|
||||||
|
/// @return 0 if success, -1 if stderr
|
||||||
int prepaddr(char *host, char *port, struct addrinfo **ainfo, int afamily, int flags) {
|
int prepaddr(char *host, char *port, struct addrinfo **ainfo, int afamily, int flags) {
|
||||||
struct addrinfo hints; // hints to what we want
|
struct addrinfo hints; // hints to what we want
|
||||||
memset(&hints, 0, sizeof hints);
|
memset(&hints, 0, sizeof hints);
|
||||||
|
@ -39,14 +48,18 @@ int prepaddr(char *host, char *port, struct addrinfo **ainfo, int afamily, int f
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Binds to sockd
|
||||||
|
/// @param sockd the socket to bind to
|
||||||
|
/// @param ainfo information for binding
|
||||||
|
/// @return sockd freshly bound socket or -1 if error
|
||||||
int bindsocket(int sockd, struct addrinfo *ainfo) {
|
int bindsocket(int sockd, struct addrinfo *ainfo) {
|
||||||
if (bind(SOCKFD, ainfo->ai_addr, ainfo->ai_addrlen) != 0) {
|
if (bind(sockd, ainfo->ai_addr, ainfo->ai_addrlen) != 0) {
|
||||||
perror("bind");
|
perror("bind");
|
||||||
cleanup(0);
|
cleanup(0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen(SOCKFD, 5) != 0) {
|
if (listen(sockd, 5) != 0) {
|
||||||
perror("listen");
|
perror("listen");
|
||||||
cleanup(0);
|
cleanup(0);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -54,6 +67,9 @@ int bindsocket(int sockd, struct addrinfo *ainfo) {
|
||||||
return sockd;
|
return sockd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Prepares SOCKFD socket for further use
|
||||||
|
/// @param ainfo addrinfo struct to configure SOCKFD
|
||||||
|
/// @returns SOCKFD if successfull
|
||||||
int prepsocket(struct addrinfo *ainfo) {
|
int prepsocket(struct addrinfo *ainfo) {
|
||||||
SOCKFD = socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
|
SOCKFD = socket(ainfo->ai_family, ainfo->ai_socktype, ainfo->ai_protocol);
|
||||||
|
|
||||||
|
@ -304,7 +320,7 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
char* host = NULL;
|
char* host = NULL;
|
||||||
char* port = "5000";
|
char* port = "5000";
|
||||||
int afamiliy = AF_UNSPEC;
|
int afamiliy = AF_UNSPEC; /// should do v4 and v6 compatibility
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
|
|
||||||
if (argc > 1 && strcmp(argv[1], "-c") == 0) {
|
if (argc > 1 && strcmp(argv[1], "-c") == 0) {
|
||||||
|
@ -320,7 +336,7 @@ int main(int argc, char *argv[]) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
CLIENT = 0;
|
CLIENT = 0;
|
||||||
afamiliy = AF_INET6;
|
afamiliy = AF_UNSPEC;
|
||||||
flags |= AI_PASSIVE;
|
flags |= AI_PASSIVE;
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
port = argv[1];
|
port = argv[1];
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue