sw_result sw_ipv4_address_init_from_this_host( sw_ipv4_address * self) { #if defined(__VXWORKS__) sw_int8 current_addr[INET_ADDR_LEN]; if (ifAddrGet(sysEnetNameGet(), current_addr) != OK) { sw_debug(SW_LOG_ERROR, "sw_ipv4_address_init_from_this_host", "ifAddrGet() failed\n"); return SW_E_INIT; } self->m_addr = inet_addr(current_addr); #else struct sockaddr_in addr; sw_result err; sw_sockdesc_t desc; sw_socklen_t len; desc = socket(AF_INET, SOCK_DGRAM, 0); sw_check(desc != SW_INVALID_SOCKET, exit, err = SW_E_UNKNOWN); sw_memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = inet_addr("192.168.1.1"); addr.sin_port = htons(5555); err = connect(desc, (struct sockaddr*) &addr, sizeof(addr)); sw_check_okay(err, exit); sw_memset(&addr, 0, sizeof(addr)); len = sizeof(addr); err = getsockname(desc, (struct sockaddr*) &addr, &len); sw_check_okay(err, exit); self->m_addr = addr.sin_addr.s_addr; #endif exit: if (desc != SW_INVALID_SOCKET) { sw_close_socket(desc); } if (err != SW_OKAY) { err = sw_ipv4_address_init_from_address(self, sw_ipv4_address_loopback()); } return err; }
/** * @brief the function to encode and decode a buffer using rc4 algorithm. * The output buffer has to be atleast the same length as the input buffer. * Expect segmentation faults if not. * * @param input_buf: the input buffer that needs to be converted * @param input_len: the length of the input buffer * @param output_buf: the output buffer into which the data needs to be filled. * The size has to be atleast the same size as the input buffer. * @param output_len: the length of the converted data * * @return 0 if successful. No other return values are expected for now **/ int otzone_rc4_algorithm(char *input_buf, int input_len, char *output_buf, int *output_len) { char *key_seq = ")*(&$%^!@#~`+="; /* key for encryption & decryption*/ unsigned char key_seq_len = 14; /* length of the key_seq variable*/ unsigned char enc_array[MODULO_VALUE]; unsigned short loop_cntr=0,index=0; sw_memset(enc_array,INITIALIZATION_CONST,(MODULO_VALUE-1)*sizeof(unsigned char)); for(loop_cntr=0; loop_cntr<MODULO_VALUE; loop_cntr++) { index = (index+enc_array[loop_cntr]+ (unsigned char)(key_seq[loop_cntr%key_seq_len]))%MODULO_VALUE; if(enc_array[loop_cntr] == INITIALIZATION_CONST) { enc_array[loop_cntr] = loop_cntr; } if(enc_array[index] == INITIALIZATION_CONST) { enc_array[index] = loop_cntr; } SWAP(enc_array[loop_cntr],enc_array[index]); } loop_cntr = 0; index = 0; for(*output_len = 0; *output_len<input_len; (*output_len)++) { loop_cntr = (loop_cntr+1)%MODULO_VALUE; index = (index+enc_array[loop_cntr])%MODULO_VALUE; SWAP(enc_array[loop_cntr],enc_array[index]); output_buf[*output_len] = input_buf[*output_len] ^ (enc_array[(enc_array[loop_cntr]+enc_array[index])%MODULO_VALUE]); } return(0); }
int echo_task_init(sa_config_t *psa_config) { sw_memset(psa_config, 0x0, sizeof(sa_config_t)); psa_config->service_uuid = OTZ_SVC_ECHO; sw_strcpy(psa_config->service_name, "echo"); psa_config->stack_size = TASK_STACK_SIZE; psa_config->data = (void*)sw_malloc(sizeof(struct echo_global)); if(!psa_config->data) { sw_printf("SW: echo task init: allocation of local storage data failed\n"); return OTZ_ENOMEM; } psa_config->entry_point = &echo_task; psa_config->process = (u32)&process_otz_echo_svc; sw_memset(psa_config->data, 0, sizeof(struct echo_global)); return OTZ_OK; }
/* UDP/mcast implementation */ static sw_result sw_socket_udp_connect( sw_socket self, sw_ipv4_address address, sw_port port) { sw_memset(&self->m_dest_addr, 0, sizeof(self->m_dest_addr)); self->m_dest_addr.sin_family = AF_INET; self->m_dest_addr.sin_addr.s_addr = sw_ipv4_address_saddr(address); self->m_dest_addr.sin_port = htons(port); self->m_connected = SW_TRUE; return SW_OKAY; }
/** * @brief Dispatcher task init * * This function initializes dispatcher task parameters and its get called * before the task creation * * @param psa_config: Configuration parameter for the task * * @return otz_return_t: * OTZ_OK \n * OTZ_FAIL \n */ int dispatch_task_init(sa_config_t *psa_config) { sw_memset(psa_config, 0x0, sizeof(sa_config_t)); psa_config->service_uuid = OTZ_SVC_GLOBAL; sw_strcpy(psa_config->service_name, "dispatcher"); psa_config->stack_size = TASK_STACK_SIZE; psa_config->entry_point = (u32) &dispatch_task; psa_config->data = (void *)sw_malloc_private(COMMON_HEAP_ID, sizeof(struct dispatch_global)); if(!psa_config->data) { sw_printf("SW: dispatch task init: allocation of local storage data failed\n"); return OTZ_ENOMEM; } sw_memset(psa_config->data, 0 , sizeof(struct dispatch_global)); g_dispatch_data = psa_config->data; /* INIT_LIST_HEAD( &((struct dispatch_global*)psa_config->data)->pending_list); */ return OTZ_OK; }
/*TEE_Result TEE_CheckMemoryAccessRights( u32 accessFlags, void* buffer, size_t size) { return TEE_SUCCESS; } void TEE_SetInstanceData( void* instanceData ) { Data = instanceData; } void* TEE_GetInstanceData( void ) { return Data; } */ void* TEE_Malloc( size_t size, u32 hint ) { void* buffer; if((buffer = malloc(size))==NULL) { sw_printf("Memory Allocation Failed\n"); return NULL; } if(hint == 0x0) { sw_memset(buffer, 0, size); } /*else { hint in the range [0x00000001, 0xFFFFFFFF] can be used in future versions. }*/ return buffer; }
sw_result sw_corby_channel_init( sw_corby_channel * self, struct _sw_corby_orb * orb, sw_socket socket, sw_socket_options options, sw_size_t bufsize) { sw_result err; *self = (sw_corby_channel) sw_malloc(sizeof(struct _sw_corby_channel)); err = sw_translate_error(*self, SW_E_MEM); sw_check_okay_log(err, exit); sw_memset(*self, 0, sizeof(struct _sw_corby_channel)); if (options) { err = sw_socket_set_options(socket, options); sw_check_okay(err, exit); } err = sw_ipv4_address_init(&(*self)->m_from); sw_check_okay(err, exit); (*self)->m_orb = orb; (*self)->m_socket = socket; (*self)->m_refs = 1; (*self)->m_state = Waiting; err = sw_corby_message_init(&(*self)->m_message); sw_check_okay(err, exit); err = sw_corby_buffer_init_with_size(&(*self)->m_send_buffer, (bufsize) ? bufsize : SW_CORBY_CHANNEL_DEFAULT_BUFFER_SIZE); sw_check_okay(err, exit); err = sw_corby_buffer_init_with_size(&(*self)->m_read_buffer, (bufsize) ? bufsize : SW_CORBY_CHANNEL_DEFAULT_BUFFER_SIZE); sw_check_okay(err, exit); exit: if (err && *self) { sw_corby_channel_fina(*self); } return err; }
sw_result sw_socket_options_init( sw_socket_options * options) { sw_result err = SW_OKAY; *options = (sw_socket_options) sw_malloc(sizeof(struct _sw_socket_options)); err = sw_translate_error(*options, SW_E_MEM); sw_check_okay_log(err, exit); sw_memset(*options, 0, sizeof(struct _sw_socket_options)); exit: return err; }
/* TCP implementation */ static sw_result sw_socket_tcp_connect( sw_socket self, sw_ipv4_address address, sw_port port) { sw_int8 host[16]; sw_socklen_t len; int nodelay = 1; struct linger l; int res; sw_result err; sw_debug(SW_LOG_VERBOSE, "sw_socket_tcp_connect() : host = %s, port = %d\n", sw_ipv4_address_name(address, host, 16), port); sw_memset(&self->m_dest_addr, 0, sizeof(self->m_dest_addr)); self->m_dest_addr.sin_family = AF_INET; self->m_dest_addr.sin_addr.s_addr = sw_ipv4_address_saddr(address); self->m_dest_addr.sin_port = htons(port); res = connect(self->m_desc, (struct sockaddr*) &self->m_dest_addr, sizeof(self->m_dest_addr)); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); len = sizeof(self->m_addr); res = getsockname(self->m_desc, (struct sockaddr*) &self->m_addr, &len); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); self->m_connected = SW_TRUE; res = setsockopt(self->m_desc, IPPROTO_TCP, TCP_NODELAY, (char*) &nodelay, sizeof(nodelay)); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); l.l_onoff = 0; l.l_linger = 0; res = setsockopt(self->m_desc, SOL_SOCKET, SO_LINGER, (char*) &l, sizeof(l)); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); exit: return err; }
static sw_result sw_socket_udp_sendto( sw_socket self, sw_octets buffer, sw_size_t len, sw_size_t * bytesWritten, sw_ipv4_address to, sw_port port) { struct sockaddr_in addr; sw_memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = sw_ipv4_address_saddr(to); addr.sin_port = htons(port); return sw_socket_udp_really_sendto(self, buffer, len, bytesWritten, (struct sockaddr*) &addr, sizeof(addr)); }
sw_result sw_socket_bind( sw_socket self, sw_ipv4_address address, sw_port port) { sw_int8 host[16]; sw_socklen_t len; int res; sw_result err = SW_OKAY; sw_debug(SW_LOG_VERBOSE, "sw_socket_bind() : fd = %d, addr = %s, port = %d\n", self->m_desc, sw_ipv4_address_name(address, host, 16), port); /* now bind */ sw_memset(&self->m_addr, 0, sizeof(self->m_addr)); self->m_addr.sin_family = AF_INET; /* if they haven't chosen a network interface, choose one for them */ self->m_addr.sin_addr.s_addr = sw_ipv4_address_saddr(address); self->m_addr.sin_port = htons(port); len = sizeof(self->m_addr); res = bind(self->m_desc, (struct sockaddr*) &self->m_addr, len); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); /* now get our port */ res = getsockname(self->m_desc, (struct sockaddr*) &self->m_addr, &len); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); exit: return err; }
sw_result sw_socket_accept( sw_socket self, sw_socket * socket) { struct sockaddr_in addr; sw_sockdesc_t desc; sw_socklen_t len; int nodelay = 1; struct linger l; int res; sw_result err; len = sizeof(addr); sw_memset(&addr, 0, sizeof(addr)); desc = accept(self->m_desc, (struct sockaddr*) &addr, &len); err = sw_translate_error(desc != SW_INVALID_SOCKET, sw_socket_errno()); sw_check_okay(err, exit); res = setsockopt(desc, IPPROTO_TCP, TCP_NODELAY, (char*) &nodelay, sizeof(nodelay)); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); l.l_onoff = 0; l.l_linger = 0; res = setsockopt(desc, SOL_SOCKET, SO_LINGER, (char*) &l, sizeof(l)); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); err = sw_tcp_socket_init_with_desc(socket, desc); sw_check_okay(err, exit); exit: return err; }
sw_result sw_socket_join_multicast_group( sw_socket self, sw_ipv4_address local_address, sw_ipv4_address multicast_address, sw_uint32 ttl) { struct in_addr addr; struct ip_mreq mreq; #if defined(__sun) || defined(__VXWORKS__) || defined(__NetBSD__) || defined(__OpenBSD__) sw_uint8 real_ttl = (sw_uint8) ttl; #else sw_uint32 real_ttl = ttl; #endif int res; sw_result err; /* initialize the group membership */ sw_memset(&addr, 0, sizeof(addr)); addr.s_addr = sw_ipv4_address_saddr(local_address); sw_memset(&mreq, 0, sizeof(mreq)); #if defined(__VXWORKS__) /* VxWorks doesn't like it if we try and use INADDR_ANY for use in setting up multicast sockets. So we please it. */ if (sw_ipv4_address_is_any(local_address)) { sw_ipv4_address current_address; err = sw_ipv4_address_init_from_this_host(¤t_address); sw_check_okay(err, exit); mreq.imr_interface.s_addr = sw_ipv4_address_saddr(current_address); } else { mreq.imr_interface = addr; } #else mreq.imr_interface = addr; #endif mreq.imr_multiaddr.s_addr = sw_ipv4_address_saddr(multicast_address); res = setsockopt(self->m_desc, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*) &mreq, sizeof(mreq)); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); res = setsockopt(self->m_desc, IPPROTO_IP, IP_MULTICAST_IF, (char*) &addr, sizeof(addr)); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); res = setsockopt(self->m_desc, IPPROTO_IP, IP_TTL, (char*) &ttl, sizeof(ttl)); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); res = setsockopt(self->m_desc, IPPROTO_IP, IP_MULTICAST_TTL, (char*) &real_ttl, sizeof(real_ttl)); err = sw_translate_error(res == 0, sw_socket_errno()); sw_check_okay_log(err, exit); exit: return err; }
/** * @brief * * @param buffer * @param x * @param size */ void TEE_MemFill( void* buffer, u32 x, u32 size) { sw_memset(buffer, x, size); }
/** * @brief * Function to check whether a given file name exists in the file system * and if so retrieve the corresponding short file name directory entry * @param fname * Pointer to the start of the file name which needs to be searched * @param entry * Pointer to a pointer of the directory entry structure in which the * short file name directory entry content is to be stored * @param strt * Pointer to the start of the region where the file/directory needs * to be searched * @param strt_clus * Starting cluster of the directory in which the file is assumed * to be present * @param is_lfn * Boolean value which indicates whether the file is short or long file * @return * true: if the file/dir is present * false:if it is not present in the respected path */ bool get_dir_entry(char *fname,dir_entry **entry,u8 *strt, u32 strt_clus,bool is_lfn) { u8 *dir_strt = strt; dir_entry *dir_ptr; u8 chk_sum; bool file_found = false; int i = 0; int tmp_cnt = 0; int lfn_len = 0; char temp_lfn[LONG_FILE_NAME_LEN]; sw_memset(temp_lfn,SPACE_VAL,LONG_FILE_NAME_LEN); u32 next_clus = strt_clus; while(!((next_clus == END_OF_CLUSTER) && (i == MAX_DIR_ENTRY_CNT + 2) && next_clus != END_OF_ROOT_CLUSTER)){ if((i - MAX_DIR_ENTRY_CNT == 1) && (next_clus != END_OF_CLUSTER) && next_clus != END_OF_ROOT_CLUSTER){ next_clus = get_fat_table_entry(next_clus); if(next_clus == END_OF_CLUSTER || next_clus == END_OF_ROOT_CLUSTER) break; dir_strt = cluster_to_memory_addr(next_clus); if(next_clus != END_OF_CLUSTER && next_clus != END_OF_ROOT_CLUSTER) i = 0; } if(is_lfn){ if(*(dir_strt + ATTR_OFF) == ATTR_LONG_FNAME){ lfn_len = get_long_file_name(temp_lfn,&dir_strt,&tmp_cnt); if(sw_memcmp(temp_lfn,fname,lfn_len) == 0) file_found = true; } } else{ if(sw_memcmp(fname,dir_strt,SHRT_FILE_NAME_LEN) == 0){ file_found = true; } } if(file_found) break; dir_strt += DIR_ENTRY_LEN; //TODO if(tmp_cnt != 0) i += tmp_cnt; else i++; //i= (tmp_cnt != 0) ? i += tmp_cnt : i++; tmp_cnt = 0; } if(file_found){ if(entry != NULL){ dir_ptr = (dir_entry*)sw_malloc(sizeof(dir_entry)); sw_memcpy(dir_ptr,dir_strt,DIR_ENTRY_LEN); *entry = dir_ptr; dir_file_offset = dir_strt; } return true; } else return false; }
/** * @brief * Function to open a specific file mentioned in the path * based on the incoming modes * @param file_path * Pointer to the location of the file path string * @param flags * Flags indicating the modes in which the file is to be opened * * @return Non-zero:File descriptor of the opened file Zero :File open is unsuccessful */ int file_open(const char *file_path,int flags) { const char *path = file_path; const char *temp_path,*delim_strt; char shrt_file_name[SHRT_FILE_NAME_LEN]; char long_file_name[LONG_FILE_NAME_LEN]; int len = 0,fl_des = 0,crt_flag,i; int delim_cnt = 0; int mode; int extn_len_cnt = 0; int seq_num = 1; bool is_file_found; dir_entry *entry = NULL; file_info *info; u8 *pwd = root_directory; u32 strt_cluster = rt_dir_strt_clus; bool is_long_file_name = false; sw_memset(long_file_name,SPACE_VAL,LONG_FILE_NAME_LEN); delim_cnt = find_depth(file_path); path = file_path; for(i=0;i<delim_cnt;i++){ if(*path == DELIMITER){ delim_strt = path; path++; } while((*path != EXTN_DELIMITER) && (*path != '\0') && (*path != DELIMITER) && (len < LONG_FILE_NAME_LEN)){ long_file_name[len] = *path; path++; len++; } temp_path = path; if(*temp_path == EXTN_DELIMITER){ temp_path++; while(*temp_path != DELIMITER && *temp_path != '\0'){ extn_len_cnt++; temp_path++; } } if(len > FILE_NAME_SHRT_LEN || extn_len_cnt > FILE_NAME_EXTN_LEN) is_long_file_name = true; if(is_long_file_name){ path = delim_strt; len = 0; if(*path == DELIMITER) path++; while(len < LONG_FILE_NAME_LEN && *path != '\0' && *path != DELIMITER){ long_file_name[len] = *path; path++; len++; } long_file_name[len] = '\0'; if(entry){ sw_free(entry); entry = NULL; } is_file_found = get_dir_entry(long_file_name,&entry, pwd,strt_cluster,true); } else{ len = FILE_NAME_SHRT_LEN; while(len < SHRT_FILE_NAME_LEN && *path != '\0' && *path != DELIMITER){ if(*path == EXTN_DELIMITER) path++; long_file_name[len] = *path; path++; len++; } convert_to_uppercase(long_file_name); if(entry){ sw_free(entry); entry = NULL; } is_file_found = get_dir_entry(long_file_name,&entry, pwd,strt_cluster,false); } if((is_file_found) & (i != delim_cnt - 1)){ strt_cluster = (entry->strt_clus_hword)<<16 | (entry->strt_clus_lword); pwd = cluster_to_memory_addr(strt_cluster); len = 0; extn_len_cnt = 0; sw_memset(shrt_file_name,SPACE_VAL,SHRT_FILE_NAME_LEN); sw_memset(long_file_name,SPACE_VAL,LONG_FILE_NAME_LEN); is_long_file_name = false; } } if(is_file_found){ if(flags & FILE_WRITE){ if(chk_file_lock(file_path) == -1) flags = FILE_READ; if(entry->attr & ATTR_READ){ sw_printf("Cannot open the file in write mode\n"); return -1; } } info = (file_info*)sw_malloc(sizeof(file_info)); fl_des = retrieve_file_info(info,entry,flags, dir_file_offset,file_path); } else{ if((flags & FILE_CREATE_NEW) || (flags & FILE_CREATE_ALWAYS) || (flags & FILE_WRITE)){ if(is_long_file_name){ get_short_file_name(long_file_name,shrt_file_name, (char)seq_num); if(get_dir_entry(shrt_file_name,NULL, pwd,strt_cluster,false) == true){ while(get_dir_entry(shrt_file_name,NULL, pwd,strt_cluster,false)){ seq_num++; get_short_file_name(long_file_name, shrt_file_name,'seq_num'); } } convert_to_uppercase(shrt_file_name); crt_flag = create_file(long_file_name, shrt_file_name,strt_cluster,&entry); } else crt_flag = create_file(NULL,long_file_name,strt_cluster,&entry); if(crt_flag == 0) sw_printf("File creation success\n"); info = (file_info*)sw_malloc(sizeof(file_info)); fl_des = retrieve_file_info(info,entry,flags, dir_file_offset,file_path); } else return -1; } return fl_des; }