ºìÁªLinuxÃÅ»§
Linux°ïÖú

¹ØÓÚlinuxÄں˵Äioctlº¯Êý

·¢²¼Ê±¼ä:2007-04-07 10:35:43À´Ô´:ºìÁª×÷Õß:trangod
¡¡¡¡Ò»°ãµÄ˵£¬,Óû§¿Õ¼äµÄIOCTLϵͳµ÷ÓÃÈçÏÂËùʾ: ioctl(int fd, int command, (char *) argstruct)ÒòΪÕâ¸öµ÷ÓÃÓµÓÐÓëÍøÂçÏà¹ØµÄ´úÂë,ËùÒÔÎļþÃèÊö·ûºÅfd¾ÍÊÇsocket()ϵͳµ÷ÓÃËù·µ»ØµÄ,¶øcommand²ÎÊý¿ÉÒÔÊÇ/usr/include/linux/sockios.hÍ·ÎļþÖеÄÈκÎÒ»¸ö,ÕâЩ¸öÃüÁî¸ù¾ÝËü¿ÉÒÔ½â¾öµÄÎÊÌâËùÉæ¼°µÄ·½Ãæ±»·ÖΪ¶àÖÖµÄÀàÐÍ.

¡¡¡¡±ÈÈç:

¡¡¡¡¸Ä±ä·ÓÉ±í£¨SIOCADDRT, SIOCDELRT£©

¡¡¡¡
¡¡¡¡¶ÁÈ¡»ò¸üÐÂARP/RARP»º´æ£¨SIOCDARP, SIOCSRARP£©
Ò»°ãµÄºÍÍøÂçÓйصĺ¯Êý£¨SIOCGIFNAME, SIOCSIFADDRµÈµÈ£©

