Skip to content

Commit c2f23fd

Browse files
authored
Merge pull request #3278 from xiangxistu/master
[net][lwip] add personalized RT-Thread sign into lwip protocol stack
2 parents df933cf + f9ef8c7 commit c2f23fd

File tree

6 files changed

+97
-54
lines changed

6 files changed

+97
-54
lines changed

components/net/lwip-1.4.1/src/lwipopts.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,8 @@
264264
#endif
265265

266266
/*
267-
* You can re-define following setting in rtcofnig.h to overwrite the default
268-
* setting in the lwip opts.h
267+
* You can re-define following setting in rtcofnig.h to overwrite the default
268+
* setting in the lwip opts.h
269269
*/
270270
/* MEMP_NUM_NETBUF: the number of struct netbufs. */
271271
// #define MEMP_NUM_NETBUF 2
@@ -499,6 +499,14 @@
499499

500500
/* no read/write/close for socket */
501501
#define LWIP_POSIX_SOCKETS_IO_NAMES 0
502+
503+
/**
504+
* LWIP_NETIF_HOSTNAME==1: Support netif hostname
505+
*/
506+
#ifndef LWIP_NETIF_HOSTNAME
507+
#define LWIP_NETIF_HOSTNAME 1
508+
#endif
509+
502510
#define LWIP_NETIF_API 1
503511

504512
/* MEMP_NUM_SYS_TIMEOUT: the number of simulateously active timeouts. */

components/net/lwip-1.4.1/src/netif/ethernetif.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static int lwip_netdev_set_dhcp(struct netdev *netif, rt_bool_t is_enabled)
165165
extern int lwip_ping_recv(int s, int *ttl);
166166
extern err_t lwip_ping_send(int s, ip_addr_t *addr, int size);
167167

