diff --git a/src/utils/dpp_sock.c b/src/utils/dpp_sock.c deleted file mode 100644 index 00d62928fecef65a70c4e88343f44322b8ff2bf9..0000000000000000000000000000000000000000 --- a/src/utils/dpp_sock.c +++ /dev/null @@ -1,402 +0,0 @@ -#include <netlink/netlink.h> -#include <netlink/route/rtnl.h> -#include <netlink/socket.h> -#include <netlink/msg.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <linux/if_bridge.h> -#include <fcntl.h> -#include <sys/ioctl.h> -#include <net/if.h> -#include <sys/select.h> - -#include <libubus.h> - -#include <easy/easy.h> - -#include "debug.h" -#include "dpp_sock.h" - -void socket_close(int *sockfd) -{ - if (*sockfd == INVALID_SOCKET) - return; - - close(*sockfd); - *sockfd = INVALID_SOCKET; -} - -/* Read "length" bytes of "data" from non-blocking socket */ -int socket_recv(int sockfd, unsigned char *data, unsigned int length) -{ - int ret = 0; - unsigned int totalread = 0; - int nbytes; - struct timeval tv; - fd_set ReadFDs, ExceptFDs; - - /* Keep on Reading, untill Total Read Bytes is less than length */ - while (totalread < length) { - FD_ZERO(&ReadFDs); - FD_ZERO(&ExceptFDs); - FD_SET(sockfd, &ReadFDs); - FD_SET(sockfd, &ExceptFDs); - tv.tv_sec = 10; - tv.tv_usec = 0; - - ret = select(sockfd + 1, &ReadFDs, NULL, &ExceptFDs, &tv); - if (ret > 0) { - if (!FD_ISSET(sockfd, &ReadFDs)) { - err("Exception occured. sockfd=%d\n", sockfd); - goto error; - } - } else { - if (ret == 0) { - err("Select timeout after %d sec. sockfd=%d\n", - 10, sockfd); - } else { - err("Read error [%s]. sockfd=%d\n", - strerror(errno), sockfd); - } - goto error; - - } - - nbytes = read(sockfd, &(data[totalread]), (length - totalread)); - if (nbytes <= 0) { - err("Read error [%s]. sockfd=%d nbytes=%d\n", - strerror(errno), sockfd, nbytes); - goto error; - } - - totalread += nbytes; - } - err("Received %d bytes. sockfd=%d\n", totalread, sockfd); - - return totalread; - -error: - return -1; -} - -/* Receive all the data from Hostapd. caller should free the memory allocated to read_buf */ -int dpp_recv_tcp_data(int sockfd, uint8_t **read_buf) -{ - uint32_t nbytes, totalread = 0, data_size = 0; - uint8_t *tmp = NULL; - - err("Read DPP TCP message from Hostapd on socket %d\n", sockfd); - - nbytes = socket_recv(sockfd, (void *)&data_size, sizeof(data_size)); - if (nbytes == -1) { - err("Read error on socket %d\n", sockfd); - return -1; - } - - if (nbytes < sizeof(data_size)) { - err("Doesn't contain enough DPP TCP message. Only %u bytes "\ - "available on socket %d\n", nbytes, sockfd); - return -1; - } - - data_size = htonl(data_size); - err("%u is the size of DPP TCP message to be read from Hostapd on "\ - "socket %d\n", data_size, sockfd); - tmp = (uint8_t *)calloc(1, data_size); - if (!tmp) { - err("out of memory!\n"); - return -1; - } - err("%u bytes to read DPP TCP message from Hostapd on socket %d\n", - data_size, sockfd); - - totalread = socket_recv(sockfd, tmp, data_size); - *read_buf = tmp; - err("DPP TCP message of %u bytes read from Hostapd on socket %d\n", - totalread, sockfd); - - return totalread; -} - -int socket_accept_connection(int server_fd) -{ - int childfd = INVALID_SOCKET; - socklen_t clientlen; - struct sockaddr_in clientaddr; - - clientlen = sizeof(clientaddr); - childfd = accept(server_fd, (struct sockaddr *)&clientaddr, &clientlen); - if (childfd < 0) { - err("Client accept error [%s]. server_fd=%d childfd=%d\n", - strerror(errno), server_fd, childfd); - return INVALID_SOCKET; - } - - err("Opened childfd %d\n", childfd); - return childfd; -} - - -int socket_dpp_tcp_server_rx(int fd) -{ - int server_fd = fd, client_fd = INVALID_SOCKET; - int ret = 0; - //uint8_t *test; - - err("New Hostapd client socket connection on server socket %d\n", - server_fd); - - /* Accept the connection */ - client_fd = socket_accept_connection(server_fd); - if (client_fd == INVALID_SOCKET) { - err("Failed to accept Hostapd client socket connection on "\ - "server socket %d\n", server_fd); - goto end; - } - - err("Accepted Hostapd client socket %d on server socket %d\n", - client_fd, server_fd); - - if (socket_set_nonblock(client_fd) != 0) { - err("Failed to set O_NONBLOCK for Hostapd client socket %d, "\ - "error: %s\n", client_fd, strerror(errno)); - - ret = -1; - goto end; - } - - return client_fd; -end: - if (ret != 0) { - err("Closing newly created Hostapd client socket %d\n", - client_fd); - socket_close(&client_fd); - } - return -1; -} - -/* Connects to the server given the IP address and port number */ -int connect_to_server(char *straddrs, unsigned int nport) -{ - struct sockaddr_in server_addr; - int res, valopt; - long arg; - fd_set readfds; - struct timeval tv; - socklen_t lon; - int sockfd; - - memset(&server_addr, 0, sizeof(server_addr)); - - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd == -1) { - err("Socket call failed for ip[%s] port=%d Error[%s]\n", - straddrs, nport, strerror(errno)); - goto error; - } - - /* Set nonblock on the socket so we can timeout */ - arg = fcntl(sockfd, F_GETFL, NULL); - if (arg < 0 || fcntl(sockfd, F_SETFL, arg | O_NONBLOCK) < 0) { - err("fcntl call failed for ip[%s] port=%d Error[%s]\n", - straddrs, nport, strerror(errno)); - goto error; - } - server_addr.sin_family = AF_INET; - server_addr.sin_port = htons(nport); - server_addr.sin_addr.s_addr = inet_addr(straddrs); - - res = connect(sockfd, (struct sockaddr *)&server_addr, - sizeof(struct sockaddr)); - if (res < 0) { - if (errno == EINPROGRESS) { - tv.tv_sec = 10; - tv.tv_usec = 0; - FD_ZERO(&readfds); - FD_SET(sockfd, &readfds); - if (select(sockfd+1, NULL, &readfds, NULL, &tv) > 0) { - lon = sizeof(int); - getsockopt(sockfd, SOL_SOCKET, SO_ERROR, - (void *)(&valopt), &lon); - if (valopt) { - err("getsockopt call failed for ip[%s]"\ - " port=%d. valopt=%d Error[%s]\n", - straddrs, nport, valopt, - strerror(valopt)); - goto error; - } - } else { - err("Select timeout/error for ip[%s] port=%d Error[%s]\n", - straddrs, nport, strerror(errno)); - goto error; - } - } else { - err("Connect failed For ip[%s] port=%d. Error[%s]\n", - straddrs, nport, strerror(errno)); - goto error; - } - } - err("Connect Successfull: ip[%s] port=%d sockfd=%d\n", - straddrs, nport, sockfd); - - return sockfd; - - /* Error Handling */ -error: - if (sockfd != INVALID_SOCKET) - socket_close(&sockfd); - return INVALID_SOCKET; -} - -/* Open a TCP socket for getting requests from hostapd client for DPP over TCP */ -int dpp_create_server(int portno) -{ - int sockfd = INVALID_SOCKET, optval = 1; - struct sockaddr_in sockaddr; - - memset(&sockaddr, 0, sizeof(sockaddr)); - sockaddr.sin_family = AF_INET; - sockaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); - sockaddr.sin_port = htons(portno); - - err("Open server socket on port %d to recieve DPP TCP message "\ - "from Hostapd\n", portno); - - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd == -1) { - err("Socket call failed for port=%d Error[%s]\n", - portno, strerror(errno)); - goto error; - } - - if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, - sizeof(optval)) < 0) { - err("Unable to setsockopt. portno=%d. sockfd=%d. Error[%s]\n", - portno, sockfd, strerror(errno)); - goto error; - } - - if (fcntl(sockfd, F_SETFL, O_NONBLOCK) == -1) { - err("Unable to set O_NONBLOCK\n"); - goto error; - } - - if (bind(sockfd, (struct sockaddr *) &sockaddr, sizeof(sockaddr)) < 0) { - err("Unable to bind to socket. portno=%d sockfd=%d Error[%s]\n", - portno, sockfd, strerror(errno)); - goto error; - } - - if (listen(sockfd, 10) < 0) { - err("Socket listen error. portno=%d sockfd=%d Error[%s]\n", - portno, sockfd, strerror(errno)); - goto error; - } - - err("Opened server socket %d on port %d to recieve DPP TCP message "\ - "from Hostapd\n", sockfd, portno); - return sockfd; - -error: - if (sockfd != INVALID_SOCKET) - socket_close(&sockfd); - - return INVALID_SOCKET; -} - -/* Set O_NONBLOCK on socket */ -int socket_set_nonblock(int sockfd) -{ - return fcntl(sockfd, F_SETFL, O_NONBLOCK); -} - -/* Sends the data to socket */ -unsigned int socket_tx_data(int sockfd, char *data, unsigned int len) -{ - int ret = 0, nret = 0; - unsigned int totalsize = len, totalsent = 0; - - /* Loop till all the data sent */ - while (totalsent < totalsize) { - fd_set WriteFDs; - struct timeval tv; - - FD_ZERO(&WriteFDs); - - if (sockfd == INVALID_SOCKET) { - err("Invalid socket. sockfd=%d\n", sockfd); - goto error; - } - - FD_SET(sockfd, &WriteFDs); - - tv.tv_sec = 10; - tv.tv_usec = 0; - ret = select(sockfd+1, NULL, &WriteFDs, NULL, &tv); - if (ret > 0) { - if (FD_ISSET(sockfd, &WriteFDs)) { - ; - } else { - err("Exception occured. sockfd=%d\n", sockfd); - goto error; - } - } else { - if (ret == 0) { - err("Select timeout after %d sec. sockfd=%d\n", - 10, sockfd); - } else { - err("Send error [%s]. sockfd=%d\n", - strerror(errno), sockfd); - } - goto error; - } - - nret = send(sockfd, &(data[totalsent]), len, 0); - if (nret < 0) { - err("Send failed [%s]. sockfd=%d\n", - strerror(errno), sockfd); - goto error; - } - totalsent += nret; - len -= nret; - nret = 0; - } - - err("Send %d bytes of %d. sockfd=%d\n", totalsent, len, sockfd); - return totalsent; -error: - return 0; -} - -/* Create DPP TCP message and send it to Hostapd */ -int socket_send_dpp_tcp_message(int sockfd, uint8_t *buf, uint32_t buf_len) -{ - int ret = 0; - uint8_t *data = NULL; - uint32_t data_len; - - data_len = buf_len + 4; - data = (uint8_t *) calloc(1, data_len); - if (!data) - return -1; - - err("%u bytes to send DPP TCP message\n", data_len); - - *((unsigned int *)data) = ntohl(buf_len); - memcpy(data+4, buf, buf_len); - err("Send DPP TCP message of size %u on socket %d to Hostapd\n", - data_len, sockfd); - - /* Send the payload */ - if (socket_tx_data(sockfd, (char *) data, data_len) == 0) { - err("Failed to send data. sockfd[%d]\n", sockfd); - ret = -18; - goto end; - } - -end: - if (data) - free(data); - - return ret; -} diff --git a/src/utils/dpp_sock.h b/src/utils/dpp_sock.h deleted file mode 100644 index 0a3366262fcbe36b98a6ad5f99bf9320ff655b4c..0000000000000000000000000000000000000000 --- a/src/utils/dpp_sock.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef DPP_SOCK_H -#define DPP_SOCK_H - -#define INVALID_SOCKET -1 - -void socket_close(int *sockfd); -int socket_recv(int sockfd, unsigned char *data, unsigned int length); -int dpp_recv_tcp_data(int sockfd, uint8_t **read_buf); -int connect_to_server(char *straddrs, unsigned int nport); -int socket_dpp_tcp_server_rx(int fd); -int dpp_create_server(int portno); -int socket_set_nonblock(int sockfd); -unsigned int socket_tx_data(int sockfd, char *data, unsigned int len); -int socket_send_dpp_tcp_message(int sockfd, uint8_t *buf, uint32_t buf_len); - -#endif