flood_id_queue.c

説明を見る。
00001 /*
00002                Kernel AODV  v2.1
00003 National Institute of Standards and Technology
00004                Luke Klein-Berndt
00005 -----------------------------------------------------
00006   Version 2.1 new features:
00007      * Much more stable!
00008      * Added locks around important areas
00009      * Multihop Internet gatewaying now works
00010      * Multicast hack added
00011      * Many bug fixes!
00012 -----------------------------------------------------
00013 
00014 Originally based upon MadHoc code. I am not
00015 sure how much of it is left anymore, but MadHoc
00016 proved to be a great starting point.
00017 
00018 MadHoc was written by - Fredrik Lilieblad,
00019 Oskar Mattsson, Petra Nylund, Dan Ouchterlony
00020 and Anders Roxenhag Mail: mad-hoc@flyinglinux.net
00021 
00022 This software is Open Source under the GNU General Public Licence.
00023 
00024 */
00025 
00026 #include "flood_id_queue.h"
00027 /****************************************************
00028 
00029    flood_id_queue
00030 ----------------------------------------------------
00031 This is used to keep track of messages which
00032 are flooded to prevent rebroadcast of messages
00033 ****************************************************/
00034 
00035 extern struct flood_id_queue_entry *rreq_id_queue;
00036 rwlock_t rreq_lock = RW_LOCK_UNLOCKED;
00037 
00038 
00039 /****************************************************
00040 
00041    init_flood_id_queue
00042 ----------------------------------------------------
00043 Gets the ball rolling!
00044 ****************************************************/
00045 
00046 int init_flood_id_queue( void )
00047 {
00048     rreq_id_queue=NULL;
00049 
00050     return 0;
00051 }
00052 
00053 void flood_read_lock()
00054 {
00055    write_lock_bh(&rreq_lock);
00056 }
00057 
00058 void flood_read_unlock()
00059 {
00060   write_unlock_bh(&rreq_lock);
00061 }
00062 
00063 void flood_write_lock()
00064 {
00065   write_lock_bh(&rreq_lock);
00066 }
00067 
00068 void flood_write_unlock()
00069 {
00070   write_unlock_bh(&rreq_lock);
00071 }
00072 
00073 
00074 /****************************************************
00075 
00076    find_flood_id_queue_entry
00077 ----------------------------------------------------
00078 will search the queue for an entry with the
00079 matching ID and src_ip
00080 ****************************************************/
00081 struct flood_id_queue_entry *find_flood_id_queue_entry(u_int32_t src_ip, u_int32_t flood_id )
00082 {
00083   struct flood_id_queue_entry  *tmp_entry;  /* Working entry in the RREQ list */
00084 
00085   
00086   u_int64_t curr=getcurrtime();
00087   
00088 
00089   /*lock table*/
00090   flood_read_lock();
00091   
00092   tmp_entry = rreq_id_queue; /* Start at the header */
00093 
00094   //go through the whole queue
00095   while (tmp_entry!=NULL)
00096     {
00097       
00098         if(tmp_entry->lifetime<curr)
00099         {
00100           /*unlock table*/
00101           flood_read_unlock();
00102 
00103           return NULL;
00104         }
00105       
00106         //if there is a match and it is still valid
00107         if (src_ip == tmp_entry->src_ip && flood_id == tmp_entry->flood_id)
00108           {
00109             /*unlock table*/
00110             flood_read_unlock();
00111 
00112             return tmp_entry;
00113           }
00114         
00115 
00116 
00117         //continue on to the next entry
00118         tmp_entry=tmp_entry->next;
00119         
00120         
00121         
00122     }
00123   /*unlock table*/
00124   flood_read_unlock();
00125 
00126   return NULL;
00127 }
00128 
00129 
00130 /****************************************************
00131 
00132    read_flood_id_proc
00133 ----------------------------------------------------
00134 prints out the flood id queue when the proc
00135 file is read
00136 ****************************************************/
00137 int read_rreq_id_proc(char *buffer, char **buffer_location, off_t offset, int buffer_length,int *eof,void *data)
00138 {
00139   int len;
00140   static char *my_buffer;
00141   char temp_buffer[200];
00142   struct flood_id_queue_entry *tmp_entry;
00143   char tmp[16];
00144   u_int64_t tmp_time;
00145 
00146 
00147     /*lock table*/
00148     flood_read_lock();
00149 
00150   tmp_entry=rreq_id_queue;
00151   
00152   my_buffer=buffer;
00153   sprintf(my_buffer,"\nFlood Id Queue\n---------------------------------\n");
00154   
00155   while (tmp_entry!=NULL)
00156     {
00157       tmp_time=tmp_entry->lifetime-getcurrtime();
00158       strcpy(tmp,inet_ntoa(tmp_entry->dst_ip));
00159       sprintf(temp_buffer,"Src IP: %-16s  Dst IP: %-16s Flood ID: %-10u sec/msec: %lu/%lu \n", inet_ntoa(tmp_entry->src_ip),tmp,tmp_entry->flood_id,100,100);//,(unsigned long)(tmp_time) / 1000, (unsigned long)(tmp_time) % 1000);
00160       strcat(my_buffer,temp_buffer);
00161       tmp_entry=tmp_entry->next;
00162     }
00163 
00164     /*unlock table*/
00165     flood_read_unlock();
00166 
00167     sprintf(temp_buffer,"\n---------------------------------\n");
00168     strcat(my_buffer,temp_buffer);
00169 
00170     *buffer_location=my_buffer;
00171     len = strlen(my_buffer);
00172     if (len <= offset+buffer_length) *eof = 1;
00173     *buffer_location = my_buffer + offset;
00174     len -= offset;
00175     if (len>buffer_length) len = buffer_length;
00176     if (len<0) len = 0;
00177     return len;
00178 
00179 }
00180 
00181 
00182 
00183 /****************************************************
00184 
00185    print_flood_id_queue
00186 ----------------------------------------------------
00187 prints out the flood id queue onto the console
00188 screen
00189 ****************************************************/
00190 void print_flood_id_queue()
00191 {
00192     struct flood_id_queue_entry *tmp_entry;
00193     char tmp[16];
00194     tmp_entry= rreq_id_queue;
00195 
00196     printk(KERN_INFO "Flood ID list:\n");
00197     while (tmp_entry!=NULL)
00198     {
00199         strcpy(tmp,inet_ntoa(tmp_entry->dst_ip));
00200         printk(KERN_INFO "Src IP: %-16s  Dst IP: %-16s Flood ID: %-10u  \n", inet_ntoa(tmp_entry->src_ip),tmp,tmp_entry->flood_id);
00201         tmp_entry=tmp_entry->next;
00202     }
00203 }
00204 
00205 /****************************************************
00206 
00207    clean_up_flood_id_queue
00208 ----------------------------------------------------
00209 Deletes everything in the flood id queue
00210 ****************************************************/
00211 void cleanup_flood_id_queue()
00212 {
00213     struct flood_id_queue_entry *tmp_entry,*dead_entry;
00214     int count=0;
00215 
00216     tmp_entry=rreq_id_queue;
00217 
00218     print_flood_id_queue();
00219 
00220     while (tmp_entry!=NULL)
00221     {
00222         dead_entry=tmp_entry;
00223         tmp_entry=tmp_entry->next;
00224         kfree(dead_entry);
00225         count++;
00226     }
00227     rreq_id_queue=NULL;
00228 
00229     printk(KERN_INFO "Removed %d Flood ID entries! \n",count);
00230     printk(KERN_INFO "---------------------------------------------\n");
00231 
00232 }
00233 
00234 /****************************************************
00235 
00236    insert_flood_id_queue_entry
00237 ----------------------------------------------------
00238 Inserts an entry into the flood ID queue
00239 ****************************************************/
00240 int insert_flood_id_queue_entry(u_int32_t ip, u_int32_t dst_ip,u_int32_t id, u_int64_t lt)
00241 {
00242     struct flood_id_queue_entry  *new_entry; /* Pointer to the working entry */
00243 
00244 
00245     /* Allocate memory for the new entry */
00246     if ((new_entry = (struct flood_id_queue_entry*)kmalloc(sizeof(struct flood_id_queue_entry),GFP_ATOMIC)) == NULL)
00247     {
00248       printk(KERN_WARNING "AODV: Not enough memory to create Flood ID queue\n");
00249       
00250       /* Failed to allocate memory for new Flood ID queue */
00251       return -ENOMEM;
00252     }
00253     
00254     /* Fill in the information in the new entry */
00255     new_entry->src_ip = ip;
00256     new_entry->dst_ip = dst_ip;
00257     new_entry->flood_id = id;
00258     new_entry->lifetime = lt;
00259 
00260 
00261     /*lock table*/
00262     flood_write_lock();
00263 
00264 
00265     new_entry->next = rreq_id_queue;
00266     /* Put the new entry in the list */
00267     rreq_id_queue=new_entry;
00268 
00269     /*unlock table*/
00270     flood_write_unlock();
00271 
00272     return 0;
00273 }
00274 
00275 /****************************************************
00276 
00277    check_flood_id_queue_entry_lifetime
00278 ----------------------------------------------------
00279 Checks the lifetime of a flood ID queue entry.
00280 Returns 0 if it is valid and 1 if it is not!
00281 ****************************************************/
00282 int check_flood_id_queue_entry_lifetime(struct flood_id_queue_entry *tmp_entry)
00283 {
00284     u_int64_t  curr_time = getcurrtime(); /* Current time */
00285 
00286     /* Check if the entry is valid */
00287     if ((tmp_entry!=NULL )&&(curr_time > tmp_entry->lifetime))
00288     {
00289         return 1;
00290     }
00291 
00292     if (tmp_entry==NULL)
00293       {
00294         printk(KERN_WARNING "AODV: check_flood_id_queue_lifetime passed a NULL\n");
00295       }
00296 
00297     return 0;
00298 }
00299 
00300 /****************************************************
00301 
00302    delete_old_flood_id_queue_entries
00303 ----------------------------------------------------
00304 Goes through the flood ID queue and deletes
00305 all of the old entries
00306 ****************************************************/
00307 int delete_old_flood_id_queue_entries( ) 
00308 {
00309     struct flood_id_queue_entry *tmp_entry,*prev_entry,*dead_entry;
00310 
00311     u_int64_t  curr_time = getcurrtime(); /* Current time */
00312     int c=0;
00313 
00314 
00315     /*lock table*/
00316     flood_write_lock( );
00317 
00318 
00319     tmp_entry= rreq_id_queue;
00320     prev_entry=NULL;
00321 
00322     //go through the entire queue
00323     while(tmp_entry!=NULL)
00324     {
00325       c++;
00326         //if the entry has expired
00327       if (curr_time > tmp_entry->lifetime)
00328         {
00329 
00330           
00331           //if it is the first entry
00332           if(prev_entry==NULL)
00333             rreq_id_queue=tmp_entry->next;
00334           else
00335             prev_entry->next=tmp_entry->next;
00336           
00337           //kill it!
00338           dead_entry=tmp_entry;
00339           tmp_entry=tmp_entry->next;
00340           kfree(dead_entry);
00341 
00342         }
00343       else
00344         {
00345 
00346           
00347           //next entry
00348           prev_entry=tmp_entry;
00349           tmp_entry=tmp_entry->next;
00350           
00351 
00352         }
00353     }
00354 
00355     /*unlock table*/
00356     flood_write_unlock( );
00357 
00358     return c;
00359 }
00360 
00361 /****************************************************
00362 
00363    delete_flood_id_queue_entry
00364 ----------------------------------------------------
00365 Deletes a flood id queue entry
00366 ****************************************************/
00367 int delete_flood_id_queue_entry(struct flood_id_queue_entry *dead_entry )
00368 {
00369     struct flood_id_queue_entry *tmp_entry, *prev_entry;
00370 
00371     tmp_entry= rreq_id_queue;
00372     prev_entry=NULL;
00373 
00374     //since we don't know the previous entry we have to go
00375     //through entire list to find it!
00376     while (tmp_entry!=NULL)
00377     {
00378         //if we find the entry we wish to delete
00379         if(tmp_entry==dead_entry)
00380         {
00381             if (prev_entry==NULL)
00382                rreq_id_queue=tmp_entry->next;
00383             else
00384                 prev_entry->next=tmp_entry->next;
00385             kfree(tmp_entry);
00386             return 0;
00387         }
00388         tmp_entry=tmp_entry->next;
00389     }
00390     return 1;
00391 }
00392 
00393 
00394 

kernel_aodvmに対してThu Nov 10 18:53:11 2005に生成されました。  doxygen 1.4.5