168-
int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
168+
int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
169169
uint32_t timeout, struct netdev_ping_resp *ping_resp)
170170
{
171171
int s, ttl, recv_len, result = 0;
@@ -180,7 +180,7 @@ int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
180180
struct addrinfo hint, *res = RT_NULL;
181181
struct sockaddr_in *h = RT_NULL;
182182
struct in_addr ina;
183-
183+
184184
RT_ASSERT(netif);
185185
RT_ASSERT(host);
186186
RT_ASSERT(ping_resp);
@@ -199,7 +199,7 @@ int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
199199
return -RT_ERROR;
200200
}
201201
rt_memcpy(&(ping_resp->ip_addr), &target_addr, sizeof(ip_addr_t));
202-
202+
203203
/* new a socket */
204204
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
205205
{
@@ -267,7 +267,7 @@ const struct netdev_ops lwip_netdev_ops =
267267
lwip_netdev_set_addr_info,
268268
#ifdef RT_LWIP_DNS
269269
lwip_netdev_set_dns_server,
270-
#else
270+
#else
271271
NULL,
272272
#endif /* RT_LWIP_DNS */
273273

@@ -315,7 +315,7 @@ static int netdev_add(struct netif *lwip_netif)
315315

316316
rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN);
317317
result = netdev_register(netdev, name, (void *)lwip_netif);
318-
318+
319319
/* Update netdev info after registered */
320320
netdev->flags = lwip_netif->flags;
321321
netdev->mtu = lwip_netif->mtu;
@@ -325,7 +325,7 @@ static int netdev_add(struct netif *lwip_netif)
325325
netdev->ip_addr = lwip_netif->ip_addr;
326326
netdev->gw = lwip_netif->gw;
327327
netdev->netmask = lwip_netif->netmask;
328-
328+
329329
#ifdef RT_LWIP_DHCP
330330
netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
331331
#endif
@@ -440,8 +440,13 @@ static err_t eth_netif_device_init(struct netif *netif)
440440
rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_uint16_t flags)
441441
{
442442
struct netif* netif;
443-
443+
#if LWIP_NETIF_HOSTNAME
444+
#define LWIP_HOSTNAME_LEN 16
445+
char *hostname = RT_NULL;
446+
netif = (struct netif*) rt_malloc (sizeof(struct netif) + LWIP_HOSTNAME_LEN);
447+
#else
444448
netif = (struct netif*) rt_malloc (sizeof(struct netif));
449+
#endif
445450
if (netif == RT_NULL)
446451
{
447452
rt_kprintf("malloc netif failed\n");
@@ -476,6 +481,13 @@ rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_
476481
netif->output = etharp_output;
477482
netif->linkoutput = ethernetif_linkoutput;
478483

484+
#if LWIP_NETIF_HOSTNAME
485+
/* Initialize interface hostname */
486+
hostname = (char *)netif + sizeof(struct netif);
487+
rt_sprintf(hostname, "rtthread_%02x%02x", name[0], name[1]);
488+
netif->hostname = hostname;
489+
#endif /* LWIP_NETIF_HOSTNAME */
490+
479491
/* if tcp thread has been started up, we add this netif to the system */
480492
if (rt_thread_find("tcpip") != RT_NULL)
481493
{
@@ -630,7 +642,7 @@ static void eth_rx_thread_entry(void* parameter)
630642
while (1)
631643
{
632644
if(device->eth_rx == RT_NULL) break;
633-
645+
634646
p = device->eth_rx(&(device->parent));
635647
if (p != RT_NULL)
636648
{

components/net/lwip-2.0.2/src/lwipopts.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,13 @@
531531
#define LWIP_TCP_KEEPALIVE 1
532532
#endif
533533

534+
/**
535+
* LWIP_NETIF_HOSTNAME==1: Support netif hostname
536+
*/
537+
#ifndef LWIP_NETIF_HOSTNAME
538+
#define LWIP_NETIF_HOSTNAME 1
539+
#endif
540+
534541
/**
535542
* LWIP_NETIF_API==1: Support netif api (in netifapi.c)
536543
*/

components/net/lwip-2.0.2/src/netif/ethernetif.c

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ static int lwip_netdev_set_dhcp(struct netdev *netif, rt_bool_t is_enabled)
173173
extern int lwip_ping_recv(int s, int *ttl);
174174
extern err_t lwip_ping_send(int s, ip_addr_t *addr, int size);
175175

176-
int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
176+
int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
177177
uint32_t timeout, struct netdev_ping_resp *ping_resp)
178178
{
179179
int s, ttl, recv_len, result = 0;
@@ -188,7 +188,7 @@ int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
188188
struct addrinfo hint, *res = RT_NULL;
189189
struct sockaddr_in *h = RT_NULL;
190190
struct in_addr ina;
191-
191+
192192
RT_ASSERT(netif);
193193
RT_ASSERT(host);
194194
RT_ASSERT(ping_resp);
@@ -207,7 +207,7 @@ int lwip_netdev_ping(struct netdev *netif, const char *host, size_t data_len,
207207
return -RT_ERROR;
208208
}
209209
rt_memcpy(&(ping_resp->ip_addr), &target_addr, sizeof(ip_addr_t));
210-
210+
211211
/* new a socket */
212212
if ((s = lwip_socket(AF_INET, SOCK_RAW, IP_PROTO_ICMP)) < 0)
213213
{
@@ -275,7 +275,7 @@ const struct netdev_ops lwip_netdev_ops =
275275
lwip_netdev_set_addr_info,
276276
#ifdef RT_LWIP_DNS
277277
lwip_netdev_set_dns_server,
278-
#else
278+
#else
279279
NULL,
280280
#endif /* RT_LWIP_DNS */
281281

@@ -323,8 +323,8 @@ static int netdev_add(struct netif *lwip_netif)
323323

324324
rt_strncpy(name, lwip_netif->name, LWIP_NETIF_NAME_LEN);
325325
result = netdev_register(netdev, name, (void *)lwip_netif);
326-
327-
/* Update netdev info after registered */
326+
327+
/* Update netdev info after registered */
328328
netdev->flags = lwip_netif->flags;
329329
netdev->mtu = lwip_netif->mtu;
330330
netdev->ops = &lwip_netdev_ops;
@@ -333,7 +333,7 @@ static int netdev_add(struct netif *lwip_netif)
333333
netdev->ip_addr = lwip_netif->ip_addr;
334334
netdev->gw = lwip_netif->gw;
335335
netdev->netmask = lwip_netif->netmask;
336-
336+
337337
#ifdef RT_LWIP_DHCP
338338
netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
339339
#endif
@@ -412,7 +412,7 @@ static err_t eth_netif_device_init(struct netif *netif)
412412
/* network interface device register */
413413
netdev_add(netif);
414414
#endif /* RT_USING_NETDEV */
415-
415+
416416
ethif = (struct eth_device*)netif->state;
417417
if (ethif != RT_NULL)
418418
{
@@ -481,8 +481,13 @@ static err_t eth_netif_device_init(struct netif *netif)
481481
rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_uint16_t flags)
482482
{
483483
struct netif* netif;
484-
484+
#if LWIP_NETIF_HOSTNAME
485+
#define LWIP_HOSTNAME_LEN 16
486+
char *hostname = RT_NULL;
487+
netif = (struct netif*) rt_malloc (sizeof(struct netif) + LWIP_HOSTNAME_LEN);
488+
#else
485489
netif = (struct netif*) rt_malloc (sizeof(struct netif));
490+
#endif
486491
if (netif == RT_NULL)
487492
{
488493
rt_kprintf("malloc netif failed\n");
@@ -519,7 +524,9 @@ rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_
519524

520525
#if LWIP_NETIF_HOSTNAME
521526
/* Initialize interface hostname */
522-
netif->hostname = "rtthread";
527+
hostname = (char *)netif + sizeof(struct netif);
528+
rt_sprintf(hostname, "rtthread_%02x%02x", name[0], name[1]);
529+
netif->hostname = hostname;
523530
#endif /* LWIP_NETIF_HOSTNAME */
524531

525532
/* if tcp thread has been started up, we add this netif to the system */
@@ -531,7 +538,7 @@ rt_err_t eth_device_init_with_flag(struct eth_device *dev, const char *name, rt_
531538
ipaddr.addr = inet_addr(RT_LWIP_IPADDR);
532539
gw.addr = inet_addr(RT_LWIP_GWADDR);
533540
netmask.addr = inet_addr(RT_LWIP_MSKADDR);
534-
#else
541+
#else
535542
IP4_ADDR(&ipaddr, 0, 0, 0, 0);
536543
IP4_ADDR(&gw, 0, 0, 0, 0);
537544
IP4_ADDR(&netmask, 0, 0, 0, 0);
@@ -683,7 +690,7 @@ static void eth_rx_thread_entry(void* parameter)
683690
while (1)
684691
{
685692
if(device->eth_rx == RT_NULL) break;
686-
693+
687694
p = device->eth_rx(&(device->parent));
688695
if (p != RT_NULL)
689696
{
@@ -706,9 +713,9 @@ static void eth_rx_thread_entry(void* parameter)
706713
}
707714
#endif
708715

709-
/* this function does not need,
710-
* use eth_system_device_init_private()
711-
* call by lwip_system_init().
716+
/* this function does not need,
717+
* use eth_system_device_init_private()
718+
* call by lwip_system_init().
712719
*/
713720
int eth_system_device_init(void)
714721
{
@@ -855,22 +862,22 @@ void list_if(void)
855862
ip6_addr_t *addr;
856863
int addr_state;
857864
int i;
858-
865+
859866
addr = (ip6_addr_t *)&netif->ip6_addr[0];
860867
addr_state = netif->ip6_addr_state[0];
861-
862-
rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr),
868+
869+
rt_kprintf("\nipv6 link-local: %s state:%02X %s\n", ip6addr_ntoa(addr),
863870
addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
864-
871+
865872
for(i=1; i<LWIP_IPV6_NUM_ADDRESSES; i++)
866873
{
867874
addr = (ip6_addr_t *)&netif->ip6_addr[i];
868875
addr_state = netif->ip6_addr_state[i];
869-
870-
rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr),
876+
877+
rt_kprintf("ipv6[%d] address: %s state:%02X %s\n", i, ip6addr_ntoa(addr),
871878
addr_state, ip6_addr_isvalid(addr_state)?"VALID":"INVALID");
872879
}
873-
880+
874881
}
875882
rt_kprintf("\r\n");
876883
#endif /* LWIP_IPV6 */

components/net/lwip-2.1.0/src/lwipopts.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,13 @@
538538
#define LWIP_TCP_KEEPALIVE 1
539539
#endif
540540

541+
/**
542+
* LWIP_NETIF_HOSTNAME==1: Support netif hostname
543+
*/
544+
#ifndef LWIP_NETIF_HOSTNAME
545+
#define LWIP_NETIF_HOSTNAME 1
546+
#endif
547+
541548
/**
542549
* LWIP_NETIF_API==1: Support netif api (in netifapi.c)
543550
*/

0 commit comments

Comments
 (0)