Use assigned address from SetSdkConnection instead of hard-coded address

master
PgSocks 7 months ago
parent 11c3ac43fb
commit c0f2b11db5

@ -7,7 +7,6 @@
#include <sys/socket.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <fcntl.h>
#include <stdio.h>
@ -82,44 +81,49 @@ int max_fd = -1;
// TODO: Close the socket and return NULL on error
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));
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
if(source_port && source_ip) {
struct sockaddr_in loc_addr;
loc_addr.sin_family = AF_INET;
loc_addr.sin_port = htons(source_port);
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);
}
conn->local_addr.sin_family = AF_INET;
conn->local_addr.sin_port = htons(source_port);
conn->local_addr.sin_addr.s_addr = source_ip;
}
// 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
memset(&conn->remote_addr, 0, sizeof(conn->remote_addr));
conn->addrlen = sizeof(conn->remote_addr);
conn->remote_addr.sin_family = AF_INET;
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,
// so save the last one as the greatest file descriptor.
// This is needed for polling the sockets later.
max_fd = conn->sockfd;
return conn;
return CONNECTION_NO_ERROR;
}
struct Connection*

@ -6,14 +6,25 @@
#include <stddef.h>
#include <netinet/in.h>
#define ROBOT_DEFAULT_WIFI_ADDR 0X102A8C0
struct Connection {
int sockfd;
socklen_t addrlen;
struct sockaddr_in remote_addr;
struct sockaddr_in local_addr;
};
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*
connection_poll_ready(struct Client* client);

@ -4,6 +4,7 @@
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <stdio.h>
@ -119,7 +120,8 @@ robot_work(Robot robot) {
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;
break;
}
@ -134,7 +136,8 @@ robot_work(Robot robot) {
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;
break;
}
@ -277,6 +280,7 @@ robot_work(Robot robot) {
robot->state = STOPPED;
break;
}
robot->client->dev_conn = connection_new(10010, resp.resp.sdkconn.config_ip, 20020, ROBOT_DEFAULT_WIFI_ADDR);
robot->state = CONNECTING_TO_DEVICE_PORT;
break;
case SET_SDK_MODE_CMD:

Loading…
Cancel
Save