안녕하세요 리눅스에서 socket 프로그램 작성에 있어 궁금한점이 있어 문의드립니다.

sjenrdj의 이미지

안녕하세요 선배님들

리눅스에서 (정확히 말하면 라즈베리파이4)에서 socket 프로그램을 작성하고 있습니다.

작성 중에 몇가지 궁금한점이 있어 문의듸립니다.

1. 우선 라즈베리파이4가 server로 동작하게 프로그램을 작성하였습니다.

2. Client가 정상적으로 접속되어 있는 상태에서 라인이 끊어 졌을 때에 fault 체크를 하고 싶었습니다.

3. 그래서 socket_fd = socket(PF_INET, SOCK_RAW, t_proto->p_proto) 이라는 명령을 사용하여 ping을 날려
라인이 정상적으로 연결되어 있는지를 체크 할려고 했습니다.

4. 디버깅을 실행하니 아래와 같이 권한이 없다고 나와 문의 드립니다.
icmp() socket open failed : [01] Operation not permitted

5. 권한 설정을 어찌 할 수 있는지요?

----------------------추가 현재 제가 테스트 중인 상태입니다 -------------------------

1. windows10 기반에서 VScode를 이용하여 크로스 컴파일 환경입니다. (윈도우10에서 VS Code의 SSH를 이용하여 라즈베리파이에 접속하여 디버깅을 진행 중 입니다.)

-----------------------아래는 제가 제가 뺏겨온 코드입니다. ------------------

#define TIME_LOOP_WAIT          200000      // 200ms
#define COUNT_CHECK_LOOP        5
#define SIZE_ICMP_PACKET        64
 
 
struct ICMP_PACKET {
    struct icmphdr hdr;
    char msg[SIZE_ICMP_PACKET - sizeof(struct icmphdr)];
} typedef ICMP_PACKET_t;
 
 
unsigned short checksum(void* _data, int _length) 
{
    unsigned short* p_data;
    unsigned short result;
    unsigned int   sum = 0;
 
    p_data = (unsigned short*)_data;
 
    for (sum = 0; _length > 1; _length -= 2) 
    {
        sum += *p_data++;
    }
 
    if( _length == 1 ) 
    {
        sum += *(unsigned char*)p_data;
    }
 
    sum = (sum >> 16) + (sum & 0xFFFF);
    sum += (sum >> 16);
    result = ~sum;
 
    return result;
}
 
bool class_socket::icmp(socketOption_t& socketOption, std::string _address) 
{
    const int sock_value = 255;
 
    int socket_fd;
    int num_sequence = 1;
    int pid = getpid();
    int idx;
 
    struct sockaddr_in r_addr;
    struct hostent *hname;
    struct sockaddr_in addr_ping,*addr;
 
    ICMP_PACKET_t pckt;
 
    int size_packet_msg = sizeof(pckt.msg);
 
    struct protoent *t_proto = NULL;
    socklen_t len;
 
    t_proto = getprotobyname("ICMP");
 
    hname = gethostbyname(_address.c_str());
 
    bzero(&addr_ping, sizeof(addr_ping));
 
    addr_ping.sin_family = hname->h_addrtype;
    addr_ping.sin_port   = 0;
    addr_ping.sin_addr.s_addr = *(long*)hname->h_addr;
 
    addr = &addr_ping;
 
    int sd;
 
    if( (socket_fd = socket(PF_INET, SOCK_RAW, t_proto->p_proto)) < 0 ) {
        printf("icmp() socket open failed : [%02d] %s\n", errno, strerror(errno));
        return false;
    }
 
    if( setsockopt(socket_fd, SOL_IP, IP_TTL, &sock_value, sizeof(sock_value)) != 0) {
        printf("icmp() set TTL option failed : [%02d] %s\n", errno, strerror(errno));
        return false;
    }
 
    if ( fcntl(socket_fd, F_SETFL, O_NONBLOCK) != 0 ) {
        printf("icmp() request nonblocking I/O failed : [%02d] %s\n", errno, strerror(errno));
        return false;
    }
 
 
    for( int loop_cnt = 0 ; loop_cnt < COUNT_CHECK_LOOP ; loop_cnt++ ) {
        len = sizeof(r_addr);
        if( recvfrom(socket_fd, &pckt, sizeof(pckt), 0x00, (struct sockaddr*)&r_addr, &len) > 0 ) {
 
            close(socket_fd);
            return true;
        }
 
        bzero(&pckt, sizeof(pckt));
        pckt.hdr.type = ICMP_ECHO;
        pckt.hdr.un.echo.id = pid;
 
        for( idx = 0; idx < size_packet_msg - 1 ; idx++ ) {
            pckt.msg[idx] = idx + '0';
        }
 
        pckt.msg[idx] = 0;
        pckt.hdr.un.echo.sequence = num_sequence++;
        pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
 
        if( sendto(socket_fd, &pckt, sizeof(pckt), 0, (struct sockaddr*)addr, sizeof(*addr)) <= 0 ) 
        {
            printf("icmp() sendto failed : [%02d] %s\n", errno, strerror(errno));
        }
 
        usleep(TIME_LOOP_WAIT);
    }
 
    close(socket_fd);
    return false;
}

서지훈의 이미지

통상 RAW socket은 root만 실행 가능합니다.
sudo로 실행 해보세요.

#include <com.h> <C2H5OH.h> <woman.h>
do { if (com) hacking(); if (money) drinking(); if (women) loving(); } while (1);

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.