chane to tcp

This commit is contained in:
PolynomialDivision 2018-01-05 16:28:06 +01:00
parent f59a6ff836
commit b34f9e3018
5 changed files with 41 additions and 17 deletions

View file

@ -15,7 +15,7 @@ start_service()
procd_set_param command $PROG procd_set_param command $PROG
procd_set_param stdout 1 procd_set_param stdout 1
procd_set_param stderr 1 procd_set_param stderr 1
procd_add_mdns "dawn" "tcp" "1025" procd_add_mdns "dawn" "tcp" "1026"
procd_close_instance procd_close_instance
echo "Dawn instance started!" echo "Dawn instance started!"
} }

View file

@ -259,6 +259,6 @@ struct network_con_s network_array[ARRAY_NETWORK_LEN];
pthread_mutex_t tcp_array_mutex; pthread_mutex_t tcp_array_mutex;
int insert_to_tcp_array(struct network_con_s entry); int insert_to_tcp_array(struct network_con_s entry);
int tcp_array_contains_address(struct sockaddr_in entry); int tcp_array_contains_address(struct sockaddr_in entry);
void send_tcp(char* msg);
#endif #endif

View file

@ -1,11 +1,8 @@
//
// Created by nick on 19.09.17.
//
// http://www.geeksforgeeks.org/socket-programming-in-cc-handling-multiple-clients-on-server-without-multi-threading/ // http://www.geeksforgeeks.org/socket-programming-in-cc-handling-multiple-clients-on-server-without-multi-threading/
#include "tcpsocket.h" #include "tcpsocket.h"
#include "datastorage.h" #include "datastorage.h"
#include "ubus.h"
//Example code: A simple server side code, which echos back the received message. //Example code: A simple server side code, which echos back the received message.
//Handle multiple socket connections with select and fd_set on Linux //Handle multiple socket connections with select and fd_set on Linux
@ -22,7 +19,7 @@
#define TRUE 1 #define TRUE 1
#define FALSE 0 #define FALSE 0
#define PORT 1025 #define PORT 1026
void *run_tcp_socket(void *arg) void *run_tcp_socket(void *arg)
{ {
@ -167,7 +164,9 @@ void *run_tcp_socket(void *arg)
//set the string terminating NULL byte on the end //set the string terminating NULL byte on the end
//of the data read //of the data read
buffer[valread] = '\0'; buffer[valread] = '\0';
send(sd, buffer, strlen(buffer), 0); //send(sd, buffer, strlen(buffer), 0);
printf("RECEIVED: %s\n", buffer);
handle_network_msg(buffer);
} }
} }
} }
@ -186,13 +185,17 @@ int add_tcp_conncection(char* ipv4, int port){
serv_addr.sin_addr.s_addr = inet_addr(ipv4); serv_addr.sin_addr.s_addr = inet_addr(ipv4);
serv_addr.sin_port = htons(port); serv_addr.sin_port = htons(port);
if(tcp_array_contains_address(serv_addr)) print_tcp_array();
if(tcp_array_contains_address(serv_addr)) {
return 0; return 0;
}
if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0)
{ {
fprintf(stderr,"ERROR connecting"); fprintf(stderr,"ERROR connecting\n");
//return 0; return 0;
} }
struct network_con_s tmp = struct network_con_s tmp =
@ -203,7 +206,7 @@ int add_tcp_conncection(char* ipv4, int port){
insert_to_tcp_array(tmp); insert_to_tcp_array(tmp);
printf("NEW TCP CONNECTION!!!"); printf("NEW TCP CONNECTION!!! to %s:%d\n", ipv4, port);
return 0; return 0;
} }

View file

@ -1338,6 +1338,27 @@ void print_tcp_entry(struct network_con_s entry)
printf("Conenctin to Port: %d\n", entry.sock_addr.sin_port); printf("Conenctin to Port: %d\n", entry.sock_addr.sin_port);
} }
void send_tcp(char* msg)
{
printf("SENDING TCP!\n");
pthread_mutex_lock(&tcp_array_mutex);
for (int i = 0; i <= tcp_entry_last; i++) {
if(send(network_array[i].sockfd, msg, strlen(msg), 0) < 0)
{
printf("Removing bad TCP connection!\n");
for (int j = i; j < client_entry_last; j++) {
network_array[j] = network_array[j + 1];
}
if (tcp_entry_last > -1) {
tcp_entry_last--;
}
}
}
pthread_mutex_unlock(&tcp_array_mutex);
}
void print_tcp_array() void print_tcp_array()
{ {
printf("--------Connections------\n"); printf("--------Connections------\n");
@ -1395,9 +1416,9 @@ int tcp_array_contains_address_help(struct sockaddr_in entry) {
if (entry.sin_addr.s_addr == network_array[i].sock_addr.sin_addr.s_addr) { if (entry.sin_addr.s_addr == network_array[i].sock_addr.sin_addr.s_addr) {
return 1; return 1;
} }
if (entry.sin_addr.s_addr > network_array[i].sock_addr.sin_addr.s_addr) { /*if (entry.sin_addr.s_addr > network_array[i].sock_addr.sin_addr.s_addr) {
return 0; return 0;
} }*/
} }
return 0; return 0;
} }

View file

@ -163,7 +163,7 @@ enum {
}; };
static const struct blobmsg_policy dawn_umdns_table_policy[__DAWN_UMDNS_TABLE_MAX] = { static const struct blobmsg_policy dawn_umdns_table_policy[__DAWN_UMDNS_TABLE_MAX] = {
[DAWN_UMDNS_TABLE] = {.name = "_dawn._udp", .type = BLOBMSG_TYPE_TABLE}, [DAWN_UMDNS_TABLE] = {.name = "_dawn._tcp", .type = BLOBMSG_TYPE_TABLE},
}; };
enum { enum {
@ -570,7 +570,8 @@ int send_blob_attr_via_network(struct blob_attr *msg, char* method)
//blobmsg_add_blob(&b, msg); //blobmsg_add_blob(&b, msg);
str = blobmsg_format_json(b_send_network.head, true); str = blobmsg_format_json(b_send_network.head, true);
send_string_enc(str); //send_string_enc(str);
send_tcp(str);
//free(str); //free(str);
//free(data_str); //free(data_str);
return 0; return 0;
@ -586,7 +587,6 @@ static int hostapd_notify(struct ubus_context *ctx, struct ubus_object *obj,
//TODO CHECK IF FREE IS CORREECT! //TODO CHECK IF FREE IS CORREECT!
free(str); free(str);
// TODO: Only handle probe request and NOT assoc, ... // TODO: Only handle probe request and NOT assoc, ...
if (strncmp(method, "probe", 5) == 0) { if (strncmp(method, "probe", 5) == 0) {