Changeset 4484:2e56863a8466 in roaraudio
- Timestamp:
- 10/11/10 23:49:50 (14 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
include/libroar/authfile.h
r4483 r4484 39 39 #include "libroar.h" 40 40 41 #define ROAR_AUTHFILE_TYPE_AUTO -1 /* auto detect type of key */ 42 #define ROAR_AUTHFILE_TYPE_NONE 0 /* dummy */ 43 #define ROAR_AUTHFILE_TYPE_ROAR 1 /* RoarAudio */ 44 #define ROAR_AUTHFILE_TYPE_ESD 2 /* EsounD, plain cookie, len=16 byte */ 45 #define ROAR_AUTHFILE_TYPE_PULSE 3 /* PulseAudio, plain cookie, len=256 byte */ 46 47 #define ROAR_AUTHFILE_VERSION_AUTO -1 48 49 struct roar_authfile; 50 51 struct roar_authfile * roar_authfile_open(int type, const char * filename, int rw, int version); 52 int roar_authfile_close(struct roar_authfile * authfile); 53 54 int roar_authfile_lock(struct roar_authfile * authfile); 55 int roar_authfile_unlock(struct roar_authfile * authfile); 56 57 int roar_authfile_sync(struct roar_authfile * authfile); 58 59 struct roar_authfile_key { 60 size_t refc; 61 int type; 62 int index; 63 const char * address; 64 void * data; 65 size_t len; 66 }; 67 68 struct roar_authfile_key * roar_authfile_key_new(int type, size_t len, const char * addr); 69 #define roar_authfile_key_free(key) roar_authfile_key_unref(key) 70 int roar_authfile_key_ref(struct roar_authfile_key * key); 71 int roar_authfile_key_unref(struct roar_authfile_key * key); 72 73 int roar_authfile_add_key(struct roar_authfile * authfile, struct roar_authfile_key * key); 74 75 struct roar_authfile_key * roar_authfile_lookup_key(struct roar_authfile * authfile, int type, int minindex, const char * address); 76 41 77 #endif 42 78 -
libroar/authfile.c
r4483 r4484 36 36 #include "libroar.h" 37 37 38 struct roar_authfile { 39 size_t refc; 40 size_t lockc; 41 int type; 42 int rw; 43 int version; 44 size_t magiclen; 45 struct roar_vio_calls vio; 46 }; 47 48 struct roar_authfile * roar_authfile_open(int type, const char * filename, int rw, int version) { 49 struct roar_vio_defaults def; 50 struct roar_authfile * ret; 51 size_t magiclen = 0; 52 53 switch (type) { 54 case ROAR_AUTHFILE_TYPE_ESD: 55 case ROAR_AUTHFILE_TYPE_PULSE: 56 break; 57 default: 58 return NULL; 59 break; 60 } 61 62 if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, rw ? O_RDWR|O_CREAT : O_RDONLY, 0600) == -1 ) 63 return NULL; 64 65 if ( (ret = roar_mm_malloc(sizeof(struct roar_authfile))) == NULL ) 66 return NULL; 67 68 memset(ret, 0, sizeof(struct roar_authfile)); 69 70 ret->refc = 1; 71 ret->lockc = 0; 72 ret->type = type; 73 ret->rw = rw; 74 ret->version = version; 75 ret->magiclen = magiclen; 76 77 filename = roar_mm_strdup(filename); 78 79 if ( roar_vio_open_dstr(&(ret->vio), (char*)filename, &def, 1) == -1 ) { 80 roar_mm_free((void*)filename); 81 roar_mm_free(ret); 82 return NULL; 83 } 84 85 roar_mm_free((void*)filename); 86 87 return ret; 88 } 89 90 int roar_authfile_close(struct roar_authfile * authfile) { 91 if ( authfile == NULL ) 92 return -1; 93 94 roar_vio_close(&(authfile->vio)); 95 96 roar_mm_free(authfile); 97 98 return 0; 99 } 100 101 int roar_authfile_lock(struct roar_authfile * authfile) { 102 if ( authfile == NULL ) 103 return -1; 104 105 // TODO: implement real locking here. 106 return 0; 107 } 108 109 int roar_authfile_unlock(struct roar_authfile * authfile) { 110 if ( authfile == NULL ) 111 return -1; 112 113 // TODO: implement real unlocking here. 114 return 0; 115 } 116 117 int roar_authfile_sync(struct roar_authfile * authfile) { 118 if ( authfile == NULL ) 119 return -1; 120 121 return roar_vio_sync(&(authfile->vio)); 122 } 123 124 125 struct roar_authfile_key * roar_authfile_key_new(int type, size_t len, const char * addr) { 126 struct roar_authfile_key * ret; 127 size_t addrlen; 128 size_t retlen; 129 130 if ( addr == NULL ) { 131 addrlen = 0; 132 } else { 133 addrlen = strlen(addr) + 1; 134 } 135 136 retlen = sizeof(struct roar_authfile_key) + len + addrlen; 137 138 if ( (ret = roar_mm_malloc(retlen)) == NULL ) 139 return NULL; 140 141 memset(ret, 0, retlen); 142 143 ret->refc = 1; 144 ret->type = type; 145 ret->index = -1; 146 147 if ( addrlen == 0 ) { 148 ret->address = NULL; 149 } else { 150 ret->address = (const void *)ret+sizeof(struct roar_authfile_key); 151 memcpy((void*)ret->address, addr, addrlen); 152 } 153 154 ret->data = (void*)ret->address + addrlen; 155 156 ret->len = len; 157 158 return ret; 159 } 160 161 int roar_authfile_key_ref(struct roar_authfile_key * key) { 162 if ( key == NULL ) 163 return -1; 164 165 key->refc++; 166 167 return 0; 168 } 169 170 int roar_authfile_key_unref(struct roar_authfile_key * key) { 171 if ( key == NULL ) 172 return -1; 173 174 if ( key->refc == 0 ) { 175 ROAR_ERR("roar_authfile_key_unref(key=%p): Key has reference count of zero. This is bad. assuming refc=1", key); 176 key->refc = 1; 177 } 178 179 key->refc--; 180 181 if ( key->refc > 0 ) 182 return 0; 183 184 roar_mm_free(key); 185 186 return 0; 187 } 188 189 int roar_authfile_add_key(struct roar_authfile * authfile, struct roar_authfile_key * key); 190 191 struct roar_authfile_key * roar_authfile_lookup_key(struct roar_authfile * authfile, 192 int type, int minindex, const char * address) { 193 struct roar_authfile_key * ret = NULL; 194 ssize_t len; 195 196 if ( authfile == NULL ) 197 return NULL; 198 199 switch (authfile->type) { 200 case ROAR_AUTHFILE_TYPE_ESD: 201 case ROAR_AUTHFILE_TYPE_PULSE: 202 if ( type != ROAR_AUTH_T_COOKIE || minindex > 0 ) 203 return NULL; 204 205 if ( (ret = roar_authfile_key_new(type, 256, NULL)) == NULL ) 206 return NULL; 207 208 len = roar_vio_read(&(authfile->vio), ret->data, ret->len); 209 210 if ( len == -1 ) { 211 roar_authfile_key_unref(ret); 212 return NULL; 213 } 214 215 ret->len = len; 216 ret->index = 0; 217 break; 218 default: 219 return NULL; 220 break; 221 } 222 223 return ret; 224 } 225 38 226 //ll
Note: See TracChangeset
for help on using the changeset viewer.