<commit_before>#include <sys/socket.h>
#include <sys/types.h>


#if defined(__APPLE_CC__) || defined(__APPLE__)
#include <sys/sysctl.h>

#undef EV_ERROR

#include <net/if.h>
#include <net/if_dl.h>
#include <net/ethernet.h>
#endif

#if defined(linux)
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <netdb.h>
#include <stdlib.h>
#include <net/ethernet.h>
#include <unistd.h>
#endif

#if defined(__sun)
#include <stdlib.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <unistd.h>
#include <stropts.h>
#define	ETHER_ADDR_LEN		6
#endif

#define	MAC_ADDR_LENGTH		18

#include <node.h>

using namespace v8;


Handle<Value> GetMacAddress(const Arguments& args) {

  HandleScope scope;

  if (args.Length() < 1) {
    ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
    return scope.Close(Undefined());
  }

  if (!args[0]->IsString()) {
    ThrowException(Exception::TypeError(String::New("Wrong arguments")));
    return scope.Close(Undefined());
  }

  v8::String::AsciiValue device(args[0]);
  char formattedMacAddress[MAC_ADDR_LENGTH];

#if defined(__APPLE_CC__) || defined(__APPLE__)
  unsigned char       macAddress[ETHER_ADDR_LEN];
  char *messageBuffer = NULL;
  int mgmtInfoBase[ETHER_ADDR_LEN];
  size_t              length;
  struct if_msghdr    *interfaceMsgStruct;
  struct sockaddr_dl  *socketStruct;

  // Setup the management Information Base (mib)
  mgmtInfoBase[0] = CTL_NET;        // Request network subsystem
  mgmtInfoBase[1] = AF_ROUTE;       // Routing table info
  mgmtInfoBase[2] = 0;
  mgmtInfoBase[3] = AF_LINK;        // Request link layer information
  mgmtInfoBase[4] = NET_RT_IFLIST;  // Request all configured interfaces

  // With all configured interfaces requested, get handle index
  if ((mgmtInfoBase[5] = if_nametoindex((char *) *device)) == 0) {

    ThrowException(Exception::TypeError(String::New("Error opening interface")));
    return scope.Close(Undefined());

  } else {

    // Get the size of the data available (store in len)
    if (sysctl(mgmtInfoBase, ETHER_ADDR_LEN, NULL, &length, NULL, 0) < 0) {

      ThrowException(Exception::TypeError(String::New("sysctl mgmtInfoBase failure")));
      return scope.Close(Undefined());
    } else {

      // Alloc memory based on above call
      if ((messageBuffer= (char *)malloc(length)) == NULL) {

        ThrowException(Exception::TypeError(String::New("message buffer allocation failure")));
        return scope.Close(Undefined());
      } else {

        // Get system information, store in buffer
        if (sysctl(mgmtInfoBase, ETHER_ADDR_LEN, messageBuffer, &length, NULL, 0) < 0){

          // Release the buffer memory
          free(messageBuffer);

          ThrowException(Exception::TypeError(String::New("sysctl msgBuffer failure")));
          return scope.Close(Undefined());
        }
      }
    }
  }

  // Map msgbuffer to interface message structure
  interfaceMsgStruct = (struct if_msghdr *) messageBuffer;

  // Map to link-level socket structure
  socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);

  // Copy link layer address data in socket structure to an array
  memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, ETHER_ADDR_LEN);

  // Release the buffer memory
  free(messageBuffer);

  snprintf(formattedMacAddress, MAC_ADDR_LENGTH, "%02X:%02X:%02X:%02X:%02X:%02X",
      macAddress[0], macAddress[1], macAddress[2],
      macAddress[3], macAddress[4], macAddress[5]);

#endif

#if defined(linux)

  struct ifreq s;
  const char *mac;

  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);

  // copy in the ethernet interface name
  strncpy(s.ifr_name, *device, IFNAMSIZ);

  if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {

    // Copy link layer address data
    mac = (const char *)&s.ifr_addr.sa_data;

    snprintf(formattedMacAddress, MAC_ADDR_LENGTH, "%02X:%02X:%02X:%02X:%02X:%02X",
        mac[0], mac[1], mac[2],
        mac[3], mac[4], mac[5]);

  } else {

    // TODO lookup the ERR for this and return it to the user for example -1 EMFILE (Too many open files)
    ThrowException(Exception::TypeError(String::New("Error opening interface")));
    return scope.Close(Undefined());
  }

  // Close the file descriptor
  close(fd);

#endif

