//Genia's add: int init_module(int max_games) { maxGames = max_games; games = kmalloc(sizeof(Game) * max_games, GFP_KERNEL); for(int i=0,i<max_games;i++ ) { games[i].board=kmalloc(sizeof(int) * N*N, GFP_KERNEL); Init(&(games[i].board)); games[i].currentPlayer=WHITE; games[i].openCount=0; games[i].isFinished=false; games[i].isRealesed=false; games[i].winner=NOT_FINISHED; sema_init(&(games[i].countLock), 0); sema_init(&(games[i].openLock), 0); sema_init(&(games[i].readWriteLock), 0); sema_init(&(games[i].whiteLock), 0); sema_init(&(games[i].blackLock), 0); sema_init(&(games[i].isFinishedLock), 0); sema_init(&(games[i].isReleasedLock), 0); sema_init(&(games[i].winnerLock), 0); } major = register_chrdev(0, "snake", &fops); MODULE_PARM(maxGames, "i"); MODULE_PARM(games, "i"); MODULE_PARM(major, "i"); SET_MODULE_OWNER(&fops); return 0; }
static int create_pipe(int nr) { int minor_in, minor_out , ret; if (dev_offset == -1) { if (inminor == -1) { minor_in = -1; } else { minor_in = inminor+nr; } if (outminor == -1) { minor_out = -1; } else { minor_out = outminor+nr; } } else { minor_in = 2*nr + dev_offset; minor_out = 2*nr+1 + dev_offset; } /* allocate space for this pipe */ loops[nr]= kmalloc(sizeof(struct vloopback_pipe), GFP_KERNEL); if (!loops[nr]) return -ENOMEM; /* set up a new video device plus our private area */ loops[nr]->vloopin= video_device_alloc(); if (loops[nr]->vloopin == NULL) return -ENOMEM; *loops[nr]->vloopin = vloopback_template; #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) video_set_drvdata(loops[nr]->vloopin,kmalloc(sizeof(struct vloopback_private),GFP_KERNEL)); #else loops[nr]->vloopin->priv= kmalloc(sizeof(struct vloopback_private),GFP_KERNEL); #endif #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) if ((priv_ptr)video_get_drvdata(loops[nr]->vloopin) == NULL) { #else if (loops[nr]->vloopin->priv == NULL) { #endif kfree(loops[nr]->vloopin); return -ENOMEM; } /* repeat for the output device */ loops[nr]->vloopout= video_device_alloc(); if (loops[nr]->vloopout == NULL) { #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) kfree((priv_ptr)video_get_drvdata(loops[nr]->vloopin)); #else kfree(loops[nr]->vloopin->priv); #endif kfree(loops[nr]->vloopin); return -ENOMEM; } *loops[nr]->vloopout = vloopback_template; #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) video_set_drvdata(loops[nr]->vloopout,kmalloc(sizeof(struct vloopback_private),GFP_KERNEL)); #else loops[nr]->vloopout->priv= kmalloc(sizeof(struct vloopback_private),GFP_KERNEL); #endif if ((priv_ptr)video_get_drvdata(loops[nr]->vloopout) == NULL) { #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) kfree((priv_ptr)video_get_drvdata(loops[nr]->vloopin)); #else kfree(loops[nr]->vloopin->priv); #endif kfree(loops[nr]->vloopin); kfree(loops[nr]->vloopout); return -ENOMEM; } #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) ((priv_ptr)video_get_drvdata(loops[nr]->vloopin))->pipenr=nr; ((priv_ptr)video_get_drvdata(loops[nr]->vloopout))->pipenr=nr; #else ((priv_ptr)loops[nr]->vloopin->priv)->pipenr=nr; ((priv_ptr)loops[nr]->vloopout->priv)->pipenr=nr; #endif loops[nr]->invalid_ioctl = 0; /* tibit */ loops[nr]->buffer=NULL; loops[nr]->width=0; loops[nr]->height=0; loops[nr]->palette=0; loops[nr]->frameswrite=0; loops[nr]->framesread=0; loops[nr]->framesdumped=0; loops[nr]->wopen=0; loops[nr]->ropen=0; loops[nr]->frame=0; loops[nr]->pendingread=0; #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) ((priv_ptr)video_get_drvdata(loops[nr]->vloopin))->in=1; ((priv_ptr)video_get_drvdata(loops[nr]->vloopout))->in=0; #else ((priv_ptr)loops[nr]->vloopin->priv)->in=1; ((priv_ptr)loops[nr]->vloopout->priv)->in=0; #endif #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27) loops[nr]->vloopin->type=0; #endif sprintf(loops[nr]->vloopin->name, "Video loopback %d input", nr); #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27) loops[nr]->vloopout->type=VID_TYPE_CAPTURE; #endif sprintf(loops[nr]->vloopout->name, "Video loopback %d output", nr); init_waitqueue_head(&loops[nr]->wait); init_MUTEX(&loops[nr]->lock); ret = video_register_device(loops[nr]->vloopout, VFL_TYPE_GRABBER, minor_out); if ((ret ==-1) || (ret == -23)) { info("error registering device %s", loops[nr]->vloopout->name); #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) kfree(((priv_ptr)video_get_drvdata(loops[nr]->vloopin))); video_unregister_device(loops[nr]->vloopin); kfree(((priv_ptr)video_get_drvdata(loops[nr]->vloopout))); #else kfree(loops[nr]->vloopin->priv); video_unregister_device(loops[nr]->vloopin); kfree(loops[nr]->vloopout->priv); #endif kfree(loops[nr]->vloopout); kfree(loops[nr]); loops[nr]=NULL; return ret; } ret = video_register_device(loops[nr]->vloopin, VFL_TYPE_GRABBER, minor_in); if ((ret == -1 ) || ( ret == -23 )) { info("error registering device %s",loops[nr]->vloopin->name); #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) kfree(((priv_ptr)video_get_drvdata(loops[nr]->vloopin))); kfree(((priv_ptr)video_get_drvdata(loops[nr]->vloopout))); #else kfree(loops[nr]->vloopin->priv); kfree(loops[nr]->vloopout->priv); #endif kfree(loops[nr]->vloopin); kfree(loops[nr]->vloopout); kfree(loops[nr]); loops[nr]=NULL; return ret; } loops[nr]->ioctldata=kmalloc(1024, GFP_KERNEL); loops[nr]->ioctlretdata=kmalloc(1024, GFP_KERNEL); return 0; } /**************************************************************************** * init stuff ****************************************************************************/ MODULE_AUTHOR("J.B. Vreeken ([email protected])"); MODULE_DESCRIPTION("Video4linux loopback device."); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) module_param(pipes, int, 000); #else MODULE_PARM(pipes, "i"); #endif MODULE_PARM_DESC(pipes, "Nr of pipes to create (each pipe uses two video devices)"); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) module_param(spares, int, 000); #else MODULE_PARM(spares, "i"); #endif MODULE_PARM_DESC(spares, "Nr of spare pipes that should be created"); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) module_param(dev_offset, int, 000); #else MODULE_PARM(dev_offset_param, "i"); #endif #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) module_param(inminor, int, 000); #else MODULE_PARM(inminor, "i"); #endif #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) module_param(outminor, int, 000); #else MODULE_PARM(outminor, "i"); #endif MODULE_PARM_DESC(dev_offset, "Prefered offset for video device numbers"); MODULE_LICENSE("GPL"); MODULE_VERSION( VLOOPBACK_VERSION ); static int __init vloopback_init(void) { int i,ret; info("Video4linux loopback driver v"VLOOPBACK_VERSION); if (pipes==-1) pipes=1; if (pipes > MAX_PIPES) { pipes=MAX_PIPES; info("Nr of pipes is limited to: %d", MAX_PIPES); } for (i=0; i<pipes; i++) { ret = create_pipe(i); if (ret == 0) { info("Loopback %d registered, input: video%d, output: video%d", i, loops[i]->vloopin->minor, loops[i]->vloopout->minor); nr_o_pipes=i+1; }else{ return ret; } } return 0; }
static int create_pipe(int nr) { int minor_in, minor_out , ret; if (debug > LOG_NODEBUG) info("Video loopback %d", nr); if (dev_offset == -1) { minor_in = minor_out = -1; /* autoassign */ } else { minor_in = 2 * nr + dev_offset; minor_out = 2 * nr + 1 + dev_offset; } /* allocate space for this pipe */ loops[nr]= kmalloc(sizeof(struct vloopback_pipe), GFP_KERNEL); if (!loops[nr]) return -ENOMEM; /* set up a new video device plus our private area */ loops[nr]->vloopin = video_device_alloc(); if (loops[nr]->vloopin == NULL) return -ENOMEM; *loops[nr]->vloopin = vloopback_template; #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32) priv_ptr ptr_in = kmalloc(sizeof(struct vloopback_private), GFP_KERNEL); if (ptr_in == NULL) { kfree(ptr_in); #else loops[nr]->vloopin->vd_private_data = kmalloc(sizeof(struct vloopback_private), GFP_KERNEL); if (loops[nr]->vloopin->vd_private_data == NULL) { kfree(loops[nr]->vloopin); #endif return -ENOMEM; } /* repeat for the output device */ loops[nr]->vloopout = video_device_alloc(); if (loops[nr]->vloopout == NULL) { #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32) kfree(ptr_in); #else kfree(loops[nr]->vloopin->vd_private_data); #endif kfree(loops[nr]->vloopin); return -ENOMEM; } *loops[nr]->vloopout = vloopback_template; #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32) priv_ptr ptr_out = kmalloc(sizeof(struct vloopback_private), GFP_KERNEL); if (ptr_out == NULL) { kfree(ptr_in); kfree(ptr_out); #else loops[nr]->vloopout->vd_private_data = kmalloc(sizeof(struct vloopback_private), GFP_KERNEL); if (loops[nr]->vloopout->vd_private_data == NULL) { kfree(loops[nr]->vloopin->vd_private_data); #endif kfree(loops[nr]->vloopin); kfree(loops[nr]->vloopout); return -ENOMEM; } #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32) ptr_in->pipenr = nr; ptr_out->pipenr = nr; #else ((priv_ptr)loops[nr]->vloopin->vd_private_data)->pipenr = nr; ((priv_ptr)loops[nr]->vloopout->vd_private_data)->pipenr = nr; #endif loops[nr]->invalid_ioctl = 0; /* tibit */ loops[nr]->buffer = NULL; loops[nr]->width = 0; loops[nr]->height = 0; loops[nr]->palette = 0; loops[nr]->frameswrite = 0; loops[nr]->framesread = 0; loops[nr]->framesdumped = 0; loops[nr]->wopen = 0; loops[nr]->ropen = 0; loops[nr]->frame = 0; #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32) ptr_in->in = 1; ptr_out->in = 0; dev_set_drvdata(&loops[nr]->vloopin->dev, ptr_in); dev_set_drvdata(&loops[nr]->vloopout->dev, ptr_out); #else ((priv_ptr)loops[nr]->vloopin->vd_private_data)->in = 1; ((priv_ptr)loops[nr]->vloopout->vd_private_data)->in = 0; #endif sprintf(loops[nr]->vloopin->name, "Video loopback %d input", nr); sprintf(loops[nr]->vloopout->name, "Video loopback %d output", nr); #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,27) loops[nr]->vloopin->type = 0; loops[nr]->vloopout->type = VID_TYPE_CAPTURE; #endif loops[nr]->vloopout->minor = minor_out; loops[nr]->vloopin->minor = minor_in; init_waitqueue_head(&loops[nr]->wait); init_MUTEX(&loops[nr]->lock); ret = video_register_device(loops[nr]->vloopin, VFL_TYPE_GRABBER, minor_in); if ((ret == -1 ) || ( ret == -23 )) { info("error registering device %s", loops[nr]->vloopin->name); #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32) kfree(ptr_in); dev_set_drvdata(&loops[nr]->vloopin->dev, NULL); #else kfree(loops[nr]->vloopin->vd_private_data); #endif kfree(loops[nr]->vloopin); #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32) kfree(ptr_out); dev_set_drvdata(&loops[nr]->vloopout->dev, NULL); #else kfree(loops[nr]->vloopout->vd_private_data); #endif kfree(loops[nr]->vloopout); kfree(loops[nr]); loops[nr] = NULL; return ret; } ret = video_register_device(loops[nr]->vloopout, VFL_TYPE_GRABBER, minor_out); if ((ret ==-1) || (ret == -23)) { info("error registering device %s", loops[nr]->vloopout->name); #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32) kfree(ptr_in); dev_set_drvdata(&loops[nr]->vloopin->dev, NULL); #else kfree(loops[nr]->vloopin->vd_private_data); #endif video_unregister_device(loops[nr]->vloopin); #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,32) kfree(ptr_out); dev_set_drvdata(&loops[nr]->vloopout->dev, NULL); #else kfree(loops[nr]->vloopout->vd_private_data); #endif kfree(loops[nr]->vloopout); kfree(loops[nr]); loops[nr] = NULL; return ret; } loops[nr]->ioctldata = kmalloc(1024, GFP_KERNEL); loops[nr]->ioctlretdata = kmalloc(1024, GFP_KERNEL); return 0; } /**************************************************************************** * init stuff ****************************************************************************/ MODULE_AUTHOR("J.B. Vreeken ([email protected])"); MODULE_DESCRIPTION("Video4linux loopback device."); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) module_param(pipes, int, 000); #else MODULE_PARM(pipes, "i"); #endif MODULE_PARM_DESC(pipes, " Nr of pipes to create (each pipe uses two video devices)"); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) module_param(spares, int, 000); #else MODULE_PARM(spares, "i"); #endif MODULE_PARM_DESC(spares, " Nr of spare pipes that should be created"); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) module_param(num_buffers, int, 000); #else MODULE_PARM(num_buffers, "i"); #endif MODULE_PARM_DESC(num_buffers, " Prefered numbers of internal buffers to map (default 2)"); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) module_param(dev_offset, int, 000); #else MODULE_PARM(dev_offset_param, "i"); #endif MODULE_PARM_DESC(dev_offset, " Prefered offset for video device numbers"); #if LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0) module_param(debug, int, 000); #else MODULE_PARM(debug_param, "i"); #endif MODULE_PARM_DESC(debug, " Enable module debug level 0-3 (by default 0)"); MODULE_LICENSE("GPL"); MODULE_VERSION( VLOOPBACK_VERSION ); static int __init vloopback_init(void) { int i, ret; info("video4linux loopback driver v"VLOOPBACK_VERSION); if (pipes == -1) pipes = 1; if (pipes > MAX_PIPES) { pipes = MAX_PIPES; info("Nr of pipes is limited to: %d", MAX_PIPES); } if (num_buffers < N_BUFFS) { num_buffers = N_BUFFS; info("Nr of buffer set to default value %d", N_BUFFS); } for (i = 0; i < pipes; i++) { ret = create_pipe(i); if (ret == 0) { info("Loopback %d registered, input: video%d," " output: video%d", i, loops[i]->vloopin->minor, loops[i]->vloopout->minor); info("Loopback %d , Using %d buffers", i, num_buffers); nr_o_pipes = i + 1; } else { return ret; } } return 0; }
unsigned int nf_aodv_hook(unsigned int hooknum, struct sk_buff **skb, const struct net_device *in, const struct net_device *out, int (*okfn) (struct sk_buff *)) { int i; /* We are only interested in IP packets */ if ((*skb)->nh.iph == NULL) goto accept; /* We want AODV control messages to go through directly to the * AODV socket.... */ if ((*skb)->nh.iph && (*skb)->nh.iph->protocol == IPPROTO_UDP) if ((*skb)->sk) if ((*skb)->sk->dport == htons(AODV_PORT) || (*skb)->sk->sport == htons(AODV_PORT)) goto accept; /* Check which hook the packet is on... */ switch (hooknum) { case NF_IP_PRE_ROUTING: /* Loop through all AODV enabled interfaces and see if the packet * is bound to any of them. */ for (i = 0; i < nif; i++) if (ifindices[i] == in->ifindex) { (*skb)->nfmark = 3; goto queue; } break; case NF_IP_LOCAL_OUT: for (i = 0; i < nif; i++) if (ifindices[i] == out->ifindex) { (*skb)->nfmark = 4; goto queue; } break; case NF_IP_POST_ROUTING: /* Re-route all packets before sending on interface. This will make sure queued packets are routed on a newly installed route (after a successful RREQ-cycle). FIXME: Make sure only "buffered" packets are re-routed. But how? */ if ((*skb)->nfmark == 3 || (*skb)->nfmark == 4) { #ifdef USE_OLD_ROUTE_ME_HARDER route_me_harder((*skb)); #else ip_route_me_harder(skb); #endif } return NF_ACCEPT; default: } accept: (*skb)->nfmark = 2; return NF_ACCEPT; queue: return NF_QUEUE; } /* * Called when the module is inserted in the kernel. */ char *ifname[MAX_INTERFACES] = { "eth0" }; MODULE_PARM(ifname, "1-" __MODULE_STRING(MAX_INTERFACES) "s"); int init_module() { struct net_device *dev = NULL; int i; EXPORT_NO_SYMBOLS; nf_hook1.list.next = NULL; nf_hook1.list.prev = NULL; nf_hook1.hook = nf_aodv_hook; nf_hook1.pf = PF_INET; nf_hook1.hooknum = NF_IP_PRE_ROUTING; nf_register_hook(&nf_hook1); nf_hook2.list.next = NULL; nf_hook2.list.prev = NULL; nf_hook2.hook = nf_aodv_hook; nf_hook2.pf = PF_INET; nf_hook2.hooknum = NF_IP_LOCAL_OUT; nf_register_hook(&nf_hook2); nf_hook3.list.next = NULL; nf_hook3.list.prev = NULL; nf_hook3.hook = nf_aodv_hook; nf_hook3.pf = PF_INET; nf_hook3.hooknum = NF_IP_POST_ROUTING; nf_register_hook(&nf_hook3); for (i = 0; i < MAX_INTERFACES; i++) { if (!ifname[i]) break; dev = dev_get_by_name(ifname[i]); if (!dev) { printk("kaodv: No device %s available, ignoring!\n", ifname[i]); dev_put(dev); continue; } ifindices[nif++] = dev->ifindex; dev_put(dev); } return 0; } /* * Called when removing the module from memory... */ void cleanup_module() { nf_unregister_hook(&nf_hook1); nf_unregister_hook(&nf_hook2); nf_unregister_hook(&nf_hook3); }