Use assigned address from SetSdkConnection instead of hard-coded address

master
PgSocks 6 months ago
parent 11c3ac43fb
commit c0f2b11db5

@ -7,7 +7,6 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/select.h> #include <sys/select.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@ -82,44 +81,49 @@ int max_fd = -1;
// TODO: Close the socket and return NULL on error // TODO: Close the socket and return NULL on error
struct Connection* struct Connection*
connection_new(unsigned int source_port, const char* source_ip, unsigned int dest_port, const char* dest_ip) connection_new(uint16_t source_port, uint32_t source_ip, uint16_t dest_port, uint32_t dest_ip)
{ {
struct Connection* conn = malloc(sizeof(struct Connection)); struct Connection* conn = malloc(sizeof(struct Connection));
memset(conn, 0, sizeof(struct Connection)); memset(conn, 0, sizeof(struct Connection));
// Request a UDP socket
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
// Set the source address and port if they are provided // Set the source address and port if they are provided
if(source_port && source_ip) { if(source_port && source_ip) {
struct sockaddr_in loc_addr; conn->local_addr.sin_family = AF_INET;
loc_addr.sin_family = AF_INET; conn->local_addr.sin_port = htons(source_port);
loc_addr.sin_port = htons(source_port); conn->local_addr.sin_addr.s_addr = source_ip;
loc_addr.sin_addr.s_addr = inet_addr(source_ip);
if(bind(conn->sockfd, (struct sockaddr*)&loc_addr, sizeof(loc_addr)) < 0)
{
perror("unable to bind local port");
exit(EXIT_FAILURE);
}
} }
// Make the socket non-blocking
int flags = fcntl(conn->sockfd, F_GETFL);
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
// Set the address of the drone // Set the address of the drone
memset(&conn->remote_addr, 0, sizeof(conn->remote_addr)); memset(&conn->remote_addr, 0, sizeof(conn->remote_addr));
conn->addrlen = sizeof(conn->remote_addr); conn->addrlen = sizeof(conn->remote_addr);
conn->remote_addr.sin_family = AF_INET; conn->remote_addr.sin_family = AF_INET;
conn->remote_addr.sin_port = htons(dest_port); conn->remote_addr.sin_port = htons(dest_port);
conn->remote_addr.sin_addr.s_addr = inet_addr(dest_ip); conn->remote_addr.sin_addr.s_addr = dest_ip;
return conn;
}
enum connection_error
connection_connect(struct Connection* conn) {
// Request a UDP socket
conn->sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if(conn->local_addr.sin_addr.s_addr
&& bind(conn->sockfd, (struct sockaddr*)&conn->local_addr, sizeof(conn->local_addr)) < 0)
return CONNECTION_LOCAL_BIND;
// Make the socket non-blocking
int flags = fcntl(conn->sockfd, F_GETFL);
fcntl(conn->sockfd, F_SETFL, flags | O_NONBLOCK);
// File descriptors are numbers that count up sequentially, // File descriptors are numbers that count up sequentially,
// so save the last one as the greatest file descriptor. // so save the last one as the greatest file descriptor.
// This is needed for polling the sockets later. // This is needed for polling the sockets later.
max_fd = conn->sockfd; max_fd = conn->sockfd;
return conn; return CONNECTION_NO_ERROR;
} }
struct Connection* struct Connection*

@ -6,14 +6,25 @@
#include <stddef.h> #include <stddef.h>
#include <netinet/in.h> #include <netinet/in.h>
#define ROBOT_DEFAULT_WIFI_ADDR 0X102A8C0
struct Connection { struct Connection {
int sockfd; int sockfd;
socklen_t addrlen; socklen_t addrlen;
struct sockaddr_in remote_addr; struct sockaddr_in remote_addr;
struct sockaddr_in local_addr;
}; };
struct Connection* struct Connection*
connection_new(unsigned int source_port, const char* source_ip, unsigned int dest_port, const char* dest_ip); connection_new(uint16_t source_port, uint32_t source_ip, uint16_t dest_port, uint32_t dest_ip);
enum connection_error {
CONNECTION_NO_ERROR,
CONNECTION_LOCAL_BIND
};
enum connection_error
connection_connect();
struct Connection* struct Connection*
connection_poll_ready(struct Client* client); connection_poll_ready(struct Client* client);

@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <arpa/inet.h>
#include <stdio.h> #include <stdio.h>
@ -119,7 +120,8 @@ robot_work(Robot robot) {
case CONNECTING_TO_PROXY_PORT: case CONNECTING_TO_PROXY_PORT:
{ {
robot->client->sdk_conn = connection_new(0, 0, 30030, "192.168.2.1"); robot->client->sdk_conn = connection_new(0, 0, 30030, ROBOT_DEFAULT_WIFI_ADDR);
connection_connect(robot->client->sdk_conn);
robot->state = SETTING_SDK_CONNECTION; robot->state = SETTING_SDK_CONNECTION;
break; break;
} }
@ -134,7 +136,8 @@ robot_work(Robot robot) {
case CONNECTING_TO_DEVICE_PORT: case CONNECTING_TO_DEVICE_PORT:
{ {
robot->client->dev_conn = connection_new(10010, "192.168.2.24", 20020, "192.168.2.1"); if(connection_connect(robot->client->dev_conn))
robot->state = STOPPED;
robot->state = SETTING_SDK_MODE; robot->state = SETTING_SDK_MODE;
break; break;
} }
@ -277,6 +280,7 @@ robot_work(Robot robot) {
robot->state = STOPPED; robot->state = STOPPED;
break; break;
} }
robot->client->dev_conn = connection_new(10010, resp.resp.sdkconn.config_ip, 20020, ROBOT_DEFAULT_WIFI_ADDR);
robot->state = CONNECTING_TO_DEVICE_PORT; robot->state = CONNECTING_TO_DEVICE_PORT;
break; break;
case SET_SDK_MODE_CMD: case SET_SDK_MODE_CMD:

Loading…
Cancel
Save