static int crl_cert_request_2_file(struct cmp_db* cmdb,int fd){ u8 *mbuf,*buf; int needlen = sizeof(time32) + 8 + sizeof(crl_series) + 2*sizeof(cmh); if( (buf = (u8*)malloc(needlen)) == NULL){ wave_error_printf("分配失败"); return -1; } mbuf = buf; pthread_mutex_lock(&cmdb->lock); ptr_host_to_be(mbuf,cmdb->crl_request_issue_date); memcpy(mbuf,cmdb->crl_request_issuer.hashedid8,8); mbuf += 8; ptr_host_to_be(mbuf,cmdb->crl_request_crl_series); ptr_host_to_be(mbuf,cmdb->req_cert_cmh); ptr_host_to_be(mbuf,cmdb->req_cert_enc_cmh); pthread_mutex_unlock(&cmdb->lock); if( write(fd,buf,needlen) != needlen){ wave_error_printf("写入失败"); goto fail; } free(buf); return needlen; fail: free(buf); return -1; }
static int file_2_lsis_array(struct cmp_db* cmdb,int fd){ u32 len; int i; if(file_2_len(&len,fd) ){ wave_error_printf("读取失败"); return -1; } pthread_mutex_lock(&cmdb->lock); cmdb->req_cert_lsises.len = len; cmdb->req_cert_lsises.lsis = (pssme_lsis*)malloc(sizeof(pssme_lsis) * len); if(cmdb->req_cert_lsises.lsis == NULL){ pthread_mutex_unlock(&cmdb->lock); wave_error_printf("分配空间失败"); return -1; } for(i=0;i<len;i++){ if(file_2_element(fd,cmdb->req_cert_lsises.lsis+i,sizeof( pssme_lsis)) ){ wave_error_printf("读取文件失败"); free(cmdb->req_cert_lsises.lsis); pthread_mutex_unlock(&cmdb->lock); return -1; } } pthread_mutex_unlock(&cmdb->lock); return 0; }
int dot2_init_netlink(struct nlmsghdr *nlh, struct msghdr *msg){ int ret =0; struct confirm_content *confirm; struct wme_dot2_pid_request req; req.type = DOT2_PID; req.operat = SET_MIB; req.pid = getpid(); int fd = create_netlink(msg, nlh); if(fd < 0){ wave_error_printf("fd创建失败"); return -1; } memcpy(NLMSG_DATA(nlh), &req, sizeof(struct wme_generic_service_request)); ret = sendmsg(fd, msg, 0); perror(strerror(errno)); if(ret < 0){ wave_error_printf("netlink发送消息失败"); return -1; } memset(nlh, 0, NLMSG_SPACE(MAX_PAYLOAD)); ret = recvmsg(fd, msg, 0); if(ret < 0){ wave_error_printf("netlink recvmsg error"); return -1; } confirm = (struct confirm_content*)NLMSG_DATA(nlh); if(confirm->result != RES_ACCPTED){ wave_error_printf("zhu ce fail!"); return -1; } nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD); return fd; }
static int file_2_ca_cert(struct cmp_db* cmdb,int fd){ u8* buf = NULL; u32 len = 400;//预估cerficate有多大。 u32 size; buf = (u8*)malloc(len); if(buf == NULL){ wave_error_printf("内存分配失败"); return -1; } pthread_mutex_lock(&cmdb->lock); if( file_2_element(fd,&cmdb->ca_cmh,sizeof(cmdb->ca_cmh))){ pthread_mutex_unlock(&cmdb->lock); return -1; } if( (size = read(fd,buf,len)) < 0 ){ wave_error_printf("读取失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } if(buf_2_certificate(&cmdb->ca_cert,buf,size)){ certificate_free(&cmdb->ca_cert); wave_error_printf("cmpd证书读取失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } pthread_mutex_unlock(&cmdb->lock); return 0; }
/************* *这里的所有编码原则,都是固定长度,我们固定编码。 *其他数组或者链表之类的我们就用4字节表示后面有多少字节。 ************/ static int cert_2_file(struct cmp_db* cmdb,int fd){ u8 *mbuf,*buf; int needlen,size = 0; buf = NULL; needlen = 500;//预估一下有多长. buf =(u8*)malloc(needlen); if(buf == NULL) goto fail; mbuf = buf; pthread_mutex_lock(&cmdb->lock); ptr_host_to_be(mbuf,cmdb->ca_cmh); size = certificate_2_buf(&cmdb->ca_cert,mbuf,needlen-size); if(size < 0){ pthread_mutex_unlock(&cmdb->lock); wave_error_printf("证书编码的时候错误了"); goto fail; } size += sizeof(cmdb->ca_cmh); pthread_mutex_unlock(&cmdb->lock); if(write(fd,buf,size) != size){ wave_error_printf("写入文件发生错误"); goto fail; } free(buf); return 0; fail: if(buf != NULL) free(buf); return -1; }
/** * 所有的长度都用四字节编码 */ static int crl_req_time_list_2_file(struct cmp_db* cmdb,int fd){ struct list_head *head; struct crl_req_time* ptr; u32 size,temp; size = 0; pthread_mutex_lock(&cmdb->lock); head = &cmdb->crl_time.list; list_for_each_entry(ptr,head,list){ if( (temp = crl_req_time_2_file(ptr,fd)) < 0){ wave_printf_fl(MSG_ERROR,"crl_req_time_2_file 失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } size +=1; } pthread_mutex_unlock(&cmdb->lock); size = host_to_be32(size); if( file_insert(fd,0,&size,4) ){ wave_error_printf("文件插入出错"); return -1; } if ( lseek(fd,4,SEEK_CUR) == -1){ wave_error_printf("文件移动偏移量出问题"); return -1; } return 1; }
static int file_2_crl_req_time_list(struct cmp_db* cmdb,int fd){ u32 len; struct crl_req_time *node; struct list_head* head = NULL; int i; if ( file_2_len(&len,fd)){ wave_error_printf("长度读取失败"); return -1; } pthread_mutex_lock(&cmdb->lock); head = &cmdb->crl_time.list; for(i=0;i<len;i++){ node = file_2_crl_req_time(fd); if(node == NULL){ pthread_mutex_unlock(&cmdb->lock); goto fail; } list_add_tail(&node->list,head); } pthread_mutex_unlock(&cmdb->lock); return 0; fail: if(head != NULL){ while( !list_empty(head)){ node = list_entry(head->next,struct crl_req_time,list); list_del(&node->list); free(node); node = NULL; } } return -1; }
static int inline file_2_len(u32* len,int fd){ if(read(fd,len,4) != 4){ wave_error_printf("读取长度失败"); return -1; } *len = be_to_host32(*len); return 0; }
int create_netlink(struct msghdr *msg, struct nlmsghdr *nlh) { int fd; struct sockaddr_nl src_addr, *dest_addr; struct iovec *iov = NULL; iov = (struct iovec *)malloc(sizeof(*iov)); dest_addr = (struct sockaddr_nl*)malloc(sizeof(struct sockaddr_nl)); if(!dest_addr || !iov){ wave_malloc_error(); return -1; } //NETLINK_WAVE是需要和.3的模块统一的,需要在linux/netlink.h中添加一个新的协议类型 21 fd = socket(PF_NETLINK, SOCK_RAW, NETLINK_WAVE); if(fd < 0){ wave_error_printf("创建fd失败"); return -1; } memset(&src_addr, 0, sizeof(struct sockaddr_nl)); src_addr.nl_family = AF_NETLINK; src_addr.nl_pid = getpid(); /* self pid */ src_addr.nl_groups = 0; /* not in mcast groups */ if(bind(fd, (struct sockaddr*)&src_addr, sizeof(struct sockaddr_nl)) < 0){ wave_error_printf("绑定失败"); return -1; } memset(dest_addr, 0, sizeof(struct sockaddr_nl)); dest_addr->nl_family = AF_NETLINK; dest_addr->nl_pid = 0; /* For Linux Kernel */ dest_addr->nl_groups = 0; /* unicast */ /* Fill the netlink message header */ nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD); nlh->nlmsg_pid = getpid(); /* self pid */ nlh->nlmsg_flags = 0; /* Fill in the netlink message payload */ memset(iov, 0, sizeof(*iov)); iov->iov_base = (void *)nlh; iov->iov_len = nlh->nlmsg_len; msg->msg_name = (void *)dest_addr; msg->msg_namelen = sizeof(struct sockaddr_nl); msg->msg_iov = iov; msg->msg_iovlen = 1; return fd; }
static int file_2_crl_cert(struct cmp_db* cmdb,int fd){ pthread_mutex_lock(&cmdb->lock); if( file_2_element(fd,&cmdb->crl_request_issue_date,sizeof(time32)) ){ wave_error_printf("读取数据错误"); goto fail; } if( read(fd,&cmdb->crl_request_issuer.hashedid8,8) != 8){ wave_error_printf("读取数据错误"); goto fail; } if( file_2_element(fd,&cmdb->crl_request_crl_series,sizeof(crl_series)) || file_2_element(fd,&cmdb->req_cert_cmh,sizeof(cmh)) || file_2_element(fd,&cmdb->req_cert_enc_cmh ,sizeof(cmh)) ){ wave_error_printf("读取数据失败"); goto fail; } pthread_mutex_unlock(&cmdb->lock); return 0; fail: pthread_mutex_unlock(&cmdb->lock); return -1; }
/** * 这个是固定长度,我们编码成固定长度 */ static int crl_req_time_2_file(struct crl_req_time* ptr,int fd){ int needlen ; u8 *buf,*obuf; needlen = sizeof(time32) + 8 +sizeof(crl_series) + sizeof(time32); if( (buf = (u8*)malloc(needlen)) == NULL) { wave_error_printf("空间分配失败"); return -1; } obuf = buf; ptr_host_to_be(buf,ptr->time); memcpy(buf,&ptr->issuer.hashedid8,8); buf +=8; ptr_host_to_be(buf,ptr->crl_series); ptr_host_to_be(buf,ptr->issue_date); if( write(fd,obuf,needlen) != needlen){ free(obuf); wave_error_printf("写入失败"); return -1; } free(obuf); return 1; }
static struct crl_req_time* file_2_crl_req_time(int fd){ struct crl_req_time* crt =NULL; time32 time; u8 issuer[8]; crl_series crl_series; time32 issue_date; crt = (struct crl_req_time*)malloc(sizeof(struct crl_req_time)); if(crt == NULL){ wave_error_printf("内存分配失败"); return NULL; } if(file_2_element(fd,&crt->time,sizeof(time32)) || read(fd,crt->issuer.hashedid8,8) != 8|| file_2_element(fd,&crt->crl_series,sizeof(crl_series)) || file_2_element(fd,&crt->issue_date,sizeof(issue_date)) ){ wave_error_printf("文件读取失败"); goto fail; } return crt; fail: if(crt != NULL) free(crt); return NULL; }
static int file_2_cmp_db(struct cmp_db* cmdb,const char* name){ int fd; if( (fd = open(name,O_RDONLY)) <0){ wave_error_printf("文件 %s 打开失败\n",name); close(fd); return -2; } if( file_2_crl_req_time_list(cmdb,fd) || file_2_crl_cert(cmdb,fd) || file_2_lsis_array(cmdb,fd) || file_2_others(cmdb,fd) || file_2_ca_cert(cmdb,fd)){ close(fd); return -1; } close(fd); return 0; }
int wave_start(){ struct sec_db* sdb; int res; sdb = init_sec_db(); if(sdb == NULL){ return -1; } if(atexit(wave_exit_fun)){ wave_error_printf("注册退出程序失败"); return -1; } if(wme_serv_start(sdb)) return -1; if(wave_cmp_start(sdb)) return -1; if( app_start(sdb)) return -1; return 0; }
static int inline file_2_element(int fd,void* ele,int size){ if(read(fd,ele,size) != size){ wave_error_printf("读取长度失败"); return -1; } switch(size){ case 1: *(u8*)ele = *(u8*)ele; break; case 2: *(u16*)ele = be_to_host16( *(u16*)ele ); break; case 4: *(u32*)ele = be_to_host32( *(u32*)ele ); break; case 8: *(u64*)ele = be_to_host64( *(u64*)ele ); break; } return 0; }
static void* wme_loop(void* sdb){ //启动netlink, int i = 0; int recv_data_len = 0;//接收到的整个数据报文的长度 char sbuf[MAX_PAYLOAD];//发送报文缓冲区 char rbuf[MAX_PAYLOAD];//接收报文缓冲区 struct nlmsghdr nlh; struct msghdr msg; result ret; /* * 签名请求需要用到的参数 * */ struct wme_tobe_signed_wsa s_head;//接收到的需要签名报文头部 struct dot3_signed_wsa s_wsa; int permission_len = 0;//permission部分报文的长度 struct permission per; string unsigned_wsa;//接收到的wsa数据部分 serviceinfo_array permissions;//需要填充的permission数组 time32 life_time; string signed_wsa; /* * 验证请求需要用到的参数 * */ struct dot2_tobe_verified_wsa v_head;//接收到的需要验证的报文头部 struct verified_wsa v_wsa;//验证成功需要发送给内核的报文头部 string unverified_wsa; result_array results; string wsa_data; ssp_array ssp_array; time64_with_standard_deviation generation_time; time64 expiry_time; three_d_location location; struct time32_array last_crl_time; struct time32_array next_crl_time; certificate cert; int fd = dot2_init_netlink(&nlh, &msg); if(fd < 0){ wave_error_printf("netlink初始化失败"); return; } while(1){ memset((char*)NLMSG_DATA(&nlh), 0, MAX_PAYLOAD); memset(rbuf, 0, MAX_PAYLOAD); ret = recvmsg(fd, &msg, 0); if(ret < 0){ wave_error_printf("接收netlink消息失败"); goto destructor; } recv_data_len = nlh.nlmsg_len; memcpy(rbuf, NLMSG_DATA(&nlh), recv_data_len); //解析报文 char *shift = rbuf; //0为签名 if(*shift = 0){ INIT(permissions); INIT(unsigned_wsa); INIT(signed_wsa); INIT(s_head); INIT(s_wsa); memcpy(&s_head, shift, RECV_S_HEAD_LEN); life_time = s_head.lifetime; shift += RECV_S_HEAD_LEN; unsigned_wsa.len = s_head.wsa_len; unsigned_wsa.buf = malloc(unsigned_wsa.len); if(!unsigned_wsa.buf){ wave_malloc_error(); goto destructor; } memcpy(unsigned_wsa.buf, shift, unsigned_wsa.len); shift += unsigned_wsa.len; permission_len = recv_data_len - RECV_S_HEAD_LEN - unsigned_wsa.len; permissions.len = 0; permissions.serviceinfos = malloc(sizeof(serviceinfo)*32);//分配32个为最大的长度 while(permission_len > 0){ memcpy(&per, shift, 4); shift += 4; memcpy(&permissions.serviceinfos[permissions.len].max_priority, &per.priority, 1); permissions.serviceinfos[permissions.len].psid = psidn2h(shift, per.psid_len); if(per.ssp_len > 0){ permissions.serviceinfos[permissions.len].ssp.len = per.ssp_len; permissions.serviceinfos[permissions.len].ssp.buf = malloc(per.ssp_len); if(!permissions.serviceinfos[permissions.len].ssp.buf){ wave_malloc_error(); goto destructor; } memcpy(permissions.serviceinfos[permissions.len].ssp.buf, shift, per.ssp_len); } else{ permissions.serviceinfos[permissions.len].ssp.len = 0; permissions.serviceinfos[permissions.len].ssp.buf = NULL; } shift += per.ssp_len; if(per.pssi_len > 4){ wave_error_printf("lsis太长"); goto destructor; } permissions.serviceinfos[permissions.len].lsis = 0; for(i = 0; i < per.pssi_len; i++){ permissions.serviceinfos[permissions.len].lsis += (*shift)*256; shift++; } permissions.len++; permission_len = permission_len - per.psid_len - per.ssp_len - per.pssi_len - 4; } ret = sec_signed_wsa(&sec_db, &unsigned_wsa, &permissions, life_time, &signed_wsa); memset((char*)NLMSG_DATA(&nlh), 0, MAX_PAYLOAD); memset(sbuf, 0, MAX_PAYLOAD); if(ret == SUCCESS){ s_wsa.wsa_len = signed_wsa.len; s_wsa.broadcast = s_head.broadcast; s_wsa.change_count = s_head.change_count; s_wsa.channel = s_head.channel; s_wsa.result_code = DOT2_SIGN_SUCCESS; memcpy(sbuf, &s_wsa, sizeof(struct signed_wsa)); memcpy(sbuf+sizeof(struct signed_wsa), signed_wsa.buf, signed_wsa.len); memcpy(NLMSG_DATA(&nlh), sbuf, signed_wsa.len+sizeof(struct signed_wsa)); } else{ wave_error_printf("签名失败"); s_wsa.wsa_len = 0; s_wsa.broadcast = s_head.broadcast; s_wsa.change_count = s_head.change_count; s_wsa.channel = s_head.channel; s_wsa.result_code = DOT2_SIGN_FAILURE; memcpy(sbuf, &s_wsa, sizeof(struct signed_wsa)); memcpy(NLMSG_DATA(&nlh), sbuf, signed_wsa.len+sizeof(struct signed_wsa)); } if(sendmsg(fd, &msg, 0) < 0){ wave_error_printf("发送消息给内核失败了"); goto destructor; } } //验证 else if(*shift == 1){ memcpy(&v_head, shift, RECV_V_HEAD_LEN); shift += RECV_V_HEAD_LEN; INIT(unverified_wsa); INIT(results); INIT(wsa_data); INIT(ssp_array); INIT(generation_time); INIT(expiry_time); INIT(location); INIT(last_crl_time); INIT(next_crl_time); INIT(cert); unverified_wsa.len = v_head.wsa_len; unverified_wsa.buf = malloc(v_head.wsa_len); if(!unverified_wsa.buf){ wave_malloc_error(); goto destructor; } memcpy(unverified_wsa.buf, shift, v_head.wsa_len); memset((char*)NLMSG_DATA(&nlh), 0, MAX_PAYLOAD); memset(sbuf, 0, MAX_PAYLOAD); ret = sec_signed_wsa_verification(&sec_db, &unverified_wsa, &results, &wsa_data, &ssp_array, &generation_time, &expiry_time, &location, &last_crl_time, &next_crl_time, &cert); if(ret == SUCCESS){ v_wsa.pid = getpid(); memcpy(v_wsa.src_mac, v_head.src_mac, 6); v_wsa.rcpi = v_head.rcpi; v_wsa.result_code[0] = DOT2_SUCCESS; v_wsa.result_code[1] = DOT2_SUCCESS; for(i = 0; i < results.len; i++){ if(results.result[i] == UNSECURED){ v_wsa.result_code[1] = DOT2_UNSECURED; break; } } v_wsa.wsa_len = wsa_data.len; v_wsa.gen_time = generation_time.time; v_wsa.expire_time = expiry_time; v_wsa.ssp_len = 0; v_wsa.next_crl_time_len = next_crl_time.len*sizeof(time32); memcpy(sbuf, &v_wsa, sizeof(struct verified_wsa)); memcpy(sbuf+sizeof(struct verified_wsa), wsa_data.buf, wsa_data.len); char *ssp_shift = sbuf + sizeof(struct verified_wsa) + wsa_data.len; for(i = 0; i < ssp_array.len; i++){ if(ssp_array.ssps[i].len != 0){ memcpy(ssp_shift, ssp_array.ssps[i].buf, ssp_array.ssps[i].len); ssp_shift += ssp_array.ssps[i].len; v_wsa.ssp_len += ssp_array.ssps[i].len; } else{ *ssp_shift = '\0'; ssp_shift++; v_wsa.ssp_len++; } } memcpy(ssp_shift, next_crl_time.times, sizeof(time32)*next_crl_time.len); memcpy(NLMSG_DATA(&nlh), sbuf, sizeof(struct verified_wsa)+v_wsa.wsa_len+v_wsa.ssp_len+v_wsa.next_crl_time_len); } else if(ret == INVALID_INPUT){ v_wsa.pid = getpid(); memcpy(v_wsa.src_mac, v_head.src_mac, 6); v_wsa.rcpi = v_head.rcpi; v_wsa.result_code[0] = DOT2_INVALID_INPUT; v_wsa.result_code[1] = DOT2_INVALID_INPUT; v_wsa.wsa_len = 0; v_wsa.gen_time = 0; v_wsa.expire_time = 0; v_wsa.ssp_len = 0; v_wsa.next_crl_time_len = 0; memcpy(sbuf, &v_wsa, sizeof(struct verified_wsa)); memcpy(NLMSG_DATA(&nlh), sbuf, sizeof(struct verified_wsa)); } else{ v_wsa.pid = getpid(); memcpy(v_wsa.src_mac, v_head.src_mac, 6); v_wsa.rcpi = v_head.rcpi; v_wsa.result_code[0] = DOT2_OTHER_FALURE; v_wsa.result_code[1] = DOT2_OTHER_FALURE; for(i = 0; i < results.len; i++){ if(results.result[i] == UNSECURED){ v_wsa.result_code[1] = DOT2_UNSECURED; break; } } v_wsa.wsa_len = wsa_data.len; v_wsa.gen_time = generation_time.time; v_wsa.expire_time = expiry_time; v_wsa.ssp_len = 0; v_wsa.next_crl_time_len = next_crl_time.len*sizeof(time32); memcpy(sbuf, &v_wsa, sizeof(struct verified_wsa)); memcpy(sbuf+sizeof(struct verified_wsa), wsa_data.buf, wsa_data.len); memcpy(NLMSG_DATA(&nlh), sbuf, sizeof(struct verified_wsa)+v_wsa.wsa_len); } if(sendmsg(fd, &msg, 0) < 0){ wave_error_printf("发送消息给内核失败了"); goto destructor; } } else wave_error_printf("不支持的dot3请求类型"); destructor: string_free(&unsigned_wsa); string_free(&signed_wsa); serviceinfo_array_free(&permissions); string_free(&unverified_wsa); string_free(&wsa_data); ssp_array_free(&ssp_array); result_array_free(&results); time32_array_free(&last_crl_time); time32_array_free(&next_crl_time); certificate_free(&cert); } }
static int others_2_file(struct cmp_db* cmdb,int fd){ u8 *mbuf,*buf=NULL; int needlen,i; pthread_mutex_lock(&cmdb->lock); needlen = cmdb->identifier.len + 4+ sizeof(cmdb->region.region_type)+sizeof(u32); switch(cmdb->region.region_type){ case FROM_ISSUER: case CIRCLE: needlen += sizeof(s32)*2 + sizeof(u16); break; case RECTANGLE: needlen = needlen + cmdb->region.u.rectangular_region.len * (sizeof(s32)*4) + 4; break; case POLYGON: needlen = needlen + cmdb->region.u.polygonal_region.len * (sizeof(s32) *2) + 4; break; case NONE: needlen = needlen + 4 + cmdb->region.u.other_region.len; break; default: pthread_mutex_unlock(&cmdb->lock); goto fail; } buf = (u8*)malloc(needlen); if(buf == NULL){ pthread_mutex_unlcok(&cmdb->lock); goto fail; } mbuf = buf; *(u32*)mbuf = host_to_be32(cmdb->identifier.len); mbuf += 4; memcpy(mbuf,cmdb->identifier.buf,cmdb->identifier.len); mbuf += cmdb->identifier.len; ptr_host_to_be(mbuf,cmdb->region.region_type); switch(cmdb->region.region_type){ case FROM_ISSUER: case CIRCLE: ptr_host_to_be(mbuf,cmdb->region.u.circular_region.center.latitude); ptr_host_to_be(mbuf,cmdb->region.u.circular_region.center.longitude); ptr_host_to_be(mbuf,cmdb->region.u.circular_region.radius); break; case RECTANGLE: ptr_host_to_be(mbuf,(u32)(cmdb->region.u.rectangular_region.len)); for(i=0;i<cmdb->region.u.rectangular_region.len;i++){ ptr_host_to_be(mbuf,(cmdb->region.u.rectangular_region.buf+i)->north_west.latitude); ptr_host_to_be(mbuf,(cmdb->region.u.rectangular_region.buf+i)->north_west.longitude); ptr_host_to_be(mbuf,(cmdb->region.u.rectangular_region.buf+i)->south_east.latitude); ptr_host_to_be(mbuf,(cmdb->region.u.rectangular_region.buf+i)->south_east.longitude); } break; case POLYGON: ptr_host_to_be(mbuf,(u32)(cmdb->region.u.polygonal_region.len)); for(i=0;i<cmdb->region.u.polygonal_region.len;i++){ ptr_host_to_be(mbuf,(cmdb->region.u.polygonal_region.buf+i)->latitude); ptr_host_to_be(mbuf,(cmdb->region.u.polygonal_region.buf+i)->longitude); } break; case NONE: *(u32*)mbuf = host_to_be32(cmdb->region.u.other_region.len); mbuf += 4; memcpy(mbuf,cmdb->region.u.other_region.buf,cmdb->region.u.other_region.len); break; default: pthread_mutex_unlock(&cmdb->lock); goto fail; } ptr_host_to_be(mbuf,cmdb->life_time); pthread_mutex_unlock(&cmdb->lock); if ( write(fd,buf,needlen) != needlen){ wave_error_printf("文件写入失败"); goto fail; } free(buf); return 0; fail: if(buf != NULL) free(buf); return -1; }
static int file_2_others(struct cmp_db* cmdb,int fd){ u32 len,i; if( file_2_len(&len,fd)){ return -1; } pthread_mutex_lock(&cmdb->lock); cmdb->identifier.len = len; cmdb->identifier.buf = (u8*)malloc(len); if(cmdb->identifier.buf == NULL){ wave_error_printf("分配失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } if( read(fd,cmdb->identifier.buf,len) != len){ wave_error_printf("读取文件失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } if(file_2_element(fd,&cmdb->region.region_type,sizeof(cmdb->region.region_type))){ pthread_mutex_unlock(&cmdb->lock); return -1; } switch(cmdb->region.region_type){ case FROM_ISSUER: case CIRCLE: if( file_2_element(fd,&cmdb->region.u.circular_region.center.latitude, sizeof(cmdb->region.u.circular_region.center.latitude)) || file_2_element(fd,&cmdb->region.u.circular_region.center.longitude, sizeof(cmdb->region.u.circular_region.center.longitude)) || file_2_element(fd,&cmdb->region.u.circular_region.radius, sizeof(cmdb->region.u.circular_region.radius))){ wave_error_printf("读取文件失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } break; case RECTANGLE: if ( file_2_len(&len,fd) ){ pthread_mutex_unlock(&cmdb->lock); return -1; } cmdb->region.u.rectangular_region.len = len; cmdb->region.u.rectangular_region.buf = (struct rectangular_region*)malloc( sizeof(struct rectangular_region) * len); if(cmdb->region.u.rectangular_region.buf == NULL){ wave_error_printf("内存空间分配失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } for(i=0;i<len;i++){ if( file_2_element(fd,&((cmdb->region.u.rectangular_region.buf+i)->north_west.latitude),sizeof(s32)) || file_2_element(fd,&((cmdb->region.u.rectangular_region.buf+i)->north_west.longitude),sizeof(s32)) || file_2_element(fd,&((cmdb->region.u.rectangular_region.buf+i)->south_east.latitude),sizeof(s32)) || file_2_element(fd,&((cmdb->region.u.rectangular_region.buf+i)->south_east.longitude),sizeof(s32))){ free(cmdb->region.u.rectangular_region.buf); wave_error_printf("读取文件失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } } break; case POLYGON: if( file_2_len(&len,fd) ){ pthread_mutex_unlock(&cmdb->lock); return -1; } cmdb->region.u.polygonal_region.len = len; cmdb->region.u.polygonal_region.buf = (two_d_location*)malloc(sizeof(two_d_location) * len); if(cmdb->region.u.polygonal_region.buf == NULL){ wave_error_printf("内存分配失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } if( file_2_element(fd,&(cmdb->region.u.polygonal_region.buf + i)->latitude,sizeof(s32)) || file_2_element(fd,&(cmdb->region.u.polygonal_region.buf+i)->longitude,sizeof(s32) )){ free(cmdb->region.u.polygonal_region.buf); wave_error_printf("读取文件失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } break; case NONE: if( file_2_len(&len,fd) ){ pthread_mutex_unlock(&cmdb->lock); return -1; } cmdb->region.u.other_region.len = len; cmdb->region.u.other_region.buf = (u8*)malloc(len); if(cmdb->region.u.other_region.buf == NULL){ wave_error_printf("内存分配失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } if( read(fd,cmdb->region.u.other_region.buf,len) != len){ free(cmdb->region.u.other_region.buf); wave_error_printf("读取文件失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } break; default: pthread_mutex_unlock(&cmdb->lock); return -1; } if(file_2_element(fd,&cmdb->life_time,sizeof(cmdb->life_time))){ wave_error_printf("读取文件失败"); pthread_mutex_unlock(&cmdb->lock); return -1; } pthread_mutex_unlock(&cmdb->lock); return 0; }