




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、openlldp-0.4alpha實(shí)現(xiàn)詳解(七)lldp_main lldpneighbors模塊lldp_main.c模塊調(diào)用之前的各個(gè)模塊的接口,實(shí)現(xiàn)lldp功能。使用select進(jìn)行套接字的復(fù)用,每個(gè)一秒運(yùn)行一次端口發(fā)送和接收狀態(tài)機(jī)。1. #ifdefBUILD_SERVICE2. /Wearebuildingasaservice,sothisshouldbeourServiceMain()3. intServiceMain(intargc,char*argv)4. #else5. intmain(intargc,char*argv)6. #endif/BUILD_SERVICE7. 8
2、. #ifndefWIN32/-l-9. uid_tuid;10. structtimevaltimeout;11. structtimevalun_timeout;12. intfork=1;13. #endif14. intop=0;15. char*theOpts=i:d:fshl:o;16. intsocket_width=0;17. time_tcurrent_time=0;18. time_tlast_check=0;19. intresult=0;20. structlldp_port*lldp_port=NULL;21. /-l-22. 23. #ifdefBUILD_SERV
3、ICE/-w-24. ServiceStatus.dwServiceType=25. SERVICE_WIN32;26. ServiceStatus.dwCurrentState=27. SERVICE_START_PENDING;28. ServiceStatus.dwControlsAccepted=29. SERVICE_ACCEPT_STOP|30. SERVICE_ACCEPT_SHUTDOWN;31. ServiceStatus.dwWin32ExitCode=0;32. ServiceStatus.dwServiceSpecificExitCode=0;33. ServiceSt
4、atus.dwCheckPoint=0;34. ServiceStatus.dwWaitHint=0;35. 36. hStatus=RegisterServiceCtrlHandler(37. OpenLLDP,38. (LPHANDLER_FUNCTION)ControlHandler);39. if(hStatus=(SERVICE_STATUS_HANDLE)0)40. 41. /RegisteringControlHandlerfailed42. return-1;43. 44. /*ServiceStatus.dwCurrentState=SERVICE_STOPPED;45. S
5、erviceStatus.dwWin32ExitCode=0xfe;46. SetServiceStatus(hStatus,&ServiceStatus);*/47. #endif/BUILD_SERVICE/-w-48. 49. program=argv0;50. 51. #ifndefWIN32/-l-52. /Processanyargumentswewerepassedin.53. while(op=getopt(argc,argv,theOpts)!=EOF)54. switch(op)55. cased:/*debug設(shè)置調(diào)試等級*/56. /Setthedebuglevel.5
6、7. if(atoi(optarg)=0)&(optarg0!=0)/*非數(shù)字而是字母優(yōu)先級*/58. debug_alpha_set_flags(optarg);59. else/*數(shù)字優(yōu)先級*/60. debug_set_flags(atoi(optarg);61. 62. break;63. casei:/*interface使用接口*/64. iface_filter=1;65. memcpy(iface_list,optarg,strlen(optarg);66. iface_listIF_NAMESIZE-1=0;67. debug_printf(DEBUG_NORMAL,Usin
7、ginterface%sn,iface_list);68. break;69. casel:70. #ifdefUSE_CONFUSE/*USE_CONFUSE不懂干嘛的*/71. lci.config_file=optarg;72. #else73. debug_printf(DEBUG_NORMAL,OpenLLDPwasntcompiledwithlibconfusesupport.n);74. exit(1);75. #endif/USE_CONFUSE76. break;77. caseo:/*回環(huán)接口*/78. /Turnontheloobackinterface.:)79. pr
8、ocess_loopback=1;80. break;81. casef:/*fork標(biāo)志應(yīng)該是只是進(jìn)程是否fork到后臺*/82. fork=0;83. break;84. cases:/*刪除sun_path文件*/85. unlink(local.sun_path);86. break;87. caseh:/*h暫時(shí)無用*/88. default:89. usage();/*指示用法usage*/90. exit(0);91. break;92. ;93. 94. 95. /*96. 創(chuàng)建unix的套接字(屬于本地ipc)97. 主要用于將本進(jìn)程的信息輸出到另一個(gè)進(jìn)程98. */99.
9、neighbor_local_sd=socket(AF_UNIX,SOCK_STREAM,0);100. 101. if(neighbor_local_sd0)204. FD_ZERO(&unixfds);205. FD_SET(neighbor_local_sd,&unixfds);/*將neignbor_local_sd放入到unixfds描述符集合*/206. else207. debug_printf(DEBUG_NORMAL,Couldntinitializethesocket(%d)n,neighbor_local_sd);208. 209. 210. /*Setupselect(
10、)*/211. FD_ZERO(&readfds);212. #endif/WIN32213. 214. lldp_port=lldp_ports;215. 216. while(lldp_port!=NULL)/*將所有的套接字都放入readfds里面,然后進(jìn)行IO復(fù)用*/217. /Thisisnottheinterfaceyouarelookingfor.218. if(lldp_port-if_name=NULL)219. 220. debug_printf(DEBUG_NORMAL,ERRORInterfaceindex%dwithnameisNULLat:%s():%dn,lldp
11、_port-if_index,_FUNCTION_,_LINE_);221. continue;222. 223. 224. #ifndefWIN32225. FD_SET(lldp_port-socket,&readfds);/*lldp_port里面的套接字成員何時(shí)被賦值啊?端口所用的socket用來發(fā)送和接收*/226. 227. if(lldp_port-socketsocket_width)/*socket_width為記錄套接字最大的值*/228. 229. socket_width=lldp_port-socket;230. 231. #endif232. 233. lldp_p
12、ort=lldp_port-next;234. 235. 236. 237. time(t_time);/*記錄當(dāng)前的時(shí)間*/238. 239. #ifndefWIN32240. /Willbeusedtotellselecthowlongtowaitfor.241. timeout.tv_sec=1;/*等待一秒*/242. timeout.tv_usec=0;243. 244. /Timeoutafter1secondifnothingisready245. result=select(socket_width+1,&readfds,NULL,NULL,&timeout);/*每個(gè)一秒等待
13、數(shù)據(jù)報(bào)文的到達(dá),將該接口的所有數(shù)據(jù)進(jìn)行I/O復(fù)用*/246. #endif/WIN32247. 248. /Ecessthesockets249. lldp_port=lldp_ports;/*等待報(bào)文的到達(dá)*/250. 251. /*252. 循環(huán)遍歷所有套接字,并且每個(gè)端口都執(zhí)行一步發(fā)送/接收狀態(tài)機(jī)253. */254. while(lldp_port!=NULL)255. /Thisisnottheinterfaceyouarelookingfor.256. if(lldp_port-if_name=NULL)257. debug_printf(DE
14、BUG_NORMAL,ERRORInterfaceindex%dwithnameisNULLat:%s():%dn,lldp_port-if_index,_FUNCTION_,_LINE_);258. continue;/*本次的為空那么則執(zhí)行下次循環(huán)*/259. 260. 261. #ifndefWIN32262. if(result0)/*有數(shù)據(jù)到達(dá)*/263. if(FD_ISSET(lldp_port-socket,&readfds)/*測試是不是本次端口套接字可讀,若是則執(zhí)行否則什么也不執(zhí)行因?yàn)闆]有else分支*/264. debug_printf(DEBUG_INT,%sisrea
15、dable!n,lldp_port-if_name);265. 266. /GettheframebackfromtheOS-specificframehandler.267. lldp_read(lldp_port);268. 269. if(lldp_port-rx.recvsizerx.recvsize,lldp_port-if_name);275. 276. /debug_hex_dump(DEBUG_INT,lldp_port-rx.frame,lldp_port-rx.recvsize);277. 278. /Markthatwereceivedaframesothestatema
16、chinecanprocessit.279. lldp_port-rx.rcvFrame=1;280. 281. rxStatemachineRun(lldp_port);/*執(zhí)行數(shù)據(jù)收取狀態(tài)機(jī)*/*估計(jì)是在這里面完成MSAP的寫入*/282. 283. 284. 285. 286. #endif/WIN32287. /*本接口名非空,但是沒數(shù)據(jù)到達(dá),288. 則執(zhí)行tx和rx狀態(tài)機(jī)289. 然后等待本機(jī)上其他可執(zhí)行文件讀取其鄰居信息*/290. 291. if(result=0)|(current_timelast_check)292. lldp_port-tick=1;293. 294.
17、txStatemachineRun(lldp_port);295. rxStatemachineRun(lldp_port);296. 297. #ifndefWIN32298. /Willbeusedtotellselecthowlongtowaitfor.299. un_timeout.tv_sec=0;300. un_timeout.tv_usec=2;301. 302. /*這次等待主要是等unix套接字是否有連接請求到達(dá)*/303. result=select(neighbor_local_sd+1,&unixfds,NULL,NULL,&un_timeout);304. 305.
18、if(result0)306. if(FD_ISSET(neighbor_local_sd,&unixfds)307. debug_printf(DEBUG_NORMAL,Gotarequestontheunixdomainsocket!n);308. 309. socklen_taddrlen=sizeof(remote);310. 311. neighbor_remote_sd=accept(neighbor_local_sd,(structsockaddr*)&remote,&addrlen);312. 313. if(neighbor_remote_sd0)322. 323. debu
19、g_printf(DEBUG_NORMAL,Transmitting%dbytestoclient.n,bytes_tx);324. /*發(fā)送*/325. bytes_sent=send(neighbor_remote_sd,client_msg,strlen(client_msg),0);/*發(fā)送*/326. 327. debug_printf(DEBUG_NORMAL,%dbyteslefttosend.Bytesalreadysent:%dnn,bytes_tx,bytes_sent);328. 329. bytes_tx-=bytes_sent;330. 331. 332. 333.
20、free(client_msg);334. close(neighbor_remote_sd);335. 336. 337. 338. 339. #endif/WIN32340. 341. lldp_port-tick=0;342. 343. 344. if(resultnext;/*下個(gè)網(wǎng)卡接口*/351. 352. 353. 354. time(&last_check);355. 356. 357. return0;358. lldpneighbors模塊主要為獲取主機(jī)的鄰居信息。1. /*2. *3. *OpenLLDPNeighbor4. *5. *SeeLICENSEfileform
21、oreinfo.6. *7. *File:lldpneighbors.c8. *9. *Authors:TerrySimons(terry.simons)10. *11. */12. 13. #ifndefWIN3214. #include15. #include16. #include17. #include18. #endif19. 20. #include21. #include22. #include23. #include24. #include25. 26. #defineNEIGHBORLIST_SIZE51227. #defineDEBUG028. 29. intmain(in
22、targc,char*argv)30. charmsgNEIGHBORLIST_SIZE;31. char*buf=NULL;32. char*tmp=NULL;33. ints=0;34. unsignedintmsgSize=0;35. size_tbytes=0;36. intresult=0;37. 38. buf=calloc(1,NEIGHBORLIST_SIZE);39. 40. memset(&msg0,0x0,NEIGHBORLIST_SIZE);41. 42. #ifndefWIN3243. structsockaddr_unaddr;44. 45. s=socket(AF_UNIX,SOCK_STREAM,0);46. 47. addr.sun_family=AF_UNIX;48. /*49. 使用和lldp_main模塊一樣的unix套接字對象標(biāo)識50. */51. strcpy(addr.sun_path,/var/run/lldpd.sock);52. /*53.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 北京高考各科試題及答案解析視頻
- 臺州國企筆試題庫及答案
- 2024年高校輔導(dǎo)員學(xué)生關(guān)系管理及發(fā)展策略試題及答案
- 三年級數(shù)學(xué)計(jì)算題專項(xiàng)練習(xí)匯編及答案
- 2025至2030年痘天下搽液項(xiàng)目投資價(jià)值分析報(bào)告
- 輔導(dǎo)員如何引導(dǎo)學(xué)生自我探索與試題及答案
- 農(nóng)業(yè)職業(yè)經(jīng)理人考試答案分析試題及答案
- 高校輔導(dǎo)員在個(gè)人成長中的重要性研究試題及答案
- 食品合規(guī)考試試題及答案
- 各高校輔導(dǎo)員職業(yè)規(guī)劃與發(fā)展問題試題及答案
- 部編版六年級語文下冊期中考試卷(有答案)
- 生物-華大新高考聯(lián)盟2025屆高三3月教學(xué)質(zhì)量測評試題+答案
- 演出經(jīng)紀(jì)人資格證常見試題及答案分析
- 2024年河北建投集團(tuán)招聘工作人員考試真題
- 2025年湖北省八市高三(3月)聯(lián)考物理試卷(含答案詳解)
- 貴州國企招聘2024貴州磷化(集團(tuán))有限責(zé)任公司招聘89人筆試參考題庫附帶答案詳解
- 《哪吒電影產(chǎn)品的營銷問題及完善對策研究10000字》
- 4.2依法履行義務(wù) 課件-2024-2025學(xué)年統(tǒng)編版道德與法治八年級下冊
- 《上海金茂大廈》課件
- 2025年河南交通職業(yè)技術(shù)學(xué)院高職單招職業(yè)技能測試近5年??及鎱⒖碱}庫含答案解析
- 打造具有競爭力的農(nóng)行合規(guī)文化品牌
評論
0/150
提交評論