ZMQ绀轰緥锛氫娇鐢?curve 杩涜鍔犲瘑閫氫俊

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZMQ绀轰緥锛氫娇鐢?curve 杩涜鍔犲瘑閫氫俊相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%80%8e%e4%b9%88' title='鎬庝箞'>鎬庝箞   鍛戒护   dir   wfs   too   ora   art   閾炬帴   鍒嗘瀽   

1. ZMQ 瀹樻柟鏂囨。

ZMQ 鐨勫畼鏂规枃妗?/span>涓叧浜?curve 鐨勪粙缁嶅涓嬶細

Client and server roles

A socket using CURVE can be either client or server, at any moment, but not both. The role is independent of bind/connect direction.

A socket can change roles at any point by setting new options. The role affects all zmq_connect and zmq_bind calls that follow it.

To become a CURVE server, the application sets the ZMQ_CURVE_SERVER option on the socket, and then sets the ZMQ_CURVE_SECRETKEY option to provide the socket with its long-term secret key. The application does not provide the socket with its long-term public key, which is used only by clients.

To become a CURVE client, the application sets the ZMQ_CURVE_SERVERKEY option with the long-term public key of the server it intends to connect to, or accept connections from, next. The application then sets the ZMQ_CURVE_PUBLICKEY and ZMQ_CURVE_SECRETKEY options with its client long-term key pair.

If the server does authentication it will be based on the client鈥榮 long term public key.

銆€銆€

瀹樻柟鏂囨。涓彁鍒扮殑鍑犱釜鐐归兘寰堝叧閿細

  • 涓€涓?curve socket 鏃㈠彲浠ユ槸鏈嶅姟绔紝涔熷彲浠ユ槸瀹㈡埛绔€備絾涓嶅彲浠ユ棦鏄湇鍔$鐨勫悓鏃跺張鏄鎴风
  • 涓€涓?socket 鍙互鍦ㄤ换浣?point 锛堜笉鐭ラ亾鎬庝箞缈昏瘧 point 鎵嶅悎閫傦級涓婇€氳繃璁剧疆鏂板睘鎬х殑鏂瑰紡鏀瑰彉瑙掕壊锛?鏀瑰彉涓€涓?socket 瑙掕壊鍚庯紝浼氬奖鍝嶆帴涓嬫潵瀵硅繖涓?socket 鍋氱殑鎵€鏈?zmq_connect() 鍜?zmq_bind() 璋冪敤锛?涔熷氨鏄闇€瑕佸湪杩欎袱涓皟鐢ㄤ箣鍓嶈繘琛?curve 灞炴€х殑璁剧疆銆?/span>
  • 濡傛灉瑕佹妸涓€涓?socket 浣滀负 curve server 锛岄渶瑕佽缃?ZMQ_CURVE_SERVER 鍜?ZMQ_CURVE_SECRETKEY 灞炴€?/span>
  • 濡傛灉瑕佹妸涓€涓?socket 浣滀负 curve client锛岄渶瑕佽缃?ZMQ_CURVE_SERVERKEY 浠ュ強 ZMQ_CURVE_PUBLICKEY銆乑MQ_CURVE_SECRETKEY 杩欎笁涓睘鎬?/span>

2. 鍑犺鏍稿績浠g爜

鎯宠浣跨敤 ZMQ curve 杩涜鍔犲瘑閫氫俊锛岄渶瑕佸湪 ZMQ 鐨?server 绔拰 client 绔繘琛岀浉搴旂殑閰嶇疆锛?鍑犺闇€瑕佽繘琛岀殑閰嶇疆浠g爜濡備笅锛?/span>

2.1 curve server 绔?/span>

1 char szsecertkey[128] = {0};
2 char szpublickey[128] = {0}; //闇€瑕佹妸杩欎釜 curve 鍏挜鍙戠粰 client
3 zmq_curve_keypair(szpublickey, szsecertkey);
4 
5 int option = 1;
6 zmq_setsockopt(sock, ZMQ_CURVE_SERVER, &option, sizeof(option));
7 zmq_setsockopt(sock, ZMQ_CURVE_SECRETKEY, szsecertkey, strlen(szsecertkey));
8 
9 zmq_bind(sock, szaddr);

 