¡¡¡¡GoodiesĿ¼Öаüº¬Á˺ܶàչʾioctlÓ÷¨µÄʾÀý³ÌÐò,¿´ÕâЩ³ÌÐòµÄʱºò,×¢Òâ¸ù¾ÝioctlµÄÃüÁîÀàÐÍÀ´Ñ§Ï°¾ßÌåµÄµ÷ÓòÎÊý½á¹¹,±ÈÈç:ºÍ·ÓɱíÏà¹ØµÄIOCTLÓÃRTENTRY½á¹¹, rtentry½á¹¹ÊDZ»¶¨ÒåÔÚ/usr/include/linux/route.hÎļþÖеģ¬ÔÙÒ»¸öºÍARPÏà¹ØµÄioctlµ÷ÓÃÓõ½µÄarpreq½á¹¹±»¶¨ÒåÔÚ/usr/include/linux/if_arp.hÎļþÖ®ÖÐ.ÍøÂç½Ó¿ÚÏà¹ØµÄioctlÃüÁî×î¾ßÓдú±íÐÔµÄÌØÕ÷Ϊ¶¼ÊÇÒÔS»òG¿ªÍ·,Æäʵ¾ÍÊÇÉèÖûòµÃµ½Êý¾Ý, getifinfo.c³ÌÐòÓÃÕâЩÃüÁîÈ¥¶ÁÈ¡IPµØÖ·ÐÅÏ¢,Ó²¼þµØÖ·ÐÅÏ¢£¬¹ã²¥µØÖ·ÐÅÏ¢,ºÍÓëÍøÂç½Ó¿ÚÏà¹ØµÄ±êÖ¾.¶ÔÓÚÕâЩioctl,µÚÈý¸ö²ÎÊýÊÇÒ»¸öIFREQ½á¹¹Ìå,Õâ¸ö½á¹¹Ìå±»¶¨ÒåÔÚ/usr/include/linux/if.hÍ·ÎļþÖÐ,ÔÚһЩÇé¿öÏÂ,еÄioctlÃüÁî¿ÉÄܱ»ÐèÒª£¨³ýÁËÔÚÄǸöÍ·ÎļþÖỶ¨ÒåµÄÖ®Í⣩,±ÈÈç WAVELANÎÞÏßÍø¿¨±£³Ö×ÅÎÞÏßÐźÅÇ¿¶ÈµÄÐÅÏ¢,ÕâЩÐÅÎ÷¿ÉÄÜÒª ¶ÔÓû§³ÌÐòÓÐÓÃ.Óû§³ÌÐòÊÇÔõô·ÃÎʵ½ÕâЩÐÅÏ¢µÄÄØ?ÎÒÃǵĵÚÒ»·´Ó¦¾ÍÊǶ¨ÒåÒ»¸öеÄÃüÁîÔÚsockios.hÍ·ÎļþÖУ¬±ÈÈçSIOCGIFWVLNSS,²»ÐÒµÄÊÇ,Õâ¸öÃüÁîÔÚÆäËûµÄÍøÂç½Ó¿ÚÉÏÊǸù±¾Ã»ÓÐÒâÒåµÄ,ÁíÍâÊÔͼÔÚÆäËû½Ó¿ÚÉÏÓÃÕâ¸öÃûÁí¶ø²¢·ÇÊÇÔÚÎÞÏßÍø¿ÚÉÏÓûá³öÏÖÎ¥¹æ·ÃÎÊ,ÎÒÃÇÐèÒªµÄÊǶ¨ÒåÐÂÌØÐÔ½Ó¿ÚÃüÁîµÄ»úÀí¡£ÐÒÔ˵ÄÊÇ,LINUX²Ù×÷ϵͳΪ´ËÄ¿µÄÄÚÖÃÁ˹³×Ó,Èç¹ûÄãÔÙ¿´Ò»ÏÂÄǸöÍ·Îļþsockios.hÄã»á×¢Ò⵽ÿһ¸öÉ豸¶¼ÓÐÒ»¸öÔ¤¶¨ÒåµÄSIOCDEVPRIVATEÃüÁʵÏÖËüµÄÈÎÎñ¾ÍȫȨ½»¸øÁËдÕâ¸öÉ豸Çý¶¯µÄ³ÌÐòÔ±ÁË.¸ù¾Ý³£¹æÔ¼¶¨,Ò»¸öÓû§³ÌÐòµ÷ÓÃÒ»¸öÌض¨µÄioctlÃüÁîÈçÏÂ: ioctl(sockid, SIOCDEVPRIVATE, (char *) &ifr)ÕâÀïifrÊÇÒ»¸öifreq½á¹¹Ìå±äÁ¿,ËüÓÃÒ»¸öºÍÕâ¸öÉ豸ÁªÏµµÄ½Ó¿ÚÃû³ÆÌî³äifrµÄifr NAMEÓò,±ÈÈç,Ç°ÊöµÄÎÞÏßÍø¿¨½Ó¿ÚÃû³ÆΪeth1¡£

¡¡¡¡²»Ê§Ò»°ãÐÔ,Ò»¸öÓû§³ÌÐò½«Í¬ÑùÒªÓëÄں˽»»»ÃüÁî²ÎÊýºÍ²Ù×÷½á¹û,¶øÕâЩÒѾ­Í¨¹ýÒ»¸öÓòifr.ifr_dataµÄÌî³ä¶ø×öµ½ÁË,±ÈÈç,Õâ¸öÍø¿¨µÄÐźÅÇ¿¶ÈÐÅÏ¢±»·µ»Øµ½Õâ¸öÓòµ±ÖС£LINUXÔ´´úÂëÒѾ­°üº¬ÁËÁ½¸öÌØÊâÉ豸de4x5ºÍewrk3,ËûÃǶ¨ÒåºÍʵÏÖÁËÌØÊâµÄioctlÃüÁî.,ÕâЩÉ豸µÄÔ´´úÂëÔÚÒÔϵÄÎļþÖУºde4x5.h, de4x5.c, ewrk3.h, ewrk3.c, ËûÃÇÁ½¸öÉ豸¶¼ÎªÔÚÓû§¿Õ¼äºÍÇý¶¯¼ä½»»»Êý¾Ý¶¨ÒåÁËËûÃÇ×Ô¼ºµÄ˽Óнṹ,ÔÚioctl֮ǰ,Óû§³ÌÐòÌî³äÁËÐèÒªµÄÊý¾Ý²¢ÇÒ½«ifr.ifr_dataÖ¸ÏòÕâ¸ö½á¹¹Ìå.

