uuid.pp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. {$mode objfpc}
  2. {$H+}
  3. unit uuid;
  4. Interface
  5. uses SysUtils;
  6. Function CreateMacGUID(Out GUID : TGUID) : Integer;
  7. Implementation
  8. uses unixtype, sockets, baseunix, unix;
  9. Const
  10. MAX_ADJUSTMENT = 10;
  11. IPPROTO_IP = 0;
  12. // AF_INET = 2;
  13. // SOCK_DGRAM = 2;
  14. IF_NAMESIZE = 16;
  15. SIOCGIFCONF = $8912;
  16. SIOCGIFHWADDR = $8927;
  17. Type
  18. {$packrecords c}
  19. tifr_ifrn = record
  20. case integer of
  21. 0 : (ifrn_name: array [0..IF_NAMESIZE-1] of char);
  22. end;
  23. tifmap = record
  24. mem_start : cardinal;
  25. mem_end : cardinal;
  26. base_addr : word;
  27. irq : byte;
  28. dma : byte;
  29. port : byte;
  30. end;
  31. TIFrec = record
  32. ifr_ifrn : tifr_ifrn;
  33. case integer of
  34. 0 : (ifru_addr : TSockAddr);
  35. 1 : (ifru_dstaddr : TSockAddr);
  36. 2 : (ifru_broadaddr : TSockAddr);
  37. 3 : (ifru_netmask : TSockAddr);
  38. 4 : (ifru_hwaddr : TSockAddr);
  39. 5 : (ifru_flags : word);
  40. 6 : (ifru_ivalue : longint);
  41. 7 : (ifru_mtu : longint);
  42. 8 : (ifru_map : tifmap);
  43. 9 : (ifru_slave : Array[0..IF_NAMESIZE-1] of char);
  44. 10 : (ifru_newname : Array[0..IF_NAMESIZE-1] of char);
  45. 11 : (ifru_data : pointer);
  46. end;
  47. TIFConf = record
  48. ifc_len : longint;
  49. case integer of
  50. 0 : (ifcu_buf : pointer);
  51. 1 : (ifcu_req : ^tifrec);
  52. end;
  53. tuuid = record
  54. time_low : cardinal;
  55. time_mid : Word;
  56. time_hi_and_version : Word;
  57. clock_seq : Word;
  58. node : Array[0..5] of byte;
  59. end;
  60. Var
  61. MacAddr : Packed Array[1..6] of byte = (0,0,0,0,0,0);
  62. MacAddrTried : Byte = 0 ;
  63. Last : TTimeVal = (tv_sec:0;tv_usec:0);
  64. ClockSeq : Word = 0;
  65. AdjustMent : Integer = 0;
  66. Procedure GetRandomBytes(Var Buf; NBytes : Integer);
  67. Var
  68. I : Integer;
  69. P : PByte;
  70. begin
  71. P:=@Buf;
  72. Randomize;
  73. For I:=0 to NBytes-1 do
  74. P[i]:=Random(256);
  75. end;
  76. Function GetMacAddr : Boolean;
  77. var
  78. i,j,n,Sd : Integer;
  79. buf : Array[0..1023] of byte;
  80. ifc : TIfConf;
  81. ifr : TIFRec;
  82. ifp : ^TIFRec;
  83. p : PChar;
  84. begin
  85. Result:=MacAddrTried>0;
  86. If Result then
  87. Result:=MacAddrTried>1
  88. else
  89. begin
  90. MacAddrTried:=1;
  91. sd:=fpSocket(AF_INET,SOCK_DGRAM,IPPROTO_IP);
  92. if (sd<0) then
  93. exit;
  94. Try
  95. ifc.ifc_len:=Sizeof(Buf);
  96. ifc.ifcu_buf:=@buf;
  97. if fpioctl(sd, SIOCGIFCONF, @ifc)<0 then
  98. Exit;
  99. n:= ifc.ifc_len;
  100. i:=0;
  101. While (Not Result) and (I<N) do
  102. begin
  103. ifp:=@PByte(ifc.ifcu_buf)[i];
  104. move(ifp^.ifr_ifrn.ifrn_name,ifr.ifr_ifrn.ifrn_name,IF_NAMESIZE);
  105. if (fpioctl(sd, SIOCGIFHWADDR, @ifr) >= 0) then
  106. begin
  107. P:=Pchar(@ifr.ifru_hwaddr.sa_data);
  108. Result:=(p[0]<>#0) or (p[1]<>#0) or (p[2]<>#0)
  109. or (p[3]<>#0) or (p[4]<>#0) or (p[5]<>#0);
  110. If Result Then
  111. begin
  112. Move(P^,MacAddr,SizeOf(MacAddr));
  113. MacAddrTried:=2;
  114. // DumpMacAddr;
  115. end;
  116. end;
  117. I:=I+sizeof(tifrec);
  118. end;
  119. Finally
  120. fileClose(sd);
  121. end;
  122. end;
  123. end;
  124. Function GetClock(Var ClockHigh,ClockLow : Cardinal; Var RetClockSeq : Word) : boolean;
  125. Var
  126. TV : TTImeVal;
  127. ClockReg : QWord;
  128. OK : Boolean;
  129. begin
  130. OK:=True;
  131. Repeat
  132. FPGetTimeOfDay(@Tv,Nil);
  133. If (Last.tv_sec=0) and (last.tv_sec=0) then
  134. begin
  135. GetRandomBytes(ClockSeq,SizeOf(ClockSeq));
  136. ClockSeq:=ClockSeq and $1FFF;
  137. last:=TV;
  138. Dec(last.tv_sec);
  139. end;
  140. if (tv.tv_sec<last.tv_sec) or
  141. ((tv.tv_sec=last.tv_sec) and (tv.tv_usec<last.tv_usec)) then
  142. begin
  143. ClockSeq:=(ClockSeq+1) and $1FFF;
  144. Adjustment:=0;
  145. Last:=Tv;
  146. end
  147. else if (tv.tv_sec=last.tv_sec) and (tv.tv_usec=last.tv_usec) then
  148. begin
  149. If Adjustment>=MAX_ADJUSTMENT then
  150. OK:=False
  151. else
  152. inc(AdjustMent);
  153. end
  154. else
  155. begin
  156. AdjustMent:=0;
  157. Last:=tv;
  158. end;
  159. Until OK;
  160. ClockReg:=tv.tv_usec*10+adjustment;
  161. Inc(ClockReg,tv.tv_sec*10000000);
  162. Inc(ClockReg,($01B21DD2 shl 32) + $13814000);
  163. ClockHigh :=Hi(ClockReg);
  164. ClockLow :=Lo(ClockReg);
  165. RetClockSeq :=ClockSeq;
  166. Result :=True;
  167. end;
  168. Procedure UUIDPack(Const UU : TUUID; Var GUID : TGUID);
  169. Var
  170. tmp : Cardinal;
  171. P : PByte;
  172. begin
  173. P:=@GUID;
  174. tmp:=uu.time_low;
  175. P[3]:=tmp and $FF;
  176. tmp:=tmp shr 8;
  177. P[2]:=tmp and $FF;
  178. tmp:=tmp shr 8;
  179. P[1]:=tmp and $FF;
  180. tmp:=tmp shr 8;
  181. P[0]:=tmp and $FF;
  182. tmp:=uu.time_mid;
  183. P[5]:=tmp and $FF;
  184. tmp:=tmp shr 8;
  185. P[4]:=tmp and $FF;
  186. tmp:=uu.time_hi_and_version;
  187. P[7]:=tmp and $FF;
  188. tmp:=tmp shr 8;
  189. P[6]:=tmp and $FF;
  190. tmp:=uu.clock_seq;
  191. P[9]:=tmp and $FF;
  192. tmp:=tmp shr 8;
  193. P[8]:=tmp and $FF;
  194. Move(uu.node,P[10],6);
  195. end;
  196. Procedure DumpMacAddr;
  197. var
  198. I : Integer;
  199. begin
  200. Write('Mac Addr: ');
  201. For i:=1 to 6 do
  202. write(hexstr(MacAddr[i],2),':');
  203. end;
  204. Function CreateMacGUID(Out GUID : TGUID) : Integer;
  205. Var
  206. UU : TUUId;
  207. ClockMid : Cardinal;
  208. begin
  209. Result:=Ord(not GetMacAddr);
  210. If (Result=0) then
  211. begin
  212. // DumpMacAddr;
  213. // Writeln;
  214. GetClock(ClockMid,uu.time_low,uu.clock_seq);
  215. uu.Clock_seq:=uu.Clock_seq or $8000;
  216. uu.time_mid:=lo(clockMid);
  217. uu.time_hi_and_version:=hi(ClockMid) or $1000;
  218. move(MacAddr,uu.node,sizeof(MacAddr));
  219. UUIDPack(UU,GUID);
  220. end;
  221. end;
  222. initialization
  223. OnCreateGUID:=@CreateMacGUID;
  224. end.