From 427f0fa0102ea96117e61610af9d9db835828a1c Mon Sep 17 00:00:00 2001 From: PolynomialDivision Date: Tue, 19 Sep 2017 14:51:04 +0200 Subject: [PATCH] Add example tcp connection --- src/CMakeLists.txt | 2 +- src/include/tcpsocket.h | 19 ++++ src/main.c | 3 + src/network/tcpsocket.c | 196 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 src/include/tcpsocket.h create mode 100644 src/network/tcpsocket.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d48466e..e484261 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -39,7 +39,7 @@ SET(SOURCES crypto/base64.c include/base64.h - utils/utils.c) + utils/utils.c include/tcpsocket.h network/tcpsocket.c) SET(LIBS ubox ubus json-c blobmsg_json config uci gcrypt ssl crypto) diff --git a/src/include/tcpsocket.h b/src/include/tcpsocket.h new file mode 100644 index 0000000..3520abc --- /dev/null +++ b/src/include/tcpsocket.h @@ -0,0 +1,19 @@ +// +// Created by nick on 19.09.17. +// + +#ifndef DAWN_TCPSOCKET_H +#define DAWN_TCPSOCKET_H + +#include + +int run_tcp_socket(); +int add_tcp_conncection(char* ipv4, int port); + +struct network_con_s +{ + int sockfd; + struct sockaddr_in sock_addr; +}; + +#endif //DAWN_TCPSOCKET_H diff --git a/src/main.c b/src/main.c index 4036535..850ae8a 100644 --- a/src/main.c +++ b/src/main.c @@ -14,6 +14,7 @@ #include "networksocket.h" #include "ubus.h" #include "dawn_uci.h" +#include "tcpsocket.h" #define BUFSIZE 17 #define BUFSIZE_DIR 256 @@ -170,6 +171,8 @@ int main(int argc, char **argv) { } } + run_tcp_socket(); + argc -= optind; argv += optind; diff --git a/src/network/tcpsocket.c b/src/network/tcpsocket.c new file mode 100644 index 0000000..4b60352 --- /dev/null +++ b/src/network/tcpsocket.c @@ -0,0 +1,196 @@ +// +// Created by nick on 19.09.17. +// + +// http://www.geeksforgeeks.org/socket-programming-in-cc-handling-multiple-clients-on-server-without-multi-threading/ + +#include "tcpsocket.h" + +//Example code: A simple server side code, which echos back the received message. +//Handle multiple socket connections with select and fd_set on Linux +#include +#include //strlen +#include +#include +#include //close +#include //close +#include +#include +#include +#include //FD_SET, FD_ISSET, FD_ZERO macros + +#define TRUE 1 +#define FALSE 0 +#define PORT 1025 + +//struct network_con_s connections[100]; +//int tcp_entry_last = -1; + +int run_tcp_socket() +{ + int opt = TRUE; + int master_socket, addrlen, new_socket, client_socket[30], + max_clients = 30, activity, i, valread, sd; + int max_sd; + struct sockaddr_in address; + + char buffer[1025]; //data buffer of 1K + + //set of socket descriptors + fd_set readfds; + + char *message = "ECHO Daemon v1.0 \r\n"; + + //initialise all client_socket[] to 0 so not checked + for (i = 0; i < max_clients; i++) { + client_socket[i] = 0; + } + + //create a master socket + if ((master_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) { + perror("socket failed"); + exit(EXIT_FAILURE); + } + + //set master socket to allow multiple connections , + //this is just a good habit, it will work without this + if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, + sizeof(opt)) < 0) { + perror("setsockopt"); + exit(EXIT_FAILURE); + } + + //type of socket created + address.sin_family = AF_INET; + address.sin_addr.s_addr = INADDR_ANY; + address.sin_port = htons(PORT); + + //bind the socket to localhost port 8888 + if (bind(master_socket, (struct sockaddr *) &address, sizeof(address)) < 0) { + perror("bind failed"); + exit(EXIT_FAILURE); + } + printf("Listener on port %d \n", PORT); + + //try to specify maximum of 3 pending connections for the master socket + if (listen(master_socket, 3) < 0) { + perror("listen"); + exit(EXIT_FAILURE); + } + + //accept the incoming connection + addrlen = sizeof(address); + puts("Waiting for connections ..."); + + while (TRUE) { + //clear the socket set + FD_ZERO(&readfds); + + //add master socket to set + FD_SET(master_socket, &readfds); + max_sd = master_socket; + + //add child sockets to set + for (i = 0; i < max_clients; i++) { + //socket descriptor + sd = client_socket[i]; + + //if valid socket descriptor then add to read list + if (sd > 0) + FD_SET(sd, &readfds); + + //highest file descriptor number, need it for the select function + if (sd > max_sd) + max_sd = sd; + } + + //wait for an activity on one of the sockets , timeout is NULL , + //so wait indefinitely + activity = select(max_sd + 1, &readfds, NULL, NULL, NULL); + + if ((activity < 0) && (errno != EINTR)) { + printf("select error"); + } + + //If something happened on the master socket , + //then its an incoming connection + if (FD_ISSET(master_socket, &readfds)) { + if ((new_socket = accept(master_socket, + (struct sockaddr *) &address, (socklen_t *) &addrlen)) < 0) { + perror("accept"); + exit(EXIT_FAILURE); + } + + //inform user of socket number - used in send and receive commands + printf("New connection , socket fd is %d , ip is : %s , port : %d\n", new_socket, + inet_ntoa(address.sin_addr), ntohs + (address.sin_port)); + + //send new connection greeting message + if (send(new_socket, message, strlen(message), 0) != strlen(message)) { + perror("send"); + } + + puts("Welcome message sent successfully"); + + //add new socket to array of sockets + for (i = 0; i < max_clients; i++) { + //if position is empty + if (client_socket[i] == 0) { + client_socket[i] = new_socket; + printf("Adding to list of sockets as %d\n", i); + + break; + } + } + } + + //else its some IO operation on some other socket + for (i = 0; i < max_clients; i++) { + sd = client_socket[i]; + + if (FD_ISSET(sd, &readfds)) { + //Check if it was for closing , and also read the + //incoming message + if ((valread = read(sd, buffer, 1024)) == 0) { + //Somebody disconnected , get his details and print + getpeername(sd, (struct sockaddr *) &address, \ + (socklen_t *) &addrlen); + printf("Host disconnected , ip %s , port %d \n", + inet_ntoa(address.sin_addr), ntohs(address.sin_port)); + + //Close the socket and mark as 0 in list for reuse + close(sd); + client_socket[i] = 0; + } + + //Echo back the message that came in + else { + //set the string terminating NULL byte on the end + //of the data read + buffer[valread] = '\0'; + send(sd, buffer, strlen(buffer), 0); + } + } + } + } + return 0; +} + +int add_tcp_conncection(char* ipv4, int port){ + int sockfd; + struct sockaddr_in serv_addr; + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) + fprintf(stderr,"ERROR opening socket"); + + memset(&serv_addr, 0, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = inet_addr(ipv4); + serv_addr.sin_port = htons(port); + + if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) + fprintf(stderr,"ERROR connecting"); + return 0; +} +