12345678910111213141516171819202122232425262728293031323334353637383940 |
- //
- // Created by miti on 21/07/19.
- //
- #include "Ipc.h"
- namespace Ipc {
- // Sets up a socket to bind and listen to the given port. Returns FD of the socket on success, -1 on failure (and prints a msg to stdout with the errno)
- int set_up_socket(int port, sockaddr_in *address) {
- int server_fd = 0;
- // Creating socket file descriptor for listening for attestation requests.
- server_fd = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
- if (server_fd == -1) {
- printf("Error in creating a socket - %d", errno);
- return -1;
- }
- // Preparing the address struct for binding
- address->sin_family = AF_INET;
- address->sin_addr.s_addr = INADDR_ANY; // Todo: should this be localhost?
- address->sin_port = htons(port);
- // memset(address->sin_zero,0,sizeof(address->sin_zero));
- socklen_t addrlen = sizeof(*address);
- // Binding
- if (bind(server_fd, (sockaddr *) address, addrlen) < 0) {
- printf("Error in binding %d - port was %d - ", errno, port);
- return -1;
- }
- // Listening
- if (listen(server_fd, 128) < 0) {
- printf("Error in listening %d", errno);
- return -1;
- }
- return server_fd;
- }
- }
|