void Media::init() { ChowdrenAudio::open_audio(); double start_time = platform_get_time(); AssetFile fp; fp.open(); #ifdef CHOWDREN_IS_WIIU // experimental code to load faster unsigned int start = AssetFile::get_offset(0, AssetFile::SOUND_DATA); unsigned int size = AssetFile::get_size(AssetFile::SOUND_DATA); startup_data = new unsigned char[size]; startup_size = size; fp.seek(start); fp.read(startup_data, size); for (int i = 0; i < SOUND_COUNT; i++) { add_cache(i); } delete[] startup_data; #else for (int i = 0; i < SOUND_COUNT; i++) { fp.set_item(i, AssetFile::SOUND_DATA); add_cache(i, fp); } #endif std::cout << "Sound bank took " << (platform_get_time() - start_time) << std::endl; }
void cache_object_addindex(struct cache_object *co, struct strbuf *buf, struct cache_index_entry_list *node) { size_t start = co->cc.cache_buf.len; add_cache(&co->cc, buf); node->start = start; make_index_entry_details(&co->ci, node); }
blob cache_dtable::lookup(const dtype & key, bool * found, ATX_DEF) const { if(atx != NO_ABORTABLE_TX) return base->lookup(key, found, atx); cache_map::const_iterator iter = cache.find(key); if(iter != cache.end()) { *found = (*iter).second.found; return (*iter).second.value; } blob value = base->lookup(key, found); add_cache(key, value, *found); return value; }
int cmsg(const char *msgid) { Item *itemp; int len; if (msgs_cache == (Cache *) NULL) if (init_cache(&msgs_cache, HASHSIZE, BSZ, (int (*)())NULL, (int (*)())NULL) == -1) { (void) fprintf(stderr, gettext("cmsg(): init_cache() failed.\n")); exit(1); } len = strlen(msgid) + 1; if ((itemp = lookup_cache(msgs_cache, (void *) msgid, len)) == Null_Item) { if ((itemp = (Item *) malloc(sizeof (*itemp))) == Null_Item) { (void) fprintf(stderr, gettext("cmsg(): itemp=malloc(%d)\n"), sizeof (*itemp)); exit(1); } if ((itemp->key = (char *) malloc(len)) == NULL) { (void) fprintf(stderr, gettext("cmsg(): itemp->key=malloc(%d)\n"), len); exit(1); } (void) memmove(itemp->key, msgid, len); itemp->keyl = len; itemp->data = NULL; itemp->datal = 0; if (add_cache(msgs_cache, itemp) == -1) (void) fprintf(stderr, gettext("cmsg(): add_cache() failed.\n")); return (0); } else { return (1); } }
int main(int argc, char* argv[]) { int low, up; long long result = 0; for (low = 1; low < 10000; low++) { int sum = low * low; for (up = low + 1; up < 10000; up++) { sum += up * up; if (sum > 10000 * 10000) break; if (!in_cache(sum) && is_palindromic(sum)) { add_cache(sum); result += sum; } } } printf("%lli\n", result); return 0; }
int cache_dtable::remove(const dtype & key, ATX_DEF) { if(atx != NO_ABORTABLE_TX) return base->remove(key, atx); cache_map::iterator iter; int value = base->remove(key); if(value < 0) return value; iter = cache.find(key); if(iter != cache.end()) { (*iter).second.found = false; (*iter).second.value = blob(); } else add_cache(key, blob(), false); return value; }
int cache_dtable::insert(const dtype & key, const blob & blob, bool append, ATX_DEF) { if(atx != NO_ABORTABLE_TX) return base->insert(key, blob, append, atx); cache_map::iterator iter; int value = base->insert(key, blob, append); if(value < 0) return value; iter = cache.find(key); if(iter != cache.end()) { (*iter).second.found = true; (*iter).second.value = blob; } else add_cache(key, blob, true); return value; }
// // Initialize system_icache_idx & system_dcache_idx variables with // information on board level system caches. Caches implemented // on the CPU itself will dealt with in init_cpuinfo(). // void init_cacheattr_mx3x(int enable) { /* Disable L2-Cache */ iMX3x_L2CC_CTRL_REG = 0; /* Configure Auxiliary Control Register (ACR) */ iMX3x_L2CC_ACR_REG = 0x3001b; /* Invalidate Cache */ iMX3x_L2CC_INVAL_BY_WAY_REG = 0xff; while (iMX3x_L2CC_INVAL_BY_WAY_REG) ; if (enable) { /* * FIXME: until we can ensure coherency with page table walks we * must set the L2 to be write-through. * * callout_cache_l2_mx3x.S assumes a write-through cache, and * will need to be updated if this code configures the cache * for write-back. */ iMX3x_L2CC_DEBUG_CTRL_REG |= 0x2; /* Enable Cache */ iMX3x_L2CC_CTRL_REG = 1; /* * Add the L2 cache. * FIXME: should have CACHE_FLAG_CTRL_PHYS but we always flush the whole * cache so it doesn't matter. */ system_icache_idx = system_dcache_idx = add_cache(CACHE_LIST_END, CACHE_FLAG_UNIFIED, 32, (128*1024)/32, &cache_imx3x_l2); } if (debug_flag) { kprintf("L2 cache %s\n", enable ? "enabled" : "disabled"); } }
static int build_vpbp(VirtualPBP *vpbp) { int ret, i; u32 off; printk("Need to build vpbp %s\n", vpbp->name); memset(vpbp->header, 0, sizeof(vpbp->header)); memset(vpbp->sects, 0, sizeof(vpbp->sects)); vpbp->enabled = 1; vpbp->file_pointer = 0; vpbp->header[0] = 0x50425000; // PBP magic vpbp->header[1] = 0x10000; // version // fill vpbp offsets off = 0x28; ret = isoOpen(vpbp->name); if (ret < 0) { printk("%s: isoOpen -> %d\n", __func__, ret); ret = add_cache(vpbp); return ret; } for(i=0; i<NELEMS(pbp_entries); ++i) { vpbp->header[i+2] = off; if (pbp_entries[i].enabled) { PBPSection *sec = &vpbp->sects[i]; ret = isoGetFileInfo(pbp_entries[i].name, &sec->size, &sec->lba); if (ret < 0) { if (i == 0) { // no PARAM.SFO? // then it's a bad ISO isoClose(); return -36; } else { continue; } } if (i == 0) { off += sizeof(virtualsfo); } else { off += sec->size; } } } vpbp->pbp_total_size = vpbp->header[9]; get_iso_file_size(vpbp->name, &vpbp->iso_total_size); ret = add_cache(vpbp); printk("%s: add_cache -> %d\n", __func__, ret); isoClose(); return ret; }
static int32_t cacheex_add_to_cache_int(struct s_client *cl, ECM_REQUEST *er, int8_t csp) { if(er->rc >= E_NOTFOUND) { return 0; } if(!cl) { return 0; } if(!csp && cl->reader && cl->reader->cacheex.mode != 2) //from reader { cs_debug_mask(D_CACHEEX, "CACHEX received, but disabled for %s", username(cl)); return 0; } if(!csp && !cl->reader && cl->account && cl->account->cacheex.mode != 3) //from user { cs_debug_mask(D_CACHEEX, "CACHEX received, but disabled for %s", username(cl)); return 0; } if(!csp && !cl->reader && !cl->account) //not active! { cs_debug_mask(D_CACHEEX, "CACHEX received, but invalid client state %s", username(cl)); return 0; } uint8_t i, c; uint8_t null = 0; for(i = 0; i < 16; i += 4) { c = ((er->cw[i] + er->cw[i + 1] + er->cw[i + 2]) & 0xff); null |= (er->cw[i] | er->cw[i + 1] | er->cw[i + 2]); if(er->cw[i + 3] != c) { cs_ddump_mask(D_CACHEEX, er->cw, 16, "push received cw with chksum error from %s", csp ? "csp" : username(cl)); cl->cwcacheexerr++; if(cl->account) { cl->account->cwcacheexerr++; } return 0; } } if(null == 0 || chk_is_null_CW(er->cw)) { cs_ddump_mask(D_CACHEEX, er->cw, 16, "push received null cw from %s", csp ? "csp" : username(cl)); cl->cwcacheexerr++; if(cl->account) { cl->account->cwcacheexerr++; } return 0; } er->grp |= cl->grp; //ok for mode2 reader too: cl->reader->grp er->rc = E_CACHEEX; er->cacheex_src = cl; er->selected_reader = cl->reader; er->client = NULL; //No Owner! So no fallback! if(check_client(cl)) { cl->cwcacheexgot++; if(cl->account) { cl->account->cwcacheexgot++; } first_client->cwcacheexgot++; } add_hitcache(cl, er); //we have to call it before add_cache, because in chk_process we could remove it! add_cache(er); cacheex_add_stats(cl, er->caid, er->srvid, er->prid, 1); cs_writelock(&ecm_pushed_deleted_lock); er->next = ecm_pushed_deleted; ecm_pushed_deleted = er; cs_writeunlock(&ecm_pushed_deleted_lock); return 1; //NO free, we have to wait cache push out stuff ends. }
static void read_snmp_response(int fd) /* I - SNMP socket file descriptor */ { char addrname[256]; /* Source address name */ cups_snmp_t packet; /* Decoded packet */ snmp_cache_t key, /* Search key */ *device; /* Matching device */ /* * Read the response data... */ if (!_cupsSNMPRead(fd, &packet, -1.0)) { fprintf(stderr, "ERROR: Unable to read data from socket: %s\n", strerror(errno)); return; } if (HostNameLookups) httpAddrLookup(&(packet.address), addrname, sizeof(addrname)); else httpAddrString(&(packet.address), addrname, sizeof(addrname)); debug_printf("DEBUG: %.3f Received data from %s...\n", run_time(), addrname); /* * Look for the response status code in the SNMP message header... */ if (packet.error) { fprintf(stderr, "ERROR: Bad SNMP packet from %s: %s\n", addrname, packet.error); return; } debug_printf("DEBUG: community=\"%s\"\n", packet.community); debug_printf("DEBUG: request-id=%d\n", packet.request_id); debug_printf("DEBUG: error-status=%d\n", packet.error_status); if (packet.error_status && packet.request_id != DEVICE_TYPE) return; /* * Find a matching device in the cache... */ key.addrname = addrname; device = (snmp_cache_t *)cupsArrayFind(Devices, &key); /* * Process the message... */ switch (packet.request_id) { case DEVICE_TYPE : /* * Got the device type response... */ if (device) { debug_printf("DEBUG: Discarding duplicate device type for \"%s\"...\n", addrname); return; } /* * Add the device and request the device data... */ add_cache(&(packet.address), addrname, NULL, NULL, NULL); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_DESCRIPTION, DescriptionOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_ID, DeviceIdOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_URI, UriOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_LOCATION, LocationOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_PRODUCT, LexmarkProductOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_PRODUCT, LexmarkProductOID2); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_ID, LexmarkDeviceIdOID); _cupsSNMPWrite(fd, &(packet.address), CUPS_SNMP_VERSION_1, packet.community, CUPS_ASN1_GET_REQUEST, DEVICE_PRODUCT, XeroxProductOID); break; case DEVICE_DESCRIPTION : if (device && packet.object_type == CUPS_ASN1_OCTET_STRING) { /* * Update an existing cache entry... */ char make_model[256]; /* Make and model */ if (strchr((char *)packet.object_value.string.bytes, ':') && strchr((char *)packet.object_value.string.bytes, ';')) { /* * Description is the IEEE-1284 device ID... */ char *ptr; /* Pointer into device ID */ for (ptr = (char *)packet.object_value.string.bytes; *ptr; ptr ++) if (*ptr == '\n') *ptr = ';'; /* A lot of bad printers put a newline */ if (!device->id) device->id = strdup((char *)packet.object_value.string.bytes); backendGetMakeModel((char *)packet.object_value.string.bytes, make_model, sizeof(make_model)); if (device->info) free(device->info); device->info = strdup(make_model); } else { /* * Description is plain text... */ fix_make_model(make_model, (char *)packet.object_value.string.bytes, sizeof(make_model)); if (device->info) free(device->info); device->info = strdup((char *)packet.object_value.string.bytes); } if (!device->make_and_model) device->make_and_model = strdup(make_model); } break; case DEVICE_ID : if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && (!device->id || strlen(device->id) < packet.object_value.string.num_bytes)) { /* * Update an existing cache entry... */ char make_model[256]; /* Make and model */ char *ptr; /* Pointer into device ID */ for (ptr = (char *)packet.object_value.string.bytes; *ptr; ptr ++) if (*ptr == '\n') *ptr = ';'; /* A lot of bad printers put a newline */ if (device->id) free(device->id); device->id = strdup((char *)packet.object_value.string.bytes); /* * Convert the ID to a make and model string... */ backendGetMakeModel((char *)packet.object_value.string.bytes, make_model, sizeof(make_model)); if (device->make_and_model) free(device->make_and_model); device->make_and_model = strdup(make_model); } break; case DEVICE_LOCATION : if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && !device->location) device->location = strdup((char *)packet.object_value.string.bytes); break; case DEVICE_PRODUCT : if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && !device->id) { /* * Update an existing cache entry... */ if (!device->info) device->info = strdup((char *)packet.object_value.string.bytes); if (device->make_and_model) free(device->make_and_model); device->make_and_model = strdup((char *)packet.object_value.string.bytes); } break; case DEVICE_URI : if (device && packet.object_type == CUPS_ASN1_OCTET_STRING && !device->uri && packet.object_value.string.num_bytes > 3) { /* * Update an existing cache entry... */ char scheme[32], /* URI scheme */ userpass[256], /* Username:password in URI */ hostname[256], /* Hostname in URI */ resource[1024]; /* Resource path in URI */ int port; /* Port number in URI */ if (!strncmp((char *)packet.object_value.string.bytes, "lpr:", 4)) { /* * We want "lpd://..." for the URI... */ packet.object_value.string.bytes[2] = 'd'; } if (httpSeparateURI(HTTP_URI_CODING_ALL, (char *)packet.object_value.string.bytes, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)) >= HTTP_URI_OK) device->uri = strdup((char *)packet.object_value.string.bytes); } break; } }
static void *status_thread (void *p) { THREAD_SIGINIT; /* (void)p; */ /* To inhibit "unused variable" warning */ if (!global.strict_suid) { if (!run_as(global.run_as)) { pdnsd_exit(); } } if (listen(stat_sock,5)==-1) { log_warn("Error: could not listen on socket: %s.\nStatus readback will be impossible",strerror(errno)); goto exit_thread; } for(;;) { struct sockaddr_un ra; socklen_t res=sizeof(ra); int rs; if ((rs=accept(stat_sock,(struct sockaddr *)&ra,&res))!=-1) { uint16_t cmd; DEBUG_MSG("Status socket query pending.\n"); if (read_short(rs,&cmd)) { /* Check magic number in command */ if((cmd & 0xff00) == CTL_CMDVERNR) { const char *errmsg; cmd &= 0xff; switch(cmd) { case CTL_STATS: { struct utsname nm; DEBUG_MSG("Received STATUS query.\n"); if(!print_succ(rs)) break; uname(&nm); if(fsprintf(rs,"pdnsd-%s running on %s.\n",VERSION,nm.nodename)<0 || report_cache_stat(rs)<0 || report_thread_stat(rs)<0 || report_conf_stat(rs)<0) { DEBUG_MSG("Error writing to control socket: %s\n" "Failed to send status report.\n",strerror(errno)); } } break; case CTL_SERVER: { char *label,*dnsaddr; int indx; uint16_t cmd2; DEBUG_MSG("Received SERVER command.\n"); if (read_allocstring(rs,&label,NULL)<=0) { print_serr(rs,"Error reading server label."); break; } if (!read_short(rs,&cmd2)) { print_serr(rs,"Missing up|down|retest."); goto free_label_break; } if(!read_allocstring(rs, &dnsaddr,NULL)) { print_serr(rs,"Error reading DNS addresses."); goto free_label_break; } /* Note by Paul Rombouts: We are about to access server configuration data. Now that the configuration can be changed during run time, we should be using locks before accessing server config data, even if it is read-only access. However, as long as this is the only thread that calls reload_config_file() it should be OK to read the server config without locks, but it is something to keep in mind. */ { char *endptr; indx=strtol(label,&endptr,0); if(!*endptr) { if (indx<0 || indx>=DA_NEL(servers)) { print_serr(rs,"Server index out of range."); goto free_dnsaddr_label_break; } } else { if (!strcmp(label, "all")) indx=-2; /* all servers */ else indx=-1; /* compare names */ } } if(cmd2==CTL_S_UP || cmd2==CTL_S_DOWN || cmd2==CTL_S_RETEST) { if(!dnsaddr) { if (indx==-1) { int i; for (i=0;i<DA_NEL(servers);++i) { char *servlabel=DA_INDEX(servers,i).label; if (servlabel && !strcmp(servlabel,label)) goto found_label; } print_serr(rs,"Bad server label."); goto free_dnsaddr_label_break; found_label:; } if(mark_servers(indx,(indx==-1)?label:NULL,(cmd2==CTL_S_RETEST)?-1:(cmd2==CTL_S_UP))==0) print_succ(rs); else print_serr(rs,"Could not start up or signal server status thread."); } else { /* Change server addresses */ if(indx==-2) { print_serr(rs,"Can't use label \"all\" to change server addresses."); goto free_dnsaddr_label_break; } if(indx==-1) { int i; for(i=0;i<DA_NEL(servers);++i) { char *servlabel=DA_INDEX(servers,i).label; if (servlabel && !strcmp(servlabel,label)) { if(indx!=-1) { print_serr(rs,"server label must be unique to change server addresses."); goto free_dnsaddr_label_break; } indx=i; } } if(indx==-1) { print_serr(rs,"Bad server label."); goto free_dnsaddr_label_break; } } { char *ipstr,*q=dnsaddr; addr_array ar=NULL; pdnsd_a addr; int err; for(;;) { for(;;) { if(!*q) goto change_servs; if(*q!=',' && !isspace(*q)) break; ++q; } ipstr=q; for(;;) { ++q; if(!*q) break; if(*q==',' || isspace(*q)) {*q++=0; break; } } if(!str2pdnsd_a(ipstr,&addr)) { print_serr(rs,"Bad server ip"); goto free_ar; } if(!(ar=DA_GROW1(ar))) { print_serr(rs,"Out of memory."); goto free_dnsaddr_label_break; } DA_LAST(ar)=addr; } change_servs: err=change_servers(indx,ar,(cmd2==CTL_S_RETEST)?-1:(cmd2==CTL_S_UP)); if(err==0) print_succ(rs); else print_serr(rs,err==ETIMEDOUT?"Timed out while trying to gain access to server data.": err==ENOMEM?"Out of memory.": "Could not start up or signal server status thread."); free_ar: da_free(ar); } } } else print_serr(rs,"Bad command."); free_dnsaddr_label_break: free(dnsaddr); free_label_break: free(label); } break; case CTL_RECORD: { uint16_t cmd2; unsigned char name[DNSNAMEBUFSIZE],buf[DNSNAMEBUFSIZE]; DEBUG_MSG("Received RECORD command.\n"); if (!read_short(rs,&cmd2)) goto incomplete_command; if (read_domain(rs, charp buf, sizeof(buf))<=0) goto incomplete_command; if ((errmsg=parsestr2rhn(buf,sizeof(buf),name))!=NULL) goto bad_domain_name; switch (cmd2) { case CTL_R_DELETE: del_cache(name); print_succ(rs); break; case CTL_R_INVAL: invalidate_record(name); print_succ(rs); break; default: print_serr(rs,"Bad command."); } } break; case CTL_SOURCE: { uint32_t ttl; char *fn; uint16_t servaliases,flags; unsigned char buf[DNSNAMEBUFSIZE],owner[DNSNAMEBUFSIZE]; DEBUG_MSG("Received SOURCE command.\n"); if (read_allocstring(rs,&fn,NULL)<=0) { print_serr(rs,"Bad filename name."); break; } if (read_domain(rs, charp buf, sizeof(buf))<=0 || !read_long(rs,&ttl) || !read_short(rs,&servaliases) || /* serve aliases */ !read_short(rs,&flags)) /* caching flags */ { print_serr(rs,"Malformed or incomplete command."); goto free_fn; } if ((errmsg=parsestr2rhn(buf,sizeof(buf),owner))!=NULL) { print_serr(rs,errmsg); goto free_fn; } if (ttl < 0) { print_serr(rs, "Bad TTL."); goto free_fn; } if(flags&DF_NEGATIVE) { print_serr(rs, "Bad cache flags."); goto free_fn; } { char *errmsg; if (read_hosts(fn,owner,ttl,flags,servaliases,&errmsg)) print_succ(rs); else { print_serr(rs,errmsg?:"Out of memory."); free(errmsg); } } free_fn: free(fn); } break; case CTL_ADD: { uint32_t ttl; unsigned sz; uint16_t tp,flags,nadr=0; unsigned char name[DNSNAMEBUFSIZE],buf[DNSNAMEBUFSIZE],dbuf[2+DNSNAMEBUFSIZE]; size_t adrbufsz=0; unsigned char *adrbuf=NULL; DEBUG_MSG("Received ADD command.\n"); if (!read_short(rs,&tp)) goto incomplete_command; if (read_domain(rs, charp buf, sizeof(buf))<=0) goto incomplete_command; if (!read_long(rs,&ttl)) goto incomplete_command; if (!read_short(rs,&flags)) /* caching flags */ goto incomplete_command; if ((errmsg=parsestr2rhn(buf,sizeof(buf),name))!=NULL) goto bad_domain_name; if (ttl < 0) goto bad_ttl; if(flags&DF_NEGATIVE) goto bad_flags; switch (tp) { case T_A: sz=sizeof(struct in_addr); #if ALLOW_LOCAL_AAAA goto read_adress_list; case T_AAAA: sz=sizeof(struct in6_addr); read_adress_list: #endif if (!read_short(rs,&nadr)) goto incomplete_command; if (!nadr) goto bad_arg; adrbufsz= nadr * (size_t)sz; adrbuf= malloc(adrbufsz); if(!adrbuf) goto out_of_memory; { size_t nread=0; while(nread<adrbufsz) { ssize_t m=read(rs,adrbuf+nread,adrbufsz-nread); if(m<=0) {free(adrbuf); goto bad_arg;} nread += m; } } break; case T_CNAME: case T_PTR: case T_NS: if (read_domain(rs, charp buf, sizeof(buf))<=0) goto incomplete_command; if ((errmsg=parsestr2rhn(buf,sizeof(buf),dbuf))!=NULL) goto bad_domain_name; sz=rhnlen(dbuf); break; case T_MX: if (read(rs,dbuf,2)!=2) goto bad_arg; if (read_domain(rs, charp buf, sizeof(buf))<=0) goto incomplete_command; if ((errmsg=parsestr2rhn(buf,sizeof(buf),dbuf+2))!=NULL) goto bad_domain_name; sz=rhnlen(dbuf+2)+2; break; default: goto bad_arg; } { dns_cent_t cent; if (!init_cent(¢, name, 0, 0, flags DBG1)) { free(adrbuf); goto out_of_memory; } if(adrbuf) { unsigned char *adrp; int i; for(adrp=adrbuf,i=0; i<nadr; adrp += sz,++i) { if (!add_cent_rr(¢,tp,ttl,0,CF_LOCAL,sz,adrp DBG1)) { free_cent(¢ DBG1); free(adrbuf); goto out_of_memory; } } free(adrbuf); } else if (!add_cent_rr(¢,tp,ttl,0,CF_LOCAL,sz,dbuf DBG1)) { free_cent(¢ DBG1); goto out_of_memory; } if(cent.qname[0]==1 && cent.qname[1]=='*') { /* Wild card record. Set the DF_WILD flag for the name with '*.' removed. */ if(!set_cent_flags(¢.qname[2],DF_WILD)) { print_serr(rs, "Before defining records for a name with a wildcard" " you must first define some records for the name" " with '*.' removed."); goto cleanup_cent; } } add_cache(¢); print_succ(rs); cleanup_cent: free_cent(¢ DBG1); } } break; case CTL_NEG: { uint32_t ttl; uint16_t tp; unsigned char name[DNSNAMEBUFSIZE],buf[DNSNAMEBUFSIZE]; DEBUG_MSG("Received NEG command.\n"); if (read_domain(rs, charp buf, sizeof(buf))<=0) goto incomplete_command; if (!read_short(rs,&tp)) goto incomplete_command; if (!read_long(rs,&ttl)) goto incomplete_command; if ((errmsg=parsestr2rhn(buf,sizeof(buf),name))!=NULL) { DEBUG_MSG("NEG: received bad domain name.\n"); goto bad_domain_name; } if (tp!=255 && PDNSD_NOT_CACHED_TYPE(tp)) { DEBUG_MSG("NEG: received bad record type.\n"); print_serr(rs,"Bad record type."); break; } if (ttl < 0) goto bad_ttl; { dns_cent_t cent; if (tp==255) { if (!init_cent(¢, name, ttl, 0, DF_LOCAL|DF_NEGATIVE DBG1)) goto out_of_memory; } else { if (!init_cent(¢, name, 0, 0, 0 DBG1)) goto out_of_memory; if (!add_cent_rrset_by_type(¢,tp,ttl,0,CF_LOCAL|CF_NEGATIVE DBG1)) { free_cent(¢ DBG1); goto out_of_memory; } } add_cache(¢); free_cent(¢ DBG1); } print_succ(rs); } break; case CTL_CONFIG: { char *fn,*errmsg; DEBUG_MSG("Received CONFIG command.\n"); if (!read_allocstring(rs,&fn,NULL)) { print_serr(rs,"Bad filename name."); break; } if (reload_config_file(fn,&errmsg)) print_succ(rs); else { print_serr(rs,errmsg?:"Out of memory."); free(errmsg); } free(fn); } break; case CTL_INCLUDE: { char *fn,*errmsg; DEBUG_MSG("Received INCLUDE command.\n"); if (read_allocstring(rs,&fn,NULL)<=0) { print_serr(rs,"Bad filename name."); break; } if (read_config_file(fn,NULL,NULL,0,&errmsg)) print_succ(rs); else { print_serr(rs,errmsg?:"Out of memory."); free(errmsg); } free(fn); } break; case CTL_EVAL: { char *str,*errmsg; DEBUG_MSG("Received EVAL command.\n"); if (!read_allocstring(rs,&str,NULL)) { print_serr(rs,"Bad input string."); break; } if (confparse(NULL,str,NULL,NULL,0,&errmsg)) print_succ(rs); else { print_serr(rs,errmsg?:"Out of memory."); free(errmsg); } free(str); } break; case CTL_EMPTY: { slist_array sla=NULL; char *names; unsigned len; DEBUG_MSG("Received EMPTY command.\n"); if (!read_allocstring(rs,&names,&len)) { print_serr(rs,"Bad arguments."); break; } if(names) { char *p=names, *last=names+len; while(p<last) { int tp; char *q; slist_t *sl; unsigned sz; unsigned char rhn[DNSNAMEBUFSIZE]; if(*p=='-') { tp=C_EXCLUDED; ++p; } else { tp=C_INCLUDED; if(*p=='+') ++p; } /* skip a possible leading dot. */ if(p+1<last && *p=='.' && *(p+1)) ++p; q=p; while(q<last && *q) ++q; if ((errmsg=parsestr2rhn(ucharp p,q-p,rhn))!=NULL) { DEBUG_MSG("EMPTY: received bad domain name: %s\n",p); print_serr(rs,errmsg); goto free_sla_names_break; } sz=rhnlen(rhn); if (!(sla=DA_GROW1_F(sla,free_slist_domain))) { print_serr(rs,"Out of memory."); goto free_names_break; } sl=&DA_LAST(sla); if (!(sl->domain=malloc(sz))) { print_serr(rs,"Out of memory."); goto free_sla_names_break; } memcpy(sl->domain,rhn,sz); sl->exact=0; sl->rule=tp; p = q+1; } } if(empty_cache(sla)) print_succ(rs); else print_serr(rs,"Could not lock the cache."); free_sla_names_break: free_slist_array(sla); free_names_break: free(names); } break; case CTL_DUMP: { int rv,exact=0; unsigned char *nm=NULL; char buf[DNSNAMEBUFSIZE]; unsigned char rhn[DNSNAMEBUFSIZE]; DEBUG_MSG("Received DUMP command.\n"); if (!(rv=read_domain(rs,buf,sizeof(buf)))) { print_serr(rs,"Bad domain name."); break; } if(rv>0) { int sz; exact=1; nm= ucharp buf; sz=sizeof(buf); if(buf[0]=='.' && buf[1]) { exact=0; ++nm; --sz; } if ((errmsg=parsestr2rhn(nm,sz,rhn))!=NULL) goto bad_domain_name; nm=rhn; } if(!print_succ(rs)) break; if((rv=dump_cache(rs,nm,exact))<0 || (!rv && fsprintf(rs,"Could not find %s%s in the cache.\n", exact?"":nm?"any entries matching ":"any entries", nm?buf:"")<0)) { DEBUG_MSG("Error writing to control socket: %s\n",strerror(errno)); } } break; incomplete_command: print_serr(rs,"Malformed or incomplete command."); break; bad_arg: print_serr(rs,"Bad arg."); break; bad_domain_name: print_serr(rs,errmsg); break; bad_ttl: print_serr(rs, "Bad TTL."); break; bad_flags: print_serr(rs, "Bad cache flags."); break; out_of_memory: print_serr(rs,"Out of memory."); break; default: print_serr(rs,"Unknown command."); } } else { DEBUG_MSG("Incorrect magic number in status-socket command code: %02x\n",cmd>>8); print_serr(rs,"Command code contains incompatible version number."); } } else {
int tsin_parse_recur(int start, TSIN_PARSE *out, short *r_match_phr_N, short *r_no_match_ch_N) { int plen; double bestscore = -1; int bestusecount = 0; *r_match_phr_N = 0; *r_no_match_ch_N = tsin_parse_len - start; for(plen=1; start + plen <= tsin_parse_len && plen <= MAX_PHRASE_LEN; plen++) { #if DBG dbg("---- aa st:%d hh plen:%d ", start, plen);utf8_putchar(tss.chpho[start].ch); dbg("\n"); #endif if (plen > 1) { if (tsin_is_gtab) { if (gbuf[start+plen-1].flag & FLAG_CHPHO_PHRASE_USER_HEAD) break; } else if (tss.chpho[start+plen-1].flag & FLAG_CHPHO_PHRASE_USER_HEAD) break; } phokey_t pp[MAX_PHRASE_LEN + 1]; u_int pp32[MAX_PHRASE_LEN + 1]; u_int64_t pp64[MAX_PHRASE_LEN + 1]; int sti, edi; TSIN_PARSE pbest[MAX_PH_BF_EXT+1]; #define MAXV 1000 int maxusecount = 5-MAXV; int remlen; short match_phr_N=0, no_match_ch_N = plen; void *ppp; if (ph_key_sz==2) ppp=pp; else if (ph_key_sz==4) ppp=pp32; else ppp=pp64; bzero(pbest, sizeof(TSIN_PARSE) * tsin_parse_len); pbest[0].len = plen; pbest[0].start = start; int i, ofs; if (tsin_is_gtab) for(ofs=i=0; i < plen; i++) ofs += utf8cpy((char *)pbest[0].str + ofs, gbuf[start + i].ch); else for(ofs=i=0; i < plen; i++) ofs += utf8cpy((char *)pbest[0].str + ofs, tss.chpho[start + i].ch); #if DBG dbg("st:%d hh plen:%d ", start, plen);utf8_putchar(tss.chpho[start].ch); dbg("\n"); #endif if (tsin_is_gtab) extract_gtab_key(start, plen, ppp); else { extract_pho(start, plen, (phokey_t *)ppp); if (c_pinyin_set) mask_tone(pp, plen, c_pinyin_set + start); } #if DBG for(i=0; i < plen; i++) { prph(pp[i]); dbg("%d", c_pinyin_set[i+start]); } dbg("\n"); #endif char *pinyin_set = c_pinyin_set ? c_pinyin_set+start:NULL; if (!tsin_seek(ppp, plen, &sti, &edi, pinyin_set)) { // dbg("tsin_seek not found...\n"); if (plen > 1) break; goto next; } phokey_t mtk[MAX_PHRASE_LEN]; u_int mtk32[MAX_PHRASE_LEN]; u_int64_t mtk64[MAX_PHRASE_LEN]; void *pho; if (ph_key_sz==2) pho=mtk; else if (ph_key_sz==4) pho=mtk32; else pho=mtk64; for (;sti < edi; sti++) { char mtch[MAX_PHRASE_LEN*CH_SZ+1]; char match_len; usecount_t usecount; load_tsin_entry(sti, &match_len, &usecount, pho, (u_char *)mtch); if (match_len < plen) continue; if (tsin_is_gtab) { if (check_gtab_fixed_mismatch(start, mtch, plen)) continue; } else if (check_fixed_mismatch(start, mtch, plen)) continue; if (usecount < 0) usecount = 0; int i; if (ph_key_sz==2) { if (c_pinyin_set) { // mask_tone(pp, plen, c_pinyin_set + start); mask_tone(mtk, plen, c_pinyin_set + start); } for(i=0;i < plen;i++) if (mtk[i]!=pp[i]) break; } else if (ph_key_sz==4) { for(i=0;i < plen;i++) if (mtk32[i]!=pp32[i]) break; } else { for(i=0;i < plen;i++) if (mtk64[i]!=pp64[i]) break; } if (i < plen) continue; if (match_len > plen) { continue; } if (usecount <= maxusecount) continue; pbest[0].len = plen; maxusecount = usecount; utf8cpyN((char *)pbest[0].str, mtch, plen); pbest[0].flag |= FLAG_TSIN_PARSE_PHRASE; match_phr_N = 1; no_match_ch_N = 0; #if DBG utf8_putcharn(mtch, plen); dbg(" plen %d usecount:%d ", plen, usecount); utf8_putcharn(mtch, plen); dbg("\n"); #endif } next: #if 0 if (!match_phr_N) { if (tsin_is_gtab) { if (!(gbuf[start].ch[0] & 0x80)) no_match_ch_N = 0; } else if (!(tss.chpho[start].ch[0] & 0x80)) no_match_ch_N = 0; } #else // dbg("no_match_ch_N %d\n", no_match_ch_N); #endif remlen = tsin_parse_len - (start + plen); if (remlen) { int next = start + plen; CACHE *pca; short smatch_phr_N, sno_match_ch_N; int uc; if ((pca = cache_lookup(next))) { uc = pca->usecount; smatch_phr_N = pca->match_phr_N; sno_match_ch_N = pca->no_match_ch_N; memcpy(&pbest[1], pca->best, (tsin_parse_len - next) * sizeof(TSIN_PARSE)); } else { uc = tsin_parse_recur(next, &pbest[1], &smatch_phr_N, &sno_match_ch_N); // dbg(" gg %d\n", smatch_phr_N); add_cache(next, uc, &pbest[1], smatch_phr_N, sno_match_ch_N, tsin_parse_len); } match_phr_N += smatch_phr_N; no_match_ch_N += sno_match_ch_N; maxusecount += uc; } double score = log((double)maxusecount + MAXV) / (pow((double)match_phr_N, 10)+ 1.0E-6) / (pow((double)no_match_ch_N, 20) + 1.0E-6); #if DBG dbg("st:%d plen:%d zz muse:%d ma:%d noma:%d score:%.4e %.4e\n", start, plen, maxusecount, match_phr_N, no_match_ch_N, score, bestscore); #endif if (score > bestscore) { #if DBG dbg("is best org %.4e\n", bestscore); #endif bestscore = score; memcpy(out, pbest, sizeof(TSIN_PARSE) * (tsin_parse_len - start)); #if DBG dbg(" str:%d ", start); int i; for(i=0; i < tsin_parse_len - start; i++) { utf8_putcharn((char *)out[i].str, out[i].len); } dbg("\n"); #endif bestusecount = maxusecount; *r_match_phr_N = match_phr_N; *r_no_match_ch_N = no_match_ch_N; } } if (bestusecount < 0) bestusecount = 0; return bestusecount; }
/* Reads SIZE bytes from INODE into BUFFER, starting at position OFFSET. Returns the number of bytes actually read, which may be less than SIZE if an error occurs or end of file is reached. */ off_t inode_read_at (struct inode *inode, void *buffer_, off_t size, off_t offset) { if( inode->sector > 20000) { printf("inode : %#x node[%#x]->extension=%d \n", inode,inode->sector, inode->extension); ASSERT(inode->sector < 20000); } uint8_t *buffer = buffer_; off_t bytes_read = 0; uint8_t *bounce = NULL; while(inode->extension); while (size > 0) { /* Disk sector to read, starting byte offset within sector. */ block_sector_t sector_idx = byte_to_sector (inode, offset); int sector_ofs = offset % BLOCK_SECTOR_SIZE; /* Bytes left in inode, bytes left in sector, lesser of the two. */ off_t inode_left = inode_length (inode) - offset; int sector_left = BLOCK_SECTOR_SIZE - sector_ofs; int min_left = inode_left < sector_left ? inode_left : sector_left; /* Number of bytes to actually copy out of this sector. */ int chunk_size = size < min_left ? size : min_left; if (chunk_size <= 0) break; struct cache * cache = find_cache(sector_idx); if(cache == NULL) { cache = add_cache(sector_idx); if(cache == NULL) { printf("offset : %d inode : %d inode_sector : %d\n",offset,inode,inode->sector); printf("read Error\n"); ASSERT(false); return bytes_read; } } cache->accessed = true; cache->pin = true; read_cache(cache,buffer+bytes_read,sector_ofs,chunk_size); // if(inode_left > BLOCK_SECTOR_SIZE) // read_ahead(byte_to_sector(inode,offset + sector_left + 1)); cache->pin = false; /* if (sector_ofs == 0 && chunk_size == BLOCK_SECTOR_SIZE) { */ /* Read full sector directly into caller's buffer. */ /* block_read (fs_device, sector_idx, buffer + bytes_read); } else { */ /* Read sector into bounce buffer, then partially copy into caller's buffer. */ /* if (bounce == NULL) { bounce = malloc (BLOCK_SECTOR_SIZE); if (bounce == NULL) break; } block_read (fs_device, sector_idx, bounce); memcpy (buffer + bytes_read, bounce + sector_ofs, chunk_size); }*/ /* Advance. */ size -= chunk_size; offset += chunk_size; bytes_read += chunk_size; } //free (bounce); return bytes_read; }
/* Writes SIZE bytes from BUFFER into INODE, starting at OFFSET. Returns the number of bytes actually written, which may be less than SIZE if end of file is reached or an error occurs. (Normally a write at end of file would extend the inode, but growth is not yet implemented.) */ off_t inode_write_at (struct inode *inode, const void *buffer_, off_t size, off_t offset) { const uint8_t *buffer = buffer_; off_t bytes_written = 0; uint8_t *bounce = NULL; /* Debugging */ bool debug = false; if (inode->deny_write_cnt) return 0; //printf("inode_length : %d\n",inode_length(inode)); ASSERT(inode->sector <20000); //printf("*node[%d]->extension=%d \n", inode->sector, inode->extension); while(inode->extension); //printf("inode_write_at called size: %d, offset : %d, inode_length : %d\n",size,offset,inode->data.length); while (size > 0) { /* Sector to write, starting byte offset within sector. */ block_sector_t sector_idx = byte_to_sector (inode, offset); int sector_ofs = offset % BLOCK_SECTOR_SIZE; /* Bytes left in inode, bytes left in sector, lesser of the two. */ off_t inode_left = inode_length (inode) - offset; int sector_left = BLOCK_SECTOR_SIZE - sector_ofs; int min_left = inode_left < sector_left ? inode_left : sector_left; /* Number of bytes to actually write into this sector. */ int chunk_size = size < min_left ? size : min_left; struct cache * cache = find_cache(sector_idx); //if(debug) printf("sector_idx : %d\n",sector_idx); if(offset > inode_length(inode)) { //printf("setting node[%d]->extension=true \n", inode->sector); inode->extension = true; //printf("You made me true\n"); int left = (offset - inode_length(inode)); int i; int sectors = (left+1)/BLOCK_SECTOR_SIZE; //printf("Offset Over\n"); block_sector_t new_sector; int check = inode_length(inode) + BLOCK_SECTOR_SIZE - inode_length(inode)%512+1; inode->data.length += left; block_write(fs_device,inode->sector,&(inode->data)); //printf("offset inode length increase from :%d to %d sector num : %d check : %d\n",inode_length(inode),inode_length(inode)-left,sectors,check); for(i=0; i<sectors; i++) { //printf("check is : %d\n",check); if(!free_map_allocate(1,&new_sector)) { printf("Fail to allocate freemap\n"); ASSERT(false); } updateBlock(inode,check,new_sector); //printf("Allocate new Sector : %d\n",new_sector); check += BLOCK_SECTOR_SIZE; } //debug = true; continue; } else if((size >= inode_left - offset) && min_left == 0) { int sector_num; int i; //printf("Size Over\n"); if((size-sector_left+1) <=0) sector_num = 0; else sector_num = bytes_to_sectors((size-sector_left)+1); //printf("size : %d , offset : %d, sector_left : %d\n",size,offset,sector_left); block_sector_t new_sector; int check = inode_length(inode) + sector_left ; //printf("setting node[%d]->extension=true \n", inode->sector); inode->extension = true; //printf("You made me true\n"); //file_extension(inode,buffer+bytes_written,size); inode->data.length = inode->data.length + size; block_write(fs_device,inode->sector,&(inode->data)); //printf("Sector_left : %d\n",sector_left); //printf("size inode length increase from :%d to %d now sector num : %d sector_num : %d\n",inode_length(inode),inode_length(inode)-size,sector_idx,sector_num); for(i=0; i<sector_num; i++) { if(!free_map_allocate(1,&new_sector)) { printf("Freemap Fail\n"); ASSERT(false); } updateBlock(inode,check,new_sector); //printf("Allocate new Sector : %d\n",new_sector); // print_table_info(inode); check +=BLOCK_SECTOR_SIZE; } debug = true; continue; } else if(sector_idx == -1) { print_table_info(inode); //printf("offset : %d inode : %#x inode_sector : %d length : %d\n",offset,inode,inode->sector,inode_length(inode)); } if(debug) { } //ASSERT(sector_idx < 20000); if (chunk_size <= 0) break; /* if(debug) { print_table_info(inode); } */ //printf("inode read at called chunksize : %d , sector_ofs : %d, sector_idx : %d\n",chunk_size,sector_ofs,sector_idx); if(cache == NULL) { cache = add_cache(sector_idx); if(cache == NULL) { print_table_info(inode); printf("read Error\n"); return bytes_written; } } cache->accessed = true; cache->pin = true; //if(inode_left - sector_ofs > 512) // read_ahead(sector_idx +1); write_cache(cache , buffer+bytes_written , sector_ofs ,chunk_size); /* Advance. */ cache->pin = false; size -= chunk_size; offset += chunk_size; bytes_written += chunk_size; } // free (bounce); if(inode->extension == true) { //printf("set node[%d]->extension as false\n", inode->sector); inode->extension = false; ASSERT(inode->extension==false); } return bytes_written; }
int keyzen_dbus_get_pid_of_name(DBusConnection *connection, const char *name, pid_t *pid) { int result; DBusError error; DBusMessage *message, *reply; dbus_bool_t b; dbus_uint32_t u32; assert(connection); assert(name); assert(pid); /* look at cache */ if (read_cache(name, pid)) return 0; /* create the message */ message = dbus_message_new_method_call( DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS, get_pid_method); if (message == NULL) { result = -ENOMEM; /* can't create the message */ } else { /* fulfill the message with the name */ b = dbus_message_append_args( message, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID); if (b != TRUE) { result = -ENOMEM; /* can't fullfill the message */ } else { dbus_error_init(&error); reply = dbus_connection_send_with_reply_and_block( connection, message, timeout, &error); if (!reply) { result = -ENOTSUP; /* got no reply */ } else { if (dbus_message_get_type(reply) != DBUS_MESSAGE_TYPE_METHOD_RETURN) { result = -ENOTSUP; /* unexpected answer kind */ } else { dbus_error_init(&error); b = dbus_message_get_args( reply, &error, DBUS_TYPE_UINT32, &u32, DBUS_TYPE_INVALID); if (b != TRUE) { result = -ENOTSUP; /* unexpected argument types */ } else { *pid = (pid_t)u32; result = 0; add_cache(name, (pid_t)u32); } } dbus_message_unref(reply); } } dbus_message_unref(message); } return result; }
HttpMessage RequestHandler :: handleRequest( HttpMessage& request ){ HttpMessage response; std::string body, path, ckey; handler_function func = NULL; size_t pattern_length = 0; uint64_t cache_key; std::unordered_map<uint64_t, CachedEntry>::iterator cache_it; path = request.get_path(); response.set_version( "HTTP/1.1" ); response.set_header_field( "Server", SERVER_VERSION ); response.set_header_field( "Content-Type", "text/html" ); response.set_header_field( "Date", get_gmt_time(0) ); /* ckey = request.get_method() + ":" + path; cache_key = fnv1a_hash( ckey ); */ cache_key = compute_hash( request ); pthread_rwlock_rdlock( &RequestHandler::cache_lock ); cache_it = cache.find( cache_key ); if( cache_it != cache.end() ){ if( (cache_it->second).is_valid() ){ pthread_rwlock_unlock( &RequestHandler::cache_lock ); //std::cerr << "Found @ cache" << std::endl; return (cache_it->second).get_content(); } } pthread_rwlock_unlock( &RequestHandler::cache_lock ); for( std::map< std::string, handler_function >::iterator it = handlers.begin() ; it != handlers.end() ; it++ ){ if( string_startswith( it->first, path ) ){ if( (it->first).size() > pattern_length ){ pattern_length = (it->first).size(); func = it->second; } } } if( func == NULL ){ response.set_code( 404 ); response.set_code_string( "Not found." ); response.set_version( "HTTP/1.1" ); response.set_header_field( "Connection", "close" ); response.set_header_field( "Date", get_gmt_time(0) ); response.set_header_field( "Server", SERVER_VERSION ); response.set_header_field( "Last-Modified", get_gmt_time(0) ); response.set_header_field( "Content-Type", "text/plain" ); response.set_body( "Error 404 - Not found!\n" ); } else{ response_options_t result; try{ result = func( request, response ); } catch( std::string error ){ response.set_code( 500 ); response.set_code_string( "Internal server error." ); } if( !result.ok ){ response.set_code( 404 ); response.set_code_string( "Not found." ); response.set_version( "HTTP/1.1" ); response.set_header_field( "Connection", "close" ); response.set_header_field( "Date", get_gmt_time(0) ); response.set_header_field( "Server", SERVER_VERSION ); response.set_header_field( "Last-Modified", get_gmt_time(0) ); response.set_header_field( "Content-Type", "text/plain" ); response.set_body( "Error 404 - Not found!\n" ); } else{ if( request.has_header_field( "Accept-Encoding" ) ){ if( request.get_header_field( "Accept-Encoding" ).find( "gzip" ) != std::string::npos ){ response.set_body( zlib_gzip_deflate( response.get_body() ) ); response.set_header_field( "Content-Encoding", "gzip" ); } else{ if( request.get_header_field( "Accept-Encoding" ).find( "deflate" ) != std::string::npos ){ response.set_body( zlib_deflate( response.get_body() ) ); response.set_header_field( "Content-Encoding", "deflate" ); } } } if( result.cached ){ add_cache( cache_key, response, result.expires ); } } } return response; }
static int32_t cacheex_add_to_cache_int(struct s_client *cl, ECM_REQUEST *er, int8_t csp) { if(er->rc >= E_NOTFOUND) { return 0; } if(!cl) { return 0; } if(!csp && cl->reader && cl->reader->cacheex.mode != 2) //from reader { cs_log_dbg(D_CACHEEX, "CACHEX received, but disabled for %s", username(cl)); return 0; } if(!csp && !cl->reader && cl->account && cl->account->cacheex.mode != 3) //from user { cs_log_dbg(D_CACHEEX, "CACHEX received, but disabled for %s", username(cl)); return 0; } if(!csp && !cl->reader && !cl->account) //not active! { cs_log_dbg(D_CACHEEX, "CACHEX received, but invalid client state %s", username(cl)); return 0; } if(!cfg.disablecrccws && ((cl->typ == 'c' && !cl->account->disablecrccacheex) || ( cl->typ == 'p' && !cl->reader->disablecrccws))) { uint8_t selectedForIgnChecksum = chk_if_ignore_checksum(er, &cfg.disablecrccws_only_for); if(cl->typ == 'c') { selectedForIgnChecksum += chk_if_ignore_checksum(er, &cl->account->disablecrccacheex_only_for); } if(cl->typ == 'p') { selectedForIgnChecksum += chk_if_ignore_checksum(er, &cl->reader->disablecrccws_only_for); } if(!selectedForIgnChecksum) { uint8_t i, c; for(i = 0; i < 16; i += 4) { c = ((er->cw[i] + er->cw[i + 1] + er->cw[i + 2]) & 0xff); if(er->cw[i + 3] != c) { cs_log_dump_dbg(D_CACHEEX, er->cw, 16, "push received cw with chksum error from %s", csp ? "csp" : username(cl)); cl->cwcacheexerr++; if(cl->account) { cl->account->cwcacheexerr++; } return 0; } } } } // Skip check for BISS1 - cw could be indeed zero // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero if(chk_is_null_CW(er->cw) && !caid_is_biss(er->caid)) { cs_log_dump_dbg(D_CACHEEX, er->cw, 16, "push received null cw from %s", csp ? "csp" : username(cl)); cl->cwcacheexerr++; if(cl->account) { cl->account->cwcacheexerr++; } return 0; } // Don't check for BISS1 and BISS2 mode 1/E or fake caid (ECM is fake for them) // Don't check for BISS2 mode CA (ECM table is always 0x80) if(!caid_is_biss(er->caid) && !caid_is_fake(er->caid) && get_odd_even(er) == 0) { cs_log_dbg(D_CACHEEX, "push received ecm with null odd/even byte from %s", csp ? "csp" : username(cl)); cl->cwcacheexerr++; if(cl->account) { cl->account->cwcacheexerr++; } return 0; } if(!chk_halfCW(er, er->cw)) { log_cacheex_cw(er, "bad half cw"); cl->cwcacheexerr++; if(cl->account) { cl->account->cwcacheexerr++; } return 0; } if((csp && cfg.csp.block_fakecws) || (cl->reader && cl->reader->cacheex.block_fakecws) || (!cl->reader && cl->account && cl->account->cacheex.block_fakecws)) { if(chk_is_fakecw(er->cw)) { cs_log_dbg(D_CACHEEX, "push received fake cw from %s", csp ? "csp" : username(cl)); cl->cwcacheexerr++; if(cl->account) { cl->account->cwcacheexerr++; } return 0; } } er->grp |= cl->grp; // ok for mode2 reader too: cl->reader->grp er->rc = E_CACHEEX; er->cacheex_src = cl; er->selected_reader = cl->reader; er->client = NULL; // No Owner! So no fallback! if(check_client(cl)) { cl->cwcacheexgot++; if(cl->account) { cl->account->cwcacheexgot++; } first_client->cwcacheexgot++; } cacheex_add_hitcache(cl, er); // we have to call it before add_cache, because in chk_process we could remove it! add_cache(er); cacheex_add_stats(cl, er->caid, er->srvid, er->prid, 1); cs_writelock(__func__, &ecm_pushed_deleted_lock); er->next = ecm_pushed_deleted; ecm_pushed_deleted = er; cs_writeunlock(__func__, &ecm_pushed_deleted_lock); return 1; // NO free, we have to wait cache push out stuff ends. }
void cache_object_add(struct cache_object *co, struct strbuf *buf) { size_t size = co->cc.cache_buf.len; add_cache(&co->cc, buf); make_index_entry(&co->ci, size, buf->len); }
/* * serve - handle one HTTP request/response transaction */ void serve(int to_client_fd) { char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char filename[MAXLINE], hostname[MAXLINE]; char port[10]; rio_t rio_to_client; rio_t rio_to_server; int to_server_fd; /* Read request line and headers */ Rio_readinitb(&rio_to_client, to_client_fd); Rio_readlineb(&rio_to_client, buf, MAXLINE); sscanf(buf, "%s %s %s", method, uri, version); /* if not GET method, ignore it */ if (strcasecmp(method, "GET")) { return; } /* Parse URI from GET request */ parse_uri(uri, hostname, port, filename); // strange display error in csapp page if (strcasecmp(hostname, "csapp.cs.cmu.edu") == 0) { return; } sprintf(buf, "%s %s %s\r\n", "GET", filename, "HTTP/1.0"); /* search content in cache list */ struct cache_block* ptr = search_cache(head, uri); /* cache found, directly send to client */ if (ptr) { // add the number of existing threads reading add_reading_cnt(ptr); // send cache to client Rio_writen(to_client_fd, ptr->file, ptr->size); // subtract the number of existing threads reading sub_reading_cnt(ptr); // update timestamp and reorder LRU list update_timestamp(head, ptr); return; } /* cache not found, connect with server */ to_server_fd = Open_clientfd(hostname, port); Rio_readinitb(&rio_to_server, to_server_fd); // send request line: GET HTTP/1.0 Rio_writen(to_server_fd, buf, strlen(buf)); // send host to server sprintf(buf, "Host: %s\r\n", hostname); Rio_writen(to_server_fd, buf, strlen(buf)); /* read http headers from client and write to server */ Rio_readlineb(&rio_to_client, buf, MAXLINE); while (strncmp(buf, "\r\n", MAXLINE) != 0) { if (strstr(buf, "User-Agent")) strncpy(buf, user_agent_hdr, MAXLINE); else if (strstr(buf, "Connection")) strncpy(buf, "Connection: close\r\n", MAXLINE); else if (strstr(buf, "Proxy-Connection")) strncpy(buf, "Proxy-Connection: close\r\n", MAXLINE); else if (strstr(buf, "Host")) { // ignore, because we already sent one Rio_readlineb(&rio_to_client, buf, MAXLINE); continue; } Rio_writen(to_server_fd, buf, strlen(buf)); Rio_readlineb(&rio_to_client, buf, MAXLINE); } /* terminates request headers */ Rio_writen(to_server_fd, "\r\n", 2); /* read http response from server and write to client */ memset(buf, 0, MAXLINE); Rio_readlineb(&rio_to_server, buf, MAXLINE); int content_len = 0; while (strncmp(buf, "\r\n", MAXLINE) != 0) { char* ptr = strstr(buf, "Content-length"); if (ptr) { content_len = atoi(ptr + 16); } //printf("write2: %s\n", uri); //printf("fd: %d\n", to_client_fd); //printf("%d: %s\n", strlen(buf), buf); Rio_writen(to_client_fd, buf, strlen(buf)); memset(buf, 0, MAXLINE); Rio_readlineb(&rio_to_server, buf, MAXLINE); } /* terminates response headers */ Rio_writen(to_client_fd, "\r\n", 2); int size = 0; int need_cache = 0; /* shall we cache it? */ if (content_len < MAX_OBJECT_SIZE) need_cache = 1; /* init a new cache block */ struct cache_block* blk = (struct cache_block*) malloc(sizeof(struct cache_block)); init_cache(blk); strncpy(blk->uri, uri, MAXLINE); if (content_len > 0) blk->file = (char*) malloc(sizeof(char) * content_len); else blk->file = (char*) malloc(sizeof(char) * MAX_OBJECT_SIZE); memset(buf, 0, MAXLINE); int total_size = 0; char* headptr = blk->file; /* read response contents and write to client */ while ((size = Rio_readnb(&rio_to_server, buf, MAXLINE)) > 0) { total_size += size; if (total_size > MAX_OBJECT_SIZE) need_cache = 0; if (need_cache) { memcpy(headptr, buf, size); headptr += size; } //printf("write3: %s\n", uri); Rio_writen(to_client_fd, buf, size); memset(buf, 0, MAXLINE); } /* add cache block */ if (need_cache) { blk->size = total_size; // resize block blk->file = (char*) realloc(blk->file, blk->size); // size overflow, need to evict using LRU if (blk->size + cache_size > MAX_CACHE_SIZE) evict_cache(head, blk->size); // add it add_cache(head, blk); } /* prevent memory leakage */ else { free_cache_node(blk); } Close(to_server_fd); return; }