#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#include <assert.h>
#include <stropts.h>
#include <sys/types.h>
#include <inet/common.h>
#include <netinet/in.h>
#include <net/sppptun.h>
#include <net/pppoe.h>
#include <arpa/inet.h>
#include "common.h"
uint32_t pkt_input[PKT_INPUT_LEN / sizeof (uint32_t)];
uint32_t pkt_octl[PKT_OCTL_LEN / sizeof (uint32_t)];
uint32_t pkt_output[PKT_OUTPUT_LEN / sizeof (uint32_t)];
const char tunnam[] = "/dev/" PPP_TUN_NAME;
const ether_addr_t ether_bcast = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
const char *
mystrerror(int err)
{
const char *estr;
static char ebuf[64];
if ((estr = strerror(err)) != NULL)
return (estr);
(void) snprintf(ebuf, sizeof (ebuf), "Error:%d", err);
return (ebuf);
}
void
myperror(const char *emsg)
{
(void) fprintf(stderr, "%s: %s: %s\n", myname, emsg,
mystrerror(errno));
}
int
mygetmsg(int fd, struct strbuf *ctrl, struct strbuf *data, int *flags)
{
int retv;
int hadflags;
hadflags = getmsg(fd, ctrl, data, flags);
if (hadflags <= 0 || !(hadflags & (MORECTL | MOREDATA)))
return (hadflags);
do {
if (flags != NULL)
*flags = 0;
retv = getmsg(fd, ctrl, data, flags);
} while (retv > 0 || (retv < 0 && errno == EINTR));
return (retv < 0 ? retv : hadflags);
}
int
strioctl(int fd, int cmd, void *ptr, int ilen, int olen)
{
struct strioctl str;
str.ic_cmd = cmd;
str.ic_timout = 0;
str.ic_len = ilen;
str.ic_dp = ptr;
if (ioctl(fd, I_STR, &str) == -1) {
return (-1);
}
if (str.ic_len != olen) {
errno = EINVAL;
return (-1);
}
return (0);
}
poep_t *
poe_mkheader(void *dptr, uint8_t codeval, int sessionid)
{
poep_t *poep;
assert(dptr != NULL && IS_P2ALIGNED(dptr, sizeof (poep_t *)));
poep = (poep_t *)dptr;
poep->poep_version_type = POE_VERSION;
poep->poep_code = codeval;
poep->poep_session_id = htons(sessionid);
poep->poep_length = htons(0);
return (poep);
}
boolean_t
poe_tagcheck(const poep_t *poep, int length, const uint8_t *tptr)
{
int plen;
const uint8_t *tstart, *tend;
if (poep == NULL || !IS_P2ALIGNED(poep, sizeof (uint16_t)) ||
tptr == NULL || length < sizeof (*poep))
return (B_FALSE);
plen = poe_length(poep);
if (plen + sizeof (*poep) > length)
return (B_FALSE);
tstart = (const uint8_t *)(poep+1);
tend = tstart + plen;
if (tptr < tstart || tptr+POET_HDRLEN > tend ||
tptr+POET_HDRLEN+POET_GET_LENG(tptr) > tend)
return (B_FALSE);
return (B_TRUE);
}
static int
poe_tag_insert(poep_t *poep, uint16_t ttype, const void *data, size_t dlen)
{
int plen;
uint8_t *dp;
plen = poe_length(poep);
if (data == NULL)
dlen = 0;
if (sizeof (*poep) + plen + POET_HDRLEN + dlen > PPPOE_MSGMAX)
return (-1);
dp = (uint8_t *)(poep + 1) + plen;
POET_SET_TYPE(dp, ttype);
POET_SET_LENG(dp, dlen);
if (dlen > 0)
(void) memcpy(POET_DATA(dp), data, dlen);
poep->poep_length = htons(plen + POET_HDRLEN + dlen);
return (0);
}
int
poe_add_str(poep_t *poep, uint16_t ttype, const char *str)
{
return (poe_tag_insert(poep, ttype, str, strlen(str)));
}
int
poe_add_long(poep_t *poep, uint16_t ttype, uint32_t val)
{
val = htonl(val);
return (poe_tag_insert(poep, ttype, &val, sizeof (val)));
}
int
poe_two_longs(poep_t *poep, uint16_t ttype, uint32_t val1, uint32_t val2)
{
uint32_t vals[2];
vals[0] = htonl(val1);
vals[1] = htonl(val2);
return (poe_tag_insert(poep, ttype, vals, sizeof (vals)));
}
int
poe_tag_copy(poep_t *poep, const uint8_t *tagp)
{
int tlen;
int plen;
tlen = POET_GET_LENG(tagp) + POET_HDRLEN;
plen = poe_length(poep);
if (sizeof (*poep) + plen + tlen > PPPOE_MSGMAX)
return (-1);
(void) memcpy((uint8_t *)(poep + 1) + plen, tagp, tlen);
poep->poep_length = htons(tlen + plen);
return (0);
}
struct tag_list {
int tl_type;
const char *tl_name;
};
static const struct tag_list tag_list[] = {
{ POETT_END, "End-Of-List" },
{ POETT_SERVICE, "Service-Name" },
{ POETT_ACCESS, "AC-Name" },
{ POETT_UNIQ, "Host-Uniq" },
{ POETT_COOKIE, "AC-Cookie" },
{ POETT_VENDOR, "Vendor-Specific" },
{ POETT_RELAY, "Relay-Session-Id" },
{ POETT_NAMERR, "Service-Name-Error" },
{ POETT_SYSERR, "AC-System-Error" },
{ POETT_GENERR, "Generic-Error" },
{ POETT_MULTI, "Multicast-Capable" },
{ POETT_HURL, "Host-URL" },
{ POETT_MOTM, "Message-Of-The-Minute" },
{ POETT_RTEADD, "IP-Route-Add" },
{ 0, NULL }
};
static const struct tag_list code_list[] = {
{ POECODE_DATA, "Data" },
{ POECODE_PADO, "Active Discovery Offer" },
{ POECODE_PADI, "Active Discovery Initiation" },
{ POECODE_PADR, "Active Discovery Request" },
{ POECODE_PADS, "Active Discovery Session-confirmation" },
{ POECODE_PADT, "Active Discovery Terminate" },
{ POECODE_PADM, "Active Discovery Message" },
{ POECODE_PADN, "Active Discovery Network" },
{ 0, NULL }
};
const char *
poe_tagname(uint16_t tagtype)
{
const struct tag_list *tlp;
static char tname[32];
for (tlp = tag_list; tlp->tl_name != NULL; tlp++)
if (tagtype == tlp->tl_type)
return (tlp->tl_name);
(void) sprintf(tname, "Tag%d", tagtype);
return (tname);
}
const char *
poe_codename(uint8_t codetype)
{
const struct tag_list *tlp;
static char tname[32];
for (tlp = code_list; tlp->tl_name != NULL; tlp++)
if (codetype == tlp->tl_type)
return (tlp->tl_name);
(void) sprintf(tname, "Code%d", codetype);
return (tname);
}
const char *
ehost2(const struct ether_addr *ea)
{
static char hbuf[MAXHOSTNAMELEN+1];
if (ea == NULL)
return ("NULL");
if (ether_ntohost(hbuf, ea) == 0)
return (hbuf);
return (ether_ntoa(ea));
}
const char *
ehost(const ppptun_atype *pap)
{
return (ehost2((const struct ether_addr *)pap));
}
const char *
ihost(uint32_t haddr)
{
struct hostent *hp;
struct sockaddr_in sin;
(void) memset(&sin, '\0', sizeof (sin));
sin.sin_addr.s_addr = haddr;
hp = gethostbyaddr((const char *)&sin, sizeof (sin), AF_INET);
if (hp != NULL)
return (hp->h_name);
return (inet_ntoa(sin.sin_addr));
}
int
hexdecode(char chr)
{
if (chr >= '0' && chr <= '9')
return ((int)(chr - '0'));
if (chr >= 'a' && chr <= 'f')
return ((int)(chr - 'a' + 10));
return ((int)(chr - 'A' + 10));
}