char* fetion_share_compute_md5(const char *absolutePath) { MD5_CTX ctx; FILE *file; unsigned char input[1024]; unsigned char output[16]; int n , i = 0; char* res = (char*)sal_malloc(33); file = fopen(absolutePath , "r"); MD5_Init(&ctx); while(1) { n = fread(input , 1 , sizeof(input) , file); if(n == 0) break; MD5_Update(&ctx , input , n); } MD5_Final(output , &ctx); memset(res, 0, 33); while(i < 16) { sprintf(res + i * 2 , "%02x" , output[i]); i ++; }; return res; }
/* Allocate a new hash. */ static ctclib_hash_t * _ctclib_hash_create_size (uint32 size, hash_mem_type_t type, uint32 (*hash_key) (), bool (*hash_cmp) ()) { ctclib_hash_t *hash; hash = sal_malloc (sizeof (ctclib_hash_t)); if (NULL == hash) return NULL; #ifdef _USER_KERNEL_SHM_ if(CTCLIB_HASH_SYS_MEM == type) hash->index = XCALLOC (CTCLLB_MEM_HASH_MODULE, sizeof (ctclib_hash_backet_t *) * size); else hash->index = XCALLOC_SHM (CTCLLB_MEM_HASH_MODULE, sizeof (ctclib_hash_backet_t *) * size); #else hash->index = sal_calloc (sizeof (ctclib_hash_backet_t *) * size); #endif if (NULL == hash->index) { sal_free(hash); return NULL; } hash->size = size; hash->hash_key = hash_key; hash->hash_cmp = hash_cmp; hash->count = 0; return hash; }
Share *fetion_share_new(const char *sipuri) { Share *share = (Share*)sal_malloc(sizeof(Share)); memset(share , 0 , sizeof(Share)); strcpy(share->sipuri , sipuri); return share; }
LOCAL ndas_error_t ndcmd_stop(void) { int i,size, slot; ndas_error_t ret; ndas_device_info info; struct ndas_registered *data; size = ndas_registered_size(); if ( !NDAS_SUCCESS(size) ) { dbgl_blk(1,"fail to get ndas_registered_size"); goto out; } dbgl_blk(1,"ndas_registered_size=%d", size); data = sal_malloc(sizeof(struct ndas_registered)* size); if ( !data ) { goto out; } for ( i = NDAS_FIRST_SLOT_NR; i < NDAS_MAX_SLOT_NR; i++) { struct ndas_slot* sd = NDAS_GET_SLOT_DEV(i); if ( !sd ) { continue; } slot = sd->slot; slot_destroy(slot); ndas_disable_slot(slot); } ret = ndas_registered_list(data, size); if ( !NDAS_SUCCESS(ret) ) { goto out_free; } if ( ret != size ) { goto out_free; } for ( i = 0; i < size; i ++ ) { dbgl_blk(1,"name[%d]=%s", i , data[i].name); ret = ndas_get_ndas_dev_info(data[i].name, &info); if ( !NDAS_SUCCESS(ret) ) continue; nproc_remove_ndev(data[i].name); ndas_unregister_device(data[i].name); } out_free: sal_free(data); out: return ndas_stop(); }
Contact* fetion_contact_new() { Contact* list = (Contact*)sal_malloc(sizeof(Contact)); if(list == NULL){ return NULL; } memset(list , 0 , sizeof(Contact)); list->imageChanged = IMAGE_NOT_INITIALIZED; list->state = P_HIDDEN; list->pre = list; list->next = list; return list; }
/* Lookup and return hash bucket in hash. If there is no corresponding hash bucket and alloc_func is specified, create new hash bucket. */ static void * _ctclib_hash_get (hash_mem_type_t type, ctclib_hash_t *hash, void *data, void * (*alloc_func) ()) { uint32 key; uint32 index; void *newdata; ctclib_hash_backet_t *backet; if (!hash) return NULL; key = (*hash->hash_key) (data); index = key % hash->size; for (backet = hash->index[index]; backet != NULL; backet = backet->next) { if (backet->key == key && (*hash->hash_cmp) (backet->data, data) == TRUE) { return backet->data; } } if (alloc_func) { newdata = (*alloc_func) (data); if (newdata == NULL) return NULL; #ifdef _USER_KERNEL_SHM_ if(CTCLIB_HASH_SYS_MEM == type) backet = XMALLOC (CTCLLB_MEM_HASH_MODULE, sizeof (ctclib_hash_backet_t)); else backet = XMALLOC_SHM (CTCLLB_MEM_HASH_MODULE, sizeof (ctclib_hash_backet_t)); #else backet = sal_malloc (sizeof (ctclib_hash_backet_t)); #endif if (!backet) return NULL; backet->data = newdata; backet->key = key; backet->next = hash->index[index]; hash->index[index] = backet; hash->count++; return backet->data; } return NULL; }
Conversation* fetion_conversation_new(User* user, const char* sipuri , FetionSip* sip) { Conversation* conversation = (Conversation*)sal_malloc(sizeof(Conversation)); memset(conversation , 0 , sizeof(Conversation)); conversation->currentUser = user; if(sipuri != NULL) conversation->currentContact = fetion_contact_list_find_by_sipuri(user->contactList , sipuri); else conversation->currentContact = NULL; if(sipuri != NULL && conversation->currentContact == NULL){ sal_free(conversation); return NULL; } conversation->currentSip = sip; return conversation; }
/* Lookup and return hash bucket in hash. If there is no corresponding hash bucket and alloc_func is specified, create new hash bucket. */ void * ctclib_hash_get2(ctclib_hash_t *hash, void *data, void *arg, void * (*alloc_func) ()) { uint32 key; uint32 index; void *newdata; ctclib_hash_backet_t *backet; if (!hash) return NULL; key = (*hash->hash_key) (data); index = key % hash->size; for (backet = hash->index[index]; backet != NULL; backet = backet->next) { if (backet->key == key && (*hash->hash_cmp) (backet->data, data) == TRUE) { return backet->data; } } if (alloc_func) { newdata = (*alloc_func) (data, arg); if (newdata == NULL) return NULL; backet = sal_malloc (sizeof (ctclib_hash_backet_t)); if (!backet) return NULL; backet->data = newdata; backet->key = key; backet->next = hash->index[index]; hash->index[index] = backet; hash->count++; return backet->data; } return NULL; }
Share *fetion_share_new_with_path(const char *sipuri , const char *absolutePath) { Share *share = (Share*)sal_malloc(sizeof(Share)); char *name = NULL; char *md5 = NULL; memset(share , 0 , sizeof(Share)); strcpy(share->guid , GUID); strcpy(share->sessionid , SESSIONID); strcpy(share->sipuri , sipuri); strcpy(share->absolutePath , absolutePath); name = basename((char*)absolutePath); strcpy(share->filename , name); share->filesize = fetion_share_get_filesize(absolutePath); md5 = fetion_share_compute_md5(absolutePath); strcpy(share->md5 , md5); sal_free(md5); return share; }
LOCAL int slot_create(int s) { struct ndas_slot *slot; dbgl_blk(1, "ing s=%d",s); slot = sal_malloc(sizeof(struct ndas_slot)); if ( slot == NULL ) return -1; memset(slot, 0, sizeof(struct ndas_slot)); slot->slot = s; sema_init(&slot->mutex, 1); slot->enabled = 0; slot->devname[0] = 0; NDAS_SET_SLOT_DEV(s, slot); dbgl_blk(3, "ed s=%d OK",s); nproc_add_slot(slot); return 0; }
int fetion_conversation_invite_friend(Conversation* conversation) { FetionSip* sip = conversation->currentUser->sip; char *res , *ip , *credential , auth[256] , *body; int port , ret; FetionConnection* conn; Proxy *proxy = conversation->currentUser->config->proxy; SipHeader *eheader , *theader , *mheader , *nheader , *aheader; /*start chat*/ fetion_sip_set_type(sip , SIP_SERVICE); eheader = fetion_sip_event_header_new(SIP_EVENT_STARTCHAT); fetion_sip_add_header(sip , eheader); res = fetion_sip_to_string(sip , NULL); tcp_connection_send(sip->tcp , res , strlen(res)); sal_free(res); res = NULL; res = fetion_sip_get_response(sip); if(!res) return -1; memset(auth , 0 , sizeof(auth)); fetion_sip_get_attr(res , "A" , auth); if(auth==NULL) return -1; fetion_sip_get_auth_attr(auth , &ip , &port , &credential); sal_free(res); res = NULL; conn = tcp_connection_new(); if(proxy != NULL && proxy->proxyEnabled) ret = tcp_connection_connect_with_proxy(conn , ip , port , proxy); else ret = tcp_connection_connect(conn , ip , port); if(ret == -1) return -1; /*clone sip*/ conversation->currentSip = fetion_sip_clone(conversation->currentUser->sip); memset(conversation->currentSip->sipuri, 0 , sizeof(conversation->currentSip->sipuri)); strcpy(conversation->currentSip->sipuri , conversation->currentContact->sipuri); fetion_sip_set_connection(conversation->currentSip , conn); sal_free(ip); ip = NULL; /*register*/ sip = conversation->currentSip; fetion_sip_set_type(sip , SIP_REGISTER); aheader = fetion_sip_credential_header_new(credential); theader = fetion_sip_header_new("K" , "text/html-fragment"); mheader = fetion_sip_header_new("K" , "multiparty"); nheader = fetion_sip_header_new("K" , "nudge"); fetion_sip_add_header(sip , aheader); fetion_sip_add_header(sip , theader); fetion_sip_add_header(sip , mheader); fetion_sip_add_header(sip , nheader); res = fetion_sip_to_string(sip , NULL); tcp_connection_send(conn , res , strlen(res)); sal_free(res);res = NULL; sal_free(credential); credential = NULL; res = fetion_sip_get_response(sip); sal_free(res); res = NULL; /*invite buddy*/ fetion_sip_set_type(sip , SIP_SERVICE); eheader = fetion_sip_event_header_new(SIP_EVENT_INVITEBUDDY); fetion_sip_add_header(sip , eheader); body = generate_invite_friend_body(conversation->currentContact->sipuri); res = fetion_sip_to_string(sip , body); sal_free(body); body = NULL; tcp_connection_send(sip->tcp , res , strlen(res)); sal_free(res); res = NULL; res = fetion_sip_get_response(sip); if(fetion_sip_get_code(res) == 200) { sal_free(res); res = (char*)sal_malloc(2048); memset(res , 0 , 2048); tcp_connection_recv(sip->tcp , res , 2048); //res = fetion_sip_get_response(sip); sal_free(res); return 1; }else{ sal_free(res); return -1; } }
LOCAL inline ndas_error_t ndcmd_probe(char *user_array, int sz, int *count, int convert_to_id) { ndas_error_t ret = NDAS_OK; struct ndas_probed *data; char id[NDAS_ID_LENGTH+1]; char key[NDAS_KEY_LENGTH+1]; int retry = 0; dbgl_blk(3,"ing sz=%d",sz); do { int i, c; c = ndas_probed_size(); if ( !NDAS_SUCCESS(c) ) { return c; } if ( user_array == NULL ) {/* just return the count */ (*count) = c; return NDAS_OK; } if ( sz != c ) return NDAS_ERROR_INVALID_PARAMETER; dbgl_blk(3,"c=%d", c); data = sal_malloc(sizeof(struct ndas_probed) * c); if ( !data ) { ret = NDAS_ERROR_OUT_OF_MEMORY; break; } // ndas_probed_list() returns the count. ret = ndas_probed_list(data, c); if ( ret == NDAS_ERROR_INVALID_PARAMETER ) { if ( retry++ > 2 ) { break; } sal_free(data); continue; } if ( ret < 0 ) { break; } // Reset the return value. ret = NDAS_OK; if (convert_to_id) { if ( !access_ok(VERIFY_WRITE, user_array, (NDAS_ID_LENGTH+1) * c) ) { ret = NDAS_ERROR_INTERNAL; break; } } else { if ( !access_ok(VERIFY_WRITE, user_array, (NDAS_SERIAL_LENGTH+1) * c) ) { ret = NDAS_ERROR_INTERNAL; break; } } for ( i = 0; i < c; i++ ) { int result; dbgl_blk(5, "serial=%s", data[i].serial); if (convert_to_id) { ret = ndas_query_ndas_id_by_serial(data[i].serial, id, key); if (ret == NDAS_OK) { result = copy_to_user(user_array + (NDAS_ID_LENGTH+1)*i, id, NDAS_ID_LENGTH+1); if ( result != 0 ) { ret = NDAS_ERROR_INTERNAL; (*count) = 0; break; } } else { // ndas_query_ndas_id_by_serial is not supported (*count) = 0; break; } } else { result = copy_to_user(user_array + (NDAS_SERIAL_LENGTH+1)*i, data[i].serial, NDAS_SERIAL_LENGTH+1); if ( result != 0 ) { ret = NDAS_ERROR_INTERNAL; (*count) = 0; break; } } } if (NDAS_SUCCESS(ret)) { (*count)=c; } break; } while(1); sal_free(data); dbgl_blk(3,"ed"); return ret; }
int32 ctclib_reconstruct_system_cmd_chld_clone(sal_sys_cmd_clone_fn func,char *cmdstring) { pid_t pid; int status; struct sigaction ignore,saveintr,savequit; sigset_t chldmask,savemask; void* stack; #define MAX_STACK 8192 if (cmdstring == NULL) return(1); /* always a command processor with UNIX */ stack = sal_malloc(MAX_STACK); if(NULL == stack) { return(-1); } ignore.sa_handler = SIG_IGN; /* ignore SIGINT and SIGQUIT */ sigemptyset(&ignore.sa_mask); ignore.sa_flags = 0; if (sigaction(SIGINT, &ignore, &saveintr) < 0) { sal_free(stack); return(-1); } if (sigaction(SIGQUIT, &ignore, &savequit) < 0) { sal_free(stack); return(-1); } sigemptyset(&chldmask); /* now block SIGCHLD */ sigaddset(&chldmask, SIGCHLD); if (sigprocmask(SIG_BLOCK, &chldmask, &savemask) < 0) { sal_free(stack); return(-1); } /* child only shares memory with parent */ /* note: unlike system() command, child process needn't to restore previous signal as sharing memory with parent. */ if ((pid = clone(func,(char*)stack+MAX_STACK,CLONE_VM,(void*)cmdstring)) < 0) { status = -1; /* probably out of processes */ } while (waitpid(pid, &status, __WCLONE|__WALL) < 0) { if (errno != EINTR) { status = -1; /* error other than EINTR from waitpid() */ break; } } /* restore previous signal actions & reset signal mask */ if (sigaction(SIGINT, &saveintr, NULL) < 0) { sal_free(stack); return(-1); } if (sigaction(SIGQUIT, &savequit, NULL) < 0) { sal_free(stack); return(-1); } if (sigprocmask(SIG_SETMASK, &savemask, NULL) < 0) { sal_free(stack); return(-1); } sal_free(stack); return(status); }