조각코드(Snippets)




여기 소개하는 작은 단위의 조각코드들은 간편하게 빠르게 사용하기 위한 함수만 정리한 것입니다. 가급적 하기 코드 사용하실 경우 출처를 주석으로 함께 표기하여 사용 부탁합니다.

1.1. Assembly


1.2. C


  • Hexa dump 함수 - 주어진 data pointer로부터 size만큼 Hexa dump로 stdout으로 출력해줍니다.
    static void __inline hwport_dump(char *s_title, void *s_data, int s_size)
    {
     int s_o,s_w,s_i;unsigned char s_b[17];
     (void)fprintf(stdout, "%s:\n", s_title);
     s_b[16]='\0';s_o=(int)0;
     while(s_o<(s_size)){
      s_w=((s_size)-s_o)<16?((s_size)-s_o):16;printf("%08X",s_o);for(s_i=0;s_i<s_w;s_i++){if(s_i==8)printf(" | ");else printf(" ");
      s_b[s_i]=*(((unsigned char *)(s_data))+s_o+s_i);printf("%02X",s_b[s_i]);if((s_b[s_i]&0x80)||(s_b[s_i]<' '))s_b[s_i]='.';}
      while(s_i<16){if(s_i==8)printf("     ");else printf("   ");s_b[s_i]=' ';s_i++;}
      printf(" [%s]\n",(char *)s_b);s_o+=16;}
     (void)fprintf(stdout, "\n");
    }
    
  • GLIBC(libc)의 버젼을 확인하는 코드 (Run-time)
    #include <stdio.h>
    #include <gnu/libc-version.h>
    
    static void __inline hwport_print_glibc_version(void)
    {
        (void)fprintf(stdout, "glibc v%s (%s)\n", gnu_get_libc_version(), gnu_get_libc_release());
    }
    
  • Hotplug storage 장치(USB, CD-ROM등) 를 "장치제거" 하는 eject 예제
    #include <sys/types.h>
    #include <sys/ioctl.h>
    #include <stdio.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <getopt.h>
    #if 0L
    #include <linux/cdrom.h>
    #endif
    
    static int __inline hwport_eject(const char *s_device, int s_mode)
    {
     int s_result, s_cmd, s_handle = open(s_device, O_RDONLY | O_NONBLOCK);
     if(s_handle != (-1)) {
      if(s_mode != 0)  {
    #if defined(CDROMEJECT)
       s_cmd = CDROMEJECT;
    #else
       s_cmd = 0x5309;
    #endif
      }
      else  {
    #if defined(CDROMCLOSETRAY)
       s_cmd = CDROMCLOSETRAY;
    #else
       s_cmd = 0x5319;
    #endif
      }
      if(ioctl(s_handle, s_cmd) == 0)s_result = 1;
      else  {
       (void)fprintf(stdout, "error: can not control cdrom ! (device=\"%s\")\n", s_device);
       s_result = 0;
      }
      (void)close(s_handle);
     }
     else {
      (void)fprintf(stdout, "error: can not open cdrom ! (device=\"%s\")\n", s_device);
      s_result = (-1);
     }
     return(s_result);
    }
    
  • 간단하게 E-Mail 을 전송하는 예제 - 별도의 데몬서비스등에 의존하지 않고 직접 구현하는 방법
    #include <sys/types.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <memory.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #if 0
    char myhost[] = "infoeq.com";
    char myemail[] = "minzkn@infoeq.com";
    char rcptmail[] = "minzkn@infoeq.com";
    char subject[] = "E-Mail hacking test by minzkn";
    char body[] = "요것은 제가 한번 다른 사람을 인용하여 E-Mail을 보낼수 있는지 테스트 하는것입니다.";
    #else
    char myhost[] = "infoeq.com";
    char myemail[] = "minzkn@infoeq.com";
    char rcptmail[] = "minzkn@naver.com";
    char subject[] = "E-Mail hacking test by minzkn";
    char body[] = "요것은 제가 한번 다른 사람을 인용하여 E-Mail을 보낼수 있는지 테스트 하는것입니다.";
    #endif
    int main(int argc, char* argv[])
    {
        int c_sock;
        char buffer[10000];
        int len= 0, res;
        struct sockaddr_in cli_addr;
        struct hostent *s_host_entry;
        /*----------- 소켓 구조체 초기화 ---------------------*/
        c_sock = socket(PF_INET, SOCK_STREAM, 0);
        memset(&cli_addr, 0, sizeof cli_addr);
        cli_addr.sin_family = AF_INET;
        cli_addr.sin_port = htons(25);
        s_host_entry = gethostbyname((char *)myhost);
        if(s_host_entry != ((struct hostent *)0))
        {
         cli_addr.sin_addr.s_addr = *((unsigned long *)(s_host_entry->h_addr));
        }
        else cli_addr.sin_addr.s_addr = inet_addr(myhost);
        len = sizeof(cli_addr);
        res = connect(c_sock, (struct sockaddr*)&cli_addr, len);
        if(res<0)
        {
            perror("connect()");
            exit(1);
        }
        (void)fprintf(stdout, "connected.\n");
        /*------ (1) read and send my greeting. ---------------*/
        len = recv(c_sock, buffer, sizeof(buffer), 0);  /* 환영 메세지 읽음. */
        buffer[len] = 0;
        printf("%s", buffer);
        sprintf(buffer, "HELO unknown.ppp\n");
        send(c_sock, buffer, strlen(buffer), 0);
        /*------ (2) read and send my email address -----------*/
        len = recv(c_sock, buffer, sizeof(buffer), 0);
        buffer[len] = 0;
        printf("%s", buffer);
        sprintf(buffer, "MAIL FROM: %s\n", myemail);
        send(c_sock, buffer, strlen(buffer), 0);
        /*------ (3) read and send recipient address ----------*/
        len = recv(c_sock, buffer, sizeof(buffer), 0);
        buffer[len] = 0;
        printf("%s", buffer);
        sprintf(buffer, "RCPT TO: %s\n", rcptmail);
        send(c_sock, buffer, strlen(buffer), 0);
        /*------ (4) read and send DATA Header. ------------*/
        len = recv(c_sock, buffer, sizeof(buffer), 0);
        buffer[len] = 0;
        printf("%s", buffer);
        sprintf(buffer, "DATA\n");  /* double  */
        send(c_sock, buffer, strlen(buffer), 0);
        /*------ (5) read and send real body Header. ----------*/
        len = recv(c_sock, buffer, sizeof(buffer), 0);
        buffer[len] = 0;
        printf("%s", buffer);
        /*------ (6) read and send subject header. ------------*/
        sprintf(buffer, "SUBJECT: %s\n\n", subject);
        send(c_sock, buffer, strlen(buffer), 0 );
        sprintf(buffer, "%s\n", body);
        send(c_sock, buffer, strlen(buffer), 0 );
        sprintf(buffer, ".\n");
        send(c_sock, buffer, strlen(buffer), 0 );
        sprintf(buffer, "QUIT\n");
        send(c_sock, buffer, strlen(buffer), 0);
        /*------ (7) read Bye message -------------------------*/
        close(c_sock);
     return(EXIT_SUCCESS);
    }
    
  • Flow control 제어문자에 의한 Process stop 방지 ("stty -ixon")
    #include <termios.h>
    /* 사용법
        Flow control 끄려면 아래 링크릐 flow_control 함수소스를 긁어서 붙여 넣으시고 main 함수 선두에서 아래 3줄의 코드를 넣으시면 끝납니다.
        hwport_flow_controlSTDIN_FILENO, 0, 0, 0);
        hwport_flow_controlSTDOUT_FILENO, 0, 0, 0);
        hwport_flow_controlSTDERR_FILENO, 0, 0, 0);
    */
    static int __inline hwport_flow_control(int s_fd, int s_ixon, int s_ixoff, int s_ixany)
    {
        struct termios s_termios;
        if(tcgetattr(s_fd, (struct termios *)(&s_termios)) == (-1)) {
            return(-1);
        }
        if(s_ixon == 0) {
            s_termios.c_iflag &= (tcflag_t)(~(IXON));
        }
        else if(s_ixon != (-1)) {
            s_termios.c_iflag |= (tcflag_t)(IXON);
        }
        if(s_ixoff == 0) {
            s_termios.c_iflag &= (tcflag_t)(~(IXOFF));
        }
        else if(s_ixoff != (-1)) {
            s_termios.c_iflag |= (tcflag_t)(IXOFF);
        }
    #if defined(IXANY) /* This is not POSIX : IXANY */
        if(s_ixany == 0) {
            s_termios.c_iflag &= (tcflag_t)(~(IXANY));
        }
        else if(s_ixany != (-1)) {
            s_termios.c_iflag |= (tcflag_t)(IXANY);
        }
    #endif
        if(tcsetattr(s_fd, TCSANOW, (struct termios *)(&s_termios)) == (-1)) {
            return(-1);
        }
        return(0);
    }
    


1.3. Java


1.4. JavaScript


1.5. Python


1.6. Shell script




/*
[ FrontPage | PrintView | RawView | RSS ]

Copyright ⓒ MINZKN.COM
All Rights Reserved.

MINZKN
*/