|
@@ -13,6 +13,9 @@
|
|
|
|
|
|
#define UNIX_PATH "hostage"
|
|
#define UNIX_PATH "hostage"
|
|
|
|
|
|
|
|
+#define TCP "TCP"
|
|
|
|
+#define UDP "UDP"
|
|
|
|
+
|
|
int ipc_sock() {
|
|
int ipc_sock() {
|
|
int fd;
|
|
int fd;
|
|
struct sockaddr_un addr;
|
|
struct sockaddr_un addr;
|
|
@@ -35,12 +38,12 @@ int ipc_sock() {
|
|
return fd;
|
|
return fd;
|
|
}
|
|
}
|
|
|
|
|
|
-int net_sock(int port) {
|
|
|
|
|
|
+int net_sock(int type, int port) {
|
|
int fd;
|
|
int fd;
|
|
- int reuse = 1;
|
|
|
|
|
|
+ int reuseaddr = 1;
|
|
struct sockaddr_in addr;
|
|
struct sockaddr_in addr;
|
|
|
|
|
|
- if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
|
|
|
|
|
|
+ if ((fd = socket(AF_INET, (type == 1 ? SOCK_STREAM : SOCK_DGRAM), 0)) == -1) {
|
|
perror("Unable to create net socket");
|
|
perror("Unable to create net socket");
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
@@ -50,9 +53,12 @@ int net_sock(int port) {
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
addr.sin_port = htons(port);
|
|
addr.sin_port = htons(port);
|
|
|
|
|
|
- if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1) {
|
|
|
|
- perror("Unable to set socket options");
|
|
|
|
- return -1;
|
|
|
|
|
|
+ if (type == 1) {
|
|
|
|
+ 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, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
|
|
@@ -60,9 +66,11 @@ int net_sock(int port) {
|
|
return -1;
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
|
|
- if (listen(fd, 5) == -1) {
|
|
|
|
- perror("Unable to listen net socket");
|
|
|
|
- return -1;
|
|
|
|
|
|
+ if (type == 1) {
|
|
|
|
+ if (listen(fd, 5) == -1) {
|
|
|
|
+ perror("Unable to listen net socket");
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
return fd;
|
|
return fd;
|
|
@@ -98,14 +106,23 @@ int send_fd(int fd, int fd_to_send) {
|
|
}
|
|
}
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
int main(int argc, char *argv[]) {
|
|
|
|
+ int type;
|
|
int port;
|
|
int port;
|
|
int ipc_fd, net_fd;
|
|
int ipc_fd, net_fd;
|
|
|
|
|
|
- if (argc < 2) {
|
|
|
|
|
|
+ if (argc < 3) {
|
|
|
|
+ exit(EXIT_FAILURE);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (strncmp(argv[1], TCP) == 0) {
|
|
|
|
+ type = 1;
|
|
|
|
+ } else if (strncmp(argv[1], UDP) == 0) {
|
|
|
|
+ type = 0;
|
|
|
|
+ } else {
|
|
exit(EXIT_FAILURE);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
|
|
- if ((port = atoi(argv[1])) < 1 || (port = atoi(argv[1])) > 65535) {
|
|
|
|
|
|
+ if ((port = atoi(argv[2])) < 1 || (port = atoi(argv[2])) > 65535) {
|
|
exit(EXIT_FAILURE);
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -115,7 +132,7 @@ int main(int argc, char *argv[]) {
|
|
}
|
|
}
|
|
printf("ipc_fd: %d\n", ipc_fd);
|
|
printf("ipc_fd: %d\n", ipc_fd);
|
|
|
|
|
|
- if ((net_fd = net_sock(port)) == -1) {
|
|
|
|
|
|
+ if ((net_fd = net_sock(type, port)) == -1) {
|
|
close(ipc_fd);
|
|
close(ipc_fd);
|
|
close(net_fd);
|
|
close(net_fd);
|
|
exit(EXIT_FAILURE);
|
|
exit(EXIT_FAILURE);
|