int ppfs_chown (const char *path, uid_t uid, gid_t gid){ fprintf(stderr, "ppfs_chown path : %s\n", path); ppacket *s = createpacket_s(4+strlen(path)+8, CLTOMD_CHOWN, -1); uint8_t* ptr = s->startptr+HEADER_LEN; put32bit(&ptr, strlen(path)); memcpy(ptr, path, strlen(path)); ptr+=strlen(path); put32bit(&ptr, uid); put32bit(&ptr, gid); sendpacket(fd, s); free(s); s = receivepacket(fd); const uint8_t* ptr2 = s->startptr; int status = get32bit(&ptr2); if(status == 0){ print_attr((const attr*)ptr2); attr_cache* ac; if(lookup_attr_cache(path,&ac) == 0){ ac->a.uid = uid; ac->a.gid = gid; } } free(s); return status; }
int ppfs_utimens(const char* path,const struct timespec tv[2]){ //tv[0]: atime, tv[1] mtime ppacket* p = createpacket_s(4+strlen(path)+4+4,CLTOMD_UTIMENS,-1); uint8_t* ptr = p->startptr + HEADER_LEN; int len = strlen(path); put32bit(&ptr,len); memcpy(ptr,path,len); ptr += len; put32bit(&ptr,tv[0].tv_sec); put32bit(&ptr,tv[1].tv_sec); sendpacket(fd,p); free(p); p = receivepacket(fd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); if(status == 0){ attr_cache* ac; if(lookup_attr_cache(path,&ac) == 0){ ac->a.atime = tv[0].tv_sec; ac->a.ctime = tv[1].tv_sec; } } free(p); return status; }
byte PN532::getCommandResponse(byte * resp, const long & wmillis) { if (!IRQ_wait(wmillis)) { #ifdef PN532DEBUG Serial.println("IRQ wail expired."); Serial.print("Couldn't wait "); Serial.println(wmillis); #endif return 0; } comm_status = REQUEST_RECEIVE; byte count = receivepacket(); #ifdef PN532COMM Serial.print("CommandResp. >> "); printBytes(packet, count); Serial.println('\n'); #endif //#undef PN532DEBUG if (!(packet[0] == 0xd5 && packet[1] == (last_command + 1))) { #ifdef PN532DEBUG Serial.println("Missmatch with the last command."); #endif comm_status = RESP_COMMAND_MISSMATCH; return 0; } comm_status = RESP_RECEIVED; count -= 2; memmove(resp, packet + 2, count); return count; }
/* the main loop that needs to be run at least 200 times per second. */ void doMessenger() { IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; while (receivepacket(&ip_port, data, &length) != -1) { #ifdef DEBUG /* if(rand() % 3 != 1) //simulate packet loss */ /* { */ if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) /* if packet is discarded */ printf("Received unhandled packet with length: %u\n", length); else printf("Received handled packet with length: %u\n", length); /* } */ printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id); #else DHT_handlepacket(data, length, ip_port); LosslessUDP_handlepacket(data, length, ip_port); friendreq_handlepacket(data, length, ip_port); LANdiscovery_handlepacket(data, length, ip_port); #endif } doDHT(); doLossless_UDP(); doNetCrypto(); doInbound(); doFriends(); LANdiscovery(); }
void* receivepacket_callback(void* _p_session) { rtp_msg_t* _msg; rtp_session_t* _session = _p_session; uint32_t _bytes; tox_IP_Port _from; uint8_t _socket_data[MAX_UDP_PACKET_SIZE]; int _m_socket = _socket; while ( 1 ) { int _status = receivepacket ( _m_socket, &_from, _socket_data, &_bytes ); if ( _status == FAILURE ) { /* nothing recved */ usleep(1000); continue; } pthread_mutex_lock ( &_mutex ); _msg = rtp_msg_parse ( NULL, _socket_data, _bytes ); rtp_handlepacket(_session, _msg); pthread_mutex_unlock ( &_mutex ); } pthread_exit(NULL); }
int ppfs_chmod (const char *path, mode_t mt){ fprintf(stderr, "ppfs_chmod path : %s\n", path); ppacket *s = createpacket_s(4+strlen(path)+4, CLTOMD_CHMOD,-1); uint8_t* ptr = s->startptr+HEADER_LEN; put32bit(&ptr, strlen(path)); memcpy(ptr, path, strlen(path)); ptr+=strlen(path); put32bit(&ptr, mt); sendpacket(fd, s); free(s); s = receivepacket(fd); const uint8_t* ptr2 = s->startptr; int status = get32bit(&ptr2); if(status == 0){ print_attr((const attr*)ptr2); attr_cache* ac; if(lookup_attr_cache(path,&ac) == 0){ ac->a.mode = mt; } } free(s); return status; }
int ppfs_rmdir (const char *path){ fprintf(stderr, "ppfs_rmdir path : %s\n", path); dir_cache* dc; if(lookup_dir_cache(path,&dc) == 0){ remove_dir_cache(dc); free_dir_cache(dc); } ppacket* p = createpacket_s(4+strlen(path),CLTOMD_RMDIR,-1); uint8_t* ptr = p->startptr + HEADER_LEN; put32bit(&ptr,strlen(path)); memcpy(ptr,path,strlen(path)); ptr += strlen(path); sendpacket(fd,p); free(p); p = receivepacket(fd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); fprintf(stderr, "rmdir status:%d\n", status); free(p); return status; }
int main(int argc, char *argv[]) { //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); if (argc < 4) { printf("usage %s ip port client_id(of friend to find ip_port of)\n", argv[0]); exit(0); } DHT_addfriend((uint8_t *)argv[3]); //initialize networking //bind to ip 0.0.0.0:PORT IP ip; ip.i = 0; init_networking(ip, PORT); int randdomnum = random_int(); memcpy(self_client_id, &randdomnum, 4); perror("Initialization"); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); //bootstrap_ip_port.ip.c[0] = 127; //bootstrap_ip_port.ip.c[1] = 0; //bootstrap_ip_port.ip.c[2] = 0; //bootstrap_ip_port.ip.c[3] = 1; bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(bootstrap_ip_port); IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; while(1) { doDHT(); while(receivepacket(&ip_port, data, &length) != -1) { if(DHT_handlepacket(data, length, ip_port)) { //unhandled packet printpacket(data, length, ip_port); } else { printf("Received handled packet with length: %u\n", length); } } print_clientlist(); print_friendlist(); c_sleep(300); } shutdown_networking(); return 0; }
int main(int argc, char *argv[]) { manage_keys(); printf("Public key: "); uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); } printf("\n"); printf("Port: %u\n", PORT); //initialize networking //bind to ip 0.0.0.0:PORT IP ip; ip.i = 0; init_networking(ip, PORT); perror("Initialization"); if (argc > 3) { printf("Trying to bootstrap into the network...\n"); IP_Port bootstrap_info; bootstrap_info.ip.i = inet_addr(argv[1]); bootstrap_info.port = htons(atoi(argv[2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[3]); DHT_bootstrap(bootstrap_info, bootstrap_key); free(bootstrap_key); } IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; int is_waiting_for_dht_connection = 1; while(1) { if (is_waiting_for_dht_connection && DHT_isconnected()) { printf("Connected to other bootstrap server successfully.\n"); is_waiting_for_dht_connection = 0; } doDHT(); while(receivepacket(&ip_port, data, &length) != -1) { DHT_handlepacket(data, length, ip_port); friendreq_handlepacket(data, length, ip_port); } c_sleep(1); } shutdown_networking(); return 0; }
void networking_poll(Networking_Core *net) { IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; while (receivepacket(net->sock, &ip_port, data, &length) != -1) { if (length < 1) continue; if (!(net->packethandlers[data[0]].function)) continue; net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length); } }
int ppfs_unlink (const char *path){ fprintf(stderr, "ppfs_unlink path : %s\n", path); ppacket* p = createpacket_s(4+strlen(path),CLTOMD_UNLINK,-1); uint8_t* ptr = p->startptr + HEADER_LEN; put32bit(&ptr,strlen(path)); memcpy(ptr,path,strlen(path)); ptr += strlen(path); sendpacket(fd,p); free(p); p = receivepacket(fd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); fprintf(stderr, "unlink status:%d\n", status); if(status == 0){ attr_cache* ac; chunk_cache* cc; if(lookup_attr_cache(path,&ac) == 0){ remove_attr_cache(ac); free_attr_cache(ac); } if(lookup_chunk_cache(path,&cc) == 0){ remove_chunk_cache(cc); free_chunk_cache(cc); } dir_cache* dc; char* ppath = parent_path(path); fprintf(stderr,"/n/n/npath=%s,parent_path:%s\n\n\n",path,ppath); if(lookup_dir_cache(ppath,&dc) == 0){ remove_dir_cache(dc); free_dir_cache(dc); } free(ppath); } free(p); return status; }
/* recieve packets and send them to the packethandler * run doLossless_UDP(); */ void Lossless_UDP() { IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; while (receivepacket(&ip_port, data, &length) != -1) { //if(rand() % 3 != 1)//add packet loss //{ if (LosslessUDP_handlepacket(data, length, ip_port)) { printpacket(data, length, ip_port); } else { //printconnection(0); printf("Received handled packet with length: %u\n", length); } //} } doLossless_UDP(); }
int ppfs_open(const char* path, struct fuse_file_info* fi){ fprintf(stderr, "ppfs_open path : %s\n", path); int status; ppacket* s = createpacket_s(4+strlen(path),CLTOMD_OPEN,-1); uint8_t* ptr = s->startptr + HEADER_LEN; put32bit(&ptr,strlen(path)); memcpy(ptr,path,strlen(path)); sendpacket(fd,s); free(s); s = receivepacket(fd); const uint8_t* ptr2 = s->startptr; status = get32bit(&ptr2); free(s); return status; }
void networking_poll(Networking_Core *net) { IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; while (receivepacket(net->sock, &ip_port, data, &length) != -1) { if (length < 1) continue; if (!(net->packethandlers[data[0]].function)) { #ifdef LOGGING sprintf(logbuffer, "[%02u] -- Packet has no handler.\n", data[0]); loglog(logbuffer); #endif continue; } net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length); } }
boolean PN532::checkACKframe(long timeout) { // Wait for chip to say its ready! if (!IRQ_wait(timeout)) return false; // read acknowledgement byte frame_head[] = { PREAMBLE, STARTCODE_1, STARTCODE_2 }; receivepacket(6); //printHexString(packet, 6); if ((0 == memcmp(packet, frame_head, 3)) && (packet[5] == 0)) { if (packet[3] == 0x00 && packet[4] == 0xff) { comm_status = ACK_FRAME_RECEIVED; return true; // ack'd command } else if (packet[3] == 0xff && packet[4] == 0x00) { comm_status = NACK_FRAME_RECEIVED; return true; // ack'd command } } comm_status = WRONG_ACK; return false; }
int ppfs_create(const char *path, mode_t mt, struct fuse_file_info *fi){ fprintf(stderr, "\n\n\n\nppfs_create path : %s\n", path); fprintf(stderr, "ppfs_create mode : %o\n\n\n\n\n", mt); ppacket* p = createpacket_s(4+strlen(path)+4,CLTOMD_CREATE,-1); uint8_t* ptr = p->startptr + HEADER_LEN; mt = (mt & 0777) | S_IFREG; put32bit(&ptr,strlen(path)); memcpy(ptr,path,strlen(path)); ptr += strlen(path); put32bit(&ptr,mt); sendpacket(fd,p); free(p); p = receivepacket(fd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); fprintf(stderr, "create status:%d\n", status); free(p); if(status == 0){ dir_cache* dc; char* ppath = parent_path(path); fprintf(stderr,"/n/n/npath=%s,parent_path:%s\n\n\n",path,ppath); if(lookup_dir_cache(ppath,&dc) == 0){ remove_dir_cache(dc); free_dir_cache(dc); } free(ppath); } return status; }
int ppfs_mkdir(const char* path, mode_t mt){ fprintf(stderr, "ppfs_mkdir path : %s\n", path); mt |= S_IFDIR; ppacket* p = createpacket_s(4+strlen(path)+4,CLTOMD_MKDIR,-1); uint8_t* ptr = p->startptr + HEADER_LEN; put32bit(&ptr,strlen(path)); memcpy(ptr,path,strlen(path)); ptr += strlen(path); put32bit(&ptr, mt); sendpacket(fd,p); free(p); p = receivepacket(fd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); fprintf(stderr, "mkdir status:%d\n", status); free(p); return status; }
int main(int argc, char *argv[]) { //memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); if (argc < 4) { printf("usage %s ip port public_key\n", argv[0]); exit(0); } new_keys(); printf("OUR ID: "); uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); } char temp_id[128]; printf("\nEnter the client_id of the friend you wish to add (32 bytes HEX format):\n"); if(scanf("%s", temp_id) != 1) exit(0); DHT_addfriend(hex_string_to_bin(temp_id)); /* initialize networking */ /* bind to ip 0.0.0.0:PORT */ IP ip; ip.i = 0; init_networking(ip, PORT); perror("Initialization"); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); /* bootstrap_ip_port.ip.c[0] = 127; * bootstrap_ip_port.ip.c[1] = 0; * bootstrap_ip_port.ip.c[2] = 0; * bootstrap_ip_port.ip.c[3] = 1; */ bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; while(1) { doDHT(); while(receivepacket(&ip_port, data, &length) != -1) { if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) { //unhandled packet printpacket(data, length, ip_port); } else { printf("Received handled packet with length: %u\n", length); } } print_clientlist(); print_friendlist(); c_sleep(300); } shutdown_networking(); return 0; }
int main(int argc, char *argv[]) { if (argc < 4) { printf("usage %s ip port filename(of file to send)\n", argv[0]); exit(0); } new_keys(); printf("OUR ID: "); uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) printf("0"); printf("%hhX",self_public_key[i]); } printf("\n"); memcpy(self_client_id, self_public_key, 32); char temp_id[128]; printf("Enter the client_id of the friend to connect to (32 bytes HEX format):\n"); scanf("%s", temp_id); uint8_t friend_id[32]; memcpy(friend_id, hex_string_to_bin(temp_id), 32); /* memcpy(self_client_id, "qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq", 32); */ DHT_addfriend(friend_id); IP_Port friend_ip; int connection = -1; int inconnection = -1; uint8_t acceptedfriend_public_key[crypto_box_PUBLICKEYBYTES]; int friendrequest = -1; uint8_t request_data[512]; /* initialize networking * bind to ip 0.0.0.0:PORT */ IP ip; ip.i = 0; init_networking(ip, PORT); initNetCrypto(); perror("Initialization"); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); bootstrap_ip_port.ip.i = inet_addr(argv[1]); DHT_bootstrap(bootstrap_ip_port); IP_Port ip_port; uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; uint8_t buffer1[128]; int read1 = 0; uint8_t buffer2[128]; int read2 = 0; FILE *file1 = fopen(argv[3], "rb"); if ( file1==NULL ){printf("Error opening file.\n");return 1;} FILE *file2 = fopen("received.txt", "wb"); if ( file2==NULL ){return 1;} read1 = fread(buffer1, 1, 128, file1); while(1) { while(receivepacket(&ip_port, data, &length) != -1) { if(rand() % 3 != 1) { /* simulate packet loss */ if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { /* if packet is not recognized */ printf("Received unhandled packet with length: %u\n", length); } else { printf("Received handled packet with length: %u\n", length); } } } friend_ip = DHT_getfriendip(friend_id); if(friend_ip.ip.i != 0) { if(connection == -1 && friendrequest == -1) { printf("Sending friend request to peer:"); printip(friend_ip); friendrequest = send_friendrequest(friend_id, friend_ip,(uint8_t *) "Hello World", 12); /* connection = crypto_connect((uint8_t *)friend_id, friend_ip); */ /* connection = new_connection(friend_ip); */ } if(check_friendrequest(friendrequest) == 1) { printf("Started connecting to friend:"); connection = crypto_connect(friend_id, friend_ip); } } if(inconnection == -1) { uint8_t secret_nonce[crypto_box_NONCEBYTES]; uint8_t public_key[crypto_box_PUBLICKEYBYTES]; uint8_t session_key[crypto_box_PUBLICKEYBYTES]; inconnection = crypto_inbound(public_key, secret_nonce, session_key); inconnection = accept_crypto_inbound(inconnection, acceptedfriend_public_key, secret_nonce, session_key); /* inconnection = incoming_connection(); */ if(inconnection != -1) { printf("Someone connected to us:\n"); /* printip(connection_ip(inconnection)); */ } } if(handle_friendrequest(acceptedfriend_public_key, request_data) > 1) { printf("RECIEVED FRIEND REQUEST: %s\n", request_data); } /* if someone connected to us write what he sends to a file * also send him our file. */ if(inconnection != -1) { if(write_cryptpacket(inconnection, buffer1, read1)) { printf("Wrote data1.\n"); read1 = fread(buffer1, 1, 128, file1); } read2 = read_cryptpacket(inconnection, buffer2); if(read2 != 0) { printf("Received data1.\n"); if(!fwrite(buffer2, read2, 1, file2)) { printf("file write error1\n"); } if(read2 < 128) { printf("Closed file1 %u\n", read2); fclose(file2); } } /* if buffer is empty and the connection timed out. */ else if(is_cryptoconnected(inconnection) == 4) { crypto_kill(inconnection); } } /* if we are connected to a friend send him data from the file. * also put what he sends us in a file. */ if(is_cryptoconnected(connection) >= 3) { if(write_cryptpacket(0, buffer1, read1)) { printf("Wrote data2.\n"); read1 = fread(buffer1, 1, 128, file1); } read2 = read_cryptpacket(0, buffer2); if(read2 != 0) { printf("Received data2.\n"); if(!fwrite(buffer2, read2, 1, file2)) { printf("file write error2\n"); } if(read2 < 128) { printf("Closed file2 %u\n", read2); fclose(file2); } } /* if buffer is empty and the connection timed out. */ else if(is_cryptoconnected(connection) == 4) { crypto_kill(connection); } } doDHT(); doLossless_UDP(); doNetCrypto(); /*print_clientlist(); *print_friendlist(); *c_sleep(300); */ c_sleep(1); } shutdown_networking(); return 0; }
int ppfs_getattr(const char* path, struct stat* stbuf){ fprintf(stderr, "ppfs_getattr path : %s\n", path); attr_cache* ac; if(lookup_attr_cache(path,&ac) == 0){ stbuf->st_mode = ac->a.mode; //S_IFREG | 0755; stbuf->st_nlink = ac->a.link; if(stbuf->st_mode & S_IFDIR ) stbuf->st_size = 4096; else stbuf->st_size = ac->a.size; stbuf->st_ctime = ac->a.ctime; stbuf->st_atime = ac->a.atime; stbuf->st_mtime = ac->a.mtime; stbuf->st_uid = ac->a.uid; stbuf->st_gid = ac->a.gid; stbuf->st_blocks = 0; return 0; } ppacket *s = createpacket_s(4+strlen(path), CLTOMD_GETATTR,-1); fprintf(stderr, "createpacket_s packet size:%u, cmd:%X\n", s->size, s->cmd); //5,4096 uint8_t* ptr = s->startptr+HEADER_LEN; put32bit(&ptr, strlen(path)); memcpy(ptr, path, strlen(path)); ptr += strlen(path); sendpacket(fd, s); free(s); s = receivepacket(fd); const uint8_t* ptr2 = s->startptr; int status = get32bit(&ptr2); fprintf(stderr,"status:%d\n",status); if(status == 0){ print_attr((const attr*)ptr2); memset(stbuf, 0, sizeof(struct stat)); const attr* a = (const attr*)ptr2; stbuf->st_mode = a->mode; //S_IFREG | 0755; stbuf->st_nlink = a->link; if(stbuf->st_mode & S_IFDIR ) stbuf->st_size = 4096; else stbuf->st_size = a->size; stbuf->st_ctime = a->ctime; stbuf->st_atime = a->atime; stbuf->st_mtime = a->mtime; stbuf->st_uid = a->uid; stbuf->st_gid = a->gid; stbuf->st_blocks = 0; attr_cache_add(path,*a); } free(s); return status; } //always called before open
int ppfs_write (const char *path, const char *buf, size_t st, off_t off, struct fuse_file_info *fi){ fprintf(stderr,"\n\n\nppfs_write:%s,size:%d,offset:%d\n\n\n",path,st,off); int nwrite = 0; int ost,ooff; ost = st; ooff = off; ppacket* p = createpacket_s(4+strlen(path),CLTOMD_READ_CHUNK_INFO,-1); uint8_t* ptr = p->startptr + HEADER_LEN; int plen = strlen(path); uint32_t ip; uint64_t* chunklist = NULL; int clen,calloc; const char* wbuf = buf; put32bit(&ptr,plen); memcpy(ptr,path,plen); ptr += plen; fprintf(stderr,"just to be clear\n"); const uint8_t* tmpptr = p->startptr + HEADER_LEN; int i; for(i=0;i<p->size;i+=1){ int x = get8bit(&tmpptr); fprintf(stderr,"%X\t",x); } fprintf(stderr,"\n"); sendpacket(fd,p); free(p); p = receivepacket(fd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); fprintf(stderr,"status:%d\n",status); if(status == 0){ ip = get32bit(&ptr2); if(ip == -1){ fprintf(stderr,"local mds\n"); } else { fprintf(stderr,"remote mds:%X\n",ip); } int chunks = get32bit(&ptr2); fprintf(stderr,"chunks=%d\n",chunks); int i; chunklist = (uint64_t*)malloc(sizeof(uint64_t)*(chunks+20)); clen = 0; calloc = chunks+20; for(i=0;i<chunks;i++){ uint64_t chunkid = get64bit(&ptr2); fprintf(stderr,"(%d):id=%lld\n",i,chunkid); chunklist[clen++] = chunkid; } ppfs_conn_entry* e = NULL; if(ip != -1){ if(remote_mds.sockfd != -1 && remote_mds.peerip != ip){ tcpclose(remote_mds.sockfd); remote_mds.sockfd = -1; } if(remote_mds.sockfd == -1){ if(serv_connect(&remote_mds,ip,MDS_PORT) < 0){ return -1; } } e = &remote_mds; } else { e = &local_mds; } fprintf(stderr,"connected\n"); if(chunks * CHUNKSIZE <= off + st){ fprintf(stderr,"clearing cache\n"); chunk_cache* cc; attr_cache* ac; if(lookup_chunk_cache(path,&cc) == 0){ remove_chunk_cache(cc); free_chunk_cache(cc); } if(lookup_attr_cache(path,&ac) == 0){ remove_attr_cache(ac); free_attr_cache(ac); } fprintf(stderr,"appending chunk\n"); while(chunks * CHUNKSIZE <= off + st){ ppacket* p = createpacket_s(4+plen,CLTOMD_APPEND_CHUNK,-1); uint8_t* ptr = p->startptr + HEADER_LEN; put32bit(&ptr,plen); memcpy(ptr,path,plen); sendpacket(e->sockfd,p); free(p); ppacket* rp = receivepacket(e->sockfd); const uint8_t* ptr2 = rp->startptr; int status = get32bit(&ptr2); printf("status:%d\n",status); if(status == 0){ uint64_t chunkid = get64bit(&ptr2); printf("chunkid=%lld\n",chunkid); if(clen < calloc){ chunklist[clen++] = chunkid; } else { chunklist = (uint64_t*)realloc(chunklist,calloc<<1); chunklist[clen++] = chunkid; } } else { free(rp); return status; } free(rp); chunks++; } } fprintf(stderr,"chunklist now:\n"); for(i=0;i<clen;i++){ fprintf(stderr,"\t(%d):%lld\n",i,chunklist[i]); } ppacket* p = createpacket_s(4+plen+4+4,CLTOMD_WRITE,-1); ptr = p->startptr + HEADER_LEN; put32bit(&ptr,plen); memcpy(ptr,path,plen); ptr += plen; put32bit(&ptr,ooff); put32bit(&ptr,ost); sendpacket(e->sockfd,p); free(p); int starti = off/CHUNKSIZE; int buflen = min(st,CHUNKSIZE - off % CHUNKSIZE); ppfs_conn_entry cs; cs.sockfd = -1; fprintf(stderr,"off=%d,st=%lld\n",off,st); while(st > 0){ uint64_t chunkid = chunklist[starti]; ppacket* p = createpacket_s(8,CLTOMD_LOOKUP_CHUNK,-1); uint8_t* ptr = p->startptr + HEADER_LEN; put64bit(&ptr,chunkid); sendpacket(e->sockfd,p); free(p); p = receivepacket(e->sockfd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); printf("status:%d\n",status); if(status == 0){ int csip = get32bit(&ptr2); printf("cid:%lld,csip:%X\n",chunkid,csip); if(cs.sockfd != -1 && cs.peerip != csip){ tcpclose(cs.sockfd); } if(cs.sockfd == -1){ if(serv_connect(&cs,csip,CS_PORT) < 0){ return -1; } } } else { return -1; } p = createpacket_s(8+4+4+buflen,CLTOCS_WRITE_CHUNK,-1); ptr = p->startptr + HEADER_LEN; put64bit(&ptr,chunkid); put32bit(&ptr,off % CHUNKSIZE); put32bit(&ptr,buflen); memcpy(ptr,wbuf,buflen); fprintf(stderr,"starti=%d,chunkid=%lld,off=%d,buflen=%d\n",starti,chunkid,off % CHUNKSIZE,buflen); sendpacket(cs.sockfd,p); free(p); p = receivepacket(cs.sockfd); ptr2 = p->startptr; status = get32bit(&ptr2); printf("status=%d\n",status); if(status == 0){ int wlen = get32bit(&ptr2); nwrite += wlen; printf("wlen=%d,nwrite=%d\n",wlen,nwrite); wbuf += wlen; } st -= buflen; off += buflen; starti = off/CHUNKSIZE; buflen = min(st,CHUNKSIZE - off % CHUNKSIZE); } } else { return status; } fprintf(stderr,"off=%d,st=%d,nwrite=%d\n",ooff,ost,nwrite); return nwrite; }
int ppfs_read(const char * path, char * buf, size_t st, off_t off, struct fuse_file_info *fi){ int nread = 0; int ooff = off; int ost = st; char* rbuf = buf; chunk_cache* cc; if(lookup_chunk_cache(path,&cc) != 0){ ppacket* p = createpacket_s(4+strlen(path),CLTOMD_READ_CHUNK_INFO,-1); uint8_t* ptr = p->startptr + HEADER_LEN; int plen = strlen(path); uint64_t* chunklist = NULL; int clen,calloc; put32bit(&ptr,plen); memcpy(ptr,path,plen); ptr += plen; fprintf(stderr,"just to be clear\n"); const uint8_t* tmpptr = p->startptr + HEADER_LEN; int i; for(i=0;i<p->size;i+=1){ int x = get8bit(&tmpptr); fprintf(stderr,"%X\t",x); } fprintf(stderr,"\n"); sendpacket(fd,p); free(p); p = receivepacket(fd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); fprintf(stderr,"status:%d\n",status); if(status != 0){ return -1; } uint32_t ip = get32bit(&ptr2); if(ip == -1){ fprintf(stderr,"local mds\n"); } else { fprintf(stderr,"remote mds:%X\n",ip); } int chunks = get32bit(&ptr2); fprintf(stderr,"chunks=%d\n",chunks); chunklist = (uint64_t*)malloc(sizeof(uint64_t)*(chunks+20)); clen = 0; calloc = chunks+20; for(i=0;i<chunks;i++){ uint64_t chunkid = get64bit(&ptr2); fprintf(stderr,"(%d):id=%lld\n",i,chunkid); chunklist[clen++] = chunkid; } cc = chunk_cache_add(path,chunklist,clen,ip); } else { fprintf(stderr,"\n\n\n\nfound chunk_cache\n\n\n\n"); } fprintf(stderr,"preparing mds connection:%X\n",ip); ppfs_conn_entry* e = NULL; uint32_t ip = cc->mdsid; if(ip != -1){ if(remote_mds.sockfd != -1 && remote_mds.peerip != ip){ tcpclose(remote_mds.sockfd); remote_mds.sockfd = -1; } if(remote_mds.sockfd == -1){ fprintf(stderr,"connecting\n"); if(serv_connect(&remote_mds,ip,MDS_PORT) < 0){ return -1; } } e = &remote_mds; } else { e = &local_mds; } fprintf(stderr,"done\n"); fprintf(stderr,"off=%d,st=%d\n",ooff,ost); if(cc->chunks * CHUNKSIZE < off + st){ return 0; } fprintf(stderr,"start reading now\n"); int starti = off/CHUNKSIZE; int buflen = min(st,CHUNKSIZE - off % CHUNKSIZE); ppfs_conn_entry cs; cs.sockfd = -1; while(st > 0){ uint64_t chunkid = cc->chunklist[starti]; ppacket* p = createpacket_s(8,CLTOMD_LOOKUP_CHUNK,-1); uint8_t* ptr = p->startptr + HEADER_LEN; put64bit(&ptr,chunkid); sendpacket(e->sockfd,p); free(p); p = receivepacket(e->sockfd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); printf("status:%d\n",status); if(status == 0){ int csip = get32bit(&ptr2); printf("cid:%lld,csip:%X\n",chunkid,csip); if(cs.sockfd != -1 && cs.peerip != csip){ tcpclose(cs.sockfd); } if(cs.sockfd == -1){ if(serv_connect(&cs,csip,CS_PORT) < 0){ return -1; } } } else { return -1; } fprintf(stderr,"chunkid=%lld,off=%d,buflen=%d\n",chunkid,off,buflen); p = createpacket_s(8+4+4,CLTOCS_READ_CHUNK,-1); ptr = p->startptr + HEADER_LEN; put64bit(&ptr,chunkid); put32bit(&ptr,off % CHUNKSIZE); put32bit(&ptr,buflen); sendpacket(cs.sockfd,p); free(p); p = receivepacket(cs.sockfd); ptr2 = p->startptr; status = get32bit(&ptr2); printf("status=%d\n",status); if(status == 0){ int rlen = get32bit(&ptr2); nread += rlen; printf("rlen=%d\n",rlen); memcpy(rbuf,ptr2,rlen); rbuf += rlen; } else { return -1; } st -= buflen; off += buflen; starti = off/CHUNKSIZE; buflen = min(st,CHUNKSIZE - off % CHUNKSIZE); } return nread; }
int ppfs_readdir (const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi){ fprintf(stderr, "ppfs_readdir path : %s\n", path); (void) offset; (void) fi; dir_cache* dc; if(lookup_dir_cache(path,&dc) == 0){ fprintf(stderr,"f*****g dir_cache\n"); int i; for(i=0;i<dc->n;++i) { filler(buf, dc->entries[i], NULL, 0); } return 0; } ppacket *s = createpacket_s(4+strlen(path)+4, CLTOMD_READDIR,-1); uint8_t* ptr = s->startptr+HEADER_LEN; put32bit(&ptr, strlen(path)); memcpy(ptr, path, strlen(path)); ptr+=strlen(path); put32bit(&ptr, offset); sendpacket(fd, s); free(s); s = receivepacket(fd); const uint8_t* ptr2 = s->startptr; int status = get32bit(&ptr2); if(status == 0){ int nfiles = get32bit(&ptr2); int i; char** files = malloc(sizeof(char*)*nfiles); for(i=0;i<nfiles;++i) { int flen = get32bit(&ptr2); char * fn = (char*)malloc(flen*sizeof(char)+1); memcpy(fn, ptr2, flen); fn[flen] = 0; ptr2 += flen; filler(buf, fn, NULL, 0); files[i] = fn; } dir_cache_add(path,files,nfiles); for(i=0;i<nfiles;i++){ free(files[i]); } free(files); } else { if(status == -ENOENT){ fprintf(stderr,"\tENOENT\n"); } if(status == -ENOTDIR){ fprintf(stderr,"\tENOTDIR\n"); } } free(s); return 0; }
int ppfs_truncate(const char* path,off_t off){ fprintf(stderr,"\n\n\n+ppfs_truncate\n\n\n"); chunk_cache* cc; if(lookup_chunk_cache(path,&cc) == 0){ remove_chunk_cache(cc); free_chunk_cache(cc); } ppacket* p = createpacket_s(4+strlen(path)+4+4,CLTOMD_READ_CHUNK_INFO,-1); uint8_t* ptr = p->startptr + HEADER_LEN; int len = strlen(path); uint32_t ip; int chunks; put32bit(&ptr,len); memcpy(ptr,path,len); ptr += len; sendpacket(fd,p); free(p); p = receivepacket(fd); const uint8_t* ptr2 = p->startptr; int status = get32bit(&ptr2); fprintf(stderr,"status:%d\n",status); if(status == 0){ ip = get32bit(&ptr2); if(ip == -1){ fprintf(stderr,"local mds\n"); } else { fprintf(stderr,"remote mds:%X\n",ip); } chunks = get32bit(&ptr2); fprintf(stderr,"chunks=%d\n",chunks); } else { free(p); return status; } free(p); ppfs_conn_entry* e = NULL; if(ip != -1){ if(remote_mds.sockfd != -1 && remote_mds.peerip != ip){ tcpclose(remote_mds.sockfd); remote_mds.sockfd = -1; } if(remote_mds.sockfd == -1){ if(serv_connect(&remote_mds,ip,MDS_PORT) < 0){ return -1; } } e = &remote_mds; } else { e = &local_mds; } off_t size = chunks * CHUNKSIZE; while(size >= off + CHUNKSIZE){ p = createpacket_s(4+len,CLTOMD_POP_CHUNK,-1); ptr = p->startptr + HEADER_LEN; put32bit(&ptr,len); memcpy(ptr,path,len); sendpacket(e->sockfd,p); free(p); ppacket* rp = receivepacket(e->sockfd); ptr2 = rp->startptr; status = get32bit(&ptr2); printf("status:%d\n",status); if(status != 0){ return status; } free(rp); size -= CHUNKSIZE; } p = createpacket_s(4+len,CLTOMD_APPEND_CHUNK,-1); ptr = p->startptr + HEADER_LEN; put32bit(&ptr,len); memcpy(ptr,path,len); while(size < off){ sendpacket(e->sockfd,p); ppacket* rp = receivepacket(e->sockfd); ptr2 = rp->startptr; status = get32bit(&ptr2); printf("status:%d\n",status); if(status != 0){ return status; } free(rp); size += CHUNKSIZE; } free(p); return 0; }