2.2 curve client 绔?/span>

//杩欎釜 server_publickey 灏辨槸浠?server 绔嬁鍒扮殑 curve 鍏挜
zmq_setsockopt(sock, ZMQ_CURVE_SERVERKEY, server_publickey, strlen(server_publickey));

char szpubkey[128] = {0};
char szprikey[128] = {0};
zmq_curve_keypair(szpubkey, szprikey); 
zmq_setsockopt(sock, ZMQ_CURVE_PUBLICKEY, szpubkey, strlen(szpubkey));
zmq_setsockopt(sock, ZMQ_CURVE_SECRETKEY, szprikey, strlen(szprikey));

zmq_connect(sock, szaddr);

 

3. 瀹屾暣浠g爜鍙婃墽琛?/span>
3.1 瀹屾暣浠g爜

浠g爜閾炬帴: https://pan.baidu.com/s/1ytJdEa2VmYWfSx6mxIKYVw 鎻愬彇鐮? ebyv

涓嬭浇浠g爜鍚庯紝瑙e帇锛屾墽琛岀紪璇戝懡浠わ細

g++ -g -o server server.c -lzmq -lpthread 
g++ -g -o client client.c -lzmq

 


3.2 绋嬪簭鎵ц

浠g爜婕旂ず浜嗕袱涓ā寮忥紝涓€涓槸浣跨敤 curve 鍔犲瘑鐨勪紶杈撴ā寮忥紝鍙︿竴涓槸涓嶄娇鐢?curve 鍔犲瘑鐨勪紶杈撴ā寮忋€?濡傛灉瑕佷娇鐢?curve 鍔犲瘑鐨勪紶杈撴ā寮忥紝闇€瑕佸湪鎵ц server 鍜?client 绋嬪簭鐨勬椂鍊欓兘鍔犱笂 -s 鍙傛暟銆?/span>

1) 鍏堟墽琛?server

