diff --git a/commands.c b/commands.c index bcb9767..18cf5f0 100644 --- a/commands.c +++ b/commands.c @@ -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(" Forcing adapter to keep using 8-bit mode!")); + mobile_debug_endl(adapter); packet->command = MOBILE_COMMAND_REINIT; s->mode_32bit = false; #endif @@ -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) @@ -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(" 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; @@ -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(" 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; diff --git a/config.c b/config.c index 73249ec..b2914e4 100644 --- a/config.c +++ b/config.c @@ -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; @@ -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); @@ -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); } @@ -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; +} \ No newline at end of file diff --git a/config.h b/config.h index 2a0bd8e..851b312 100644 --- a/config.h +++ b/config.h @@ -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]; }; diff --git a/mobile.h b/mobile.h index 43028ab..578e700 100644 --- a/mobile.h +++ b/mobile.h @@ -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 //