/********************************************************************* * Process message Functions * *********************************************************************/ void process_arp_request(int sockfd, struct arp_msg *msg) { int i; struct arp_cache_entry cache_entry; bzero(&cache_entry, sizeof(cache_entry)); cache_entry.connfd = sockfd; cache_entry.sll_ifindex = my_index; cache_entry.sll_hatype = msg->hrd; strcpy(cache_entry.ip, msg->sender_ip); for (i = 0; i < IF_HADDR; i++){ cache_entry.mac[i] = msg->sender_mac[i]; } cache_entry.isComplete = 1; cache_entry.isValid = 1; cache_entry.isLocal = 0; // Pertains to the request, insert new entry if (strcmp(my_ip, msg->target_ip) == 0 || isDest(msg->target_ip) ) { puts("----->ARP: DESTINATION."); if ((i = search_cache(msg->sender_ip) == -1)) { puts("----->ARP: I DO pertain to this request. Create new entry for this <senderIP, senderMAC>.\n"); if (insert_entry(cache_entry) < 0) { perror("unable to insert cache entry."); } } else { puts("----->ARP: I DO pertain to this request. Update <senderIP, senderMAC>.\n"); update_cache(i, msg); } // Send ARP reply msg->op = ARP_REP; for (i = 0; i < INET_ADDRSTRLEN; i ++) { msg->target_ip[i] = msg->sender_ip[i]; } printf("Target ip:%s\n", msg->target_ip); for (i = 0; i < IF_HADDR; i++){ msg->target_mac[i] = msg->sender_mac[i]; } strcpy(msg->sender_ip, my_ip); for (i = 0; i < IF_HADDR; i++){ msg->sender_mac[i] = if_hwaddr[my_index][i]; } puts("----->ARP: I'm responsible for sending reply..."); send_arp_msg(sockfd, msg->target_mac, msg); puts("----->ARP: Reply sent.\n"); } else if ((i = search_cache(msg->sender_ip) != -1)) { // There is an existing entry, update puts("----->ARP: I'm not pertain to this request, but I have an existing entry, updating...\n"); update_cache(i, msg); } }
void process_arp_reply(int sockfd, struct arp_msg *msg) { int i, j; struct hwaddr *hwaddr_info = malloc(sizeof(struct hwaddr)); bzero(hwaddr_info, sizeof(struct hwaddr)); puts("----->ARP: Processing ARP reply..."); if ((i = search_cache(msg->sender_ip)) == -1) { printf("----->ARP: No valid entry for: %s.\n", msg->sender_ip); } else { printf("----->ARP: Complete entry %d.\n", i); if (cache[i].isValid == 1) { cache[i].connfd = un_connfd; for (j = 0; j < IF_HADDR; j++){ cache[i].mac[j] = msg->sender_mac[j]; hwaddr_info->sll_addr[j] = cache[i].mac[j]; } hwaddr_info->sll_halen = HARD_SIZE; hwaddr_info->sll_hatype = cache[i].sll_hatype; hwaddr_info->sll_ifindex = cache[i].sll_ifindex; strcpy(hwaddr_info->sll_ip, cache[i].ip); if (cache[i].isComplete == 0) { cache[i].isComplete = 1; puts("----->ARP: Sending back hwadr..."); send_un(cache[i].connfd, hwaddr_info); close(cache[i].connfd); un_connfd = -1; puts("----->ARP: Reply proceeded, close connection.\n"); } } } }
char *file_cache_mutable_file_data(file_cache *cache, const char *file){ printf("\n File to be changed is %s",file); file_cache *search_val = search_cache(cache,file); // If file exist in cache and already dirty , flush contents to storage before writing new content. if(search_val && search_val->pin && search_val->dirty){ write_to_file(search_val); } // write to cache when entry exist in cache and is pinned and is not dirty already else if(search_val && search_val->pin && !search_val->dirty){ printf("\n File %s exist in cache.\n",file); printf("\n Copy file_content to buffer \n"); strcpy(search_val->buf,file_content); } // File doesnt exist in cache. Add it to cache and update buffer of file_cache object. else { #ifdef DEBUG printf("\n File not found in cache. Adding it to cache. \n"); fflush(stdout); #endif /* DEBUG */ search_val = add_to_cache(file); strcpy(search_val->buf,file_content); } search_val->dirty = 1; // make dirty bit to 1 to indicate that buffer has some data which is not yet written to disk. return search_val->buf; }
void _tnl_UpdateFixedFunctionProgram( GLcontext *ctx ) { TNLcontext *tnl = TNL_CONTEXT(ctx); struct state_key *key; GLuint hash; const struct gl_vertex_program *prev = ctx->VertexProgram._Current; if (!ctx->VertexProgram._Current || ctx->VertexProgram._Current == ctx->VertexProgram._TnlProgram) { struct gl_vertex_program *newProg; /* Grab all the relevent state and put it in a single structure: */ key = make_state_key(ctx); hash = hash_key(key); /* Look for an already-prepared program for this state: */ newProg = search_cache( tnl->vp_cache, hash, key, sizeof(*key)); /* OK, we'll have to build a new one: */ if (!newProg) { if (0) _mesa_printf("Build new TNL program\n"); newProg = (struct gl_vertex_program *) ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0); create_new_program( key, newProg, ctx->Const.VertexProgram.MaxTemps ); if (ctx->Driver.ProgramStringNotify) ctx->Driver.ProgramStringNotify( ctx, GL_VERTEX_PROGRAM_ARB, &newProg->Base ); /* Our ownership of newProg is transferred to the cache */ cache_item(ctx, tnl->vp_cache, hash, key, newProg); } else { FREE(key); } _mesa_reference_vertprog(ctx, &ctx->VertexProgram._TnlProgram, newProg); _mesa_reference_vertprog(ctx, &ctx->VertexProgram._Current, newProg); } /* Tell the driver about the change. Could define a new target for * this? */ if (ctx->VertexProgram._Current != prev && ctx->Driver.BindProgram) { ctx->Driver.BindProgram(ctx, GL_VERTEX_PROGRAM_ARB, (struct gl_program *) ctx->VertexProgram._Current); } }
int cache_check(char *URL, int accepted_client){ cache_t *cache_entry; if((cache_entry = search_cache(&cache, URL)) == NULL){ printf("Entry not in cache\n\n"); return 0; } else { printf("entry in cache\n\n"); enforce_LRU_middle(&cache, URL); send_data(accepted_client, cache_entry->content); return 1; } return 0; }
int isDest(char ip[]) { int i; int isDest = -1; if ((i = search_cache(ip)) != -1) { // if (strcmp(cache[i].mac, if_hwaddr[my_index]) == 0) { if (memcmp(cache[i].mac, if_hwaddr[my_index], ETH_ALEN) == 0) { puts("****DESTINATION******"); isDest = 1; } } return isDest; }
void process_un(int sockfd, struct hwaddr *hwaddr_info) { int index; int i; // puts("----->ARP:processing domain sockets..."); if ((index = search_cache(hwaddr_info->sll_ip)) == -1) { puts("----->ARP: domain sockets: no cache entry found..."); struct arp_cache_entry cache_entry; struct arp_msg msg; bzero(&msg, sizeof(msg)); puts("----->ARP: domain sockets: create a new incomplete entry..."); // Insert a temp entry cache_entry.connfd = sockfd; cache_entry.sll_ifindex = my_index; cache_entry.sll_hatype = hwaddr_info->sll_hatype; strcpy(cache_entry.ip, hwaddr_info->sll_ip); cache_entry.isComplete = 0; cache_entry.isValid = 1; cache_entry.isLocal = 0; if (insert_entry(cache_entry) < 0) { perror("unable to insert cache entry."); } puts("----->ARP: domain sockets: broadcast ARP_REQ..."); // Send ARP request msg.op = ARP_REQ; strcpy(msg.sender_ip, my_ip); strcpy(msg.target_ip, hwaddr_info->sll_ip); msg.id = htons(ARP_ID); send_arp_msg(if_sockfd, broadcast_mac, &msg); } else { puts("----->ARP: domain sockets: existing entry found..."); if (cache[index].isComplete == 1){ cache[index].connfd = sockfd; for (i = 0; i < ETH_ALEN; i++){ hwaddr_info->sll_addr[i] = cache[index].mac[i]; } // strcpy(hwaddr_info->sll_addr, cache[index].mac); hwaddr_info->sll_ifindex = cache[index].sll_ifindex; puts("----->ARP: domain sockets: sending reply through domain socket..."); send_un(cache[index].connfd, hwaddr_info); close(cache[index].connfd); un_connfd = -1; puts("----->ARP: domain sockets: sending OK, connection closed."); } } puts("----->ARP:domain sockets processed..."); }
/* Calculate interpolants for triangle and line rasterization. */ static void upload_gs_prog( struct brw_context *brw ) { struct brw_gs_prog_key key; /* Populate the key: */ populate_key(brw, &key); if (brw->gs.prog_active != key.need_gs_prog) { brw->state.dirty.cache |= CACHE_NEW_GS_PROG; brw->gs.prog_active = key.need_gs_prog; } if (brw->gs.prog_active) { if (!search_cache(brw, &key)) compile_gs_prog( brw, &key ); } }
/******************************************************************* * Adds entry to ARP Cache if not already in Cache. Also deletes any entries that are past * their expiration time. *******************************************************************/ void add_cache_entry(struct packet_state* ps,const uint32_t ip, const unsigned char* mac) { if(search_cache(ps, ip)==NULL) /*Entry is not already in cache so add. */ { struct arp_cache_entry* cache_walker=0; assert(ps); assert(mac); assert(ip); if(ps->sr->arp_cache ==0) /*If there are no entries in cache */ { ps->sr->arp_cache=(struct arp_cache_entry*)malloc(sizeof(struct arp_cache_entry)); assert(ps->sr->arp_cache); ps->sr->arp_cache->next=0; ps->sr->arp_cache->ip_add=ip; memmove(ps->sr->arp_cache->mac, mac,ETHER_ADDR_LEN); ps->sr->arp_cache->timenotvalid=time(NULL) +ARP_TIMEOUT; } else { cache_walker = ps->sr->arp_cache; while(cache_walker->next) { if(cache_walker->timenotvalid < time(NULL)) { cache_walker = delete_entry(ps,cache_walker); } else { cache_walker=cache_walker->next; } } cache_walker->next=(struct arp_cache_entry*)malloc(sizeof(struct arp_cache_entry)); assert(cache_walker->next); cache_walker=cache_walker->next; cache_walker->ip_add=ip; memmove(cache_walker->mac, mac,ETHER_ADDR_LEN); cache_walker->timenotvalid=time(NULL) +ARP_TIMEOUT; cache_walker->next=0; } } }
static void update_tnl_program( struct brw_context *brw ) { GLcontext *ctx = &brw->intel.ctx; struct state_key key; GLuint hash; struct gl_vertex_program *old = brw->tnl_program; /* _NEW_PROGRAM */ if (brw->attribs.VertexProgram->_Enabled) return; /* Grab all the relevent state and put it in a single structure: */ make_state_key(ctx, &key); hash = hash_key(&key); /* Look for an already-prepared program for this state: */ brw->tnl_program = (struct gl_vertex_program *) search_cache( &brw->tnl_program_cache, hash, &key, sizeof(key) ); /* OK, we'll have to build a new one: */ if (!brw->tnl_program) { brw->tnl_program = (struct gl_vertex_program *) ctx->Driver.NewProgram(ctx, GL_VERTEX_PROGRAM_ARB, 0); build_new_tnl_program( &key, brw->tnl_program, /* ctx->Const.MaxVertexProgramTemps */ 32 ); if (ctx->Driver.ProgramStringNotify) ctx->Driver.ProgramStringNotify( ctx, GL_VERTEX_PROGRAM_ARB, &brw->tnl_program->Base ); cache_item( &brw->tnl_program_cache, hash, &key, brw->tnl_program ); } if (old != brw->tnl_program) brw->state.dirty.brw |= BRW_NEW_TNL_PROGRAM; }
static void draw_page(int i, float x, float y, float a) { int j, n = terrain[i].n; /* Search for this page in the page cache. */ if ((j = search_cache(i, x, y, a)) == -1) { struct page *cache = terrain[i].cache; int prev = cache[terrain[i].tail].prev; /* This is a new page. Promote the oldest cache line. */ cache[cache[terrain[i].tail].prev].next = -1; cache[terrain[i].head].prev = terrain[i].tail; cache[terrain[i].tail].next = terrain[i].head; cache[terrain[i].tail].prev = -1; terrain[i].head = terrain[i].tail; terrain[i].tail = prev; /* Initialize this cache line's VBO to the given area. */ j = terrain[i].head; load_scratch(i, x, y, a); terrain[i].cache[j].x = x; terrain[i].cache[j].y = y; terrain[i].cache[j].a = a; glBindBufferARB(GL_ARRAY_BUFFER_ARB, terrain[i].cache[j].vbo); glBufferDataARB(GL_ARRAY_BUFFER_ARB, (n + 1) * (n + 1) * sizeof (struct vertex), terrain[i].scratch, GL_STATIC_DRAW_ARB); } draw_data(terrain[i].cache[j].vbo, terrain[i].n); count++; }
void file_cache_unpin_files(file_cache *cache,const char **files,int num_files){ int i; for(i=0;i<num_files;i++){ // Search for the file in the file_cache list file_cache *search_val = search_cache(cache,files[i]); if(search_val){ if(search_val->dirty == 1){ // If its a dirty object , write contents to disk before unpinning. #ifdef DEBUG printf("\n cache dirty. Write contents to file before unpininng \n"); fflush(stdout); #endif /* DEBUG */ write_to_file(search_val); } search_val->pin--; if(search_val->pin == 0 ){ // Remove only when pin becomes 0 , that is no client is accessing file // Code to adjust pointers before freeing the object if(search_val == head){ head = search_val->next; } else if(search_val == tail){ tail = search_val->prev; } else { search_val->prev->next = search_val->next; search_val->next->prev = search_val->prev; } free(search_val); // free file_cache object max_entries--; // reduce max_entries to reflect total no of objects #ifdef DEBUG printf("\n Decreasing max_cache_entries. new value is %d \n",max_entries); fflush(stdout); #endif /* DEBUG */ printf("\n File %s successfully unpinned \n",files[i]); } cache = head; } else { printf("\n File %s not found in cache \n",files[i]); } } }
const char *file_cache_file_data(file_cache *cache, const char *file){ file_cache *search_val = search_cache(cache,file); // Check if file exist in cache. if(search_val && search_val->pin){ #ifdef DEBUG printf("\n File exist in cache \n"); fflush(stdout); #endif /* DEBUG */ return search_val->buf; // If exist read from cache. } else { #ifdef DEBUG printf("\n File not found in cache. Adding it to cache. \n"); fflush(stdout); #endif /* DEBUG */ search_val = add_to_cache(file); // File not found in cache . Add file to cache and read. return search_val->buf; } }
void file_cache_pin_files(file_cache *cache,const char **files,int num_files) { int i; printf("\n Starting file_cache_pin_files \n"); for(i=0;i<num_files;i++) { // Search if the file to file_cache* search_val = search_cache(cache,*(files+i)); if(search_val && search_val->pin){ printf("\n File %s already exist in cache \n",files[i]); //mutex search_val->pin++; } else { #ifdef DEBUG printf("\n File %s not found in cache. Adding it to cache. \n",files[i]); fflush(stdout); #endif /* DEBUG */ search_val = add_to_cache(files[i]); if(search_val){ printf("\n File %s successfully added to cache \n",files[i]); } } cache = head; } }
/******************************************************************* * Adds information from received ARP Reply to ARP Cache and returns newly added ARP Cache entry. *******************************************************************/ struct arp_cache_entry* got_Reply(struct packet_state * ps, struct sr_arphdr * arp) { add_cache_entry(ps, arp->ar_sip, arp->ar_sha); /*Add IP and MAC address from reply to cache */ return search_cache(ps, arp->ar_sip); /*Return the newly added entry. */ }
/* * 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; }
/* Calculate interpolants for triangle and line rasterization. */ static void upload_sf_prog( struct brw_context *brw ) { const struct brw_fragment_program *fs = brw->attribs.FragmentProgram; struct brw_sf_prog_key key; struct tgsi_parse_context parse; int i, done = 0; memset(&key, 0, sizeof(key)); /* Populate the key, noting state dependencies: */ /* CACHE_NEW_VS_PROG */ key.vp_output_count = brw->vs.prog_data->outputs_written; /* BRW_NEW_FS */ key.fp_input_count = brw->attribs.FragmentProgram->info.file_max[TGSI_FILE_INPUT] + 1; /* BRW_NEW_REDUCED_PRIMITIVE */ switch (brw->reduced_primitive) { case PIPE_PRIM_TRIANGLES: // if (key.attrs & (1<<VERT_RESULT_EDGE)) // key.primitive = SF_UNFILLED_TRIS; // else key.primitive = SF_TRIANGLES; break; case PIPE_PRIM_LINES: key.primitive = SF_LINES; break; case PIPE_PRIM_POINTS: key.primitive = SF_POINTS; break; } /* Scan fp inputs to figure out what interpolation modes are * required for each incoming vp output. There is an assumption * that the state tracker makes sure there is a 1:1 linkage between * these sets of attributes (XXX: position??) */ tgsi_parse_init( &parse, fs->program.tokens ); while( !done && !tgsi_parse_end_of_tokens( &parse ) ) { tgsi_parse_token( &parse ); switch( parse.FullToken.Token.Type ) { case TGSI_TOKEN_TYPE_DECLARATION: if (parse.FullToken.FullDeclaration.Declaration.File == TGSI_FILE_INPUT) { int first = parse.FullToken.FullDeclaration.DeclarationRange.First; int last = parse.FullToken.FullDeclaration.DeclarationRange.Last; int interp_mode = parse.FullToken.FullDeclaration.Declaration.Interpolate; //int semantic = parse.FullToken.FullDeclaration.Semantic.SemanticName; //int semantic_index = parse.FullToken.FullDeclaration.Semantic.SemanticIndex; debug_printf("fs input %d..%d interp mode %d\n", first, last, interp_mode); switch (interp_mode) { case TGSI_INTERPOLATE_CONSTANT: for (i = first; i <= last; i++) key.const_mask |= (1 << i); break; case TGSI_INTERPOLATE_LINEAR: for (i = first; i <= last; i++) key.linear_mask |= (1 << i); break; case TGSI_INTERPOLATE_PERSPECTIVE: for (i = first; i <= last; i++) key.persp_mask |= (1 << i); break; default: break; } /* Also need stuff for flat shading, twosided color. */ } break; default: done = 1; break; } } /* Hack: Adjust for position. Optimize away when not required (ie * for perspective interpolation). */ key.persp_mask <<= 1; key.linear_mask <<= 1; key.linear_mask |= 1; key.const_mask <<= 1; debug_printf("key.persp_mask: %x\n", key.persp_mask); debug_printf("key.linear_mask: %x\n", key.linear_mask); debug_printf("key.const_mask: %x\n", key.const_mask); // key.do_point_sprite = brw->attribs.Point->PointSprite; // key.SpriteOrigin = brw->attribs.Point->SpriteOrigin; // key.do_flat_shading = (brw->attribs.Raster->flatshade); // key.do_twoside_color = (brw->attribs.Light->Enabled && brw->attribs.Light->Model.TwoSide); // if (key.do_twoside_color) // key.frontface_ccw = (brw->attribs.Polygon->FrontFace == GL_CCW); if (!search_cache(brw, &key)) compile_sf_prog( brw, &key ); }
char * findname(kvm_t *kd, struct kbit *vmspace, struct vm_map_entry *vme, struct kbit *vp, struct kbit *vfs, struct kbit *uvm_obj) { static char buf[1024], *name; size_t l; if (UVM_ET_ISOBJ(vme)) { if (A(vfs)) { l = strlen(D(vfs, mount)->mnt_stat.f_mntonname); switch (search_cache(kd, vp, &name, buf, sizeof(buf))) { case 0: /* found something */ if (name - (1 + 11 + l) < buf) break; name--; *name = '/'; /*FALLTHROUGH*/ case 2: /* found nothing */ name -= 11; memcpy(name, " -unknown- ", (size_t)11); name -= l; memcpy(name, D(vfs, mount)->mnt_stat.f_mntonname, l); break; case 1: /* all is well */ if (name - (1 + l) < buf) break; name--; *name = '/'; if (l != 1) { name -= l; memcpy(name, D(vfs, mount)->mnt_stat.f_mntonname, l); } break; } } else if (UVM_OBJ_IS_DEVICE(D(uvm_obj, uvm_object))) { struct kbit kdev; dev_t dev; P(&kdev) = P(uvm_obj); S(&kdev) = sizeof(struct uvm_device); KDEREF(kd, &kdev); dev = D(&kdev, uvm_device)->u_device; name = devname(dev, S_IFCHR); if (name != NULL) snprintf(buf, sizeof(buf), "/dev/%s", name); else snprintf(buf, sizeof(buf), " [ device %d,%d ]", major(dev), minor(dev)); name = buf; } else if (UVM_OBJ_IS_AOBJ(D(uvm_obj, uvm_object))) name = " [ uvm_aobj ]"; else if (UVM_OBJ_IS_VNODE(D(uvm_obj, uvm_object))) name = " [ ?VNODE? ]"; else { snprintf(buf, sizeof(buf), " [ unknown (%p) ]", D(uvm_obj, uvm_object)->pgops); name = buf; } } else if (D(vmspace, vmspace)->vm_maxsaddr <= (caddr_t)vme->start && (D(vmspace, vmspace)->vm_maxsaddr + (size_t)maxssiz) >= (caddr_t)vme->end) { name = " [ stack ]"; } else if (D(vmspace, vmspace)->vm_daddr <= (caddr_t)vme->start && D(vmspace, vmspace)->vm_daddr + BRKSIZ >= (caddr_t)vme->end) { name = " [ heap ]"; } else if (UVM_ET_ISHOLE(vme)) name = " [ hole ]"; else name = " [ anon ]"; return (name); }
/******************************************************************* * Every time handle_packet() is called, update_buffer() is called. The function walks through * the packet buffer and checks if the necessary mac address is now in the arp cache. If it is, * then the MAC address is added to the ethernet header and the packet is send and removed * from the buffer. If the address is not in the cache and less than 5 arp requests have already * been sent, then another arp request is sent. Otherwise the packet is deleted from the buffer * and an ICMP port unreachable is sent. *******************************************************************/ void update_buffer(struct packet_state* ps,struct packet_buffer* queue) { struct packet_buffer* buf_walker=0; buf_walker=queue; while(buf_walker) { uint32_t search_ip=buf_walker->gw_IP; struct arp_cache_entry* ent=search_cache(ps, search_ip); if(ent!=NULL) /*MAC Address is in ARP Cache. Send packet. */ { struct sr_ethernet_hdr *eth = (struct sr_ethernet_hdr *)(buf_walker->packet); memmove(eth->ether_dhost, ent->mac, ETHER_ADDR_LEN); struct sr_if *iface=sr_get_interface(ps->sr, buf_walker->interface); memmove(eth->ether_shost, iface->addr, ETHER_ADDR_LEN); eth->ether_type = htons(ETHERTYPE_IP); sr_send_packet(ps->sr, buf_walker->packet, buf_walker->pack_len, buf_walker->interface); buf_walker=delete_from_buffer(ps,buf_walker); } else if(buf_walker->num_arp_reqs < 5) /*Send another arp request. */ { buf_walker->num_arp_reqs++; sr_send_packet(ps->sr, buf_walker->arp_req, buf_walker->arp_len, buf_walker->interface); buf_walker=buf_walker->next; } else /* 5 ARP Request already sent, send ICMP Port Unreachable and Delete from Buffer.*/ { int off = sizeof(struct sr_ethernet_hdr) + sizeof(struct ip); ps->res_len=off; ps->response += sizeof(struct sr_ethernet_hdr); struct ip *res_ip = (struct ip*) ps->response; /*IP Header for ICMP Port Unreachable*/ ps->response += sizeof(struct ip); ps->packet = buf_walker->packet; ps->packet += sizeof(struct sr_ethernet_hdr); struct ip *ip_hdr = (struct ip*) (ps->packet); /*IP Header from original packet. */ ps->packet += sizeof(struct ip); icmp_response(ps, ip_hdr, ICMPT_DESTUN, ICMPC_HOSTUN); /*Construct ICMP */ memmove(res_ip, ip_hdr, sizeof(struct ip)); res_ip->ip_len = htons(ps->res_len - sizeof(struct sr_ethernet_hdr)); res_ip->ip_ttl = INIT_TTL; res_ip->ip_tos = ip_hdr->ip_tos; res_ip->ip_p = IPPROTO_ICMP; /* Finding interface to send ICMP out of*/ struct sr_rt* iface_rt_entry=get_routing_if(ps, ip_hdr->ip_src); struct sr_if* iface=sr_get_interface(ps->sr, iface_rt_entry->interface); res_ip->ip_src.s_addr = iface->ip; res_ip->ip_dst = ip_hdr->ip_src; res_ip->ip_sum = 0; res_ip->ip_sum = cksum((uint8_t *)res_ip, sizeof(struct ip)); res_ip->ip_sum = htons(res_ip->ip_sum); ps->response = (uint8_t *) res_ip - sizeof(struct sr_ethernet_hdr); struct sr_ethernet_hdr* eth_resp=(struct sr_ethernet_hdr*)ps->response; memmove(eth_resp->ether_dhost,buf_walker->old_eth->ether_shost,ETHER_ADDR_LEN); memmove(eth_resp->ether_shost,iface->addr, ETHER_ADDR_LEN); eth_resp->ether_type=htons(ETHERTYPE_IP); sr_send_packet(ps->sr, ps->response, ps->res_len, iface_rt_entry->interface); buf_walker=delete_from_buffer(ps,buf_walker); } } }