#if defined(__sun)

  struct ifreq s;
  unsigned char macAddress[ETHER_ADDR_LEN];

  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);

  // copy in the ethernet interface name
  strncpy(s.ifr_name, *device, IFNAMSIZ);

  if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {

    // Copy link layer address data in socket structure to an array
    memcpy(&macAddress, &s.ifr_addr.sa_data, ETHER_ADDR_LEN);
<commit_msg>Reverted to using memcpy for one conversion.<commit_after>#include <sys/socket.h>
#include <sys/types.h>


#if defined(__APPLE_CC__) || defined(__APPLE__)
#include <sys/sysctl.h>

#undef EV_ERROR

#include <net/if.h>
#include <net/if_dl.h>
#include <net/ethernet.h>
#endif

#if defined(linux)
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <linux/if.h>
#include <netdb.h>
#include <stdlib.h>
#include <net/ethernet.h>
#include <unistd.h>
#endif

#if defined(__sun)
#include <stdlib.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <unistd.h>
#include <stropts.h>
#define	ETHER_ADDR_LEN		6
#endif

#define	MAC_ADDR_LENGTH		18

#include <node.h>

using namespace v8;


Handle<Value> GetMacAddress(const Arguments& args) {

  HandleScope scope;

  if (args.Length() < 1) {
    ThrowException(Exception::TypeError(String::New("Wrong number of arguments")));
    return scope.Close(Undefined());
  }

  if (!args[0]->IsString()) {
    ThrowException(Exception::TypeError(String::New("Wrong arguments")));
    return scope.Close(Undefined());
  }

  v8::String::AsciiValue device(args[0]);
  char formattedMacAddress[MAC_ADDR_LENGTH];
  unsigned char macAddress[ETHER_ADDR_LEN];

#if defined(__APPLE_CC__) || defined(__APPLE__)
  char *messageBuffer = NULL;
  int mgmtInfoBase[ETHER_ADDR_LEN];
  size_t              length;
  struct if_msghdr    *interfaceMsgStruct;
  struct sockaddr_dl  *socketStruct;

  // Setup the management Information Base (mib)
  mgmtInfoBase[0] = CTL_NET;        // Request network subsystem
  mgmtInfoBase[1] = AF_ROUTE;       // Routing table info
  mgmtInfoBase[2] = 0;
  mgmtInfoBase[3] = AF_LINK;        // Request link layer information
  mgmtInfoBase[4] = NET_RT_IFLIST;  // Request all configured interfaces

  // With all configured interfaces requested, get handle index
  if ((mgmtInfoBase[5] = if_nametoindex((char *) *device)) == 0) {

    ThrowException(Exception::TypeError(String::New("Error opening interface")));
    return scope.Close(Undefined());

  } else {

    // Get the size of the data available (store in len)
    if (sysctl(mgmtInfoBase, ETHER_ADDR_LEN, NULL, &length, NULL, 0) < 0) {

      ThrowException(Exception::TypeError(String::New("sysctl mgmtInfoBase failure")));
      return scope.Close(Undefined());
    } else {

      // Alloc memory based on above call
      if ((messageBuffer= (char *)malloc(length)) == NULL) {

        ThrowException(Exception::TypeError(String::New("message buffer allocation failure")));
        return scope.Close(Undefined());
      } else {

        // Get system information, store in buffer
        if (sysctl(mgmtInfoBase, ETHER_ADDR_LEN, messageBuffer, &length, NULL, 0) < 0){

          // Release the buffer memory
          free(messageBuffer);

          ThrowException(Exception::TypeError(String::New("sysctl msgBuffer failure")));
          return scope.Close(Undefined());
        }
      }
    }
  }

  // Map msgbuffer to interface message structure
  interfaceMsgStruct = (struct if_msghdr *) messageBuffer;

  // Map to link-level socket structure
  socketStruct = (struct sockaddr_dl *) (interfaceMsgStruct + 1);

  // Copy link layer address data in socket structure to an array
  memcpy(&macAddress, socketStruct->sdl_data + socketStruct->sdl_nlen, ETHER_ADDR_LEN);

  // Release the buffer memory
  free(messageBuffer);

  snprintf(formattedMacAddress, MAC_ADDR_LENGTH, "%02X:%02X:%02X:%02X:%02X:%02X",
      macAddress[0], macAddress[1], macAddress[2],
      macAddress[3], macAddress[4], macAddress[5]);

#endif

#if defined(linux)

  struct ifreq s;

  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);

  // copy in the ethernet interface name
  strncpy(s.ifr_name, *device, IFNAMSIZ);

  if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {

    // Copy link layer address data in socket structure to an array
    memcpy(&macAddress, &s.ifr_addr.sa_data, ETHER_ADDR_LEN);

    snprintf(formattedMacAddress, MAC_ADDR_LENGTH, "%02X:%02X:%02X:%02X:%02X:%02X",
        macAddress[0], macAddress[1], macAddress[2],
        macAddress[3], macAddress[4], macAddress[5]);

  } else {

    // TODO lookup the ERR for this and return it to the user for example -1 EMFILE (Too many open files)
    ThrowException(Exception::TypeError(String::New("Error opening interface")));
    return scope.Close(Undefined());
  }

  // Close the file descriptor
  close(fd);

#endif

#if defined(__sun)

  struct ifreq s;

  int fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);

  // copy in the ethernet interface name
  strncpy(s.ifr_name, *device, IFNAMSIZ);

  if (0 == ioctl(fd, SIOCGIFHWADDR, &s)) {

    // Copy link layer address data in socket structure to an array
    memcpy(&macAddress, &s.ifr_addr.sa_data, ETHER_ADDR_LEN);

{'fraction_non_alphanumeric': 0.1318949941545329, 'fraction_numerical': 0.012753746413008821, 'mean_word_length': 3.9474237644584647, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6364143', 'n_tokens_mistral': 3214, 'n_tokens_neox': 3087, 'n_words': 777}