2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Bartlett 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "lib/util/debug.h"
22 #include "lib/util/fault.h"
23 #include "lib/util/server_id.h"
24 #include "lib/util/byteorder.h"
25 #include "librpc/gen_ndr/server_id.h"
27 bool server_id_same_process(const struct server_id
*p1
,
28 const struct server_id
*p2
)
30 return ((p1
->pid
== p2
->pid
) && (p1
->vnn
== p2
->vnn
));
33 int server_id_cmp(const struct server_id
*p1
, const struct server_id
*p2
)
35 if (p1
->vnn
!= p2
->vnn
) {
36 return (p1
->vnn
< p2
->vnn
) ? -1 : 1;
38 if (p1
->pid
!= p2
->pid
) {
39 return (p1
->pid
< p2
->pid
) ? -1 : 1;
41 if (p1
->task_id
!= p2
->task_id
) {
42 return (p1
->task_id
< p2
->task_id
) ? -1 : 1;
44 if (p1
->unique_id
!= p2
->unique_id
) {
45 return (p1
->unique_id
< p2
->unique_id
) ? -1 : 1;
50 bool server_id_equal(const struct server_id
*p1
, const struct server_id
*p2
)
52 int cmp
= server_id_cmp(p1
, p2
);
56 char *server_id_str_buf(struct server_id id
, struct server_id_buf
*dst
)
58 if (server_id_is_disconnected(&id
)) {
59 strlcpy(dst
->buf
, "disconnected", sizeof(dst
->buf
));
60 } else if ((id
.vnn
== NONCLUSTER_VNN
) && (id
.task_id
== 0)) {
61 snprintf(dst
->buf
, sizeof(dst
->buf
), "%llu",
62 (unsigned long long)id
.pid
);
63 } else if (id
.vnn
== NONCLUSTER_VNN
) {
64 snprintf(dst
->buf
, sizeof(dst
->buf
), "%llu.%u",
65 (unsigned long long)id
.pid
, (unsigned)id
.task_id
);
66 } else if (id
.task_id
== 0) {
67 snprintf(dst
->buf
, sizeof(dst
->buf
), "%u:%llu",
68 (unsigned)id
.vnn
, (unsigned long long)id
.pid
);
70 snprintf(dst
->buf
, sizeof(dst
->buf
), "%u:%llu.%u",
72 (unsigned long long)id
.pid
,
73 (unsigned)id
.task_id
);
78 size_t server_id_str_buf_unique(struct server_id id
, char *buf
, size_t buflen
)
80 struct server_id_buf idbuf
;
81 char unique_buf
[21]; /* 2^64 is 18446744073709551616, 20 chars */
82 size_t idlen
, unique_len
, needed
;
84 server_id_str_buf(id
, &idbuf
);
86 idlen
= strlen(idbuf
.buf
);
87 unique_len
= snprintf(unique_buf
, sizeof(unique_buf
), "%"PRIu64
,
89 needed
= idlen
+ unique_len
+ 2;
91 if (buflen
>= needed
) {
92 memcpy(buf
, idbuf
.buf
, idlen
);
94 memcpy(buf
+ idlen
+ 1, unique_buf
, unique_len
+1);
100 struct server_id
server_id_from_string(uint32_t local_vnn
,
101 const char *pid_string
)
103 struct server_id templ
= {
104 .vnn
= NONCLUSTER_VNN
, .pid
= UINT64_MAX
106 struct server_id result
;
110 * We accept various forms with 1, 2 or 3 component forms
111 * because the server_id_str_buf() can print different forms, and
112 * we want backwards compatibility for scripts that may call
117 ret
= sscanf(pid_string
, "%"SCNu32
":%"SCNu64
".%"SCNu32
"/%"SCNu64
,
118 &result
.vnn
, &result
.pid
, &result
.task_id
,
125 ret
= sscanf(pid_string
, "%"SCNu32
":%"SCNu64
".%"SCNu32
,
126 &result
.vnn
, &result
.pid
, &result
.task_id
);
132 ret
= sscanf(pid_string
, "%"SCNu32
":%"SCNu64
"/%"SCNu64
,
133 &result
.vnn
, &result
.pid
, &result
.unique_id
);
139 ret
= sscanf(pid_string
, "%"SCNu32
":%"SCNu64
,
140 &result
.vnn
, &result
.pid
);
146 ret
= sscanf(pid_string
, "%"SCNu64
".%"SCNu32
"/%"SCNu64
,
147 &result
.pid
, &result
.task_id
, &result
.unique_id
);
149 result
.vnn
= local_vnn
;
154 ret
= sscanf(pid_string
, "%"SCNu64
".%"SCNu32
,
155 &result
.pid
, &result
.task_id
);
157 result
.vnn
= local_vnn
;
162 ret
= sscanf(pid_string
, "%"SCNu64
"/%"SCNu64
,
163 &result
.pid
, &result
.unique_id
);
165 result
.vnn
= local_vnn
;
170 ret
= sscanf(pid_string
, "%"SCNu64
, &result
.pid
);
172 result
.vnn
= local_vnn
;
176 if (strcmp(pid_string
, "disconnected") == 0) {
177 server_id_set_disconnected(&result
);
185 * Set the serverid to the special value that represents a disconnected
186 * client for (e.g.) durable handles.
188 void server_id_set_disconnected(struct server_id
*id
)
190 *id
= (struct server_id
) {
192 .task_id
= UINT32_MAX
,
193 .vnn
= NONCLUSTER_VNN
,
194 .unique_id
= SERVERID_UNIQUE_ID_NOT_TO_VERIFY
,
199 * check whether a serverid is the special placeholder for
200 * a disconnected client
202 bool server_id_is_disconnected(const struct server_id
*id
)
204 struct server_id dis
;
206 SMB_ASSERT(id
!= NULL
);
208 server_id_set_disconnected(&dis
);
210 return server_id_equal(id
, &dis
);
213 void server_id_put(uint8_t buf
[SERVER_ID_BUF_LENGTH
],
214 const struct server_id id
)
216 SBVAL(buf
, 0, id
.pid
);
217 SIVAL(buf
, 8, id
.task_id
);
218 SIVAL(buf
, 12, id
.vnn
);
219 SBVAL(buf
, 16, id
.unique_id
);
222 void server_id_get(struct server_id
*id
,
223 const uint8_t buf
[SERVER_ID_BUF_LENGTH
])
225 id
->pid
= BVAL(buf
, 0);
226 id
->task_id
= IVAL(buf
, 8);
227 id
->vnn
= IVAL(buf
, 12);
228 id
->unique_id
= BVAL(buf
, 16);