U E D R , A S I H C RSS

이영호/미니프로젝트#1

~cpp 
  행 하.
  2000~3000        .

// 한  C++  C     3   .
// 6000~9000  ...
//    C++  C 0.8    .
// 클      .

 : IRC bot
 : irc     . &  AI( AI 패) bot   
OS : Linux 
Language : C & Linux System Function

 :       .
 : Zombie Process   System Call   .

1. Client Console   IRC Server  . -> Main Process
2.    PING   -> 1 Child Process
3.        -> 2 Chile Process
 (3 Master    -> Private   IP   .)


main.c -> IRC Server      .
parse.c -> IRC Server   .
file.c -> 파    Log하  .      .
request.c -> IRC Server   PING 한 PONG .
bot.c ->  한 함. 
attack_#.c(# ==  ) -> .   크하   .
// 향       .
connect.c -> .    //   .


(2  ...)
  : main , parse PING , request 


  .  !   .
// signal .
// fork  .


~cpp 
//  .   .
//    .
//  #linux   .
//     privmsg process call mirc  함.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <arpa/inet.h>

#define MSG_MAX 1024
#define NICK    "whoami_"
#define HOST    "irc.hanirc.org"
#define PORT    "6667"

void error_handling(char *msg);
void handler(int sig);

typedef struct _info{
        char *nick;
        char *host;
        char *port;
        char *user_name;
        char *user_whois1;
        char *user3;
        char *user_whois2;
} INFO;

INFO info = {NICK, HOST, PORT, "a", "b", "c", "d"};

int main(int argc, char *argv[])
{
        int sockfd;
        struct sockaddr_in ina;
        struct sigaction act;
        struct hostent *h;
        pid_t pid;
        char msg[MSG_MAX];
        int msg_len;
        char check_ping[7];
        char pong[11];
        int join_flag = 0;

        sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
        if(sockfd == -1)
                error_handling("socket() error: sockfd");

        h = gethostbyname(info.host);

        memset(&ina, 0, sizeof(ina));
        ina.sin_family = AF_INET;
        ina.sin_addr = *(struct in_addr *)h->h_addr_list[0];
        ina.sin_port = htons(atoi(info.port));

        if(connect(sockfd, (struct sockaddr *)&ina, sizeof(ina)) == -1)
                error_handling("connect() error: ");

        act.sa_handler = handler;
        sigemptyset(&act.sa_mask);
        act.sa_flags = 0;

        sigaction(SIGCHLD, &act, 0);

        pid = fork();

        if(pid == -1)
                error_handling("fork() error: ");
        else if(pid == 0){
                while(1){
                        msg_len = recv(sockfd, msg, MSG_MAX-1, 0);
                        if(msg_len > 0){
                                                                msg[msg_len] = '';
                                printf(msg);
                                                }

                        if(strstr(msg, "NOTICE AUTH :*** Checking Ident") != NULL){
                                sleep(1);
                                sprintf(msg, "user %s %s %s %sn", info.user_name, info.user_whois1, info.user3, info.user_whois2);
                                send(sockfd, msg, strlen(msg), 0);
                                sprintf(msg, "NICK %sn", info.nick);
                                send(sockfd, msg, strlen(msg), 0);
                                printf("Connect Initialization Succeed!n");
                        }
                        else{
                                sscanf(msg, "%6c%s",check_ping, pong);

                                if(strncmp(check_ping, "PING :", 6) == 0){
                                        sprintf(msg, "PONG %sn", pong);
                                        send(sockfd, msg, strlen(msg), 0);

                                        if(join_flag == 0){
                                                sleep(1);
                                                join_flag = 1;
                                                send(sockfd, "join #linuxn", 12, 0);
                                        }
                                }
                        }

                        msg[0] = '';
                }

                exit(0);
        }
        else{
                fprintf(stderr, "Child Process Created!: pid = %dn", pid);
        }

        while(1){
                fgets(msg, MSG_MAX, stdin);
                send(sockfd, msg, strlen(msg), 0);

                if(strcmp(msg, "quitn") == 0)
                        break;
        }

        kill(0, SIGKILL);                //      .

        return 0;
}


void error_handling(char *msg)
{
        fprintf(stderr, "%sn", msg);
        exit(-1);
}

void handler(int sig)
{
        pid_t pid;
        int last;

        pid = waitpid(-1, &last, WNOHANG);
        fprintf(stderr, "Child Process Closed!: pid = %d, WEXITSTATUS = %Xn", pid, WEXITSTATUS(last));
}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:30:32
Processing time 0.0153 sec