Changeset 4484:2e56863a8466 in roaraudio for libroar/authfile.c


Ignore:
Timestamp:
10/11/10 23:49:50 (14 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

support to read ESD and PulseAudio? Cookies

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroar/authfile.c

    r4483 r4484  
    3636#include "libroar.h" 
    3737 
     38struct 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 
     48struct 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 
     90int 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 
     101int 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 
     109int 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 
     117int 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 
     125struct 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 
     161int 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 
     170int 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 
     189int roar_authfile_add_key(struct roar_authfile * authfile, struct roar_authfile_key * key); 
     190 
     191struct 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 
    38226//ll 
Note: See TracChangeset for help on using the changeset viewer.