+ char *p, *q;
+ int unit, iffd, adrlen;
+ unsigned char *adrp;
+ char ifdev[24];
+ struct {
+ union DL_primitives prim;
+ char space[64];
+ } reply;
+
+ /*
+ * We have to open the device and ask it for its hardware address.
+ * First split apart the device name and unit.
+ */
+ strcpy(ifdev, "/dev/");
+ q = ifdev + 5; /* strlen("/dev/") */
+ while (*name != 0 && !isdigit(*name))
+ *q++ = *name++;
+ *q = 0;
+ unit = atoi(name);
+
+ /*
+ * Open the device and do a DLPI attach and phys_addr_req.
+ */
+ iffd = open(ifdev, O_RDWR);
+ if (iffd < 0) {
+ syslog(LOG_ERR, "Can't open %s: %m", ifdev);
+ return 0;
+ }
+ if (dlpi_attach(iffd, unit) < 0
+ || dlpi_get_reply(iffd, &reply.prim, DL_OK_ACK, sizeof(reply)) < 0
+ || dlpi_info_req(iffd) < 0
+ || dlpi_get_reply(iffd, &reply.prim, DL_INFO_ACK, sizeof(reply)) < 0) {
+ close(iffd);
+ return 0;
+ }
+
+ adrlen = reply.prim.info_ack.dl_addr_length;
+ adrp = (unsigned char *)&reply + reply.prim.info_ack.dl_addr_offset;
+#if DL_CURRENT_VERSION >= 2
+ if (reply.prim.info_ack.dl_sap_length < 0)
+ adrlen += reply.prim.info_ack.dl_sap_length;
+ else
+ adrp += reply.prim.info_ack.dl_sap_length;
+#endif
+ hwaddr->sa_family = AF_UNSPEC;
+ memcpy(hwaddr->sa_data, adrp, adrlen);
+
+ return 1;
+}
+
+static int
+dlpi_attach(fd, ppa)
+ int fd, ppa;
+{
+ dl_attach_req_t req;
+ struct strbuf buf;
+
+ req.dl_primitive = DL_ATTACH_REQ;
+ req.dl_ppa = ppa;
+ buf.len = sizeof(req);
+ buf.buf = (void *) &req;
+ return putmsg(fd, &buf, NULL, RS_HIPRI);
+}
+
+static int
+dlpi_info_req(fd)
+ int fd;
+{
+ dl_info_req_t req;
+ struct strbuf buf;
+
+ req.dl_primitive = DL_INFO_REQ;
+ buf.len = sizeof(req);
+ buf.buf = (void *) &req;
+ return putmsg(fd, &buf, NULL, RS_HIPRI);
+}
+
+static int
+dlpi_get_reply(fd, reply, expected_prim, maxlen)
+ union DL_primitives *reply;
+ int fd, expected_prim, maxlen;
+{
+ struct strbuf buf;
+ int flags, n;
+ struct pollfd pfd;
+
+ /*
+ * Use poll to wait for a message with a timeout.
+ */
+ pfd.fd = fd;
+ pfd.events = POLLIN | POLLPRI;
+ do {
+ n = poll(&pfd, 1, 1000);
+ } while (n == -1 && errno == EINTR);
+ if (n <= 0)
+ return -1;
+
+ /*
+ * Get the reply.
+ */
+ buf.maxlen = maxlen;
+ buf.buf = (void *) reply;
+ flags = 0;
+ if (getmsg(fd, &buf, NULL, &flags) < 0)
+ return -1;
+
+ if (buf.len < sizeof(ulong)) {
+ if (debug)
+ syslog(LOG_DEBUG, "dlpi response short (len=%d)\n", buf.len);
+ return -1;
+ }
+
+ if (reply->dl_primitive == expected_prim)
+ return 0;
+
+ if (debug) {
+ if (reply->dl_primitive == DL_ERROR_ACK) {
+ syslog(LOG_DEBUG, "dlpi error %d (unix errno %d) for prim %x\n",
+ reply->error_ack.dl_errno, reply->error_ack.dl_unix_errno,
+ reply->error_ack.dl_error_primitive);
+ } else {
+ syslog(LOG_DEBUG, "dlpi unexpected response prim %x\n",
+ reply->dl_primitive);
+ }
+ }
+
+ return -1;