Microsoft MVP성태의 닷넷 이야기
사물인터넷: 15. 라즈베리 파이용 C++ 프로젝트에 SSL Socket 적용 [링크 복사], [링크+제목 복사],
조회: 16215
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 10개 있습니다.)

라즈베리 파이용 C++ 프로젝트에 SSL Socket 적용

라즈베리 파이 등의 경우 C++에서 openssl을 사용하려면 다음과 같이 설치하면 됩니다.

sudo apt-get install libssl-dev

그런 다음 Visual Studio의 C++ 프로젝트 속성 창에서 -pthread를 사용했을 때와 유사하게,

Raspberry Pi/Windows 다중 플랫폼 지원 컴파일 관련 오류 기록
; https://www.sysnet.pe.kr/2/0/11373

컴파일 옵션에 직접 다음의 값을 추가합니다.

-lcrypto -lssl

만약 "-lcrypto -lssl" 컴파일 옵션을 주지 않으면 링크 시에 다음과 같이 openssl 관련 함수들을 찾을 수 없다는 오류가 발생합니다.

collect2 : error : ld returned 1 exit status

Error undefined reference to `SSL_read'




웬일인지 NR_OPEN 상수가 정의되지 않았다는 오류가 발생할 수 있습니다.

Error 'NR_OPEN' was not declared in this scope

분명히 limits.h 헤더 파일을 포함시켰고, 헤더 파일 안에는 NR_OPEN 상수가 조건 없이 정의되어 있습니다.

#include <linux/limits.h>

#define NR_OPEN 1024

정확한 원인을 알 수는 없지만 다음과 같이 우회를 했습니다.


#if !defined(_NR_OPEN)
#define NR_OPEN 1024
#endif




참고로, 라즈베리 파이에서 특정 파일을 찾고 싶을 때 다음과 같이 find 명령어를 내리면 됩니다.

sudo find / -name "..."

가령 openssl의 ssl.h 헤더 파일이 리눅스 파일 시스템의 어디에 위치해 있는지 알고 싶다면 다음과 같이 사용할 수 있습니다.

$ sudo find / -name ssl.h
/usr/include/openssl/ssl.h

$ find / -name ssl.h 2>/dev/null

// WSL 환경의 경우 /mnt 디렉터리를 제외하고 싶다면?
$ find / -path /mnt -prune -o -name python 2>/dev/null

윈도우의 경우에는 "dir /a/s ssl.h"라고 하는 것과 유사합니다.




openssl의 초기화 시 인증서와 키 파일의 경로를 지정해야 하는데, 실행 모듈의 경로와 같은 위치에 있는 것을 지정하려고 다음과 같이 사용하면,

int use_cert = SSL_CTX_use_certificate_file(sslContext, "test.pem", SSL_FILETYPE_PEM);
int use_prv = SSL_CTX_use_PrivateKey_file(sslContext, "key.pem", SSL_FILETYPE_PEM);

아래와 같이 해당 실행 모듈이 있는 경로로 이동 후 실행하면 정상적으로 파일을 찾는 반면,

$ cd /share
$ ./test.out

다른 경로에서 직접 실행하게 되면,

$ /share/test.out

test.out과 같은 위치의 파일을 못 찾고 pwd로 출력한 경로를 기준으로 찾게 됩니다. 사실 윈도우에서도 그렇기 때문에 당연한 결과입니다. 따라서 GetModuleFileName과 같은 Win32 API를 사용해 모듈 경로를 직접 구하는 식의 전처리가 필요한데요, 리눅스에서는 "/proc/[pid]/exe"라는 링크를 통해 실행 모듈의 경로를 구할 수 있습니다.

How to find the full path of the C++ Linux program from within?
; https://stackoverflow.com/questions/7051844/how-to-find-the-full-path-of-the-c-linux-program-from-within

[pid]가 아닌 현재 실행되고 있는 프로세스 내에서라면 self 문자열을 사용할 수 있습니다.

/proc/self/exe

다음은 "How to find the full path of the C++ Linux program from within?" 글의 답변을 참고해 작성한 것입니다.

void get_module_dir_path(char modulePath[])
{
    char arg1[] = "/proc/self/exe";
    char exepath[PATH_MAX + 1] = { 0 };

    readlink(arg1, exepath, PATH_MAX);
    strcpy(modulePath, dirname(exepath));

    printf("module path: %s\n", modulePath);
}

암튼, 제가 리눅스 초보자이다 보니 간단한 Win32 API에 상응하는 수준의 것도 저렇게 찾아보게 되는군요. ^^




참고로, 위의 정보를 기반으로 지난번에 만들었던 라즈베리 파이 제로를 위한 가상 USB 키보드/마우스 장치에 SSL 소켓 서버를 구현해 두었습니다. ^^

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드 및 마우스로 쓰는 방법 (절대 좌표, 상대 좌표, 휠)
; https://www.sysnet.pe.kr/2/0/11369

rasp_vusb 
; https://github.com/stjeong/rasp_vusb



윈도우즈 사용자를 위한 라즈베리 파이 제로 W 모델을 설정하는 방법
; https://www.sysnet.pe.kr/2/0/11372

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 이더넷 카드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11353

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11354

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11355

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11356

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법 (절대 좌표)
; https://www.sysnet.pe.kr/2/0/11364

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드 및 마우스로 쓰는 방법 (절대 좌표, 상대 좌표, 휠)
; https://www.sysnet.pe.kr/2/0/11369

라즈베리 파이 용 C++ 프로젝트에 SSL Socket 적용
; https://www.sysnet.pe.kr/2/0/11411

Raspberry Pi/Windows 다중 플랫폼 지원 컴파일 관련 오류 기록
; https://www.sysnet.pe.kr/2/0/11373

Linux 3: 라즈베리 파이 - (윈도우의 NT 서비스처럼) 부팅 시 시작하는 프로그램 설정
; https://www.sysnet.pe.kr/2/0/11374


[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 11/24/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12207정성태4/2/20208951스크립트: 19. Windows PowerShell의 NonInteractive 모드
12206정성태4/2/202011244오류 유형: 613. 파일 잠금이 바로 안 풀린다면? - The process cannot access the file '...' because it is being used by another process.
12205정성태4/2/20208637스크립트: 18. Powershell에서는 cmd.exe의 명령어를 지원하진 않습니다.
12204정성태4/1/20208440스크립트: 17. Powershell 명령어에 ';' (semi-colon) 문자가 포함된 경우
12203정성태3/18/202010476오류 유형: 612. warning: 'C:\ProgramData/Git/config' has a dubious owner: '...'.
12202정성태3/18/202013093개발 환경 구성: 486. .NET Framework 프로젝트를 위한 GitLab CI/CD Runner 구성
12201정성태3/18/202010887오류 유형: 611. git-credential-manager.exe: Using credentials for username "Personal Access Token". [1]
12200정성태3/18/202011327VS.NET IDE: 145. NuGet + Github 라이브러리 디버깅 관련 옵션 3가지 - "Enable Just My Code" / "Enable Source Link support" / "Suppress JIT optimization on module load (Managed only)"
12199정성태3/17/20209181오류 유형: 610. C# - CodeDomProvider 사용 시 Unhandled Exception: System.IO.DirectoryNotFoundException: Could not find a part of the path '...\f2_6uod0.tmp'.
12198정성태3/17/202011921오류 유형: 609. SQL 서버 접속 시 "Cannot open user default database. Login failed."
12197정성태3/17/202011065VS.NET IDE: 144. .NET Core 콘솔 응용 프로그램을 배포(publish) 시 docker image 자동 생성 - 두 번째 이야기 [1]
12196정성태3/17/20208995오류 유형: 608. The ServicedComponent being invoked is not correctly configured (Use regsvcs to re-register).
12195정성태3/16/202010708.NET Framework: 902. C# - 프로세스의 모든 핸들을 열람 - 세 번째 이야기
12194정성태3/16/202013028오류 유형: 607. PostgreSQL - Npgsql.NpgsqlException: sorry, too many clients already
12193정성태3/16/20209711개발 환경 구성: 485. docker - SAP Adaptive Server Enterprise 컨테이너 실행 [1]
12192정성태3/14/202012153개발 환경 구성: 484. docker - Sybase Anywhere 16 컨테이너 실행
12191정성태3/14/202012528개발 환경 구성: 483. docker - OracleXE 컨테이너 실행 [1]
12190정성태3/14/20208646오류 유형: 606. Docker Desktop 업그레이드 시 "The process cannot access the file 'C:\Program Files\Docker\Docker\resources\dockerd.exe' because it is being used by another process."
12189정성태3/13/202013488개발 환경 구성: 482. Facebook OAuth 처리 시 상태 정보 전달 방법과 "유효한 OAuth 리디렉션 URI" 설정 규칙
12188정성태3/13/202015792Windows: 169. 부팅 시점에 실행되는 chkdsk 결과를 확인하는 방법
12187정성태3/12/20208465오류 유형: 605. NtpClient was unable to set a manual peer to use as a time source because of duplicate error on '...'.
12186정성태3/12/20209539오류 유형: 604. The SysVol Permissions for one or more GPOs on this domain controller and not in sync with the permissions for the GPOs on the Baseline domain controller.
12185정성태3/11/202010231오류 유형: 603. The browser service was unable to retrieve a list of servers from the browser master...
12184정성태3/11/202011640오류 유형: 602. Automatic certificate enrollment for local system failed (0x800706ba) The RPC server is unavailable. [3]
12183정성태3/11/202010009오류 유형: 601. Warning: DsGetDcName returned information for \\[...], when we were trying to reach [...].
12182정성태3/11/202011216.NET Framework: 901. C# Windows Forms - Vista/7 이후의 Progress Bar 업데이트가 느린 문제파일 다운로드1
... 46  47  48  49  50  51  52  53  54  55  56  [57]  58  59  60  ...