2018-05-16 17:35:21 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 "Eric Poulsen" <eric@zyxod.com>
|
|
|
|
*
|
|
|
|
* Based on the ESP IDF example code which is Public Domain / CC0
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "py/objtype.h"
|
|
|
|
#include "py/stream.h"
|
2021-07-09 00:19:15 -04:00
|
|
|
#include "shared/netutils/netutils.h"
|
2018-05-16 17:35:21 -04:00
|
|
|
#include "modmachine.h"
|
2023-05-08 19:52:54 -04:00
|
|
|
#include "ppp_set_auth.h"
|
2018-05-16 17:35:21 -04:00
|
|
|
|
|
|
|
#include "netif/ppp/ppp.h"
|
|
|
|
#include "netif/ppp/pppos.h"
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sockets.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/netdb.h"
|
|
|
|
#include "lwip/dns.h"
|
2019-01-27 20:36:47 -05:00
|
|
|
#include "netif/ppp/pppapi.h"
|
2018-05-16 17:35:21 -04:00
|
|
|
|
2019-05-08 10:30:04 -04:00
|
|
|
#define PPP_CLOSE_TIMEOUT_MS (4000)
|
|
|
|
|
2018-05-16 17:35:21 -04:00
|
|
|
typedef struct _ppp_if_obj_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
bool active;
|
|
|
|
bool connected;
|
2019-05-08 10:30:04 -04:00
|
|
|
volatile bool clean_close;
|
2018-05-16 17:35:21 -04:00
|
|
|
ppp_pcb *pcb;
|
|
|
|
mp_obj_t stream;
|
|
|
|
SemaphoreHandle_t inactiveWaitSem;
|
2019-05-08 10:30:04 -04:00
|
|
|
volatile TaskHandle_t client_task_handle;
|
2018-05-16 17:35:21 -04:00
|
|
|
struct netif pppif;
|
|
|
|
} ppp_if_obj_t;
|
|
|
|
|
|
|
|
const mp_obj_type_t ppp_if_type;
|
|
|
|
|
|
|
|
static void ppp_status_cb(ppp_pcb *pcb, int err_code, void *ctx) {
|
2020-02-26 23:36:53 -05:00
|
|
|
ppp_if_obj_t *self = ctx;
|
2018-05-16 17:35:21 -04:00
|
|
|
struct netif *pppif = ppp_netif(self->pcb);
|
|
|
|
|
|
|
|
switch (err_code) {
|
|
|
|
case PPPERR_NONE:
|
|
|
|
self->connected = (pppif->ip_addr.u_addr.ip4.addr != 0);
|
|
|
|
break;
|
|
|
|
case PPPERR_USER:
|
2019-05-08 10:30:04 -04:00
|
|
|
self->clean_close = true;
|
2018-05-16 17:35:21 -04:00
|
|
|
break;
|
|
|
|
case PPPERR_CONNECT:
|
|
|
|
self->connected = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t ppp_make_new(mp_obj_t stream) {
|
|
|
|
mp_get_stream_raise(stream, MP_STREAM_OP_READ | MP_STREAM_OP_WRITE);
|
|
|
|
|
|
|
|
ppp_if_obj_t *self = m_new_obj_with_finaliser(ppp_if_obj_t);
|
|
|
|
|
|
|
|
self->base.type = &ppp_if_type;
|
|
|
|
self->stream = stream;
|
|
|
|
self->active = false;
|
|
|
|
self->connected = false;
|
2019-05-08 10:30:04 -04:00
|
|
|
self->clean_close = false;
|
2018-05-16 17:35:21 -04:00
|
|
|
self->client_task_handle = NULL;
|
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
2023-01-31 22:20:45 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(esp_network_ppp_make_new_obj, ppp_make_new);
|
2018-05-16 17:35:21 -04:00
|
|
|
|
|
|
|
static u32_t ppp_output_callback(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx) {
|
|
|
|
ppp_if_obj_t *self = ctx;
|
|
|
|
int err;
|
|
|
|
return mp_stream_rw(self->stream, data, len, &err, MP_STREAM_RW_WRITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pppos_client_task(void *self_in) {
|
2020-02-26 23:36:53 -05:00
|
|
|
ppp_if_obj_t *self = (ppp_if_obj_t *)self_in;
|
2018-05-16 17:35:21 -04:00
|
|
|
uint8_t buf[256];
|
|
|
|
|
|
|
|
while (ulTaskNotifyTake(pdTRUE, 0) == 0) {
|
|
|
|
int err;
|
|
|
|
int len = mp_stream_rw(self->stream, buf, sizeof(buf), &err, 0);
|
|
|
|
if (len > 0) {
|
2020-02-26 23:36:53 -05:00
|
|
|
pppos_input_tcpip(self->pcb, (u8_t *)buf, len);
|
2018-05-16 17:35:21 -04:00
|
|
|
}
|
|
|
|
}
|
2019-05-08 10:30:04 -04:00
|
|
|
|
|
|
|
self->client_task_handle = NULL;
|
2018-05-16 17:35:21 -04:00
|
|
|
vTaskDelete(NULL);
|
2023-08-15 05:34:23 -04:00
|
|
|
for (;;) {
|
|
|
|
}
|
2018-05-16 17:35:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t ppp_active(size_t n_args, const mp_obj_t *args) {
|
|
|
|
ppp_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
|
|
|
|
if (n_args > 1) {
|
|
|
|
if (mp_obj_is_true(args[1])) {
|
|
|
|
if (self->active) {
|
|
|
|
return mp_const_true;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->pcb = pppapi_pppos_create(&self->pppif, ppp_output_callback, ppp_status_cb, self);
|
|
|
|
|
|
|
|
if (self->pcb == NULL) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("init failed"));
|
2018-05-16 17:35:21 -04:00
|
|
|
}
|
|
|
|
self->active = true;
|
|
|
|
} else {
|
|
|
|
if (!self->active) {
|
|
|
|
return mp_const_false;
|
|
|
|
}
|
|
|
|
|
2019-07-09 11:30:38 -04:00
|
|
|
if (self->client_task_handle != NULL) { // is connecting or connected?
|
|
|
|
// Wait for PPPERR_USER, with timeout
|
|
|
|
pppapi_close(self->pcb, 0);
|
|
|
|
uint32_t t0 = mp_hal_ticks_ms();
|
|
|
|
while (!self->clean_close && mp_hal_ticks_ms() - t0 < PPP_CLOSE_TIMEOUT_MS) {
|
|
|
|
mp_hal_delay_ms(10);
|
|
|
|
}
|
2018-05-16 17:35:21 -04:00
|
|
|
|
2019-07-09 11:30:38 -04:00
|
|
|
// Shutdown task
|
|
|
|
xTaskNotifyGive(self->client_task_handle);
|
|
|
|
t0 = mp_hal_ticks_ms();
|
|
|
|
while (self->client_task_handle != NULL && mp_hal_ticks_ms() - t0 < PPP_CLOSE_TIMEOUT_MS) {
|
|
|
|
mp_hal_delay_ms(10);
|
|
|
|
}
|
2018-05-16 17:35:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release PPP
|
|
|
|
pppapi_free(self->pcb);
|
|
|
|
self->pcb = NULL;
|
|
|
|
self->active = false;
|
|
|
|
self->connected = false;
|
2019-05-08 10:30:04 -04:00
|
|
|
self->clean_close = false;
|
2018-05-16 17:35:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_obj_new_bool(self->active);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ppp_active_obj, 1, 2, ppp_active);
|
|
|
|
|
2019-07-09 11:30:38 -04:00
|
|
|
STATIC mp_obj_t ppp_connect_py(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
|
|
|
enum { ARG_authmode, ARG_username, ARG_password };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_authmode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PPPAUTHTYPE_NONE} },
|
2019-12-15 23:40:05 -05:00
|
|
|
{ MP_QSTR_username, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
|
|
|
{ MP_QSTR_password, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_NONE} },
|
2019-07-09 11:30:38 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
mp_arg_val_t parsed_args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, parsed_args);
|
|
|
|
|
|
|
|
ppp_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
|
|
|
|
if (!self->active) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("must be active"));
|
2019-07-09 11:30:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (self->client_task_handle != NULL) {
|
|
|
|
mp_raise_OSError(MP_EALREADY);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (parsed_args[ARG_authmode].u_int) {
|
|
|
|
case PPPAUTHTYPE_NONE:
|
|
|
|
case PPPAUTHTYPE_PAP:
|
|
|
|
case PPPAUTHTYPE_CHAP:
|
|
|
|
break;
|
|
|
|
default:
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("invalid auth"));
|
2019-07-09 11:30:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parsed_args[ARG_authmode].u_int != PPPAUTHTYPE_NONE) {
|
2020-02-26 23:36:53 -05:00
|
|
|
const char *username_str = mp_obj_str_get_str(parsed_args[ARG_username].u_obj);
|
|
|
|
const char *password_str = mp_obj_str_get_str(parsed_args[ARG_password].u_obj);
|
2019-07-09 11:30:38 -04:00
|
|
|
pppapi_set_auth(self->pcb, parsed_args[ARG_authmode].u_int, username_str, password_str);
|
|
|
|
}
|
|
|
|
if (pppapi_set_default(self->pcb) != ESP_OK) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("set default failed"));
|
2019-07-09 11:30:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ppp_set_usepeerdns(self->pcb, true);
|
|
|
|
|
|
|
|
if (pppapi_connect(self->pcb, 0) != ESP_OK) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("connect failed"));
|
2019-07-09 11:30:38 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
if (xTaskCreatePinnedToCore(pppos_client_task, "ppp", 2048, self, 1, (TaskHandle_t *)&self->client_task_handle, MP_TASK_COREID) != pdPASS) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("failed to create worker task"));
|
2019-07-09 11:30:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(ppp_connect_obj, 1, ppp_connect_py);
|
|
|
|
|
2018-05-16 17:35:21 -04:00
|
|
|
STATIC mp_obj_t ppp_delete(mp_obj_t self_in) {
|
2020-02-26 23:36:53 -05:00
|
|
|
ppp_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2018-05-16 17:35:21 -04:00
|
|
|
mp_obj_t args[] = {self, mp_const_false};
|
|
|
|
ppp_active(2, args);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(ppp_delete_obj, ppp_delete);
|
|
|
|
|
|
|
|
STATIC mp_obj_t ppp_ifconfig(size_t n_args, const mp_obj_t *args) {
|
|
|
|
ppp_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
if (n_args == 1) {
|
|
|
|
// get
|
2020-04-10 22:17:48 -04:00
|
|
|
const ip_addr_t *dns;
|
2018-05-16 17:35:21 -04:00
|
|
|
if (self->pcb != NULL) {
|
|
|
|
dns = dns_getserver(0);
|
|
|
|
struct netif *pppif = ppp_netif(self->pcb);
|
|
|
|
mp_obj_t tuple[4] = {
|
2020-02-26 23:36:53 -05:00
|
|
|
netutils_format_ipv4_addr((uint8_t *)&pppif->ip_addr, NETUTILS_BIG),
|
|
|
|
netutils_format_ipv4_addr((uint8_t *)&pppif->gw, NETUTILS_BIG),
|
|
|
|
netutils_format_ipv4_addr((uint8_t *)&pppif->netmask, NETUTILS_BIG),
|
2020-04-10 22:17:48 -04:00
|
|
|
netutils_format_ipv4_addr((uint8_t *)dns, NETUTILS_BIG),
|
2018-05-16 17:35:21 -04:00
|
|
|
};
|
|
|
|
return mp_obj_new_tuple(4, tuple);
|
|
|
|
} else {
|
|
|
|
mp_obj_t tuple[4] = { mp_const_none, mp_const_none, mp_const_none, mp_const_none };
|
|
|
|
return mp_obj_new_tuple(4, tuple);
|
|
|
|
}
|
|
|
|
} else {
|
2020-04-10 22:17:48 -04:00
|
|
|
ip_addr_t dns;
|
2018-05-16 17:35:21 -04:00
|
|
|
mp_obj_t *items;
|
|
|
|
mp_obj_get_array_fixed_n(args[1], 4, &items);
|
2020-02-26 23:36:53 -05:00
|
|
|
netutils_parse_ipv4_addr(items[3], (uint8_t *)&dns.u_addr.ip4, NETUTILS_BIG);
|
2018-05-16 17:35:21 -04:00
|
|
|
dns_setserver(0, &dns);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ppp_ifconfig_obj, 1, 2, ppp_ifconfig);
|
|
|
|
|
|
|
|
STATIC mp_obj_t ppp_status(mp_obj_t self_in) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ppp_status_obj, ppp_status);
|
|
|
|
|
|
|
|
STATIC mp_obj_t ppp_isconnected(mp_obj_t self_in) {
|
|
|
|
ppp_if_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mp_obj_new_bool(self->connected);
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_1(ppp_isconnected_obj, ppp_isconnected);
|
|
|
|
|
esp32/modnetwork: Add support for SO_BINDTODEVICE socket option.
This implements support for SO_BINDTODEVICE, which allows telling a socket
to use a specific interface instead of lwIP automatically selecting one.
This allows devices that have multiple connections (for example cellular
over PPP in addition to WLAN) to explicitly choose which data is send over
which connection, which may have different reliability and or (mobile data)
costs associated with using them.
The used lwIP network stack already has support for this, so all that was
needed was to expose this functionality in MicroPython. This commit
exposes a new constant SO_BINDTODEVICE which can be set as an socket
option. As a value it expects the name of the interface to bind to. These
names can be retrieved using `.config('ifname')` implemented on each
interface type (including adding in this commit a `.config()` method to
PPP, which it didn't have before), which returns a string with the
interface name:
>>> import machine
>>> import network
>>> network.WLAN(network.AP_IF).config('ifname')
'lo0'
>>> wlan = network.WLAN(network.AP_IF)
>>> wlan.active(True) and wlan.config('ifname')
'ap1'
>>> wlan = network.WLAN(network.STA_IF)
>>> wlan.active(True) and wlan.config('ifname')
'st1'
>>> ppp = network.PPP(machine.UART(0))
>>> ppp.active(True) and ppp.config('ifname')
'pp1'
>>> ppp = network.PPP(machine.UART(0))
>>> ppp.active(True) and ppp.config('ifname')
'pp2'
>>> ppp = network.PPP(machine.UART(0))
>>> ppp.active(True) and ppp.config('ifname')
'pp3'
Note that lo0 seems to be returned by lwIP if the interface is not yet
active. The method can also return None in the case of PPP where the
entire lwIP interface doesn't yet exist before being activated. Currently
no effort is made to unify those cases; it is expected that whatever we
receive from lwIP is valid.
When the socket option is set, this forces using a specific device:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, 'st1')
setsockopt will throw (OSError: [Errno 19] ENODEV) if the specified
interface does not exist.
Tested with LAN, WLAN, and PPP; can specify which interface should be used
and when testing with, for example, HTTP requests to ifconfig.co the
returned IP address confirms a specific interface was used.
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2023-07-04 09:36:37 -04:00
|
|
|
STATIC mp_obj_t ppp_config(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
|
|
|
if (n_args != 1 && kwargs->used != 0) {
|
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("either pos or kw args are allowed"));
|
|
|
|
}
|
|
|
|
ppp_if_obj_t *self = MP_OBJ_TO_PTR(args[0]);
|
|
|
|
|
|
|
|
if (kwargs->used != 0) {
|
|
|
|
for (size_t i = 0; i < kwargs->alloc; i++) {
|
|
|
|
if (mp_map_slot_is_filled(kwargs, i)) {
|
|
|
|
switch (mp_obj_str_get_qstr(kwargs->table[i].key)) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n_args != 2) {
|
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("can query only one param"));
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t val = mp_const_none;
|
|
|
|
|
|
|
|
switch (mp_obj_str_get_qstr(args[1])) {
|
|
|
|
case MP_QSTR_ifname: {
|
|
|
|
if (self->pcb != NULL) {
|
|
|
|
struct netif *pppif = ppp_netif(self->pcb);
|
|
|
|
char ifname[NETIF_NAMESIZE + 1] = {0};
|
|
|
|
netif_index_to_name(netif_get_index(pppif), ifname);
|
|
|
|
if (ifname[0] != 0) {
|
|
|
|
val = mp_obj_new_str((char *)ifname, strlen(ifname));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
mp_raise_ValueError(MP_ERROR_TEXT("unknown config param"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(ppp_config_obj, 1, ppp_config);
|
|
|
|
|
2018-05-16 17:35:21 -04:00
|
|
|
STATIC const mp_rom_map_elem_t ppp_if_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_active), MP_ROM_PTR(&ppp_active_obj) },
|
2019-07-09 11:30:38 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&ppp_connect_obj) },
|
2018-05-16 17:35:21 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_isconnected), MP_ROM_PTR(&ppp_isconnected_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_status), MP_ROM_PTR(&ppp_status_obj) },
|
esp32/modnetwork: Add support for SO_BINDTODEVICE socket option.
This implements support for SO_BINDTODEVICE, which allows telling a socket
to use a specific interface instead of lwIP automatically selecting one.
This allows devices that have multiple connections (for example cellular
over PPP in addition to WLAN) to explicitly choose which data is send over
which connection, which may have different reliability and or (mobile data)
costs associated with using them.
The used lwIP network stack already has support for this, so all that was
needed was to expose this functionality in MicroPython. This commit
exposes a new constant SO_BINDTODEVICE which can be set as an socket
option. As a value it expects the name of the interface to bind to. These
names can be retrieved using `.config('ifname')` implemented on each
interface type (including adding in this commit a `.config()` method to
PPP, which it didn't have before), which returns a string with the
interface name:
>>> import machine
>>> import network
>>> network.WLAN(network.AP_IF).config('ifname')
'lo0'
>>> wlan = network.WLAN(network.AP_IF)
>>> wlan.active(True) and wlan.config('ifname')
'ap1'
>>> wlan = network.WLAN(network.STA_IF)
>>> wlan.active(True) and wlan.config('ifname')
'st1'
>>> ppp = network.PPP(machine.UART(0))
>>> ppp.active(True) and ppp.config('ifname')
'pp1'
>>> ppp = network.PPP(machine.UART(0))
>>> ppp.active(True) and ppp.config('ifname')
'pp2'
>>> ppp = network.PPP(machine.UART(0))
>>> ppp.active(True) and ppp.config('ifname')
'pp3'
Note that lo0 seems to be returned by lwIP if the interface is not yet
active. The method can also return None in the case of PPP where the
entire lwIP interface doesn't yet exist before being activated. Currently
no effort is made to unify those cases; it is expected that whatever we
receive from lwIP is valid.
When the socket option is set, this forces using a specific device:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BINDTODEVICE, 'st1')
setsockopt will throw (OSError: [Errno 19] ENODEV) if the specified
interface does not exist.
Tested with LAN, WLAN, and PPP; can specify which interface should be used
and when testing with, for example, HTTP requests to ifconfig.co the
returned IP address confirms a specific interface was used.
Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2023-07-04 09:36:37 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_config), MP_ROM_PTR(&ppp_config_obj) },
|
2018-05-16 17:35:21 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ifconfig), MP_ROM_PTR(&ppp_ifconfig_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&ppp_delete_obj) },
|
2019-07-09 11:30:38 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_AUTH_NONE), MP_ROM_INT(PPPAUTHTYPE_NONE) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_AUTH_PAP), MP_ROM_INT(PPPAUTHTYPE_PAP) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_AUTH_CHAP), MP_ROM_INT(PPPAUTHTYPE_CHAP) },
|
2018-05-16 17:35:21 -04:00
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(ppp_if_locals_dict, ppp_if_locals_dict_table);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
ppp_if_type,
|
|
|
|
MP_QSTR_PPP,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-06-24 02:27:46 -04:00
|
|
|
locals_dict, &ppp_if_locals_dict
|
2021-07-14 00:38:38 -04:00
|
|
|
);
|