running the indent on the files.
This commit is contained in:
+147
-181
@@ -45,24 +45,25 @@ uint8_t Ethernet_Broadcast[MAX_MAC_LEN] =
|
||||
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
// commonly used empty address for ethernet quick compare
|
||||
uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
|
||||
|
||||
// my local device data - MAC address
|
||||
uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0 };
|
||||
|
||||
static int eth802_sockfd = -1; /* 802.2 file handle */
|
||||
static struct sockaddr eth_addr = { 0 }; // used for binding 802.2
|
||||
static int eth802_sockfd = -1; /* 802.2 file handle */
|
||||
static struct sockaddr eth_addr = { 0 }; // used for binding 802.2
|
||||
|
||||
bool ethernet_valid(void)
|
||||
{
|
||||
return (eth802_sockfd >= 0);
|
||||
return (eth802_sockfd >= 0);
|
||||
}
|
||||
|
||||
void ethernet_cleanup(void)
|
||||
{
|
||||
if (ethernet_valid())
|
||||
close(eth802_sockfd);
|
||||
eth802_sockfd = -1;
|
||||
if (ethernet_valid())
|
||||
close(eth802_sockfd);
|
||||
eth802_sockfd = -1;
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
/*----------------------------------------------------------------------
|
||||
@@ -76,23 +77,23 @@ void ethernet_cleanup(void)
|
||||
----------------------------------------------------------------------*/
|
||||
int setNonblocking(int fd)
|
||||
{
|
||||
int flags;
|
||||
int flags;
|
||||
|
||||
if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
|
||||
flags = 0;
|
||||
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
if (-1 == (flags = fcntl(fd, F_GETFL, 0)))
|
||||
flags = 0;
|
||||
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
||||
}
|
||||
|
||||
/* opens an 802.2 socket to receive and send packets */
|
||||
static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
|
||||
{
|
||||
int sock_fd = -1; // return value
|
||||
int sock_fd = -1; // return value
|
||||
int uid = 0;
|
||||
|
||||
fprintf(stderr,"ethernet: opening \"%s\"\n",interface_name);
|
||||
fprintf(stderr, "ethernet: opening \"%s\"\n", interface_name);
|
||||
/* check to see if we are being run as root */
|
||||
uid = getuid();
|
||||
if (uid != 0) {
|
||||
if (uid != 0) {
|
||||
fprintf(stderr,
|
||||
"ethernet: Unable to open an 802.2 socket. "
|
||||
"Try running with root priveleges.\n");
|
||||
@@ -102,21 +103,18 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
|
||||
// modules.conf (or in modutils/alias on Debian with update-modules)
|
||||
// alias net-pf-17 af_packet
|
||||
// Then follow it by: # modprobe af_packet
|
||||
|
||||
|
||||
/* Attempt to open the socket for 802.2 ethernet frames */
|
||||
if ((sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_802_2))) < 0)
|
||||
{
|
||||
if ((sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_802_2))) < 0) {
|
||||
/* Error occured */
|
||||
fprintf(stderr,
|
||||
"ethernet: Error opening socket: %s\n",
|
||||
strerror(errno));
|
||||
"ethernet: Error opening socket: %s\n", strerror(errno));
|
||||
fprintf(stderr,
|
||||
"You might need to add the following to modules.conf\n"
|
||||
"(or in /etc/modutils/alias on Debian with update-modules):\n"
|
||||
"alias net-pf-17 af_packet\n"
|
||||
"Also, add af_packet to /etc/modules.\n"
|
||||
"Then follow it by:\n"
|
||||
"# modprobe af_packet\n");
|
||||
"Then follow it by:\n" "# modprobe af_packet\n");
|
||||
exit(-1);
|
||||
}
|
||||
/* Bind the socket to an address */
|
||||
@@ -124,11 +122,11 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
|
||||
/* Clear the memory before copying */
|
||||
memset(eth_addr->sa_data, '\0', sizeof(eth_addr->sa_data));
|
||||
/* Strcpy the interface name into the address */
|
||||
strncpy(eth_addr->sa_data, interface_name, sizeof(eth_addr->sa_data)-1);
|
||||
fprintf(stderr,"ethernet: binding \"%s\"\n",eth_addr->sa_data);
|
||||
strncpy(eth_addr->sa_data, interface_name,
|
||||
sizeof(eth_addr->sa_data) - 1);
|
||||
fprintf(stderr, "ethernet: binding \"%s\"\n", eth_addr->sa_data);
|
||||
/* Attempt to bind the socket to the interface */
|
||||
if (bind(sock_fd, eth_addr, sizeof(struct sockaddr)) != 0)
|
||||
{
|
||||
if (bind(sock_fd, eth_addr, sizeof(struct sockaddr)) != 0) {
|
||||
/* Bind problem, close socket and return */
|
||||
fprintf(stderr,
|
||||
"ethernet: Unable to bind 802.2 socket : %s\n",
|
||||
@@ -138,8 +136,7 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name)
|
||||
"(or in /etc/modutils/alias on Debian with update-modules):\n"
|
||||
"alias net-pf-17 af_packet\n"
|
||||
"Also, add af_packet to /etc/modules.\n"
|
||||
"Then follow it by:\n"
|
||||
"# modprobe af_packet\n");
|
||||
"Then follow it by:\n" "# modprobe af_packet\n");
|
||||
/* Close the socket */
|
||||
close(sock_fd);
|
||||
exit(-1);
|
||||
@@ -155,7 +152,7 @@ static int get_local_hwaddr(const char *ifname, unsigned char *mac)
|
||||
{
|
||||
struct ifreq ifr;
|
||||
int fd;
|
||||
int rv; // return value - error value from df or ioctl call
|
||||
int rv; // return value - error value from df or ioctl call
|
||||
|
||||
/* determine the local MAC address */
|
||||
strcpy(ifr.ifr_name, ifname);
|
||||
@@ -174,19 +171,17 @@ static int get_local_hwaddr(const char *ifname, unsigned char *mac)
|
||||
bool ethernet_init(char *interface_name)
|
||||
{
|
||||
get_local_hwaddr(interface_name, Ethernet_MAC_Address);
|
||||
eth802_sockfd =
|
||||
ethernet_bind(ð_addr, interface_name);
|
||||
eth802_sockfd = ethernet_bind(ð_addr, interface_name);
|
||||
|
||||
return ethernet_valid();
|
||||
return ethernet_valid();
|
||||
}
|
||||
|
||||
/* function to send a packet out the 802.2 socket */
|
||||
/* returns bytes sent success, negative on failure */
|
||||
int ethernet_send(
|
||||
BACNET_ADDRESS *dest, // destination address
|
||||
BACNET_ADDRESS *src, // source address
|
||||
uint8_t *pdu, // any data to be sent - may be null
|
||||
unsigned pdu_len) // number of bytes of data
|
||||
int ethernet_send(BACNET_ADDRESS * dest, // destination address
|
||||
BACNET_ADDRESS * src, // source address
|
||||
uint8_t * pdu, // any data to be sent - may be null
|
||||
unsigned pdu_len) // number of bytes of data
|
||||
{
|
||||
int bytes = 0;
|
||||
uint8_t mtu[MAX_MPDU] = { 0 };
|
||||
@@ -194,101 +189,88 @@ int ethernet_send(
|
||||
int i = 0;
|
||||
|
||||
// don't waste time if the socket is not valid
|
||||
if (eth802_sockfd < 0)
|
||||
{
|
||||
if (eth802_sockfd < 0) {
|
||||
fprintf(stderr, "ethernet: 802.2 socket is invalid!\n");
|
||||
return -1;
|
||||
}
|
||||
/* load destination ethernet MAC address */
|
||||
if (dest->mac_len == 6)
|
||||
{
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
mtu[mtu_len] = dest->mac[i];
|
||||
mtu_len++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (dest->mac_len == 6) {
|
||||
for (i = 0; i < 6; i++) {
|
||||
mtu[mtu_len] = dest->mac[i];
|
||||
mtu_len++;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "ethernet: invalid destination MAC address!\n");
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* load source ethernet MAC address */
|
||||
if (src->mac_len == 6)
|
||||
{
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
mtu[mtu_len] = src->mac[i];
|
||||
mtu_len++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (src->mac_len == 6) {
|
||||
for (i = 0; i < 6; i++) {
|
||||
mtu[mtu_len] = src->mac[i];
|
||||
mtu_len++;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "ethernet: invalid source MAC address!\n");
|
||||
return -3;
|
||||
}
|
||||
if ((14 + 3 + pdu_len) > MAX_MPDU)
|
||||
{
|
||||
if ((14 + 3 + pdu_len) > MAX_MPDU) {
|
||||
fprintf(stderr, "ethernet: PDU is too big to send!\n");
|
||||
return -4;
|
||||
}
|
||||
/* packet length */
|
||||
mtu_len += encode_unsigned16(&mtu[12],
|
||||
3 /*DSAP,SSAP,LLC*/ + pdu_len);
|
||||
mtu_len += encode_unsigned16(&mtu[12],
|
||||
3 /*DSAP,SSAP,LLC */ + pdu_len);
|
||||
// Logical PDU portion
|
||||
mtu[mtu_len++] = 0x82; /* DSAP for BACnet */
|
||||
mtu[mtu_len++] = 0x82; /* SSAP for BACnet */
|
||||
mtu[mtu_len++] = 0x03; /* Control byte in header */
|
||||
mtu[mtu_len++] = 0x82; /* DSAP for BACnet */
|
||||
mtu[mtu_len++] = 0x82; /* SSAP for BACnet */
|
||||
mtu[mtu_len++] = 0x03; /* Control byte in header */
|
||||
memcpy(&mtu[mtu_len], pdu, pdu_len);
|
||||
mtu_len += pdu_len;
|
||||
|
||||
|
||||
/* Send the packet */
|
||||
bytes =
|
||||
sendto(eth802_sockfd, &mtu, mtu_len, 0,
|
||||
(struct sockaddr *) ð_addr, sizeof(struct sockaddr));
|
||||
/* did it get sent? */
|
||||
if (bytes < 0)
|
||||
fprintf(stderr,"ethernet: Error sending packet: %s\n",
|
||||
strerror(errno));
|
||||
|
||||
fprintf(stderr, "ethernet: Error sending packet: %s\n",
|
||||
strerror(errno));
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/* function to send a packet out the 802.2 socket */
|
||||
/* returns number of bytes sent on success, negative on failure */
|
||||
int ethernet_send_pdu(
|
||||
BACNET_ADDRESS *dest, // destination address
|
||||
uint8_t *pdu, // any data to be sent - may be null
|
||||
unsigned pdu_len) // number of bytes of data
|
||||
int ethernet_send_pdu(BACNET_ADDRESS * dest, // destination address
|
||||
uint8_t * pdu, // any data to be sent - may be null
|
||||
unsigned pdu_len) // number of bytes of data
|
||||
{
|
||||
int i = 0; // counter
|
||||
BACNET_ADDRESS src = {0}; // source address
|
||||
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
src.mac[i] = Ethernet_MAC_Address[i];
|
||||
src.mac_len++;
|
||||
}
|
||||
/* function to send a packet out the 802.2 socket */
|
||||
/* returns 1 on success, 0 on failure */
|
||||
return ethernet_send(dest, // destination address
|
||||
&src, // source address
|
||||
pdu, // any data to be sent - may be null
|
||||
pdu_len); // number of bytes of data
|
||||
int i = 0; // counter
|
||||
BACNET_ADDRESS src = { 0 }; // source address
|
||||
|
||||
for (i = 0; i < 6; i++) {
|
||||
src.mac[i] = Ethernet_MAC_Address[i];
|
||||
src.mac_len++;
|
||||
}
|
||||
/* function to send a packet out the 802.2 socket */
|
||||
/* returns 1 on success, 0 on failure */
|
||||
return ethernet_send(dest, // destination address
|
||||
&src, // source address
|
||||
pdu, // any data to be sent - may be null
|
||||
pdu_len); // number of bytes of data
|
||||
}
|
||||
|
||||
// receives an 802.2 framed packet
|
||||
// returns the number of octets in the PDU, or zero on failure
|
||||
uint16_t ethernet_receive(
|
||||
BACNET_ADDRESS *src, // source address
|
||||
uint8_t *pdu, // PDU data
|
||||
uint16_t max_pdu, // amount of space available in the PDU
|
||||
unsigned timeout) // number of milliseconds to wait for a packet
|
||||
uint16_t ethernet_receive(BACNET_ADDRESS * src, // source address
|
||||
uint8_t * pdu, // PDU data
|
||||
uint16_t max_pdu, // amount of space available in the PDU
|
||||
unsigned timeout) // number of milliseconds to wait for a packet
|
||||
{
|
||||
int received_bytes;
|
||||
uint8_t buf[MAX_MPDU] = {0}; // data
|
||||
uint16_t pdu_len = 0; // return value
|
||||
uint8_t buf[MAX_MPDU] = { 0 }; // data
|
||||
uint16_t pdu_len = 0; // return value
|
||||
fd_set read_fds;
|
||||
int max;
|
||||
struct timeval select_timeout;
|
||||
@@ -300,14 +282,11 @@ uint16_t ethernet_receive(
|
||||
/* we could just use a non-blocking socket, but that consumes all
|
||||
the CPU time. We can use a timeout; it is only supported as
|
||||
a select. */
|
||||
if (timeout >= 1000)
|
||||
{
|
||||
if (timeout >= 1000) {
|
||||
select_timeout.tv_sec = timeout / 1000;
|
||||
select_timeout.tv_usec =
|
||||
1000 * (timeout - select_timeout.tv_sec * 1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
select_timeout.tv_usec =
|
||||
1000 * (timeout - select_timeout.tv_sec * 1000);
|
||||
} else {
|
||||
select_timeout.tv_sec = 0;
|
||||
select_timeout.tv_usec = 1000 * timeout;
|
||||
}
|
||||
@@ -326,14 +305,15 @@ uint16_t ethernet_receive(
|
||||
// using O_NONBLOCK and no data
|
||||
// was immediately available for reading.
|
||||
if (errno != EAGAIN)
|
||||
fprintf(stderr,"ethernet: Read error in receiving packet: %s\n",
|
||||
fprintf(stderr,
|
||||
"ethernet: Read error in receiving packet: %s\n",
|
||||
strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (received_bytes == 0)
|
||||
return 0;
|
||||
|
||||
return 0;
|
||||
|
||||
/* the signature of an 802.2 BACnet packet */
|
||||
if ((buf[14] != 0x82) && (buf[15] != 0x82)) {
|
||||
//fprintf(stderr,"ethernet: Non-BACnet packet\n");
|
||||
@@ -345,109 +325,95 @@ uint16_t ethernet_receive(
|
||||
|
||||
// check destination address for when
|
||||
// the Ethernet card is in promiscious mode
|
||||
if ((memcmp(&buf[0], Ethernet_MAC_Address,6) != 0)
|
||||
&& (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0))
|
||||
{
|
||||
if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0)
|
||||
&& (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) {
|
||||
//fprintf(stderr, "ethernet: This packet isn't for us\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
(void)decode_unsigned16(&buf[12],&pdu_len);
|
||||
(void) decode_unsigned16(&buf[12], &pdu_len);
|
||||
pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ;
|
||||
// copy the buffer into the PDU
|
||||
if (pdu_len < max_pdu)
|
||||
memmove(&pdu[0],&buf[17],pdu_len);
|
||||
memmove(&pdu[0], &buf[17], pdu_len);
|
||||
// ignore packets that are too large
|
||||
else
|
||||
pdu_len = 0;
|
||||
|
||||
pdu_len = 0;
|
||||
|
||||
|
||||
return pdu_len;
|
||||
}
|
||||
|
||||
void ethernet_set_my_address(BACNET_ADDRESS *my_address)
|
||||
void ethernet_set_my_address(BACNET_ADDRESS * my_address)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
Ethernet_MAC_Address[i] = my_address->mac[i];
|
||||
}
|
||||
int i = 0;
|
||||
|
||||
return;
|
||||
for (i = 0; i < 6; i++) {
|
||||
Ethernet_MAC_Address[i] = my_address->mac[i];
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ethernet_get_my_address(BACNET_ADDRESS *my_address)
|
||||
void ethernet_get_my_address(BACNET_ADDRESS * my_address)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
my_address->mac_len = 0;
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
my_address->mac[i] = Ethernet_MAC_Address[i];
|
||||
my_address->mac_len++;
|
||||
}
|
||||
my_address->net = 0; // local only, no routing
|
||||
my_address->len = 0;
|
||||
for (i = 0; i < MAX_MAC_LEN; i++)
|
||||
{
|
||||
my_address->adr[i] = 0;
|
||||
}
|
||||
int i = 0;
|
||||
|
||||
return;
|
||||
my_address->mac_len = 0;
|
||||
for (i = 0; i < 6; i++) {
|
||||
my_address->mac[i] = Ethernet_MAC_Address[i];
|
||||
my_address->mac_len++;
|
||||
}
|
||||
my_address->net = 0; // local only, no routing
|
||||
my_address->len = 0;
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
my_address->adr[i] = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ethernet_get_broadcast_address(
|
||||
BACNET_ADDRESS *dest) // destination address
|
||||
void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address
|
||||
{
|
||||
int i = 0; // counter
|
||||
|
||||
if (dest)
|
||||
{
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
dest->mac[i] = Ethernet_Broadcast[i];
|
||||
}
|
||||
dest->mac_len = 6;
|
||||
dest->net = BACNET_BROADCAST_NETWORK;
|
||||
dest->len = 0; // denotes broadcast address
|
||||
for (i = 0; i < MAX_MAC_LEN; i++)
|
||||
{
|
||||
dest->adr[i] = 0;
|
||||
}
|
||||
}
|
||||
int i = 0; // counter
|
||||
|
||||
return;
|
||||
if (dest) {
|
||||
for (i = 0; i < 6; i++) {
|
||||
dest->mac[i] = Ethernet_Broadcast[i];
|
||||
}
|
||||
dest->mac_len = 6;
|
||||
dest->net = BACNET_BROADCAST_NETWORK;
|
||||
dest->len = 0; // denotes broadcast address
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
dest->adr[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ethernet_debug_address(
|
||||
const char *info,
|
||||
BACNET_ADDRESS *dest)
|
||||
void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest)
|
||||
{
|
||||
int i = 0; // counter
|
||||
int i = 0; // counter
|
||||
|
||||
if (info)
|
||||
fprintf(stderr,"%s",info);
|
||||
if (dest)
|
||||
{
|
||||
fprintf(stderr,"Address:\n");
|
||||
fprintf(stderr," MAC Length=%d\n",dest->mac_len);
|
||||
fprintf(stderr," MAC Address=");
|
||||
for (i = 0; i < MAX_MAC_LEN; i++)
|
||||
{
|
||||
fprintf(stderr,"%02X ",(unsigned)dest->mac[i]);
|
||||
if (info)
|
||||
fprintf(stderr, "%s", info);
|
||||
if (dest) {
|
||||
fprintf(stderr, "Address:\n");
|
||||
fprintf(stderr, " MAC Length=%d\n", dest->mac_len);
|
||||
fprintf(stderr, " MAC Address=");
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
fprintf(stderr, "%02X ", (unsigned) dest->mac[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, " Net=%hu\n", dest->net);
|
||||
fprintf(stderr, " Len=%d\n", dest->len);
|
||||
fprintf(stderr, " Adr=");
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
fprintf(stderr, "%02X ", (unsigned) dest->adr[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
fprintf(stderr,"\n");
|
||||
fprintf(stderr," Net=%hu\n",dest->net);
|
||||
fprintf(stderr," Len=%d\n",dest->len);
|
||||
fprintf(stderr," Adr=");
|
||||
for (i = 0; i < MAX_MAC_LEN; i++)
|
||||
{
|
||||
fprintf(stderr,"%02X ",(unsigned)dest->adr[i]);
|
||||
}
|
||||
fprintf(stderr,"\n");
|
||||
}
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user