00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "config.h"
00010
00011
00012 #define sockaddr_storage sockaddr
00013 #include <Packet32.h>
00014 #undef sockaddr_storage
00015 #include <Ntddndis.h>
00016
00017 #include <errno.h>
00018 #include <stdlib.h>
00019
00020 #include "dnet.h"
00021
00022 struct eth_handle {
00023 LPADAPTER lpa;
00024 LPPACKET pkt;
00025 };
00026
00027 eth_t *
00028 eth_open(const char *device)
00029 {
00030 eth_t *eth;
00031 intf_t *intf;
00032 struct intf_entry ifent;
00033 eth_addr_t ea;
00034 char *p, *buf;
00035 ULONG len;
00036
00037
00038 memset(&ifent, 0, sizeof(ifent));
00039 if ((intf = intf_open()) != NULL) {
00040 strlcpy(ifent.intf_name, device, sizeof(ifent.intf_name));
00041 intf_get(intf, &ifent);
00042 intf_close(intf);
00043 }
00044 if (ifent.intf_link_addr.addr_type != ADDR_TYPE_ETH)
00045 return (NULL);
00046
00047
00048 buf = NULL;
00049 PacketGetAdapterNames(buf, &len);
00050 if (len > 0 && (buf = malloc(len)) != NULL) {
00051 if (!PacketGetAdapterNames(buf, &len)) {
00052 free(buf);
00053 buf = NULL;
00054 }
00055 }
00056 if (buf == NULL)
00057 return (NULL);
00058
00059
00060 if ((eth = calloc(1, sizeof(*eth))) == NULL) {
00061 free(buf);
00062 return (NULL);
00063 }
00064 for (p = buf; *p != '\0'; p += strlen(p) + 1) {
00065 if ((eth->lpa = PacketOpenAdapter(p)) != NULL) {
00066 if (eth->lpa->hFile != INVALID_HANDLE_VALUE &&
00067 eth_get(eth, &ea) == 0 &&
00068 memcmp(&ea, &ifent.intf_link_addr.addr_eth,
00069 ETH_ADDR_LEN) == 0) {
00070 PacketSetBuff(eth->lpa, 512000);
00071 eth->pkt = PacketAllocatePacket();
00072 break;
00073 }
00074 PacketCloseAdapter(eth->lpa);
00075 }
00076 }
00077 free(buf);
00078 if (eth->pkt == NULL)
00079 eth = eth_close(eth);
00080
00081 return (eth);
00082 }
00083
00084 ssize_t
00085 eth_send(eth_t *eth, const void *buf, size_t len)
00086 {
00087 PacketInitPacket(eth->pkt, (void *)buf, len);
00088 PacketSendPacket(eth->lpa, eth->pkt, TRUE);
00089 return (len);
00090 }
00091
00092 eth_t *
00093 eth_close(eth_t *eth)
00094 {
00095 if (eth != NULL) {
00096 if (eth->pkt != NULL)
00097 PacketFreePacket(eth->pkt);
00098 if (eth->lpa != NULL)
00099 PacketCloseAdapter(eth->lpa);
00100 free(eth);
00101 }
00102 return (NULL);
00103 }
00104
00105 int
00106 eth_get(eth_t *eth, eth_addr_t *ea)
00107 {
00108 PACKET_OID_DATA *data;
00109 u_char buf[512];
00110
00111 data = (PACKET_OID_DATA *)buf;
00112 data->Oid = OID_802_3_CURRENT_ADDRESS;
00113 data->Length = ETH_ADDR_LEN;
00114
00115 if (PacketRequest(eth->lpa, FALSE, data) == TRUE) {
00116 memcpy(ea, data->Data, ETH_ADDR_LEN);
00117 return (0);
00118 }
00119 return (-1);
00120 }
00121
00122 int
00123 eth_set(eth_t *eth, const eth_addr_t *ea)
00124 {
00125 PACKET_OID_DATA *data;
00126 u_char buf[512];
00127
00128 data = (PACKET_OID_DATA *)buf;
00129 data->Oid = OID_802_3_CURRENT_ADDRESS;
00130 memcpy(data->Data, ea, ETH_ADDR_LEN);
00131 data->Length = ETH_ADDR_LEN;
00132
00133 if (PacketRequest(eth->lpa, TRUE, data) == TRUE)
00134 return (0);
00135
00136 return (-1);
00137 }