-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialization.cpp
More file actions
117 lines (91 loc) · 3.09 KB
/
initialization.cpp
File metadata and controls
117 lines (91 loc) · 3.09 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sstream>
#include <pthread.h>
#include "initialization.h"
#include "error.h"
using std::cout;
using std::cin;
using std::cerr;
using std::endl;
using std::string;
using std::string;
int Init(struct Info **info) {
char hostName[128];
struct addrinfo hints, *res, *counter; // info for recursing
struct sockaddr_in saddr, taddr; // for packing server and a placeholder
int sockfd; // socket file descriptor for listening, and placeholder
int result, status; // for checking return values
/* get the host name of the binder machine */
result = gethostname(hostName, sizeof hostName);
if (result == -1) {
cout << "Initialization: get host error." << endl;
return(EHOST);
}
char *actName = new char[128];
strcpy(actName, hostName);
(*info)->address = actName;
/* get the socket fd and the free port */
memset(&hints, 0 , sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
status = getaddrinfo(INADDR_ANY, "0", &hints, &res);
if (status != 0) return -6;
// go along the linked list for a valid entry
for(counter = res; counter != NULL; counter = counter->ai_next) {
sockfd = socket(counter->ai_family, counter->ai_socktype, counter->ai_protocol);
if (sockfd == -1) continue; // has not found a socket descriptor
status = bind(sockfd, counter->ai_addr, counter->ai_addrlen);
if (status == -1) continue; // cannot connect to socket
break; // found good socket descriptor
}
if (counter == NULL) return -2; // end here
/*
saddr.sin_family = AF_INET;
saddr.sin_port = htons(0); // next available port
saddr.sin_addr.s_addr = INADDR_ANY; // set ip for any
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd == -1) {
cout << "Initialization: create socket error." << endl;
return(ESOCK);
}
result = bind(sockfd, (struct sockaddr *)&saddr, sizeof saddr);
if (result == -1) {
cout << "Initialization: binding socket error." << endl;
return(EBIND);
}*/
result = listen(sockfd, 10);
if (result == -1) {
cout << "Initialization: listen socket error." << endl;
return(ELISTEN);
}
(*info)->sockfd = sockfd;
socklen_t len = sizeof(taddr);
result = getsockname(sockfd, (struct sockaddr *)&taddr, &len);
if (result == -1) {
cout << "Initialization: get socket name error." << endl;
return(ESNAME);
};
char portstring[2];
sprintf(portstring, "%hu", ntohs(taddr.sin_port));
char *actPort = new char[2];
strcpy(actPort, portstring);
(*info)->port = actPort;
return 0;
}
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa) {
if (sa->sa_family == AF_INET) {
return &((struct sockaddr_in*) sa)->sin_addr;
}
return &((struct sockaddr_in6*) sa)->sin6_addr;
}