Re: How to create an UUID (Version 1)

new topic     » goto parent     » topic index » view thread      » older message » newer message

Here is the short version without libpcap and UUID explanations:

include std/math.e 
include std/datetime.e 
include std/os.e 
include std/dll.e 
include std/machine.e 
include std/socket.e 
 
public constant 
  NORMAL = 1, REVERSE = 2, 
  SIOCGIFHWADDR = #8927 
 
constant 
  libc = open_dll("") 
 
constant 
  ioctl_         = define_c_func(libc, "ioctl",{C_INT,C_INT,C_INT},C_INT), 
  socket_        = define_c_func(libc, "socket",{C_INT,C_INT,C_INT},C_INT) 
 
------------------------------------------------------------------------------ 
 
function hex_string(sequence s) 
  sequence result = "" 
  for i = 1 to length(s) do 
    result &= sprintf("%02x", s[i]) 
  end for 
  return result 
end function 
 
------------------------------------------------------------------------------- 
 
function uint_to_bytes(atom n, integer digits, integer direction=REVERSE) 
  sequence s = repeat(0, digits) 
  if direction = NORMAL then 
    for i = digits to 1 by -1 do 
      s[i] = remainder(n, #100) 
      n = floor(n / #100) 
    end for 
  elsif direction = REVERSE then 
    for i = 1 to digits do 
      s[i] = remainder(n, #100) 
      n = floor(n / #100) 
    end for 
  end if 
  return s 
end function 
 
------------------------------------------------------------------------------ 
 
function get_iface_hw_addr(sequence iface_name) 
  sequence hw_addr 
  atom ifreq, sockid, status 
 
  hw_addr = "" 
  ifreq = allocate(200) 
  sockid = c_func(socket_,{AF_INET,SOCK_DGRAM,0}) 
  if sockid < 0 then 
    free(ifreq) 
    return {} 
  end if 
  poke(ifreq,iface_name&0) 
  status = c_func(ioctl_,{sockid, SIOCGIFHWADDR, ifreq}) -- HW Address 
  if status >= 0 then 
    hw_addr = "" 
    for ctr = 18 to 23 do 
      hw_addr = hw_addr & sprintf("%02x", peek(ifreq+ctr)) 
    end for 
    hw_addr = hw_addr[1..length(hw_addr)] 
  end if 
  return hw_addr 
end function 
 
------------------------------------------------------------------------------ 
 
function gregorian_time() 
  sequence dt = now_gmt() 
  sequence from = new(1582,10,15, 0, 0, 0) 
  atom nb = trunc(diff(from, dt)*10_000_000) 
  return nb 
end function 
 
------------------------------------------------------------------------------ 
 
function create_uuid(sequence iface_name) 
  sequence hw_addr 
  integer ok 
 
  sequence uuid = "" 
  sequence s = uint_to_bytes(gregorian_time(), 8, NORMAL) 
  sequence gt = hex_string(s) 
  sequence time_low = tail(gt, 8) 
  gt = gt[1..$-8] 
  sequence time_mid = tail(gt, 4) 
  gt = gt[1..$-4] 
  sequence time_hi = tail(gt, 3) 
 
  sequence dt = now() 
  sequence midnight = dt[1..3] & {0,0,0} 
  s = uint_to_bytes(remainder(diff(midnight, dt),3600), 2, NORMAL) 
  sequence clock_seq = hex_string(s) 
  integer n = clock_seq[1] - #30 
  clock_seq[1] = #30 + (8 + and_bits(n, #3)) 
 
  hw_addr = get_iface_hw_addr(iface_name) 
 
  uuid = time_low & "-" & time_mid & "-1" &  time_hi & 
         "-" & clock_seq & "-" & hw_addr 
  return uuid 
end function 
 
------------------------------------------------------------------------------ 
 
sequence s = create_uuid("eth0") 
puts(1, s & "\n") 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu