|
@@ -8,6 +8,7 @@
|
|
|
#include <sys/uio.h>
|
|
|
#include <sys/un.h>
|
|
|
#include <unistd.h>
|
|
|
+#include <netdb.h>
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
#include <android/log.h>
|
|
@@ -58,29 +59,36 @@ int ipc_sock() {
|
|
|
int net_sock(int type, int port) {
|
|
|
int fd;
|
|
|
int reuseaddr = 1;
|
|
|
- struct sockaddr_in addr;
|
|
|
|
|
|
- if ((fd = socket(AF_INET, (type == 1 ? SOCK_STREAM : SOCK_DGRAM), (type == 1 ? IPPROTO_IP : IPPROTO_UDP))) == -1) {
|
|
|
+ struct addrinfo hints, *res;
|
|
|
+
|
|
|
+ // first, load up address structs with getaddrinfo():
|
|
|
+ memset(&hints, 0, sizeof hints);
|
|
|
+ hints.ai_family = AF_INET; // use IPv4
|
|
|
+ hints.ai_socktype = (type == 1 ? SOCK_STREAM : SOCK_DGRAM); // TCP or UDP
|
|
|
+ hints.ai_flags = AI_PASSIVE; // fill in my IP for me
|
|
|
+
|
|
|
+ char service_name[256];
|
|
|
+ sprintf(service_name, "%d", port);
|
|
|
+ getaddrinfo(NULL, service_name, &hints, &res);
|
|
|
+
|
|
|
+ if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
|
|
|
perror("Unable to create net socket");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- memset(&addr, 0, sizeof(addr));
|
|
|
- addr.sin_family = AF_INET;
|
|
|
- addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
- addr.sin_port = htons(port);
|
|
|
-
|
|
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr)) == -1) {
|
|
|
perror("Unable to set socket options");
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
|
|
|
+ if (bind(fd, res->ai_addr, res->ai_addrlen) == -1) {
|
|
|
perror("Unable to bind net socket");
|
|
|
return -1;
|
|
|
}
|
|
|
+ printf("Hello from the new port binder\n");
|
|
|
|
|
|
- if (type == 1) {
|
|
|
+ if (type == 1) { // TCP
|
|
|
if (listen(fd, 5) == -1) {
|
|
|
perror("Unable to listen net socket");
|
|
|
return -1;
|