?? nedk_bridge.c
字號:
// | File: nedk_bridge.c
// |
// |
// | A simple ethernet-ethernet bridge, echoes all packets
// | from one side to the other.
// |
//
// ex:set tabstop=4:
// +------------------------------
// | Includes
// |
#include "excalibur.h"
#include "plugs.h"
// | Design-specific definition for which ethernet peripheral
// | to use...
#include "plugs_example_designs.h"
// +------------------------------
// | Globals State
// |
#define k_port_count 2
typedef struct
{
int plug_handle[k_port_count];
} globals;
// +------------------------------
// | Prototypes
// |
static int bridge_proc
(
int plug_handle,
void *context,
ns_plugs_packet *p,
void *payload,
int payload_length
);
// +------------------------
// | The Tofu
// |
int main(void)
{
int result;
globals g; // How polite we are, the globals aren't really global!
int i;
// |
// | Initialize the first card
// |
result = nr_plugs_initialize(0,0,
__adapter__,
__adapter_irq__,
__adapter_struct_addr__);
if(result)
goto go_home;
// |
// | Initialize the second card
// | Note the flag to nr_plugs_initialize which says,
// | we're just adding an adapter, not clearing everything.
// |
result = nr_plugs_initialize(ne_plugs_flag_add_adapter,0,
__adapter2__,
__adapter2_irq__,
__adapter_struct_addr__);
if(result)
goto go_home;
// |
// | Now, create all the plugs. They all share the same handler proc.
// |
for(i = 0; i < k_port_count; i++)
{
result = nr_plugs_create
(
&g.plug_handle[i],
ne_plugs_ethernet,
0, // port number, does not matter here
bridge_proc,
&g,
ne_plugs_flag_ethernet_all // aka promiscuous
+ i // adapter index
);
if(result)
goto go_home;
}
// |
// | Lastly, idle the plugs library forever
// |
printf("Forwarding packets between all ports now.\n\n");
printf("Press Q to quit.\n\n");
while(1)
{
int c;
nr_plugs_idle();
if((c = nr_uart_rxchar(0)) > 0)
{
printf("%c.",c);
if(c == 'q' || c == 'Q')
{
printf("\n\nQuitting.\n\n");
goto go_home;
}
}
}
go_home:
printf("nedk_bridge: exiting with %d\n",result);
return result;
}
// +--------------------------------
// | bridge_proc
// |
// | This is the packet handler for all adapters, and
// | all it does is resend a received packet on
// | every other port.
// |
// | It also prints a numeral for which adapter has
// | received a packet. If you put the NEDK bridge
// | between your computer and your LAN, for
// | example (with a crossover cable, of course)
// | then you get a nice "The Matrix" screen of
// | 1's and 0's. You cannot, however, realistically
// | ascertain hair color from this visualization,
// | even with practice. -- dvb
// |
static int bridge_proc
(
int plug_handle,
void *context,
ns_plugs_packet *p,
void *payload,
int payload_length
)
{
globals *g = context;
int result_z;
int result = 0;
int i;
for(i = 0; i < k_port_count; i++)
{
// |
// | Only rebroadcast the packet
// | onto *other* ports, not the
// | one it arrived on.
// |
if(g->plug_handle[i] != plug_handle)
{
result_z = nr_plugs_send
(
g->plug_handle[i],
payload,
payload_length,
0
);
if(result_z && !result)
result = result_z;
}
else
nr_uart_txchar(i + '0',0);
}
go_home:
return result;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -