~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));
}