📄 net__drv.c
字号:
NET_BUFFER *work_buf;
UC databuffer[MFLR+100] ;
UC *data_ptr ;
UI scc_num = 0, status ;
if (device == NU_NULL)
return NU_SUCCESS ;
if (buf_ptr == NU_NULL)
return NU_SUCCESS ;
work_buf = buf_ptr;
data_ptr = databuffer ;
length = work_buf->mem_total_data_len ;
if (length > MFLR)
{
// the send buf length larger than the MRLF
return NU_NO_DATA_TRANSFER ;
}
/* Copy the data into the buffer chain. */
while (work_buf != NULL)
{
if (work_buf->data_ptr != NULL)
{
/* Copy the data into the buffer. */
memcpy(data_ptr, work_buf->data_ptr, work_buf->data_len);
data_ptr += work_buf->data_len ;
}
work_buf = work_buf -> next_buffer ;
}
status = ethernet_tx(scc_num, databuffer, (SI)length) ;
return status ;
}
STATUS SendTcpipToEther_Interrupt(DV_DEVICE_ENTRY *device )
{
NET_BUFFER *buf_ptr ;
NET_BUFFER *tmpbuf ;
if (device == NU_NULL)
return NU_SUCCESS ;
tmpbuf = device->dev_transq.head ;
/* while ((tmpbuf != NULL) && ( device->dev_transq_length != 0)) */
while (tmpbuf != NULL)
{
SendTcpipToEther(device, tmpbuf) ;
buf_ptr = tmpbuf ;
tmpbuf = tmpbuf->next ;
MEM_One_Buffer_Chain_Free (buf_ptr, buf_ptr->mem_dlist);
device->dev_transq_length -- ;
}
device->dev_transq.head = tmpbuf ;
return NU_SUCCESS ;
}
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* SendHdlcToTcpip */
/* */
/* DESCRIPTION */
/* */
/* */
/* AUTHOR */
/* */
/* CALLED BY */
/* */
/* */
/* CALLS */
/* */
/* */
/* INPUTS */
/* buffer Pointer to the data to be sent. */
/* nbytes The number of bytes of data to send. */
/* */
/* OUTPUTS */
/* */
/* */
/* HISTORY */
/* NAME DATE REMARKS */
/* */
/* */
/*************************************************************************/
STATUS SendHdlcToTcpip(UC *buffer, UINT32 nbytes)
{
UINT16 nsent=0, s;
INT32 numbytes, bytes_left, bc;
UC *tcphead ;
UC *iphead ;
INT status ;
INT needinit ;
UINT8 iphlen ;
UINT8 tcphlen ;
UINT8 isfin ;
UINT8 issyn ;
UINT8 flags ;
UINT16 iplen ;
UINT16 srcport ;
UINT16 desport ;
UINT32 srcaddr ;
UINT32 desaddr ;
UINT8 protocal ;
UINT8 appprotocal ;
UINT8 whichtype ;
if ((buffer == NU_NULL) || (nbytes <= 10)) /* 55AA(LEN)+4 */
return NU_SUCCESS ;
/* iphead is after 55AALEN+4 = 6 bytes */
iphead = buffer + 6 ;
whichtype = (UINT8)((GET8(iphead, IP_VERSIONANDHDRLEN_OFFSET) & 0xf0) >>4 ) ;
/* If the package is IP, then whichtype == 4*/
if (whichtype != 4)
{
/* Not IP package */
return NU_SUCCESS ;
}
/* if this is a valid IP package, then it should >= 5*/
iphlen = (UINT8)((GET8(iphead, IP_VERSIONANDHDRLEN_OFFSET) & 0x0f) << 2);
if (iphlen < 20)
{
/* Not valid Ip head */
return NU_SUCCESS ;
}
/* is it the protocal we should monitor*/
protocal = (UINT8)(GET8(iphead, IP_PROTOCOL_OFFSET)) ;
/* decide which one to monitor */
/* Now we just monitor TCP protocal */
if (protocal != IP_TCP_PROT) /* == 06*/
{
/* Not TCP Packages */
return NU_SUCCESS ;
}
iplen = (UINT16)(GET16(iphead, IP_TLEN_OFFSET));
/* deside the src/des addrs do we should monitor */
srcaddr = (UINT32)(GET32(iphead, IP_SRC_OFFSET)) ;
desaddr = (UINT32)(GET32(iphead, IP_DEST_OFFSET)) ;
/* do some operations on above src/dest addresses*/
/* move to tcp head */
tcphead = iphead + iphlen ;
srcport = (UINT16)(GET16(tcphead, TCP_SRC_OFFSET)) ;
desport = (UINT16)(GET16(tcphead, TCP_DEST_OFFSET)) ;
flags = (UINT8)(GET8(tcphead,TCP_FLAGS_OFFSET)) ;
isfin = flags & TFIN ;
issyn = flags & TSYN ;
tcphlen = (UINT8)(GET8(tcphead, TCP_HLEN_OFFSET) >> 2);
/* deside which port we should monitor */
/* now we just monitor SMTP port */
/*if ( (srcport != 25) && (desport != 25) )*/
if (desport != 25)
{
/* not mail protocal package */
return NU_SUCCESS ;
}
/* if this is a tcp/ip pkg, the len of ip should >= 40 */
/* if iplen=40, it is just content control information,
but we need know if it is a FIN/SYN package, so we can estmiate
at this point , other else we would send it to the
iwatcher, and do analysis at background */
if (tcphlen + iphlen == iplen)
{
if ((!isfin) && (!issyn))
return NU_SUCCESS ;
}
if (issyn)
{
srcaddr = (UINT32)(GET32(iphead, IP_SRC_OFFSET)) ;
desaddr = (UINT32)(GET32(iphead, IP_DEST_OFFSET)) ;
}
/* reach this point, then we should send this package to iwatcher
daemons to analysis
*/
status = SendPackageToiWatcherDaemon(buffer, nbytes, 0) ;
if (status != NU_SUCCESS)
status = SendPackageToiWatcherDaemon(buffer, nbytes, 2) ;
return NU_SUCCESS ;
}
INT reinitflag=0 ;
INT totalnum=0 ;
STATUS SendPackageToiWatcherDaemon(UC *buffer, SI nbytes, SI commandtype)
{
static INT socketd = -1;
static UINT32 total_pkts = 0;
STATUS status;
INT bytes_received;
INT bytes_sent=0;
CHAR recv_buffer[256];
INT trynum = 0;
INT i = 0 ;
/* commandtype 0 : send mtu */
/* commandtype 1 : close the socked */
/* 2 : reinit the socked */
if ( (commandtype == 1) || (commandtype == 2) || (reinitflag==1) )
{
if (socketd == -1)
{
if (commandtype == 1)
return NU_SUCCESS ;
}
/* close the connection */
if (channel_close(socketd) != NU_SUCCESS)
{
if (commandtype == 1)
return -1 ;
}
if (commandtype == 1)
return NU_SUCCESS ;
else
socketd = -1 ;
reinitflag = 0 ;
}
/* if commandtype==0, then mean send mtu to iwatcher */
/* the fisrt time enter, we should init a connection*/
if (socketd < 0)
{
/*call connect Init Function.*/
while (1)
{
trynum++ ;
socketd = channel_connect(umics_ip_addr, iwatcher_serv_subnet,
iwatcher_serv_ip_addr, iwatcher_port) ;
if (socketd < 0)
{
if (trynum >=5)
return -1 ;
}
else
{
/*
NU_Reset_Pipe(&Pipe_HdlcSendToTcpip) ;
*/
break ;
}
NU_Sleep (2);
}
}
/* we don't want to be block */
/* NU_Fcntl(socketd, NU_SETFLAG, NU_FALSE); */
/* Send the datagram. */
bytes_sent = channel_send(socketd, (CHAR *)buffer, nbytes, 0);
/* If the data was not sent, we have a problem. */
if (bytes_sent < 0)
{
if (bytes_sent != NU_NO_BUFFERS)
{
return -2 ;
}
else
{
/* mean no buffers, should be waiting */
return -10 ;
}
}
if (bytes_sent != nbytes)
{
return -3 ;
socketd = -1 ;
}
totalnum += nbytes ;
#ifdef _NEED_ACK
/* turn on the "block during a read" flag */
NU_Fcntl(socketd, NU_SETFLAG, NU_BLOCK);
/* Go get the server's response. */
bytes_received = NU_Recv(socketd, recv_buffer, 256, 0);
/* turn off the "block during a read" flag -
other reads may not want to block */
NU_Fcntl(socketd, NU_SETFLAG, NU_FALSE);
/* If we got an error, its bad. */
if (bytes_received < 0)
{
return -4 ;
}
buffer[bytes_received] = (char) 0;
/* Show the user that we got something back. */
#endif
total_pkts++;
return NU_SUCCESS ;
}
STATUS Ether_open(UINT8 *id, DV_DEVICE_ENTRY *device)
{
return NU_SUCCESS ;
}
STATUS Ether_start(DV_DEVICE_ENTRY *device, NET_BUFFER *buf_ptr)
{
SendTcpipToEther(device, buf_ptr) ;
return NU_SUCCESS ;
}
STATUS Ether_receive(DV_DEVICE_ENTRY *device)
{
return NU_SUCCESS ;
}
STATUS Ether_ioctl(DV_DEVICE_ENTRY *device, INT id, DV_REQ *dev_req)
{
return NU_SUCCESS ;
}
STATUS Ether_event(DV_DEVICE_ENTRY *device, UNSIGNED event)
{
return NU_SUCCESS ;
}
/*
* send the debug message to uart port
*/
STATUS TransMsgToSI(SI scc_num, VOID *buf, SI length)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -