]>
code.delx.au - pulseaudio/blob - src/pulsecore/auth-cookie.c
2 This file is part of PulseAudio.
4 Copyright 2008 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <sys/types.h>
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/refcnt.h>
31 #include <pulsecore/macro.h>
32 #include <pulsecore/core-util.h>
33 #include <pulsecore/shared.h>
34 #include <pulsecore/authkey.h>
36 #include "auth-cookie.h"
38 struct pa_auth_cookie
{
45 pa_auth_cookie
* pa_auth_cookie_get(pa_core
*core
, const char *cn
, bool create
, size_t size
) {
52 t
= pa_sprintf_malloc("auth-cookie%s%s", cn
? "@" : "", cn
? cn
: "");
54 if ((c
= pa_shared_get(core
, t
))) {
61 return pa_auth_cookie_ref(c
);
64 c
= pa_xmalloc(PA_ALIGN(sizeof(pa_auth_cookie
)) + size
);
70 pa_assert_se(pa_shared_set(core
, t
, c
) >= 0);
72 if (pa_authkey_load_auto(cn
, create
, (uint8_t*) c
+ PA_ALIGN(sizeof(pa_auth_cookie
)), size
) < 0) {
73 pa_auth_cookie_unref(c
);
80 pa_auth_cookie
*pa_auth_cookie_create(pa_core
*core
, const void *data
, size_t size
) {
88 t
= pa_xstrdup("auth-cookie");
90 if ((c
= pa_shared_get(core
, t
))) {
97 return pa_auth_cookie_ref(c
);
100 c
= pa_xmalloc(PA_ALIGN(sizeof(pa_auth_cookie
)) + size
);
106 pa_assert_se(pa_shared_set(core
, t
, c
) >= 0);
108 memcpy((uint8_t *) c
+ PA_ALIGN(sizeof(pa_auth_cookie
)), data
, size
);
113 pa_auth_cookie
* pa_auth_cookie_ref(pa_auth_cookie
*c
) {
115 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
122 void pa_auth_cookie_unref(pa_auth_cookie
*c
) {
124 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
126 if (PA_REFCNT_DEC(c
) > 0)
129 pa_assert_se(pa_shared_remove(c
->core
, c
->name
) >= 0);
135 const uint8_t* pa_auth_cookie_read(pa_auth_cookie
*c
, size_t size
) {
137 pa_assert(PA_REFCNT_VALUE(c
) >= 1);
138 pa_assert(c
->size
== size
);
140 return (const uint8_t*) c
+ PA_ALIGN(sizeof(pa_auth_cookie
));