¡¡¡¡ÎÒÃÇÔÚÁ½¸öÇý¶¯ÖÐ×ߵĸüԶЩ´Ó¶ø½øÈë´úÂëÇ°,ÈÃÎÒÃǸú×Ùһϴ¦Àíioctlϵͳµ÷ÓõÄÈô¸É²½Öè,,ËùÓнӿÚÀàÐ͵ÄioctlÇëÇ󶼵¼ÖÂdev_ioctl()±»µ÷ÓÃ,Õâ¸öioctl½ö½öÊǸö°ü×°,´ó²¿·ÖµÄÕæʵµÄ²Ù×÷Áô¸øÁËdev_ifsioc().£¬Õâ¸ödev_ioctl()Òª×öµÄΨһһ¸öÊÂÇé¾ÍÊǼì²éµ÷Óùý³ÌÊÇ·ñÓµÓкÏÊʵÄÐí¿ÉÈ¥ºË·¢Õâ¸öÃüÁȻºódev_ifsioc()Ê×ÏÈÒª×öµÄÊÂÇéÖ®Ò»¾ÍÊǵõ½ºÍÃû×ÖÓòifr.ifr_nameÖÐËù¶ÔÓ¦µÄÉ豸½á¹¹£¬ÕâÔÚÒ»¸öºÜ´óµÄswitchÓï¿éµÄ´úÂëºóʵÏÖ¡£

¡¡¡¡SIOCDEVPRIVATEÃüÁîºÍSIOCDEVPRIVATE+15µÄÃüÁî²ÎÊýÈ«²¿½»¸øÁËĬÈϲÙ×÷,ÕâЩ¶¼ÊÇswitchµÄ·ÖÖ§Óï¾ä.ÕâÀï·¢ÉúµÄÊÇ,Äں˼ì²éÊÇ·ñÒ»¸öÉ豸ÌØÊâµÄioctlµÄ»Øµ÷ÒѾ­ÔÚÉ豸½á¹¹Öб»ÉèÖã¬Õâ¸ö»Øµ÷ÊDZ£³ÖÔÚÉ豸½á¹¹ÖеÄÒ»¸öº¯ÊýÖ¸Õë¡£Èç¹û»Øµ÷ÒѾ­±»ÉèÖÃÁË.Äں˾ͻáµ÷ÓÃËü.

¡¡¡¡ËùÒÔ,ΪÁËʵÏÖÒ»¸öÌØÊâµÄioctl,ÐèÒª×öµÄ¾ÍÊÇдһ¸öÌØÊâioctlµÄ»Øµ÷£¬È»ºóÈÃdevice½á¹¹ÖеÄdo_ioctlÓòÖ¸ÏòËü,¶ÔÓÚEWK3É豸£¬Õâ¸öº¯Êý½Ð×öewrk3_ioctl(),¶ÔÓ¦µÄÉ豸½á¹¹ÔÚewrk3_init()Öб»³õʼ»¯,ewrk3_ioctl()µÄ´úÂëÇåÎúµÄչʾÁËifr.ifr_dataµÄ×÷Óà ,ÊÇΪÁËÔÚÓû§³ÌÐòºÍÇý¶¯Ö®¼ä½»»»ÐÅÏ¢¡£×¢Ò⣬ÄÚ´æµÄÕâ¸öÇøÓòÓÐË«·½Ïò½»»»Êý¾ÝµÄ×÷ÓÃ,ÀýÈç,ÔÚewrk3Çý¶¯´úÂëÖÐ,ifr.ifr_data×î³õµÄ2¸ö×Ö½Ú±»ÓÃ×öÏòÇý¶¯´«µÝÔ¤ÏëÒªµÄ¶¯×÷¡£Í¬ÑùµÚÎå¸ö×Ö½ÚÖ¸ÏòµÄ»º³åÇøÓÃÓÚ½»»»ÆäËûµÄÐÅÏ¢¡£

