2015-04-21 84 views
0

我試圖找到簡單的方法來選擇和隨機端口監聽。我可以遍歷所有端口並嘗試打開每個端口,然後將此fd傳遞給BIO,但此解決方案太複雜並且可能被破壞。另外,我搜索便攜式解決方案。openssl和隨機端口監聽

而不是我搜索單個OpenSSL函數,但無法找到任何。是否有像OpenSSL庫提供的單一函數調用的簡單解決方案?

關於我的迴應:

我有麻煩,同時通過 「*」 或 「*:*」 來BIO_new_accept和BIO_get_accept_port得到端口號。在這種情況下,BIO_get_accept_port完全返回我傳遞給BIO_new_accept的字符串。任何建議如何獲取端口號?

附加編輯:

我發現我必須調用之後星號撥打BIO_do_accept到BIO_new_accept,但有錯誤:

140418757990096:錯誤:02003000:系統庫:getservbyname函數:系統庫:b_sock.c :224:service ='*'

+0

您的意思是說服務器應該隨機綁定到一個端口,並且監聽而不是特定的端口? (就像客戶如何不特別綁定)? – Prabhu

+0

我的意思是服務器應綁定在第一個自由端口上。客戶端知道端口號,因爲服務器運行客戶端並將端口號設置爲環境變量。 – nintyfan

回答

2

我可能找到了一個解決方案。因爲我的英文很差,所以我沒有完全理解文檔文字。 我再次閱讀文檔,我發現一個解決方案! 從網站: http://linux.die.net/man/3/bio_do_accept

BIO_set_accept_port() uses the string name to set the accept port. The port is represented as a string of the form "host:port", where "host" is the interface to use and "port" is the port. Either or both values can be "*" which is interpreted as meaning any interface or port respectively. "port" has the same syntax as the port specified in BIO_set_conn_port() for connect BIOs, that is it can be a numerical port string or a string to lookup using getservbyname() and a string table.

所以我只能通過星號字符BIO_do_accept。

將asterix字符傳遞給BIO_new_accept不起作用,因爲BIO_do_accept返回錯誤。我以這種方式解決這個問題:

而不是將asterix傳遞給BIO_new_accept我從一個開始增加int,併爲每個數字執行BIO_new_accept。