Git init
[framework/uifw/xorg/lib/libsm.git] / src / sm_genid.c
1 /*
2
3 Copyright © 2002 Sun Microsystems, Inc.  All rights reserved.
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice (including the next
13 paragraph) shall be included in all copies or substantial portions of the
14 Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23
24 */
25 /*
26
27 Copyright 1993, 1998  The Open Group
28
29 Permission to use, copy, modify, distribute, and sell this software and its
30 documentation for any purpose is hereby granted without fee, provided that
31 the above copyright notice appear in all copies and that both that
32 copyright notice and this permission notice appear in supporting
33 documentation.
34
35 The above copyright notice and this permission notice shall be included in
36 all copies or substantial portions of the Software.
37
38 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
39 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
40 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
41 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
42 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
43 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44
45 Except as contained in this notice, the name of The Open Group shall not be
46 used in advertising or otherwise to promote the sale, use or other dealings
47 in this Software without prior written authorization from The Open Group.
48
49 */
50
51 /*
52  * Author: Ralph Mor, X Consortium
53  */
54
55 #ifdef WIN32
56 #define _WILLWINSOCK_
57 #endif
58 #ifdef HAVE_CONFIG_H
59 #include <config.h>
60 #endif
61 #include <X11/SM/SMlib.h>
62 #include "SMlibint.h"
63 #ifdef XTHREADS
64 #include <X11/Xthreads.h>
65 #endif
66 #include <stdio.h>
67
68 #include <time.h>
69 #define Time_t time_t
70
71 #ifndef WIN32
72
73 #if defined(TCPCONN) || defined(STREAMSCONN)
74 #ifndef Lynx
75 #include <sys/socket.h>
76 #else
77 #include <socket.h>
78 #endif
79 #include <netinet/in.h>
80 #include <arpa/inet.h>
81 #define XOS_USE_NO_LOCKING
82 #define X_INCLUDE_NETDB_H
83 #include <X11/Xos_r.h>
84 #endif
85
86 #else /* WIN32 */
87
88 #include <X11/Xwinsock.h>
89 #include <X11/Xw32defs.h>
90 #define X_INCLUDE_NETDB_H
91 #define XOS_USE_MTSAFE_NETDBAPI
92 #include <X11/Xos_r.h>
93
94 #endif /* WIN32 */
95
96 #ifdef MNX_TCPCONN
97 #include <net/gen/netdb.h>
98
99 #define TCPCONN
100 #endif
101
102 #if defined(HAVE_UUID_CREATE)
103 #include <uuid.h>
104 #elif defined(HAVE_LIBUUID)
105 #include <uuid/uuid.h>
106 #endif
107
108 \f
109 char *
110 SmsGenerateClientID(SmsConn smsConn)
111 {
112 #if defined(HAVE_UUID_CREATE)
113     char *id;
114     char **temp;
115     uuid_t uuid;
116     uint32_t status;
117
118     uuid_create(&uuid, &status);
119
120     uuid_to_string(&uuid, &temp, &status);
121
122     if ((id = malloc (strlen (temp) + 2)) != NULL)
123     {
124         id[0] = '2';
125         strcpy (id+1, temp);
126     }
127
128     free(temp);
129
130     return id;
131 #elif defined(HAVE_LIBUUID)
132     char *id;
133     char temp[256];
134     uuid_t uuid;
135
136     uuid_generate(uuid);
137
138     temp[0] = '2';
139     temp[1] = '\0';
140     uuid_unparse_lower(uuid, &temp[1]);
141
142     if ((id = malloc (strlen (temp) + 1)) != NULL)
143         strcpy (id, temp);
144
145     return id;
146 #else
147 #if defined(TCPCONN) || defined(STREAMSCONN)
148     static const char hex[] = "0123456789abcdef";
149     char hostname[256];
150     char address[64], *addr_ptr = address;
151     char temp[256];
152     char *id;
153     static int sequence = 0;
154
155     if (gethostname (hostname, sizeof (hostname)))
156         return (NULL);
157
158     {
159     char* inet_addr;
160     char temp[4], *ptr1, *ptr2;
161     unsigned char decimal[4];
162     int i, len;
163     struct in_addr *haddr = NULL;
164 #if defined(IPv6) && defined(AF_INET6)
165     struct addrinfo *ai, *first_ai;
166     if (getaddrinfo(hostname,NULL,NULL,&ai) != 0)
167         return NULL;
168
169     for (first_ai = ai; ai != NULL; ai = ai->ai_next) {
170         if ( (ai->ai_family == AF_INET) || (ai->ai_family == AF_INET6) ) 
171             break;
172     }
173     if (ai == NULL) {
174         freeaddrinfo(first_ai);
175         return NULL;
176     } 
177
178     if (ai->ai_family == AF_INET6) {
179         unsigned char *cp = (unsigned char *) &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr.s6_addr;
180         
181         *addr_ptr++ = '6';      /* IPv6 address code */
182
183         for (i = 0 ; i < 16 ; i++) {
184             *addr_ptr++ = hex[cp[i] >> 4];
185             *addr_ptr++ = hex[cp[i] & 0x0f];
186         }
187
188         *addr_ptr++ = '\0';
189
190     } else { /* Fall through to IPv4 address handling */
191         haddr = &((struct sockaddr_in *)ai->ai_addr)->sin_addr;
192 #else
193 #ifdef XTHREADS_NEEDS_BYNAMEPARAMS
194     _Xgethostbynameparams hparams;
195 #endif
196     struct hostent *hostp;
197
198     if ((hostp = _XGethostbyname (hostname,hparams)) != NULL)
199         haddr = (struct in_addr *)(hostp->h_addr);
200     else
201         return NULL;
202 #endif
203
204     inet_addr = inet_ntoa (*haddr);
205     for (i = 0, ptr1 = inet_addr; i < 3; i++)
206     {
207         ptr2 = strchr (ptr1, '.');
208         len = ptr2 - ptr1;
209         if (!ptr2 || len > 3) {
210 #if defined(IPv6) && defined(AF_INET6)
211             freeaddrinfo(first_ai);
212 #endif
213             return (NULL);
214         }
215         strncpy (temp, ptr1, len);
216         temp[len] = '\0';
217         decimal[i] = atoi (temp);
218         ptr1 = ptr2 + 1;
219     }
220
221     decimal[3] = atoi (ptr1);
222
223     *addr_ptr++ = '1';
224
225     for (i = 0; i < 4; i++) {
226         *addr_ptr++ = hex[decimal[i] >> 4];
227         *addr_ptr++ = hex[decimal[i] & 0x0f];
228     }
229
230     *addr_ptr++ = '\0';
231
232 #if defined(IPv6) && defined(AF_INET6)
233     }
234     freeaddrinfo(first_ai);
235 #endif
236     }
237
238     sprintf (temp, "1%s%.13ld%.10ld%.4d", address, (long)time((Time_t*)0),
239              (long)getpid(), sequence);
240
241     if (++sequence > 9999)
242         sequence = 0;
243
244     if ((id = malloc (strlen (temp) + 1)) != NULL)
245         strcpy (id, temp);
246
247     return (id);
248 #else
249     return (NULL);
250 #endif
251 #endif
252 }