This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EthernetLayer.cpp
85 lines (68 loc) · 2.29 KB
/
EthernetLayer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// EthernetLayer.cpp: implementation of the CEthernetLayer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "PacketSender.h"
#include "EthernetLayer.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CEthernetLayer::CEthernetLayer( char* pName )
: CBaseLayer( pName )
{
ResetHeader( ) ;
}
CEthernetLayer::~CEthernetLayer()
{
}
void CEthernetLayer::ResetHeader()
{
memset( m_sHeader.enet_dstaddr.addrs, 0, 6 ) ;
memset( m_sHeader.enet_srcaddr.addrs, 0, 6 ) ;
memset( m_sHeader.enet_data, 0, ETHER_MAX_DATA_SIZE ) ;
m_sHeader.enet_type = ntohs(ETHER_PROTO_TYPE_IP) ; // 0x0800
}
unsigned char* CEthernetLayer::GetEnetDstAddress() // Ethernet Destination Address
{
return m_sHeader.enet_srcaddr.addrs;
}
unsigned char* CEthernetLayer::GetEnetSrcAddress() // Ethernet Source Address
{
return m_sHeader.enet_dstaddr.addrs;
}
void CEthernetLayer::SetEnetSrcAddress(unsigned char *pAddress)
{
// 이더넷 주소 12자리를 가져온다. (NI Layer에서 가져온다.)
ETHERNET_ADDR src_ether;
src_ether.addr0 = pAddress[0];
src_ether.addr1 = pAddress[1];
src_ether.addr2 = pAddress[2];
src_ether.addr3 = pAddress[3];
src_ether.addr4 = pAddress[4];
src_ether.addr5 = pAddress[5];
memcpy( m_sHeader.enet_srcaddr.addrs, pAddress, 6 ) ;
}
void CEthernetLayer::SetEnetDstAddress(unsigned char *pAddress)
{
memcpy( m_sHeader.enet_dstaddr.addrs, pAddress, 6 ) ;
}
BOOL CEthernetLayer::Send(unsigned char *ppayload, int nlength)
{
memcpy( m_sHeader.enet_data, ppayload, nlength ) ;
BOOL bSuccess = FALSE ;
bSuccess = mp_UnderLayer->Send((unsigned char*) &m_sHeader,nlength+ETHER_HEADER_SIZE);
return bSuccess ;
}
BOOL CEthernetLayer::Receive( unsigned char* ppayload )
{
// 하위 계층에서 받은 payload를 현재 계층의 header구조에 맞게 읽음.
PETHERNET_HEADER pFrame = (PETHERNET_HEADER) ppayload ;
BOOL bSuccess = FALSE ;
// 나에게 온 Ethernet Address가 아니면 discard 한다.
if(!memcmp((char *)pFrame->enet_dstaddr.addrs,(char *)m_sHeader.enet_srcaddr.addrs,6) &&
memcmp((char *)pFrame->enet_srcaddr.addrs,(char *)m_sHeader.enet_srcaddr.addrs,6))
{
bSuccess = mp_aUpperLayer[0]->Receive((unsigned char*) pFrame->enet_data);
}
return bSuccess ;
}