¡¡¡¡µ±Äãä¯ÀÀewrk3_ioctl()´úÂëµÄʱºò,¼ÇסÔÚÒ»¸öÓ¦ÓÃÖÐÓû§¿Õ¼äµÄÖ¸ÁîÊÇÎÞ·¨·ÃÎÊÄں˿ռäµÄ,ÓÉÓÚÕâ¸öÔ­Òò ,2¸öÌØÊâµÄ²½ÖèÌṩ¸øÁËÇý¶¯±àдÈËÔ±.ËûÃÇÊÇmemcpy_tofs()ºÍmemcpy_fromfs()¡£ÄÚºËÀïµÄ×ö·¨ÊÇÓÃmemcpy_tofs() ¿½±´ÄÚºËÊý¾Ýµ½Óû§¿Õ¼ä,ÀàËƵÄmemcpy_fromfs()Ò²ÊÇÕâÑùµÄ,Ö»ÊÇËû¿½±´Óû§Êý¾Ýµ½Äں˿ռä.¡£ÕâЩ³ÌÐò²½ÖèÊÇÓÉÓÚµ÷ÓÃverify_area()¶ø±»Ö´ÐеÄ,Ä¿µÄÊÇÈ·ÈÏÊý¾Ý·ÃÎʲ»»áÎ¥·¨¡£Í¬Ñù¼Çסprintk()µÄÓ÷¨ÊÇ´òÓ¡µ÷ÊÔÐÅÏ¢,Õâ¸öº¯ÊýºÍprintf()ºÜÏàÏó,µ«ÊÇËü²»ÄÜ´¦Àí¸¡µãÊý¾Ý,printf()º¯ÊýÔÚÄÚºËÖÐÊDz»Äܱ»Ê¹Óõġ£ÓÉprintk()²úÉúµÄÊä³ö±»×ª´¢µ½ÁËÒ»¸öĿ¼./usr/adm/messages¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 4 ÌõÆÀÂÛ

  1. s_songlin ÓÚ 2011-01-18 16:26:38·¢±í:

    {:2_92:}

  2. sky3010 ÓÚ 2008-04-03 09:06:34·¢±í:

    ÎÒºÜÏëÖªµÀ´æ·ÅioctlÓ¦ÓÃʾÀýµÄĿ¼Goodiesµ½µ×ÔÚÄÄÀллÁË
    lwx3010@hotmail.com

  3. 218.108.8.* ÓÚ 2007-06-30 10:47:43·¢±í:

    û¿´¶®£¬°¦

  4. 60.12.8.* ÓÚ 2007-05-18 11:02:25·¢±í:

    ÀÏ´ó½²µÄÌ«ÉîÁË£¬Äܲ»Äܼò¶ÌÒ»µã°¡¡£±ÈÈçÎÒ¿´µ½Ò»¸ö½éÉÜwriteº¯ÊýµÄ¡£
    ssize_t write(int fd,const void *buf,size_t nbytes)

    writeº¯Êý½«bufÖеÄnbytes×Ö½ÚÄÚÈÝдÈëÎļþÃèÊö·ûfd.³É¹¦Ê±·µ»ØдµÄ×Ö½ÚÊý.ʧ°Üʱ·µ»Ø-1. ²¢ÉèÖÃerrno±äÁ¿.
    ¸ßÈËÄܲ»ÄÜÒÔÕâÖÖ·½Ê½½²Ò»ÏÂioctl°¡ £¬Ð»Ð»