void serverUpdate(struct protocol_header *p_h,int sd) { struct protocol_header header; void *messaggio = NULL; int ID_b = p_h->ID_blocco; int ID_c = p_h->ID_client; lock_blocco(ID_b); if((verificaRW(ID_c,ID_b)) == 0) { unlock_blocco(ID_b); costruisciHeader(&header,UPDATE,100,p_h->ID_client,0,p_h->ID_blocco); if(spedisciDato(sd,&header,sizeof(struct protocol_header))!=0) { sys_warn("! header di 'update non necessario' non inviato"); } return ; } else { costruisciHeader(&header,UPDATE,0,p_h->ID_client,DIMBLOCK,p_h->ID_blocco); if(spedisciDato(sd,&header,sizeof(struct protocol_header))!=0) { sys_warn("! header di 'update non necessario' non inviato"); } messaggio = restituisciMemoriaLocale(p_h->ID_blocco); if(spedisciDato(sd,messaggio,DIMBLOCK) != 0) { sys_warn("! errore invio messaggio per update"); } aggiornaRW(ID_c,ID_b); unlock_blocco(ID_b); } }
int dll_rcv(char *data, int size) { assert(size <= DLL_PKT_DATALEN && data != NULL); dllrcv.recvlen = sizeof(struct sockaddr_ll); int ret; LOCK_RCV(); __build_rcvll(0); ret = recvfrom(dllrcv.rcvsock, dllrcv.rcvpkt, DLL_PKT_MAXLEN, 0, (struct sockaddr *)&dllrcv.ll, &dllrcv.recvlen); if(ret < sizeof(struct ethhdr)) { sys_warn("recv pkt failed: %s\n", strerror(errno)); UNLOCK_RCV(); return -1; } struct ethhdr *hdr = (struct ethhdr *)dllrcv.rcvpkt; if(ntohs(hdr->h_proto) != ETH_INNO) { sys_warn("Reciv error dllayer packet\n"); return -1; } memcpy(data, dllrcv.rcvpkt + sizeof(struct ethhdr), size); UNLOCK_RCV(); return (ret > size) ? size: ret; }
void serverWrite(struct protocol_header *p_h,int sd) { void *messaggio; lock_blocco(p_h->ID_blocco); if((verificaRW(p_h->ID_client,p_h->ID_blocco)) == 0) { messaggio = restituisciMemoriaLocale(p_h->ID_blocco); printf("* Sovrascrittura del dato presente in %p\n",messaggio); if(p_h->dim_dato != DIMBLOCK) sys_err("! Errore nella dimensione di DIMBLOCK dentro serverWrite"); memset(messaggio,0,DIMBLOCK); if(riceviDato(sd,messaggio,DIMBLOCK) != DIMBLOCK) { printf("! Errore nella ricezione messaggio in serverWrite"); } incrementaRW(p_h->ID_blocco); aggiornaRW(p_h->ID_client,p_h->ID_blocco); unlock_blocco(p_h->ID_blocco); struct protocol_header header; memset(&header,0,sizeof(struct protocol_header)); costruisciHeader(&header,WRITE,0,p_h->ID_client,0,p_h->ID_blocco); if(spedisciDato(sd,&header,sizeof(struct protocol_header)) != 0) { sys_warn("! Errore invio header di conferma della write"); } // TODO: implementare pacchetto di errore } else { unlock_blocco(p_h->ID_blocco); messaggio = malloc(sizeof(DIMBLOCK)); printf("* Sovrascrittura del messaggio FAKE in %p\n",messaggio); if(p_h->dim_dato != DIMBLOCK) sys_err("! Errore nella dimensione di DIMBLOCK dentro serverWrite"); memset(messaggio,0,DIMBLOCK); if(riceviDato(sd,messaggio,DIMBLOCK) != DIMBLOCK) { printf("! Errore nella ricezione messaggio in serverWrite"); } free(messaggio); struct protocol_header header; memset(&header,0,sizeof(struct protocol_header)); costruisciHeader(&header,WRITE,100,p_h->ID_client,0,p_h->ID_blocco); if(spedisciDato(sd,&header,sizeof(struct protocol_header)) != 0) { sys_warn("! Errore invio header del dato obsoleto"); } } // TODO: implementare pacchetto di errore }
void serverUnmap(struct protocol_header *p_h,int sd) { struct lista *l ; struct lista *aus; struct lista *aus2; struct protocol_header header; int ID_b = p_h->ID_blocco; int ID_c = p_h->ID_client; pthread_mutex_lock(&mutex_map); l = lista_blocchi; if(l == NULL) sys_warn("! Tabella vuota, impossibile unmap"); if( l != NULL) { if(l->list != NULL) { aus2 = l; while(aus2->list != NULL) { if((aus2->list->tab_interna.ID_blocco == ID_b)&&(aus2->list->tab_interna.ID_client == ID_c)) { aus = aus2->list; aus2->list = aus2->list->list; free(aus); } else { aus2 = aus2->list; } } } else { if((l->tab_interna.ID_blocco = ID_b) && (l->tab_interna.ID_client == ID_c)) { aus = lista_blocchi; lista_blocchi = l->list; free(aus); } } } pthread_mutex_unlock(&mutex_map); // TODO: implementare eventuale messaggio di errore da inviare al client costruisciHeader(&header,UNMAP,0,p_h->ID_client,0,p_h->ID_blocco); printf("* Invio conferma di unmapi in corso... "); if(spedisciDato(sd,&header,sizeof(struct protocol_header)) != 0) { sys_warn("! Conferma di unmap non inviato correttamente"); } printf(" fatto\n"); }
static void _proc_brd_isreg(struct msg_ac_brd_t *msg, int len, int proto) { if(!__uuid_equ(&msg->header.acuuid[0], &sysstat.acuuid[0])) { if(__uuid_equ(&msg->takeover[0], &sysstat.acuuid[0])) { if(sysstat.sock >= 0) { close(sysstat.sock); sysstat.sock = -1; } _proc_brd(msg, len, proto); } else { /* tell the broadcast ac, ap have reg in other ac */ struct msg_ap_resp_t *resp = malloc(sizeof(struct msg_ap_resp_t)); if(resp == NULL) { sys_warn("Malloc for response failed:%s\n", strerror(errno)); return; } fill_msg_header(resp, MSG_AP_RESP, msg->header.acuuid, new_random(msg->header.mac)); /* calculate chap */ chap_fill_msg_md5(resp, sizeof(*resp), msg->header.random); net_send(proto, -1, &msg->header.mac[0], (void *)resp, sizeof(struct msg_ap_resp_t)); free(resp); } } }
/* * Implement mutex using semaphore for cross process synchronization. * * http://man7.org/linux/man-pages/man3/sem_open.3.html */ mutex create_mutex(const char* name, bool locked) { if (sem_unlink(name) == -1) sys_warn("create sem_unlink [%s]", name); mutex m = (mutex) sem_open(name, O_CREAT|O_EXCL, S_IRUSR|S_IWUSR, (locked ? 0 : 1)); if (m == SEM_FAILED) sys_error_exit("create sem_open [%s]", name); return m; }
void serverMap(struct protocol_header *p_h,int sd) { // controllo esistenza ID (DA IMPLEMENTARE struct protocol_header header; struct tabella_interna tab_tmp; struct lista *lis; void *messaggio = NULL; lock_blocco(p_h->ID_blocco); pthread_mutex_lock(&mutex_map); tab_tmp.ID_blocco = p_h->ID_blocco; tab_tmp.ID_client = p_h->ID_client; tab_tmp.controllo_rw = restituisciControllorw(p_h->ID_blocco); tab_tmp.socket_client = sd; tab_tmp.indirizzo_locale = restituisciMemoriaLocale(p_h->ID_blocco); lis = (struct lista *)malloc(sizeof(struct lista)); lis->tab_interna = tab_tmp; lis->list = lista_blocchi; lista_blocchi = lis; unlock_blocco(p_h->ID_blocco); pthread_mutex_unlock(&mutex_map); costruisciHeader(&header,MAP,0,p_h->ID_client,DIMBLOCK,p_h->ID_blocco); if(spedisciDato(sd,&header,sizeof(struct protocol_header))!=0) { sys_warn("! Header di risposta non inviato"); } messaggio = restituisciMemoriaLocale(p_h->ID_blocco); if(spedisciDato(sd,messaggio,DIMBLOCK) == 0) { printf("* Messaggio inviato\n"); } else { sys_warn("! Errore invio messaggio"); } }
void serverWait(struct protocol_header *p_h,int sd) { struct protocol_header header; printf("> Richiesta la wait del blocco %d\n",p_h->ID_blocco); lock_blocco(p_h->ID_blocco); printf("> Mi metto in wait sul blocco %d\n",p_h->ID_blocco); wait_blocco(p_h->ID_blocco); printf("> UScito dalla wait sul blocco %d\n",p_h->ID_blocco); unlock_blocco(p_h->ID_blocco); printf("> Fatto unlock della wait sul blocco %d\n",p_h->ID_blocco); costruisciHeader(&header,WAIT,0,p_h->ID_client,0,p_h->ID_blocco); if(spedisciDato(sd,&header,sizeof(struct protocol_header)) != 0) { sys_warn("! Conferma di wait non inviato correttamente"); } }
static void _proc_brd(struct msg_ac_brd_t *msg, int len, int proto) { /* send current ipv4 */ struct msg_ap_reg_t *resp = malloc(sizeof(struct msg_ap_reg_t)); if(resp == NULL) { sys_warn("Malloc for response failed:%s\n", strerror(errno)); return; } /* generate random1 */ fill_msg_header((void *)resp, MSG_AP_REG, msg->header.acuuid, new_random(msg->header.mac)); resp->ipv4 = argument.addr; /* calculate chap: md5sum1 = packet + random0 + password */ chap_fill_msg_md5((void *)resp, sizeof(*resp), msg->header.random); net_send(proto, -1, &msg->header.mac[0], (void *)resp, sizeof(struct msg_ap_reg_t)); free(resp); }
int dll_sendpkt(char *dmac, char *data, int size) { assert(dmac != NULL && size <= DLL_PKT_DATALEN && data != NULL); int ret; LOCK_SDR(); __dll_buildpkt(dmac, data, size); __build_sdrll(dmac); ret = sendto(dllsdr.sdrsock, dllsdr.sdrpkt, size + sizeof(struct ethhdr), 0, (struct sockaddr *)&dllsdr.ll, sizeof(dllsdr.ll)); if(ret < 0) { sys_warn("send packet failed: %s\n", strerror(errno)); UNLOCK_SDR(); return -1; } UNLOCK_SDR(); return 0; }
int dll_brdcast(char *data, int size) { assert(size <= DLL_PKT_DATALEN && data != NULL); int ret; struct dlleth_t *deth = (void *)dllbrd.brdpkt; LOCK_BRD(); memcpy(&deth->data, data, size); ret = sendto(dllbrd.brdsock, dllbrd.brdpkt, size + sizeof(struct ethhdr), 0, (struct sockaddr *)&dllbrd.ll, sizeof(dllbrd.ll)); if(ret < 0) { sys_warn("broad cast failed: %s\n", strerror(errno)); UNLOCK_BRD(); return -1; } UNLOCK_BRD(); return 0; }
void *get_pos(void *arg) { struct timeval ti; int ret; struct ap_t *ap; int i, j, k, num = 0; long interval; struct point_t *pi = NULL; double a,b; replay: for(i = 0; i < MAX_CLI; i++) { if(clihead[i].key == IDLE_CLI) continue; now(&ti); if((ti.tv_sec - clihead[i].timestamp.tv_sec) > argument.losttime) { cli_del(&clihead[i]); continue; } pthread_mutex_lock(&clihead[i].lock); if(clihead[i].key == IDLE_CLI) goto unlock; clr_bit(&clihead[i].bitmap); num = 0; for(j = 0; j < MAX_AP; j++) { ap = &clihead[i].ap[j]; interval = ti.tv_sec - ap->timestamp.tv_sec; if((ap->valid) && (interval < argument.agetime)) { num++; set_bit(&clihead[i].bitmap, j); } } if(num < 3) goto unlock; pi = malloc(sizeof(struct point_t) * num); if(pi == NULL) { sys_warn("Malloc memory failed: %s\n", strerror(errno)); continue; } for(j = 0, k = 0; j < MAX_AP && k < num; j++) { if(isset(&clihead[i].bitmap, j)) { ret = getap_pos(clihead[i].ap[j].apmac, pi + k); if(ret < 0) goto free; pi[k].d = distance(clihead[i].ap[j].signal); mac_debug(MAC(clihead[i].climac), "(%lf, %lf, %lf) sig: %d dist: %lf\n", pi[k].x, pi[k].y, pi[k].z, clihead[i].ap[j].signal, pi[k].d); k++; } } ret = get_point(pi, num, &a, &b); if(ret < 0 || a < 0 || b < 0) goto free; ajust_pos(&clihead[i], &ti, &a, &b); mac_debug(MAC(clihead[i].climac), "%lf %lf %lf\n", a, b, pi->z); #ifndef DISABLE_MYSQL sql_insert(&sql, clihead[i].climac, a, b, pi->z); #endif free: free(pi); unlock: pthread_mutex_unlock(&clihead[i].lock); } sleep(2); goto replay; return NULL; }
void close_mutex(mutex m) { if (sem_close(SEM_T(m)) == -1) sys_warn("close sem_close"); }
void destroy_mutex(const char* name, mutex m) { if (sem_close(SEM_T(m)) == -1) sys_warn("destroy sem_close [%s]", name); if (sem_unlink(name) == -1) sys_warn("destroy sem_unlink [%s]", name); }