1 [[email protected] 14:46:19 curve]$ ./server -s
2 server.c 80 : test tZMQ with curve
3 server.c 90 : szsecertkey = [I%43ZHg-Bnls&6rZ&)<J6hwGlD&I5l7]mi]{Ch-7]
4 server.c 91 : szpublickey = [kvIP^po{(kRL<eib#Zv=.cE%BpNom2DY#jl0-Ro[]
5 server.c 102 : start secert comunication
6 server.c 112 : recv msg : [I say 1]
7 server.c 119 : send msg : [I say 1, too]

 

2) 鍐嶆墽琛?Client

1 [[email protected] 14:46:18 curve]$ ./client -s
2 client.c 82 : test tZMQ with curve
3 client.c 94 : server publickey : [kvIP^po{(kRL<eib#Zv=.cE%BpNom2DY#jl0-Ro[]
4 client.c 102 : start secert comunication
5 client.c 109 : send msg : [I say 1]
6 client.c 112 : recv msg : [I say 1, too]

 

4. ZMQ curve 鍔犲瘑鏁堟灉鍒嗘瀽
閫氳繃瀵瑰姞瀵嗗墠涓庡姞瀵嗗悗鐨?ZMQ 娑堟伅杩涜 tcpdump 鎶撳寘锛屽彲浠ョ湅鍒?ZMQ curve 鐨勫姞瀵嗘晥鏋滃浣曘€?/span>

鍥犱负浣跨敤鐨勬槸 127.0.0.1 浣滀负娴嬭瘯鍦板潃锛屾墍浠ュ湪浣跨敤 tcpdump 鎶撳寘鐨勬椂鍊欓€夋嫨 lo 缃戝彛锛?/span>

tcpdump -i lo -A -vv -X

 


4.1 鍔犲瘑鍓嶇殑鎶撳寘璁板綍

瀵逛簬涓嶅彲鎵撳嵃鐨勫瓧绗︼紝tcpdump 閮戒互鐐瑰彿 鈥?鈥?杩涜浜嗘樉绀?/span>

 1 14:13:49.788893 >.E.........
 2 10:53:07.333841 <.E.>.E.....
 3 14:13:49.788919 >.E.
 4 14:13:49.788990 <.E...........
 5 14:13:49.788997 >.E.
 6 14:13:49.789294 <.E............
 7 14:13:49.789303 >.E.
 8 14:13:49.789634 <.E...NULL................................................
 9 14:13:49.789875 >.E..NULL................................................
10 14:13:49.789900 <.E..).READY.Socket-Type....DEALER.Identity....
11 14:13:49.790309 >.E.
12 14:13:49.790369 <.E..).READY.Socket-Type....DEALER.Identity......I.say.1
13 14:13:49.790561 >.E...I.say.1,.too
14 14:13:49.829945 <.E.

 

涓婇潰鏄剧ず鐨勪俊鎭槸缁忚繃绠€鍖栧悗鐨勮В鏋愭暟鎹紝涓嶈繃鍙互鐪嬪埌 ZMQ 鐨勪袱涓?socket 涔嬮棿閫氫俊鐨勭粏鑺傦紝渚嬪鍙互鐪嬪埌鍑犱釜 ZMQ 鐨勫叧閿瓧锛?DEALER銆?strong>Socket-Type銆?strong>Identity銆?/span>

浠ュ強 client 鍙戦€佺殑鏁版嵁 I say 1 锛屽拰 server 杩斿洖鐨勬暟鎹?I say 1, too銆?/span>

寰堟槑鏄撅紝鏁版嵁鍦?ZMQ 涓槸浠ユ槑鏂囪繘琛屼紶杈撶殑銆?/span>

4.2 鍔犲瘑鍚庣殑鎶撳寘璁板綍

15:05:06.178841 ....>.6/........
09:09:44.849972 ....>.6/>.6/....
15:05:06.178864 >.6/>.6/
15:05:06.178975 >.6/>.6/..........
15:05:06.178982 >.6/>.6/
15:05:06.179369 >.6/>.6/...........
15:05:06.179376 >.6/>.6/
15:05:06.180410 >.60>.6/..NULL................................................
15:05:06.180571 >.61>.60.NULL................................................
15:05:06.180588 >.61>.60.).READY.Socket-Type....DEALER.Identity....
15:05:06.181017 >.61>.61
15:05:06.181064 >.61>.61.).READY.Socket-Type....DEALER.Identity......request.publickey
15:05:06.181191 >.61>.61.((#65P{8[email protected]/[email protected]@gN{4nF3:?jz<^-
15:05:06.183369 ....>.63........
09:10:02.029846 ....>.63>.63....
15:05:06.183389 >.63>.63
15:05:06.183485 >.64>.63..........
15:05:06.183491 >.64>.64
15:05:06.183676 >.64>.64...........
15:05:06.183681 >.64>.64
15:05:06.184210 >.64>.64..CURVE...............................................
15:05:06.185457 >.66>.64.CURVE...............................................
15:05:06.188362 >.68>.66...HELLO........................................................
                 ....................{...:H_....."R&.....w.N..r..L...........Rv(b...F...
                 -+u.w....M...j).B.!<j.鈥?/span>...(..j...*..Qw........G.....c.....Y....:...
15:05:06.190931 >.6;>.68...WELCOME..6k.......,......a}.R..C.....>....|..a.z#...A[;..Yy..
                 ..F`......k.......~...k.fDa./../.A7..m..J......i.b.......<.k.....T.....
                 ..{[email protected]
15:05:06.196571 >.6A>.6;........$.INITIATEX......fXA..{J%D<...O.i?4.3$.../.../J#....ZpF
                 xZ...r...pj...C.&.k,...]`KF..|.i4.H(K..W..Fm".BU...........!...x....<..
                 ....T....Z...6a....A....P.......c.V...........[E.n...|`@.~..5.aua..p...
                 #..N.7...._.....A....!.}....^Y2=2..)5..K.(.6+.e6]..`..$;..k".g.].~.A.Hn
                 ...GS%.U.ot..whb<..>..D15:05:06.201254 >.6E>.6A.A.READY.........H.f.n....sS.....p.y.ZM..F........l........wY.3?G.
15:05:06.202853 >.6G>.6E.(.MESSAGE..........t.oR..&:.SI..QI.m.....
15:05:06.203034 >.6G>.6G.-.MESSAGE.............O]i.......9.M....to....#
15:05:06.220452 >.6Y>.61
15:05:06.242435 >.6o>.6G
15:05:08.539483 >.?h>.6G.(.MESSAGE.........3n..l6~~..N.......[:T.
15:05:08.539814 >.?h>.?h.-.MESSAGE...........<.3..A.9..Mq..=.........vs
15:05:08.539824 >.?h>.?h

 

浠?tcpdump 鎶撳彇鐨勬暟鎹彲浠ョ湅鍒?CURVE 杩欐牱鐨勫瓧绗︼紝杩欒鏄?ZMQ 寮€濮嬪缓绔?curve 鍔犲瘑閾炬帴浜嗐€?涓嶈繃鍦ㄥ缓绔?curve 閾炬帴涔嬪墠锛屾湁涓€涓?client 鍚?server 璇锋眰鍏挜鐨勬搷浣滐紝鎴戜滑鐢氳嚦鍙互鍦ㄦ姄鍒扮殑鏁?鎹腑鐪嬪埌 server 鍙戦€佺粰 client 鐨勫叕閽?[(#65P{[email protected]/[email protected]@gN{4nF3:?jz<^-]銆?/span>

鍥犱负浼犺緭鍏挜鏄湪寤虹珛 curve 閾炬帴涔嬪墠杩涜鐨勶紝 鎵€浠ユ垜浠繕鏄彲浠ョ湅鍒版槑鏂囨暟鎹殑銆?/span>

鍦ㄤ笂闈㈢殑鎶撳寘鏁版嵁涓紝 CURVE 鍏抽敭瀛楀嚭鐜扮殑鏃跺€欙紝灏辨槸 server 鍜宑lient 寮€濮嬭繘琛?curve 閾炬帴鐨勮繃绋嬩簡锛?鍙互鐪嬪埌杩欏嚑涓叧閿瓧锛欻ELLO銆乄ELCOME銆両NITIATEX銆丷EADY銆丮ESSAGE銆傝繖浜涢兘鏄?ZMQ 鐨?curve 鍗忚鐨勪竴閮ㄥ垎銆?/span>

鍦ㄤ笂闈㈢殑鎶撳寘鏁版嵁涓紝鎴戜滑娌℃湁鐪嬪埌 I say 1锛屼篃娌℃湁鐪嬪埌 I say 1, too銆傚洜涓鸿繖浜涙暟鎹凡缁忚鍔犲瘑浼犺緭浜嗐€?涓嶈繃鎴戜滑鍙互鐚滃埌锛孖 say 1 鍜?I say 1, too 灏辫窡鍦ㄦ渶鍚庝笁琛岀殑涓や釜 MESSAGE 鐨勫悗闈紝鍙笉杩囪鍔犲瘑浜嗭紝鎴戜滑鐪嬩笉鍒般€?/span>


 

鍚屾鍙戣〃锛?a href="https://www.fengbohello.top/archives/zeromq-curve" target="_blank">https://www.fengbohello.top/archives/zeromq-curve

以上是关于ZMQ绀轰緥锛氫娇鐢?curve 杩涜鍔犲瘑閫氫俊的主要内容,如果未能解决你的问题,请参考以下文章

python鍑芥暟

android寮€鍙戠瑪璁颁箣缃戠粶缂栫▼鈥斾娇鐢═CP鍗忚鍜孶RL杩涜缃戠粶缂栫▼

iOS涔嬮挜鍖欎覆鍔犲瘑瑙e瘑

鎬庝箞鐞嗚В鈥滈潪瀵圭О鍔犲瘑鈥濓紵

缃戞槗浜戦煶涔愯瘎璁虹埇铏?js鍙傛暟鍔犲瘑

matlab鑷甫鍚勭鍒嗙被鍣ㄧ殑浣跨敤绀轰緥