Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,8 @@ static struct mobile_packet *command_change_clock(struct mobile_adapter *adapter
// Replying with a different command in the header tricks the official GBA
// library into never changing its serial mode. Using the REINIT command
// forces it to set its serial mode to 8-bit.
mobile_debug_print(adapter, PSTR("<NO32BIT> Forcing adapter to keep using 8-bit mode!"));
mobile_debug_endl(adapter);
packet->command = MOBILE_COMMAND_REINIT;
s->mode_32bit = false;
#endif
Expand Down Expand Up @@ -882,7 +884,8 @@ enum process_tcp_connect {
};

enum procdata_tcp_connect {
PROCDATA_TCP_CONNECT_CONN
PROCDATA_TCP_CONNECT_CONN,
PROCDATA_TCP_CONNECT_FALLBACK
};

static struct mobile_packet *command_tcp_connect_begin(struct mobile_adapter *adapter, struct mobile_packet *packet)
Expand All @@ -904,6 +907,15 @@ static struct mobile_packet *command_tcp_connect_begin(struct mobile_adapter *ad
}
s->connections[conn] = true;

b->processing_data[PROCDATA_TCP_CONNECT_FALLBACK] = 1;
if (adapter->config.mail_port) {
if ((packet->data[4] << 8 | packet->data[5]) == 25) {
mobile_debug_print(adapter, PSTR("<SMTP> Replacing port 25 to 587!"));
mobile_debug_endl(adapter);
b->processing_data[PROCDATA_TCP_CONNECT_FALLBACK] = 0;
}
}

b->processing_data[PROCDATA_TCP_CONNECT_CONN] = conn;
b->processing = PROCESS_TCP_CONNECT_CONNECTING;
return NULL;
Expand All @@ -920,15 +932,42 @@ static struct mobile_packet *command_tcp_connect_connecting(struct mobile_adapte
.type = MOBILE_ADDRTYPE_IPV4,
.port = packet->data[4] << 8 | packet->data[5],
};

if (adapter->config.mail_port) {
if (!b->processing_data[PROCDATA_TCP_CONNECT_FALLBACK]) {
if (addr.port == 25) addr.port = 587;
}
}

memcpy(addr.host, packet->data, 4);

int rc = mobile_cb_sock_connect(adapter, conn,
(struct mobile_addr *)&addr);
if (rc == 0) return NULL;
if (rc < 0) {
mobile_cb_sock_close(adapter, conn);
s->connections[conn] = false;
return error_packet(packet, 3);

if (!b->processing_data[PROCDATA_TCP_CONNECT_FALLBACK]) {
mobile_debug_print(adapter, PSTR("<SMTP> Failed to connect to port 587, trying port 25!"));
mobile_debug_endl(adapter);

mobile_cb_sock_close(adapter, conn);
if (!mobile_cb_sock_open(adapter, conn, MOBILE_SOCKTYPE_TCP,
MOBILE_ADDRTYPE_IPV4, 0)) {
s->connections[conn] = false;
return error_packet(packet, 3);
}

b->processing_data[PROCDATA_TCP_CONNECT_FALLBACK] = 1;
addr.port = 25;
rc = mobile_cb_sock_connect(adapter, conn, (struct mobile_addr *)&addr);
if (rc == 0) return NULL;
}

if (rc < 0) {
mobile_cb_sock_close(adapter, conn);
s->connections[conn] = false;
return error_packet(packet, 3);
}
}

packet->data[0] = conn;
Expand Down
16 changes: 15 additions & 1 deletion config.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ static bool config_library_load(struct mobile_adapter *adapter)
config->p2p_port |= buffer[0x09] << 8;
config->relay.type = buffer[0x0a];
config->relay_token_init = buffer[0x0b];
config->mail_port = buffer[0x0c];

if (!config_check_addrtype(config->dns1.type)) return false;
if (!config_check_addrtype(config->dns2.type)) return false;
Expand Down Expand Up @@ -159,8 +160,9 @@ static void config_library_save(struct mobile_adapter *adapter)
buffer[0x09] = config->p2p_port >> 8;
buffer[0x0a] = config->relay.type;
buffer[0x0b] = config->relay_token_init;
buffer[0x0c] = config->mail_port;

// 0x0c - 0x19 unused
// 0x0d - 0x19 unused

config_library_save_host(&config->dns1, buffer + 0x20, buffer + 0x1a);
config_library_save_host(&config->dns2, buffer + 0x30, buffer + 0x1c);
Expand Down Expand Up @@ -191,6 +193,7 @@ void mobile_config_init(struct mobile_adapter *adapter)
adapter->config.p2p_port = MOBILE_DEFAULT_P2P_PORT;
adapter->config.relay = (struct mobile_addr){.type = MOBILE_ADDRTYPE_NONE};
adapter->config.relay_token_init = false;
adapter->config.mail_port = true;
memset(adapter->config.relay_token, 0, MOBILE_RELAY_TOKEN_SIZE);
}

Expand Down Expand Up @@ -307,3 +310,14 @@ bool mobile_config_get_relay_token(struct mobile_adapter *adapter, unsigned char
memcpy(token, adapter->config.relay_token, MOBILE_RELAY_TOKEN_SIZE);
return true;
}

void mobile_config_set_alt_mail(struct mobile_adapter *adapter, bool alt_mail)
{
adapter->config.mail_port = alt_mail;
mobile_config_apply(adapter);
}

void mobile_config_get_alt_mail(struct mobile_adapter *adapter, bool *alt_mail)
{
*alt_mail = adapter->config.mail_port;
}
3 changes: 3 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ struct mobile_adapter_config {
// for p2p communication, instead of direct TCP connections
struct mobile_addr relay;

// Makes the adapter use port 587 instead 25 for SMTP connections.
bool mail_port: 1;

// Authentication token used for relay connections
unsigned char relay_token[MOBILE_RELAY_TOKEN_SIZE];
};
Expand Down
2 changes: 2 additions & 0 deletions mobile.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ void mobile_config_set_relay(struct mobile_adapter *adapter, const struct mobile
void mobile_config_get_relay(struct mobile_adapter *adapter, struct mobile_addr *relay);
void mobile_config_set_relay_token(struct mobile_adapter *adapter, const unsigned char *token);
bool mobile_config_get_relay_token(struct mobile_adapter *adapter, unsigned char *token);
void mobile_config_set_alt_mail(struct mobile_adapter *adapter, bool alt_mail);
void mobile_config_get_alt_mail(struct mobile_adapter *adapter, bool *alt_mail);

// mobile_config_load - Manually force a load of the configuration values
//
Expand Down