Ver3.0
์ ์ฒด ์ด๋
Ver3.0 ๐ C์ธ์ด๋ก ๋คํธ์ํฌ ํ๋กํ ์ฝ ์ ๋ณตํ๊ธฐ

๐ C์ธ์ด๋ก ๋คํธ์ํฌ ํ๋กํ ์ฝ ์ ๋ณตํ๊ธฐ
TCP/IP ์คํ ๊ตฌํ๋ถํฐ ๋คํธ์ํน ๋๊ตฌ ์ ์๊น์ง, ์ค์ ๊ฐ์ด๋
๐ ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ, ์ C์ธ์ด์ผ๊น?
์ผ, ์น๊ตฌ! ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ ํ๋ฉด ๋ญ๊ฐ ๋ ์ฌ๋ผ? ์๋ง ๋ณต์กํ ํ๋กํ ์ฝ, ํจํท, ์์ผ... ์ด๋ฐ ๋จ์ด๋ค์ด ๋จธ๋ฆฟ์์ ์ค์ณ ์ง๋๊ฐ ๊ฑฐ์ผ. ๊ทผ๋ฐ ๋ง์ด์ผ, ์ด ๋ชจ๋ ๊ฑธ ๊ฐ์ฅ ๋ก์ฐ๋ ๋ฒจ์์ ์ ์ดํ ์ ์๋ ์ธ์ด๊ฐ ๋ฐ๋ก C์ธ์ด๋ผ๋ ๊ฑฐ ์๊ณ ์์์ด? ๐C์ธ์ด๋ ํ๋์จ์ด์ ๊ฐ์ฅ ๊ฐ๊น์ด ๊ณ ๊ธ ์ธ์ด์ผ. ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ง์ ๋ค๋ฃจ๊ณ , ํฌ์ธํฐ๋ก ์ฃผ์๋ฅผ ์กฐ์ํ๊ณ , ๋นํธ ๋จ์๋ก ๋ฐ์ดํฐ๋ฅผ ์ ์ดํ ์ ์์ง. ๋คํธ์ํฌ ํจํท์ ๊ฒฐ๊ตญ ๋ฐ์ดํธ ์คํธ๋ฆผ์ด๊ณ , ์ด๊ฑธ ์ ๋ฐํ๊ฒ ๋ค๋ฃจ๋ ค๋ฉด C์ธ์ด๋ง ํ ๊ฒ ์์ด. ๋ฆฌ๋ ์ค ์ปค๋์ TCP/IP ์คํ๋, BSD ์์ผ API๋ ๋ชจ๋ C๋ก ์์ฑ๋์๋ค๋ ์ฌ์ค! ๐ฅ
์ค์ ๋ก ํ์ ์์ ๋คํธ์ํฌ ์ฅ๋น ํ์จ์ด, ๋ผ์ฐํฐ ์ํํธ์จ์ด, ๋ฐฉํ๋ฒฝ, VPN ์๋ฃจ์ ๋ฑ์ ๋๋ถ๋ถ C์ธ์ด๋ก ๊ฐ๋ฐ๋ผ. ์ฑ๋ฅ์ด ์ค์ํ ๋คํธ์ํฌ ์ ํ๋ฆฌ์ผ์ด์ ์์๋ ์ฌ์ ํ C์ธ์ด๊ฐ ์ต๊ณ ์ ์ ํ์ง๊ฑฐ๋ . ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์๋ ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ ๊ด๋ จ ์ฌ๋ฅ์ ์ฐพ๋ ์ฌ๋๋ค์ด ๋ง์๋ฐ, C์ธ์ด ๊ธฐ๋ฐ ๋คํธ์ํน ์คํฌ์ ์ ๋ง ๊ทํ ์ฌ๋ฅ์ด์ผ.
๐ ์์ผ ํ๋ก๊ทธ๋๋ฐ์ ๊ธฐ์ด
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ๋ค์ด๊ฐ ๋ณผ๊น? ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์ ์์์ ๋ฐ๋ก ์์ผ(Socket)์ด์ผ. ์์ผ์ ๋คํธ์ํฌ ํต์ ์ ์๋ํฌ์ธํธ๋ผ๊ณ ์๊ฐํ๋ฉด ๋ผ. ๋ง์น ์ ํ๊ธฐ์ฒ๋ผ ๋ง์ด์ผ. ์ ํ๋ฅผ ๊ฑธ๋ ค๋ฉด ์ ํ๊ธฐ๊ฐ ํ์ํ๋ฏ, ๋คํธ์ํฌ ํต์ ์ ํ๋ ค๋ฉด ์์ผ์ด ํ์ํ์ง! ๐์์ผ์ ์ข ๋ฅ
C์ธ์ด์์ ์ฌ์ฉํ ์ ์๋ ์ฃผ์ ์์ผ ํ์ ์ ํฌ๊ฒ ์ธ ๊ฐ์ง์ผ:
๐ฏ Stream Socket (SOCK_STREAM)
TCP ํ๋กํ ์ฝ์ ์ฌ์ฉํ๋ ์์ผ์ด์ผ. ์ฐ๊ฒฐ ์งํฅ์ ์ด๊ณ , ๋ฐ์ดํฐ์ ์์๊ฐ ๋ณด์ฅ๋๋ฉฐ, ์ ๋ขฐ์ฑ ์๋ ํต์ ์ ์ ๊ณตํด. ์น ๋ธ๋ผ์ฐ์ง, ํ์ผ ์ ์ก, ์ด๋ฉ์ผ ๋ฑ ๋๋ถ๋ถ์ ์ธํฐ๋ท ์๋น์ค๊ฐ ์ด๊ฑธ ์ฌ์ฉํ์ง.
๐ฏ Datagram Socket (SOCK_DGRAM)
UDP ํ๋กํ ์ฝ์ ์ฌ์ฉํ๋ ์์ผ์ด์ผ. ๋น์ฐ๊ฒฐํ์ด๊ณ , ๋ฐ์ดํฐ ์์๋ ๋์ฐฉ์ด ๋ณด์ฅ๋์ง ์์ง๋ง ๋น ๋ฅด๊ณ ๊ฐ๋ฒผ์. ์ค์๊ฐ ์คํธ๋ฆฌ๋ฐ, ์จ๋ผ์ธ ๊ฒ์, DNS ์กฐํ ๊ฐ์ ๊ณณ์์ ํ์ฉ๋ผ.
๐ฏ Raw Socket (SOCK_RAW)
๊ฐ์ฅ ๋ก์ฐ๋ ๋ฒจ์ ์์ผ์ด์ผ. IP ํค๋๋ถํฐ ์ง์ ์กฐ์ํ ์ ์์ด์ ์ปค์คํ
ํ๋กํ ์ฝ์ ๋ง๋ค๊ฑฐ๋, ๋คํธ์ํฌ ๋ถ์ ๋๊ตฌ๋ฅผ ๊ฐ๋ฐํ ๋ ์ฌ์ฉํด. ๊ด๋ฆฌ์ ๊ถํ์ด ํ์ํ๋ค๋ ์ ! ๐
๊ธฐ๋ณธ ์์ผ ํ๋ก๊ทธ๋๋ฐ ํ๋ก์ฐ
TCP ์๋ฒ๋ฅผ ๋ง๋ ๋ค๊ณ ์๊ฐํด๋ณด์. ๊ธฐ๋ณธ์ ์ธ ํ๋ฆ์ ์ด๋:#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
int main() {
// 1. ์์ผ ์์ฑ
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
perror("socket failed");
return 1;
}
// 2. ์ฃผ์ ๊ตฌ์กฐ์ฒด ์ค์
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(8080);
// 3. ์์ผ์ ์ฃผ์ ๋ฐ์ธ๋ฉ
if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
perror("bind failed");
return 1;
}
// 4. ์ฐ๊ฒฐ ๋๊ธฐ (๋ฆฌ์ค๋)
if (listen(server_fd, 3) < 0) {
perror("listen failed");
return 1;
}
printf("์๋ฒ๊ฐ 8080 ํฌํธ์์ ๋๊ธฐ ์ค...\n");
// 5. ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์๋ฝ
int client_fd;
int addrlen = sizeof(address);
client_fd = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen);
if (client_fd < 0) {
perror("accept failed");
return 1;
}
// 6. ๋ฐ์ดํฐ ์ก์์
char buffer[1024] = {0};
read(client_fd, buffer, 1024);
printf("๋ฐ์ ๋ฉ์์ง: %s\n", buffer);
char *response = "Hello from server!";
send(client_fd, response, strlen(response), 0);
// 7. ์์ผ ๋ซ๊ธฐ
close(client_fd);
close(server_fd);
return 0;
}
์ด ์ฝ๋๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ TCP ์์ฝ ์๋ฒ์ผ. ์ค์ ๋ก๋ ์๋ฌ ์ฒ๋ฆฌ, ๋ฉํฐํ๋ ์ฑ, ๋
ผ๋ธ๋กํน I/O ๋ฑ ๋ ๋ง์ ๊ณ ๋ ค์ฌํญ์ด ์์ง๋ง, ๊ธฐ๋ณธ ๊ตฌ์กฐ๋ ์ด๋ ๊ฒ ์๊ฒผ์ด! ๐ก
๐ฆ TCP/IP ํ๋กํ ์ฝ ์คํ ์ดํดํ๊ธฐ
์, ์ด์ ์ข ๋ ๊น์ด ๋ค์ด๊ฐ ๋ณผ๊น? TCP/IP ์คํ์ ๋คํธ์ํฌ ํต์ ์ ์ํ ํ๋กํ ์ฝ๋ค์ ๊ณ์ธต ๊ตฌ์กฐ์ผ. ๊ฐ ๊ณ์ธต์ ํน์ ํ ์ญํ ์ ๋ด๋นํ๊ณ , ์์ ๊ณ์ธต์ ์๋น์ค๋ฅผ ์ ๊ณตํ์ง. ๐๏ธApplication Layer (์์ฉ ๊ณ์ธต)
๊ฐ์ฅ ์์ ์๋ ๊ณ์ธต์ด์ผ. ์ฌ์ฉ์์ ์ง์ ์ํธ์์ฉํ๋ ํ๋กํ ์ฝ๋ค์ด ์ฌ๊ธฐ ์์ด. HTTP, FTP, SMTP, DNS ๊ฐ์ ๊ฒ๋ค ๋ง์ด์ผ. C์ธ์ด๋ก ์ด ๊ณ์ธต์ ํ๋กํ ์ฝ์ ๊ตฌํํ ์๋ ์์ด!์๋ฅผ ๋ค์ด, ๊ฐ๋จํ HTTP ์์ฒญ์ ๋ณด๋ด๋ ์ฝ๋๋ฅผ ๋ง๋ค์ด๋ณผ๊น?
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
int main() {
char *hostname = "www.example.com";
char *port = "80";
// DNS ์กฐํ
struct addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(hostname, port, &hints, &res) != 0) {
perror("getaddrinfo failed");
return 1;
}
// ์์ผ ์์ฑ ๋ฐ ์ฐ๊ฒฐ
int sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (connect(sockfd, res->ai_addr, res->ai_addrlen) < 0) {
perror("connect failed");
return 1;
}
// HTTP GET ์์ฒญ ์ ์ก
char request[1024];
snprintf(request, sizeof(request),
"GET / HTTP/1.1\r\n"
"Host: %s\r\n"
"Connection: close\r\n"
"\r\n", hostname);
send(sockfd, request, strlen(request), 0);
// ์๋ต ๋ฐ๊ธฐ
char response[4096];
int bytes_received;
while ((bytes_received = recv(sockfd, response, sizeof(response) - 1, 0)) > 0) {
response[bytes_received] = '\0';
printf("%s", response);
}
close(sockfd);
freeaddrinfo(res);
return 0;
}
์ด ์ฝ๋๋ HTTP/1.1 ํ๋กํ ์ฝ์ ์ง์ ๊ตฌํํ ๊ฑฐ์ผ. ์ค์ ์น ๋ธ๋ผ์ฐ์ ๊ฐ ํ๋ ์ผ์ C์ธ์ด๋ก ์ฌํํ ์
์ด์ง! ๐Transport Layer (์ ์ก ๊ณ์ธต)
TCP์ UDP๊ฐ ์ฌ๊ธฐ ์ํด. ์ด ๊ณ์ธต์ ์ข ๋จ ๊ฐ(end-to-end) ํต์ ์ ๋ด๋นํด. TCP๋ ์ ๋ขฐ์ฑ ์๋ ์ฐ๊ฒฐ์ ์ ๊ณตํ๊ณ , UDP๋ ๋น ๋ฅธ ์ ์ก์ ์ ๊ณตํ์ง.
๐ก TCP vs UDP ์ ํ ๊ธฐ์ค
TCP๋ฅผ ์ฌ์ฉํด์ผ ํ ๋:
โข ๋ฐ์ดํฐ ์์ค์ด ํ์ฉ๋์ง ์์ ๋ (ํ์ผ ์ ์ก, ์ด๋ฉ์ผ)
โข ์์๊ฐ ์ค์ํ ๋ (์ฑํ , ์น ํ์ด์ง ๋ก๋ฉ)
โข ์ฐ๊ฒฐ ์ํ๋ฅผ ์ ์งํด์ผ ํ ๋
UDP๋ฅผ ์ฌ์ฉํด์ผ ํ ๋:
โข ์ค์๊ฐ์ฑ์ด ์ค์ํ ๋ (ํ์ ํตํ, ์จ๋ผ์ธ ๊ฒ์)
โข ์ฝ๊ฐ์ ๋ฐ์ดํฐ ์์ค์ด ํ์ฉ๋ ๋ (์คํธ๋ฆฌ๋ฐ)
โข ๋ธ๋ก๋์บ์คํธ/๋ฉํฐ์บ์คํธ๊ฐ ํ์ํ ๋
UDP ์์ผ ์์ ๋ ํ๋ฒ ๋ณผ๊น?โข ๋ฐ์ดํฐ ์์ค์ด ํ์ฉ๋์ง ์์ ๋ (ํ์ผ ์ ์ก, ์ด๋ฉ์ผ)
โข ์์๊ฐ ์ค์ํ ๋ (์ฑํ , ์น ํ์ด์ง ๋ก๋ฉ)
โข ์ฐ๊ฒฐ ์ํ๋ฅผ ์ ์งํด์ผ ํ ๋
UDP๋ฅผ ์ฌ์ฉํด์ผ ํ ๋:
โข ์ค์๊ฐ์ฑ์ด ์ค์ํ ๋ (ํ์ ํตํ, ์จ๋ผ์ธ ๊ฒ์)
โข ์ฝ๊ฐ์ ๋ฐ์ดํฐ ์์ค์ด ํ์ฉ๋ ๋ (์คํธ๋ฆฌ๋ฐ)
โข ๋ธ๋ก๋์บ์คํธ/๋ฉํฐ์บ์คํธ๊ฐ ํ์ํ ๋
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
// UDP ์๋ฒ
int main() {
int sockfd;
struct sockaddr_in server_addr, client_addr;
char buffer[1024];
socklen_t addr_len = sizeof(client_addr);
// UDP ์์ผ ์์ฑ
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(8080);
// ๋ฐ์ธ๋ฉ
bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
printf("UDP ์๋ฒ ์์...\n");
while (1) {
// ๋ฐ์ดํฐ ์์ (์ฐ๊ฒฐ ์์ด!)
int n = recvfrom(sockfd, buffer, sizeof(buffer), 0,
(struct sockaddr*)&client_addr, &addr_len);
buffer[n] = '\0';
printf("๋ฐ์ ๋ฉ์์ง: %s\n", buffer);
// ์๋ต ์ ์ก
sendto(sockfd, buffer, n, 0,
(struct sockaddr*)&client_addr, addr_len);
}
close(sockfd);
return 0;
}
UDP๋ connect()๋ accept() ์์ด ๋ฐ๋ก ๋ฐ์ดํฐ๋ฅผ ์ฃผ๊ณ ๋ฐ์ ์ ์์ด. ํจ์ฌ ๊ฐ๋จํ์ง? ํ์ง๋ง ๊ทธ๋งํผ ์ ๋ขฐ์ฑ์ ๋จ์ด์ ธ. ๐ฒInternet Layer (์ธํฐ๋ท ๊ณ์ธต)
IP ํ๋กํ ์ฝ์ด ์ฌ๊ธฐ ์์ด. ํจํท์ ๋ผ์ฐํ ๊ณผ ์ฃผ์ ์ง์ ์ ๋ด๋นํ์ง. C์ธ์ด๋ก Raw Socket์ ์ฌ์ฉํ๋ฉด IP ํค๋๋ฅผ ์ง์ ์กฐ์ํ ์ ์์ด!#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <unistd.h>
// ์ฒดํฌ์ฌ ๊ณ์ฐ ํจ์
unsigned short checksum(void *b, int len) {
unsigned short *buf = b;
unsigned int sum = 0;
unsigned short result;
for (sum = 0; len > 1; len -= 2)
sum += *buf++;
if (len == 1)
sum += *(unsigned char*)buf;
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
int main() {
int sockfd;
char packet[4096];
struct sockaddr_in dest;
// Raw ์์ผ ์์ฑ (๊ด๋ฆฌ์ ๊ถํ ํ์!)
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
if (sockfd < 0) {
perror("Raw socket ์์ฑ ์คํจ (sudo ํ์)");
return 1;
}
// IP ํค๋ ์ง์ ์์ฑํ๊ธฐ
int one = 1;
if (setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &one, sizeof(one)) < 0) {
perror("setsockopt failed");
return 1;
}
memset(packet, 0, 4096);
// IP ํค๋ ๊ตฌ์ฑ
struct iphdr *iph = (struct iphdr*)packet;
iph->ihl = 5;
iph->version = 4;
iph->tos = 0;
iph->tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
iph->id = htons(54321);
iph->frag_off = 0;
iph->ttl = 255;
iph->protocol = IPPROTO_TCP;
iph->check = 0;
iph->saddr = inet_addr("192.168.1.100"); // ์ถ๋ฐ์ง IP
iph->daddr = inet_addr("192.168.1.1"); // ๋ชฉ์ ์ง IP
// IP ์ฒดํฌ์ฌ ๊ณ์ฐ
iph->check = checksum((unsigned short*)packet, iph->tot_len);
// TCP ํค๋ ๊ตฌ์ฑ
struct tcphdr *tcph = (struct tcphdr*)(packet + sizeof(struct iphdr));
tcph->source = htons(12345);
tcph->dest = htons(80);
tcph->seq = 0;
tcph->ack_seq = 0;
tcph->doff = 5;
tcph->syn = 1; // SYN ํ๋๊ทธ
tcph->window = htons(5840);
tcph->check = 0;
tcph->urg_ptr = 0;
// ๋ชฉ์ ์ง ์ค์
dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr("192.168.1.1");
// ํจํท ์ ์ก
if (sendto(sockfd, packet, iph->tot_len, 0,
(struct sockaddr*)&dest, sizeof(dest)) < 0) {
perror("sendto failed");
return 1;
}
printf("์ปค์คํ
TCP SYN ํจํท ์ ์ก ์๋ฃ!\n");
close(sockfd);
return 0;
}
์ด ์ฝ๋๋ TCP SYN ํจํท์ ์ง์ ๋ง๋ค์ด์ ๋ณด๋ด๋ ๊ฑฐ์ผ. IP ํค๋์ TCP ํค๋๋ฅผ ์๋์ผ๋ก ๊ตฌ์ฑํ์ง. ์ด๋ฐ ์์ผ๋ก ๋คํธ์ํฌ ์ค์บ๋๋ ํจํท ๋ถ์ ๋๊ตฌ๋ฅผ ๋ง๋ค ์ ์์ด! ๐
โ ๏ธ ์ฃผ์์ฌํญ
Raw Socket์ ๊ฐ๋ ฅํ ๋งํผ ์ํํด. ์๋ชป ์ฌ์ฉํ๋ฉด ๋คํธ์ํฌ๋ฅผ ๋ง๋น์ํฌ ์๋ ์๊ณ , ๋ณด์ ๋ฌธ์ ๋ฅผ ์ผ์ผํฌ ์๋ ์์ด. ํญ์ ํ
์คํธ ํ๊ฒฝ์์๋ง ์ฌ์ฉํ๊ณ , ์ค์ ๋คํธ์ํฌ์์๋ ์ ์คํ๊ฒ ๋ค๋ค์ผ ํด!
Network Access Layer (๋คํธ์ํฌ ์ ๊ทผ ๊ณ์ธต)
๊ฐ์ฅ ํ์ ๊ณ์ธต์ด์ผ. ์ด๋๋ท, WiFi ๊ฐ์ ๋ฌผ๋ฆฌ์ ๋คํธ์ํฌ ์ธํฐํ์ด์ค๋ฅผ ๋ค๋ฃจ์ง. C์ธ์ด์์๋ AF_PACKET ์์ผ์ ์ฌ์ฉํ๋ฉด ์ด๋๋ท ํ๋ ์๊น์ง ์ง์ ์กฐ์ํ ์ ์์ด!#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <unistd.h>
int main() {
int sockfd;
unsigned char buffer[2048];
// ํจํท ์์ผ ์์ฑ (๋ชจ๋ ํ๋กํ ์ฝ ์บก์ฒ)
sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sockfd < 0) {
perror("socket failed (sudo ํ์)");
return 1;
}
printf("ํจํท ์บก์ฒ ์์...\n");
while (1) {
int data_size = recvfrom(sockfd, buffer, 2048, 0, NULL, NULL);
// ์ด๋๋ท ํค๋ ํ์ฑ
struct ethhdr *eth = (struct ethhdr*)buffer;
printf("\n=== ์ด๋๋ท ํ๋ ์ ===\n");
printf("์ถ๋ฐ์ง MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
eth->h_source[0], eth->h_source[1], eth->h_source[2],
eth->h_source[3], eth->h_source[4], eth->h_source[5]);
printf("๋ชฉ์ ์ง MAC: %02x:%02x:%02x:%02x:%02x:%02x\n",
eth->h_dest[0], eth->h_dest[1], eth->h_dest[2],
eth->h_dest[3], eth->h_dest[4], eth->h_dest[5]);
printf("ํ๋กํ ์ฝ: 0x%04x\n", ntohs(eth->h_proto));
// IP ํจํท์ธ ๊ฒฝ์ฐ
if (ntohs(eth->h_proto) == ETH_P_IP) {
struct iphdr *iph = (struct iphdr*)(buffer + sizeof(struct ethhdr));
struct in_addr src, dst;
src.s_addr = iph->saddr;
dst.s_addr = iph->daddr;
printf("IP: %s -> %s\n", inet_ntoa(src), inet_ntoa(dst));
}
// 10๊ฐ๋ง ์บก์ฒํ๊ณ ์ข
๋ฃ
static int count = 0;
if (++count >= 10) break;
}
close(sockfd);
return 0;
}
์ด ์ฝ๋๋ Wireshark ๊ฐ์ ํจํท ์บก์ฒ ๋๊ตฌ์ ๊ธฐ๋ณธ ์๋ฆฌ์ผ. ๋คํธ์ํฌ ์ธํฐํ์ด์ค๋ฅผ ์ง๋๊ฐ๋ ๋ชจ๋ ํจํท์ ์ก์์ ๋ถ์ํ ์ ์์ง! ๐ฃ
๐ ๏ธ ์ค์ ๋คํธ์ํน ๋๊ตฌ ๋ง๋ค๊ธฐ
์ด๋ก ์ ์ถฉ๋ถํ ๋ดค์ผ๋, ์ด์ ์ค์ ๋ก ์ธ๋ชจ ์๋ ๋๊ตฌ๋ค์ ๋ง๋ค์ด๋ณผ๊น? ์ฌ๋ฅ๋ท์์ ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ ์ฌ๋ฅ์ ๊ณต์ ํ ๋๋ ์ด๋ฐ ์ค์ ์์ ๋ค์ด ํฐ ๋์์ด ๋ ๊ฑฐ์ผ! ๐1. ํฌํธ ์ค์บ๋ (Port Scanner)
๋คํธ์ํฌ ๋ณด์ ์ง๋จ์ ๊ธฐ๋ณธ์ด์ง. ํน์ ํธ์คํธ์ ์ด๋ฆฐ ํฌํธ๋ฅผ ์ฐพ์๋ด๋ ๋๊ตฌ์ผ.#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/time.h>
int check_port(const char *host, int port, int timeout_sec) {
int sockfd;
struct sockaddr_in addr;
struct timeval timeout;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) return -1;
// ๋
ผ๋ธ๋กํน ๋ชจ๋ ์ค์
int flags = fcntl(sockfd, F_GETFL, 0);
fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
inet_pton(AF_INET, host, &addr.sin_addr);
// ์ฐ๊ฒฐ ์๋
connect(sockfd, (struct sockaddr*)&addr, sizeof(addr));
// select๋ก ํ์์์ ์ฒ๋ฆฌ
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(sockfd, &fdset);
timeout.tv_sec = timeout_sec;
timeout.tv_usec = 0;
int result = select(sockfd + 1, NULL, &fdset, NULL, &timeout);
close(sockfd);
if (result > 0) {
int so_error;
socklen_t len = sizeof(so_error);
getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &so_error, &len);
return (so_error == 0) ? 1 : 0; // 1: ์ด๋ฆผ, 0: ๋ซํ
}
return 0; // ํ์์์
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("์ฌ์ฉ๋ฒ: %s <IP์ฃผ์>\n", argv[0]);
return 1;
}
char *target = argv[1];
printf("ํฌํธ ์ค์บ ์์: %s\n", target);
printf("=====================\n");
// ์ฃผ์ ํฌํธ๋ค ์ค์บ
int common_ports[] = {21, 22, 23, 25, 53, 80, 110, 143, 443,
3306, 3389, 5432, 8080, 8443};
int num_ports = sizeof(common_ports) / sizeof(common_ports[0]);
for (int i = 0; i < num_ports; i++) {
int port = common_ports[i];
printf("ํฌํธ %d ๊ฒ์ฌ ์ค... ", port);
fflush(stdout);
if (check_port(target, port, 1)) {
printf("โ ์ด๋ฆผ\n");
} else {
printf("โ ๋ซํ\n");
}
}
printf("=====================\n");
printf("์ค์บ ์๋ฃ!\n");
return 0;
}
์ด ํฌํธ ์ค์บ๋๋ ๋
ผ๋ธ๋กํน ์์ผ๊ณผ select()๋ฅผ ์ฌ์ฉํด์ ํจ์จ์ ์ผ๋ก ์ฌ๋ฌ ํฌํธ๋ฅผ ๊ฒ์ฌํด. ์ค์ nmap ๊ฐ์ ๋๊ตฌ์ ๊ฐ๋จํ ๋ฒ์ ์ด๋ผ๊ณ ๋ณผ ์ ์์ง! ๐2. ๊ฐ๋จํ HTTP ์๋ฒ
์น ์๋ฒ์ ๊ธฐ๋ณธ ์๋ฆฌ๋ฅผ ์ดํดํ๋ ๋ฐ ์ต๊ณ ์ผ. ์ ์ ํ์ผ์ ์๋นํ๋ ๋ฏธ๋ ์น ์๋ฒ๋ฅผ ๋ง๋ค์ด๋ณด์!#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#define PORT 8080
#define BUFFER_SIZE 4096
void send_response(int client_fd, int status_code, const char *status_text,
const char *content_type, const char *body, int body_len) {
char header[1024];
snprintf(header, sizeof(header),
"HTTP/1.1 %d %s\r\n"
"Content-Type: %s\r\n"
"Content-Length: %d\r\n"
"Connection: close\r\n"
"\r\n",
status_code, status_text, content_type, body_len);
send(client_fd, header, strlen(header), 0);
if (body && body_len > 0) {
send(client_fd, body, body_len, 0);
}
}
void handle_request(int client_fd) {
char buffer[BUFFER_SIZE];
int bytes_read = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
if (bytes_read <= 0) return;
buffer[bytes_read] = '\0';
// ์์ฒญ ๋ผ์ธ ํ์ฑ
char method[16], path[256], version[16];
sscanf(buffer, "%s %s %s", method, path, version);
printf("์์ฒญ: %s %s\n", method, path);
// GET ๋ฉ์๋๋ง ์ง์
if (strcmp(method, "GET") != 0) {
char *msg = "405 Method Not Allowed";
send_response(client_fd, 405, "Method Not Allowed",
"text/plain", msg, strlen(msg));
return;
}
// ๋ฃจํธ ๊ฒฝ๋ก ์ฒ๋ฆฌ
if (strcmp(path, "/") == 0) {
strcpy(path, "/index.html");
}
// ํ์ผ ๊ฒฝ๋ก ์์ฑ (๋ณด์์ ์ํด ์ค์ ๋ก๋ ๋ ์๊ฒฉํ ๊ฒ์ฆ ํ์)
char filepath[512];
snprintf(filepath, sizeof(filepath), ".%s", path);
// ํ์ผ ์ด๊ธฐ
int fd = open(filepath, O_RDONLY);
if (fd < 0) {
char *msg = "404 Not Found";
send_response(client_fd, 404, "Not Found",
"text/plain", msg, strlen(msg));
return;
}
// ํ์ผ ํฌ๊ธฐ ํ์ธ
struct stat st;
fstat(fd, &st);
// Content-Type ๊ฒฐ์
char *content_type = "text/html";
if (strstr(path, ".css")) content_type = "text/css";
else if (strstr(path, ".js")) content_type = "application/javascript";
else if (strstr(path, ".jpg") || strstr(path, ".jpeg"))
content_type = "image/jpeg";
else if (strstr(path, ".png")) content_type = "image/png";
// ํ์ผ ๋ด์ฉ ์ฝ์ด์ ์ ์ก
char *file_content = malloc(st.st_size);
read(fd, file_content, st.st_size);
send_response(client_fd, 200, "OK", content_type,
file_content, st.st_size);
free(file_content);
close(fd);
}
int main() {
int server_fd, client_fd;
struct sockaddr_in address;
int addrlen = sizeof(address);
// ์์ผ ์์ฑ
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == 0) {
perror("socket failed");
return 1;
}
// ์ฃผ์ ์ฌ์ฌ์ฉ ์ต์
int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
// ๋ฐ์ธ๋ฉ
if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) {
perror("bind failed");
return 1;
}
// ๋ฆฌ์ค๋
if (listen(server_fd, 10) < 0) {
perror("listen failed");
return 1;
}
printf("๐ HTTP ์๋ฒ ์์! http://localhost:%d\n", PORT);
// ํด๋ผ์ด์ธํธ ์์ฒญ ์ฒ๋ฆฌ
while (1) {
client_fd = accept(server_fd, (struct sockaddr*)&address,
(socklen_t*)&addrlen);
if (client_fd < 0) {
perror("accept failed");
continue;
}
handle_request(client_fd);
close(client_fd);
}
close(server_fd);
return 0;
}
์ด HTTP ์๋ฒ๋ ๊ธฐ๋ณธ์ ์ธ GET ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ์ ์ ํ์ผ์ ์๋นํ ์ ์์ด. Apache๋ Nginx์ ์์ฃผ ๊ฐ๋จํ ๋ฒ์ ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋ผ! ๐3. ํจํท ์ค๋ํผ (Packet Sniffer)
๋คํธ์ํฌ ํธ๋ํฝ์ ๋ถ์ํ๋ ๋๊ตฌ์ผ. Wireshark์ ๊ธฐ๋ณธ ์๋ฆฌ๋ฅผ ์ดํดํ ์ ์์ง!#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <netinet/ip_icmp.h>
#include <unistd.h>
void print_ip_header(unsigned char *buffer) {
struct iphdr *iph = (struct iphdr*)buffer;
struct sockaddr_in source, dest;
memset(&source, 0, sizeof(source));
source.sin_addr.s_addr = iph->saddr;
memset(&dest, 0, sizeof(dest));
dest.sin_addr.s_addr = iph->daddr;
printf("\n=== IP ํค๋ ===\n");
printf("๋ฒ์ : %d\n", (unsigned int)iph->version);
printf("ํค๋ ๊ธธ์ด: %d DWORDS (%d Bytes)\n",
(unsigned int)iph->ihl, ((unsigned int)(iph->ihl)) * 4);
printf("์๋น์ค ํ์
: %d\n", (unsigned int)iph->tos);
printf("์ ์ฒด ๊ธธ์ด: %d Bytes\n", ntohs(iph->tot_len));
printf("ID: %d\n", ntohs(iph->id));
printf("TTL: %d\n", (unsigned int)iph->ttl);
printf("ํ๋กํ ์ฝ: %d\n", (unsigned int)iph->protocol);
printf("์ฒดํฌ์ฌ: %d\n", ntohs(iph->check));
printf("์ถ๋ฐ์ง IP: %s\n", inet_ntoa(source.sin_addr));
printf("๋ชฉ์ ์ง IP: %s\n", inet_ntoa(dest.sin_addr));
}
void print_tcp_header(unsigned char *buffer, int size) {
struct iphdr *iph = (struct iphdr*)buffer;
unsigned short iphdrlen = iph->ihl * 4;
struct tcphdr *tcph = (struct tcphdr*)(buffer + iphdrlen);
printf("\n=== TCP ํค๋ ===\n");
printf("์ถ๋ฐ์ง ํฌํธ: %u\n", ntohs(tcph->source));
printf("๋ชฉ์ ์ง ํฌํธ: %u\n", ntohs(tcph->dest));
printf("์ํ์ค ๋ฒํธ: %u\n", ntohl(tcph->seq));
printf("ACK ๋ฒํธ: %u\n", ntohl(tcph->ack_seq));
printf("ํค๋ ๊ธธ์ด: %d DWORDS (%d Bytes)\n",
(unsigned int)tcph->doff, (unsigned int)tcph->doff * 4);
printf("ํ๋๊ทธ: ");
if (tcph->urg) printf("URG ");
if (tcph->ack) printf("ACK ");
if (tcph->psh) printf("PSH ");
if (tcph->rst) printf("RST ");
if (tcph->syn) printf("SYN ");
if (tcph->fin) printf("FIN ");
printf("\n");
printf("์๋์ฐ ํฌ๊ธฐ: %d\n", ntohs(tcph->window));
printf("์ฒดํฌ์ฌ: %d\n", ntohs(tcph->check));
}
void print_udp_header(unsigned char *buffer, int size) {
struct iphdr *iph = (struct iphdr*)buffer;
unsigned short iphdrlen = iph->ihl * 4;
struct udphdr *udph = (struct udphdr*)(buffer + iphdrlen);
printf("\n=== UDP ํค๋ ===\n");
printf("์ถ๋ฐ์ง ํฌํธ: %u\n", ntohs(udph->source));
printf("๋ชฉ์ ์ง ํฌํธ: %u\n", ntohs(udph->dest));
printf("๊ธธ์ด: %d\n", ntohs(udph->len));
printf("์ฒดํฌ์ฌ: %d\n", ntohs(udph->check));
}
int main() {
int sock_raw;
int saddr_size, data_size;
struct sockaddr saddr;
unsigned char buffer[65536];
// Raw ์์ผ ์์ฑ
sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
if (sock_raw < 0) {
printf("์์ผ ์์ฑ ์คํจ. sudo๋ก ์คํํ์ธ์.\n");
return 1;
}
printf("๐ ํจํท ์ค๋ํผ ์์...\n");
printf("Ctrl+C๋ก ์ข
๋ฃ\n\n");
int tcp_count = 0, udp_count = 0, icmp_count = 0, other_count = 0;
while (1) {
saddr_size = sizeof(saddr);
data_size = recvfrom(sock_raw, buffer, 65536, 0, &saddr, &saddr_size);
if (data_size < 0) {
printf("ํจํท ์์ ์คํจ\n");
return 1;
}
struct iphdr *iph = (struct iphdr*)buffer;
switch (iph->protocol) {
case 6: // TCP
tcp_count++;
printf("\n[ํจํท #%d] TCP ํจํท\n", tcp_count);
print_ip_header(buffer);
print_tcp_header(buffer, data_size);
break;
case 17: // UDP
udp_count++;
printf("\n[ํจํท #%d] UDP ํจํท\n", udp_count);
print_ip_header(buffer);
print_udp_header(buffer, data_size);
break;
case 1: // ICMP
icmp_count++;
printf("\n[ํจํท #%d] ICMP ํจํท\n", icmp_count);
print_ip_header(buffer);
break;
default:
other_count++;
break;
}
printf("\nํต๊ณ: TCP=%d UDP=%d ICMP=%d ๊ธฐํ=%d\n",
tcp_count, udp_count, icmp_count, other_count);
printf("========================================\n");
// ์ฒ์ 20๊ฐ ํจํท๋ง ์ถ๋ ฅ
if (tcp_count + udp_count + icmp_count >= 20) break;
}
close(sock_raw);
return 0;
}
์ด ํจํท ์ค๋ํผ๋ ๋คํธ์ํฌ๋ฅผ ์ง๋๊ฐ๋ TCP, UDP, ICMP ํจํท์ ์บก์ฒํด์ ํค๋ ์ ๋ณด๋ฅผ ์ถ๋ ฅํด. ๋คํธ์ํฌ ๋๋ฒ๊น
์ด๋ ๋ณด์ ๋ถ์์ ์ ์ฉํ์ง! ๐ต๏ธ4. DNS ๋ฆฌ์กธ๋ฒ
๋๋ฉ์ธ ์ด๋ฆ์ IP ์ฃผ์๋ก ๋ณํํ๋ DNS ์ฟผ๋ฆฌ ๋๊ตฌ์ผ. DNS ํ๋กํ ์ฝ์ ์ง์ ๊ตฌํํด๋ณด์!#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
// DNS ํค๋ ๊ตฌ์กฐ์ฒด
struct DNS_HEADER {
unsigned short id;
unsigned char rd :1;
unsigned char tc :1;
unsigned char aa :1;
unsigned char opcode :4;
unsigned char qr :1;
unsigned char rcode :4;
unsigned char cd :1;
unsigned char ad :1;
unsigned char z :1;
unsigned char ra :1;
unsigned short q_count;
unsigned short ans_count;
unsigned short auth_count;
unsigned short add_count;
};
// DNS ์ง์ ๊ตฌ์กฐ์ฒด
struct QUESTION {
unsigned short qtype;
unsigned short qclass;
};
// ๋๋ฉ์ธ ์ด๋ฆ์ DNS ํ์์ผ๋ก ๋ณํ
void ChangetoDnsNameFormat(unsigned char* dns, unsigned char* host) {
int lock = 0;
strcat((char*)host, ".");
for (int i = 0; i < strlen((char*)host); i++) {
if (host[i] == '.') {
*dns++ = i - lock;
for (; lock < i; lock++) {
*dns++ = host[lock];
}
lock++;
}
}
*dns++ = '\0';
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("์ฌ์ฉ๋ฒ: %s <๋๋ฉ์ธ>\n", argv[0]);
return 1;
}
unsigned char buf[65536], *qname, *reader;
int s;
struct sockaddr_in dest;
struct DNS_HEADER *dns = NULL;
struct QUESTION *qinfo = NULL;
// UDP ์์ผ ์์ฑ
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
// DNS ์๋ฒ ์ค์ (Google DNS)
dest.sin_family = AF_INET;
dest.sin_port = htons(53);
dest.sin_addr.s_addr = inet_addr("8.8.8.8");
// DNS ํค๋ ์ค์
dns = (struct DNS_HEADER *)&buf;
dns->id = (unsigned short)htons(getpid());
dns->qr = 0;
dns->opcode = 0;
dns->aa = 0;
dns->tc = 0;
dns->rd = 1;
dns->ra = 0;
dns->z = 0;
dns->ad = 0;
dns->cd = 0;
dns->rcode = 0;
dns->q_count = htons(1);
dns->ans_count = 0;
dns->auth_count = 0;
dns->add_count = 0;
// ์ง์ ์ด๋ฆ ์ถ๊ฐ
qname = (unsigned char*)&buf[sizeof(struct DNS_HEADER)];
ChangetoDnsNameFormat(qname, (unsigned char*)argv[1]);
qinfo = (struct QUESTION*)&buf[sizeof(struct DNS_HEADER) +
(strlen((const char*)qname) + 1)];
qinfo->qtype = htons(1); // A ๋ ์ฝ๋
qinfo->qclass = htons(1); // IN ํด๋์ค
printf("๐ DNS ์ฟผ๋ฆฌ ์ ์ก: %s\n", argv[1]);
// DNS ์ฟผ๋ฆฌ ์ ์ก
if (sendto(s, (char*)buf, sizeof(struct DNS_HEADER) +
(strlen((const char*)qname) + 1) + sizeof(struct QUESTION),
0, (struct sockaddr*)&dest, sizeof(dest)) < 0) {
perror("sendto failed");
return 1;
}
// ์๋ต ์์
int i = sizeof(dest);
if (recvfrom(s, (char*)buf, 65536, 0,
(struct sockaddr*)&dest, (socklen_t*)&i) < 0) {
perror("recvfrom failed");
return 1;
}
dns = (struct DNS_HEADER*)buf;
printf("\n=== DNS ์๋ต ===\n");
printf("์๋ต ์ฝ๋: %d\n", dns->rcode);
printf("๋ต๋ณ ๊ฐ์: %d\n", ntohs(dns->ans_count));
// ๋ต๋ณ ์น์
ํ์ฑ (๊ฐ๋จํ ๋ฒ์ )
reader = &buf[sizeof(struct DNS_HEADER) +
(strlen((const char*)qname) + 1) + sizeof(struct QUESTION)];
printf("\nโ
IP ์ฃผ์:\n");
for (int j = 0; j < ntohs(dns->ans_count); j++) {
reader = reader + 12; // ์ด๋ฆ, ํ์
, ํด๋์ค, TTL ๊ฑด๋๋ฐ๊ธฐ
unsigned short data_len;
memcpy(&data_len, reader, 2);
reader += 2;
if (ntohs(data_len) == 4) { // IPv4 ์ฃผ์
unsigned char ip[4];
memcpy(ip, reader, 4);
printf(" %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
reader += 4;
}
}
close(s);
return 0;
}
์ด DNS ๋ฆฌ์กธ๋ฒ๋ UDP๋ฅผ ์ฌ์ฉํด์ DNS ์๋ฒ์ ์ง์ ์ฟผ๋ฆฌ๋ฅผ ๋ณด๋ด๊ณ ์๋ต์ ํ์ฑํด. nslookup์ด๋ dig ๊ฐ์ ๋๊ตฌ์ ๊ธฐ๋ณธ ์๋ฆฌ๋ฅผ ์ดํดํ ์ ์์ด! ๐
โก ๊ณ ๊ธ ๋คํธ์ํน ๊ธฐ๋ฒ
๊ธฐ๋ณธ์ ๋์ด์ ์ด์ ์ข ๋ ๊ณ ๊ธ ๊ธฐ์ ๋ค์ ์์๋ณผ๊น? ์ค๋ฌด์์ ์ ๋ง ๋ง์ด ์ฐ์ด๋ ํ ํฌ๋๋ค์ด์ผ! ๐1. ๋ ผ๋ธ๋กํน I/O์ ๋ฉํฐํ๋ ์ฑ
์ฌ๋ฌ ํด๋ผ์ด์ธํธ๋ฅผ ๋์์ ์ฒ๋ฆฌํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํ ๊น? ๊ฐ์ฅ ํจ์จ์ ์ธ ๋ฐฉ๋ฒ์ I/O ๋ฉํฐํ๋ ์ฑ์ด์ผ. select(), poll(), epoll() ๊ฐ์ ์์คํ ์ฝ์ ์ฌ์ฉํ์ง.
๐ I/O ๋ฉํฐํ๋ ์ฑ ๋ฐฉ์ ๋น๊ต
select(): ๊ฐ์ฅ ์ค๋๋๊ณ ์ด์์ฑ์ด ์ข์. ํ์ง๋ง FD_SETSIZE(๋ณดํต 1024) ์ ํ์ด ์์ด.
poll(): select์ ์ ํ์ ๊ทน๋ณต. ํ์ผ ๋์คํฌ๋ฆฝํฐ ๊ฐ์ ์ ํ์ด ์์ง๋ง, ์ฌ์ ํ O(n) ๋ณต์ก๋.
epoll() (Linux): ๊ฐ์ฅ ํจ์จ์ ! O(1) ๋ณต์ก๋๋ก ์๋ง ๊ฐ์ ์ฐ๊ฒฐ๋ ์ฒ๋ฆฌ ๊ฐ๋ฅ. ๋๊ท๋ชจ ์๋ฒ์ ์ต์ !
epoll์ ์ฌ์ฉํ ๊ณ ์ฑ๋ฅ ์์ฝ ์๋ฒ ์์ ๋ฅผ ๋ณผ๊น?poll(): select์ ์ ํ์ ๊ทน๋ณต. ํ์ผ ๋์คํฌ๋ฆฝํฐ ๊ฐ์ ์ ํ์ด ์์ง๋ง, ์ฌ์ ํ O(n) ๋ณต์ก๋.
epoll() (Linux): ๊ฐ์ฅ ํจ์จ์ ! O(1) ๋ณต์ก๋๋ก ์๋ง ๊ฐ์ ์ฐ๊ฒฐ๋ ์ฒ๋ฆฌ ๊ฐ๋ฅ. ๋๊ท๋ชจ ์๋ฒ์ ์ต์ !
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#define MAX_EVENTS 1024
#define PORT 8080
// ๋
ผ๋ธ๋กํน ๋ชจ๋ ์ค์
int set_nonblocking(int fd) {
int flags = fcntl(fd, F_GETFL, 0);
if (flags == -1) return -1;
return fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
int main() {
int listen_fd, conn_fd, epoll_fd;
struct sockaddr_in server_addr, client_addr;
struct epoll_event ev, events[MAX_EVENTS];
socklen_t client_len;
// ๋ฆฌ์ค๋ ์์ผ ์์ฑ
listen_fd = socket(AF_INET, SOCK_STREAM, 0);
if (listen_fd < 0) {
perror("socket");
exit(1);
}
// ์ฃผ์ ์ฌ์ฌ์ฉ
int opt = 1;
setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
// ๋
ผ๋ธ๋กํน ๋ชจ๋ ์ค์
set_nonblocking(listen_fd);
// ๋ฐ์ธ๋ฉ
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(PORT);
if (bind(listen_fd, (struct sockaddr*)&server_addr,
sizeof(server_addr)) < 0) {
perror("bind");
exit(1);
}
// ๋ฆฌ์ค๋
if (listen(listen_fd, SOMAXCONN) < 0) {
perror("listen");
exit(1);
}
// epoll ์ธ์คํด์ค ์์ฑ
epoll_fd = epoll_create1(0);
if (epoll_fd < 0) {
perror("epoll_create1");
exit(1);
}
// ๋ฆฌ์ค๋ ์์ผ์ epoll์ ๋ฑ๋ก
ev.events = EPOLLIN | EPOLLET; // Edge-triggered ๋ชจ๋
ev.data.fd = listen_fd;
if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_fd, &ev) < 0) {
perror("epoll_ctl");
exit(1);
}
printf("๐ ๊ณ ์ฑ๋ฅ ์์ฝ ์๋ฒ ์์! (ํฌํธ: %d)\n", PORT);
printf("epoll์ ์ฌ์ฉํ ๋น๋๊ธฐ I/O ์ฒ๋ฆฌ\n\n");
char buffer[4096];
int active_connections = 0;
// ์ด๋ฒคํธ ๋ฃจํ
while (1) {
int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
for (int i = 0; i < nfds; i++) {
// ์๋ก์ด ์ฐ๊ฒฐ ์์ฒญ
if (events[i].data.fd == listen_fd) {
while (1) {
client_len = sizeof(client_addr);
conn_fd = accept(listen_fd,
(struct sockaddr*)&client_addr,
&client_len);
if (conn_fd < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break; // ๋ชจ๋ ์ฐ๊ฒฐ ์ฒ๋ฆฌ ์๋ฃ
} else {
perror("accept");
break;
}
}
// ์ ์ฐ๊ฒฐ์ ๋
ผ๋ธ๋กํน์ผ๋ก ์ค์
set_nonblocking(conn_fd);
// epoll์ ๋ฑ๋ก
ev.events = EPOLLIN | EPOLLET;
ev.data.fd = conn_fd;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, conn_fd, &ev);
active_connections++;
printf("โ
์ ์ฐ๊ฒฐ! (ํ์ฑ ์ฐ๊ฒฐ: %d)\n", active_connections);
}
}
// ๊ธฐ์กด ์ฐ๊ฒฐ์์ ๋ฐ์ดํฐ ์์
else {
int fd = events[i].data.fd;
while (1) {
int n = read(fd, buffer, sizeof(buffer));
if (n < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
break; // ๋ชจ๋ ๋ฐ์ดํฐ ์ฝ์
} else {
perror("read");
close(fd);
active_connections--;
break;
}
} else if (n == 0) {
// ์ฐ๊ฒฐ ์ข
๋ฃ
printf("โ ์ฐ๊ฒฐ ์ข
๋ฃ (ํ์ฑ ์ฐ๊ฒฐ: %d)\n",
--active_connections);
close(fd);
break;
} else {
// ์์ฝ ์๋ต
write(fd, buffer, n);
}
}
}
}
}
close(listen_fd);
close(epoll_fd);
return 0;
}
์ด ์๋ฒ๋ epoll์ Edge-Triggered ๋ชจ๋๋ฅผ ์ฌ์ฉํด์ ์์ฒ ๊ฐ์ ๋์ ์ฐ๊ฒฐ๋ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌํ ์ ์์ด. Nginx๋ Redis ๊ฐ์ ๊ณ ์ฑ๋ฅ ์๋ฒ๋ค์ด ์ด๋ฐ ๋ฐฉ์์ ์ฌ์ฉํ์ง! โก2. ๋ฉํฐ์ค๋ ๋ ์๋ฒ
๊ฐ ํด๋ผ์ด์ธํธ๋ฅผ ๋ณ๋ ์ค๋ ๋์์ ์ฒ๋ฆฌํ๋ ๋ฐฉ์๋ ์์ด. ๊ตฌํ์ ๊ฐ๋จํ์ง๋ง, ์ค๋ ๋ ๊ด๋ฆฌ์ ์ฃผ์ํด์ผ ํด!#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#define PORT 8080
// ํด๋ผ์ด์ธํธ ์ฒ๋ฆฌ ์ค๋ ๋
void* handle_client(void* arg) {
int client_fd = *(int*)arg;
free(arg);
char buffer[1024];
int bytes_read;
printf("๐ ์ค๋ ๋ %lu: ํด๋ผ์ด์ธํธ ์ฒ๋ฆฌ ์์\n", pthread_self());
while ((bytes_read = recv(client_fd, buffer, sizeof(buffer), 0)) > 0) {
buffer[bytes_read] = '\0';
printf("๐ฉ ๋ฐ์: %s", buffer);
// ์์ฝ ์๋ต
send(client_fd, buffer, bytes_read, 0);
}
printf("๐ ์ค๋ ๋ %lu: ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ข
๋ฃ\n", pthread_self());
close(client_fd);
return NULL;
}
int main() {
int server_fd, client_fd;
struct sockaddr_in address;
int addrlen = sizeof(address);
// ์์ผ ์์ฑ
server_fd = socket(AF_INET, SOCK_STREAM, 0);
int opt = 1;
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
bind(server_fd, (struct sockaddr*)&address, sizeof(address));
listen(server_fd, 10);
printf("๐งต ๋ฉํฐ์ค๋ ๋ ์๋ฒ ์์! (ํฌํธ: %d)\n\n", PORT);
while (1) {
client_fd = accept(server_fd, (struct sockaddr*)&address,
(socklen_t*)&addrlen);
if (client_fd < 0) {
perror("accept failed");
continue;
}
printf("โ
์ ์ฐ๊ฒฐ ์๋ฝ!\n");
// ์ ์ค๋ ๋ ์์ฑ
pthread_t thread_id;
int *pclient = malloc(sizeof(int));
*pclient = client_fd;
if (pthread_create(&thread_id, NULL, handle_client, pclient) != 0) {
perror("pthread_create failed");
free(pclient);
close(client_fd);
continue;
}
// ์ค๋ ๋ ๋ถ๋ฆฌ (์๋ ์ ๋ฆฌ)
pthread_detach(thread_id);
}
close(server_fd);
return 0;
}
๋ฉํฐ์ค๋ ๋ ๋ฐฉ์์ ์ฝ๋๊ฐ ์ง๊ด์ ์ด์ง๋ง, ์ค๋ ๋๊ฐ ๋๋ฌด ๋ง์์ง๋ฉด ์ปจํ
์คํธ ์ค์์นญ ์ค๋ฒํค๋๊ฐ ์ปค์ ธ. ๊ทธ๋์ ์ค๋ฌด์์๋ ์ค๋ ๋ ํ(Thread Pool)์ ์ฌ์ฉํ๋ ๊ฒ ์ผ๋ฐ์ ์ด์ผ! ๐3. Zero-Copy ๊ธฐ๋ฒ
๋์ฉ๋ ํ์ผ์ ์ ์กํ ๋๋ sendfile() ์์คํ ์ฝ์ ์ฌ์ฉํ๋ฉด ์ปค๋ ๊ณต๊ฐ๊ณผ ์ ์ ๊ณต๊ฐ ์ฌ์ด์ ๋ณต์ฌ๋ฅผ ์ค์ผ ์ ์์ด!#include <stdio.h>
#include <sys/socket.h>
#include <sys/sendfile.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
int send_file_zerocopy(int client_fd, const char *filename) {
int file_fd = open(filename, O_RDONLY);
if (file_fd < 0) {
perror("open");
return -1;
}
// ํ์ผ ํฌ๊ธฐ ํ์ธ
struct stat stat_buf;
fstat(file_fd, &stat_buf);
off_t offset = 0;
printf("๐ค Zero-copy๋ก ํ์ผ ์ ์ก ์ค... (%ld bytes)\n", stat_buf.st_size);
// sendfile๋ก ์ปค๋ ๊ณต๊ฐ์์ ์ง์ ์ ์ก!
ssize_t sent = sendfile(client_fd, file_fd, &offset, stat_buf.st_size);
if (sent < 0) {
perror("sendfile");
close(file_fd);
return -1;
}
printf("โ
์ ์ก ์๋ฃ! (%ld bytes)\n", sent);
close(file_fd);
return 0;
}
sendfile()์ ์ฌ์ฉํ๋ฉด ํ์ผ ๋ฐ์ดํฐ๊ฐ ์ ์ ๊ณต๊ฐ์ ๊ฑฐ์น์ง ์๊ณ ์ปค๋์์ ์ง์ ์์ผ์ผ๋ก ์ ์ก๋ผ. ์์ฒญ๋ ์ฑ๋ฅ ํฅ์์ด์ง! ๐
๐ ๋คํธ์ํฌ ๋ณด์ ๊ณ ๋ ค์ฌํญ
๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์์ ๋ณด์์ ์ ๋ง ์ค์ํด! ๋ช ๊ฐ์ง ํต์ฌ ํฌ์ธํธ๋ฅผ ์ง์ด๋ณผ๊ฒ. ๐ก๏ธ1. ๋ฒํผ ์ค๋ฒํ๋ก์ฐ ๋ฐฉ์ง
C์ธ์ด์ ๊ฐ์ฅ ํฐ ์ํ ์ค ํ๋์ผ. ํญ์ ๋ฒํผ ํฌ๊ธฐ๋ฅผ ์ฒดํฌํด์ผ ํด!
โ ์ํํ ์ฝ๋:
char buffer[100];
recv(sockfd, buffer, 1000, 0); // ๋ฒํผ ์ค๋ฒํ๋ก์ฐ!
โ
์์ ํ ์ฝ๋:char buffer[100];
int n = recv(sockfd, buffer, sizeof(buffer) - 1, 0);
if (n > 0) {
buffer[n] = '\0'; // null ์ข
๋ฃ
}
2. ์ ๋ ฅ ๊ฒ์ฆ
ํด๋ผ์ด์ธํธ์์ ์ค๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ ์ ๋ขฐํ ์ ์์ด. ์ฒ ์ ํ ๊ฒ์ฆ์ด ํ์์ผ!#include <ctype.h>
#include <string.h>
// ์์ ํ ๋ฌธ์์ด ๊ฒ์ฆ
int is_valid_input(const char *input, size_t max_len) {
if (!input) return 0;
size_t len = strnlen(input, max_len + 1);
if (len > max_len) return 0;
// ํ์ฉ๋ ๋ฌธ์๋ง ์ฒดํฌ
for (size_t i = 0; i < len; i++) {
if (!isalnum(input[i]) && input[i] != '_' && input[i] != '-') {
return 0;
}
}
return 1;
}
// ๊ฒฝ๋ก ์ํ ๊ณต๊ฒฉ ๋ฐฉ์ง
int is_safe_path(const char *path) {
// ".." ํฌํจ ์ฌ๋ถ ์ฒดํฌ
if (strstr(path, "..") != NULL) {
return 0;
}
// ์ ๋ ๊ฒฝ๋ก ๊ธ์ง
if (path[0] == '/') {
return 0;
}
return 1;
}
3. DoS ๊ณต๊ฒฉ ๋ฐฉ์ด
์๋น์ค ๊ฑฐ๋ถ ๊ณต๊ฒฉ์ ๋ง๊ธฐ ์ํ ๊ธฐ๋ณธ์ ์ธ ๋ฐฉ์ด ๊ธฐ๋ฒ๋ค์ด์ผ.#include <time.h>
#include <sys/socket.h>
// ์ฐ๊ฒฐ ์ ํ
#define MAX_CONNECTIONS_PER_IP 5
#define RATE_LIMIT_WINDOW 60 // ์ด
typedef struct {
char ip[INET_ADDRSTRLEN];
int count;
time_t window_start;
} RateLimit;
int check_rate_limit(const char *ip, RateLimit *limits, int max_limits) {
time_t now = time(NULL);
for (int i = 0; i < max_limits; i++) {
if (strcmp(limits[i].ip, ip) == 0) {
// ์๊ฐ ์๋์ฐ ์ฒดํฌ
if (now - limits[i].window_start > RATE_LIMIT_WINDOW) {
limits[i].count = 1;
limits[i].window_start = now;
return 1;
}
// ์ ํ ์ด๊ณผ ์ฒดํฌ
if (limits[i].count >= MAX_CONNECTIONS_PER_IP) {
printf("โ ๏ธ Rate limit exceeded for %s\n", ip);
return 0;
}
limits[i].count++;
return 1;
}
}
return 1;
}
// ํ์์์ ์ค์
void set_socket_timeout(int sockfd, int seconds) {
struct timeval timeout;
timeout.tv_sec = seconds;
timeout.tv_usec = 0;
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO,
&timeout, sizeof(timeout));
setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO,
&timeout, sizeof(timeout));
}
4. SSL/TLS ์ํธํ
๋ฏผ๊ฐํ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ ๋๋ ๋ฐ๋์ ์ํธํํด์ผ ํด! OpenSSL์ ์ฌ์ฉํ ์์ ์ผ.#include <openssl/ssl.h>
#include <openssl/err.h>
// SSL ์ด๊ธฐํ
SSL_CTX* init_ssl_context() {
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
const SSL_METHOD *method = TLS_server_method();
SSL_CTX *ctx = SSL_CTX_new(method);
if (!ctx) {
ERR_print_errors_fp(stderr);
return NULL;
}
// ์ธ์ฆ์์ ๊ฐ์ธํค ๋ก๋
if (SSL_CTX_use_certificate_file(ctx, "cert.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
return NULL;
}
if (SSL_CTX_use_PrivateKey_file(ctx, "key.pem", SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
return NULL;
}
return ctx;
}
// SSL ์ฐ๊ฒฐ ์ฒ๋ฆฌ
void handle_ssl_client(SSL *ssl) {
char buffer[1024];
if (SSL_accept(ssl) <= 0) {
ERR_print_errors_fp(stderr);
return;
}
printf("๐ SSL ์ฐ๊ฒฐ ์ฑ๊ณต!\n");
printf("์ํธํ ๋ฐฉ์: %s\n", SSL_get_cipher(ssl));
int bytes = SSL_read(ssl, buffer, sizeof(buffer));
if (bytes > 0) {
buffer[bytes] = '\0';
printf("์ํธํ๋ ๋ฐ์ดํฐ ์์ : %s\n", buffer);
SSL_write(ssl, buffer, bytes);
}
SSL_shutdown(ssl);
SSL_free(ssl);
}
HTTPS, FTPS ๊ฐ์ ๋ณด์ ํ๋กํ ์ฝ๋ค์ด ๋ชจ๋ SSL/TLS๋ฅผ ์ฌ์ฉํด. ์์ฆ์ ํ์์ผ! ๐
๐ฏ ์ค์ ํ๋ก์ ํธ ์์ด๋์ด
์ด์ ๋ฐฐ์ด ๊ฑธ ํ์ฉํด์ ๋ญ ๋ง๋ค ์ ์์๊น? ๋ช ๊ฐ์ง ์ฌ๋ฏธ์๋ ํ๋ก์ ํธ ์์ด๋์ด๋ฅผ ์๊ฐํ ๊ฒ! ๐ก
๐ ํ๋ก์ ํธ 1: ์ฑํ
์๋ฒ
๋ฉํฐํด๋ผ์ด์ธํธ ์ฑํ
์๋ฒ๋ฅผ ๋ง๋ค์ด๋ด. epoll๋ก ์์ฒ ๋ช
์ ๋์ ์ ์์๋ฅผ ์ฒ๋ฆฌํ๊ณ , ๋ฐฉ ๊ธฐ๋ฅ, ๊ท์๋ง, ํ์ผ ์ ์ก ๋ฑ์ ๊ตฌํํ ์ ์์ด. WebSocket ํ๋กํ ์ฝ์ ์ง์ ๊ตฌํํด๋ณด๋ ๊ฒ๋ ์ข์ ํ์ต์ด ๋ ๊ฑฐ์ผ!
ํต์ฌ ๊ธฐ์ : epoll, ๋ฉํฐํ๋ ์ฑ, ํ๋กํ ์ฝ ์ค๊ณ, ๋ฉ์์ง ํ
ํต์ฌ ๊ธฐ์ : epoll, ๋ฉํฐํ๋ ์ฑ, ํ๋กํ ์ฝ ์ค๊ณ, ๋ฉ์์ง ํ
๐ ํ๋ก์ ํธ 2: ๋คํธ์ํฌ ๋ชจ๋ํฐ๋ง ๋๊ตฌ
์ค์๊ฐ์ผ๋ก ๋คํธ์ํฌ ํธ๋ํฝ์ ๋ถ์ํ๋ ๋๊ตฌ์ผ. ํจํท ์บก์ฒ, ํ๋กํ ์ฝ ๋ถ์, ํต๊ณ ์์ฑ, ์ด์ ํ์ง ๋ฑ์ ๊ตฌํํ ์ ์์ด. Wireshark์ ๊ฐ๋จํ ๋ฒ์ ์ ๋ง๋๋ ๊ฑฐ์ง!
ํต์ฌ ๊ธฐ์ : Raw Socket, ํจํท ํ์ฑ, ํต๊ณ ์ฒ๋ฆฌ, ncurses UI
ํต์ฌ ๊ธฐ์ : Raw Socket, ํจํท ํ์ฑ, ํต๊ณ ์ฒ๋ฆฌ, ncurses UI
๐ ํ๋ก์ ํธ 3: HTTP ํ๋ก์ ์๋ฒ
ํด๋ผ์ด์ธํธ์ ์๋ฒ ์ฌ์ด์์ ์ค๊ณํ๋ ํ๋ก์ ์๋ฒ์ผ. ์บ์ฑ, ํํฐ๋ง, ๋ก๊น
๊ธฐ๋ฅ์ ์ถ๊ฐํ ์ ์์ด. ๋ ๋์๊ฐ๋ฉด ๋ฆฌ๋ฒ์ค ํ๋ก์๋ ๋ก๋ ๋ฐธ๋ฐ์๋ ๋ง๋ค ์ ์์ง!
ํต์ฌ ๊ธฐ์ : HTTP ํ๋กํ ์ฝ, ์์ผ ์ค๊ณ, ์บ์ฑ ์๊ณ ๋ฆฌ์ฆ, ๋ฉํฐ์ค๋ ๋ฉ
ํต์ฌ ๊ธฐ์ : HTTP ํ๋กํ ์ฝ, ์์ผ ์ค๊ณ, ์บ์ฑ ์๊ณ ๋ฆฌ์ฆ, ๋ฉํฐ์ค๋ ๋ฉ
๐ฎ ํ๋ก์ ํธ 4: P2P ํ์ผ ๊ณต์ ์์คํ
BitTorrent ๊ฐ์ P2P ๋คํธ์ํฌ๋ฅผ ๊ตฌํํด๋ด. DHT(๋ถ์ฐ ํด์ ํ
์ด๋ธ), ํผ์ด ํ์, ์ฒญํฌ ๊ธฐ๋ฐ ์ ์ก ๋ฑ ๊ณ ๊ธ ๋คํธ์ํน ๊ฐ๋
์ ๋ฐฐ์ธ ์ ์์ด!
ํต์ฌ ๊ธฐ์ : UDP ๋ฉํฐ์บ์คํธ, ๋ถ์ฐ ์์คํ , ํด์ ์๊ณ ๋ฆฌ์ฆ, ํ์ผ I/O
ํต์ฌ ๊ธฐ์ : UDP ๋ฉํฐ์บ์คํธ, ๋ถ์ฐ ์์คํ , ํด์ ์๊ณ ๋ฆฌ์ฆ, ํ์ผ I/O
๐ก๏ธํ๋ก์ ํธ 5: ๊ฐ๋จํ ๋ฐฉํ๋ฒฝ
iptables ๊ฐ์ ํจํท ํํฐ๋ง ๋๊ตฌ๋ฅผ ๋ง๋ค์ด๋ด. IP ์ฃผ์, ํฌํธ, ํ๋กํ ์ฝ ๊ธฐ๋ฐ์ผ๋ก ํธ๋ํฝ์ ์ ์ดํ๊ณ , ๋ก๊น
๊ธฐ๋ฅ๋ ์ถ๊ฐํ ์ ์์ด!
ํต์ฌ ๊ธฐ์ : Raw Socket, ํจํท ํํฐ๋ง, ๊ท์น ์์ง, ์ปค๋ ๋ชจ๋(์ ํ)
์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์ด๋ฐ ํ๋ก์ ํธ ๊ฒฝํ์ ๊ณต์ ํ๋ฉด ์ ๋ง ๋ง์ ์ฌ๋๋ค์๊ฒ ๋์์ด ๋ ๊ฑฐ์ผ! ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์ ์์๊ฐ ๋ง์ ๋ถ์ผ๊ฑฐ๋ . ๐
ํต์ฌ ๊ธฐ์ : Raw Socket, ํจํท ํํฐ๋ง, ๊ท์น ์์ง, ์ปค๋ ๋ชจ๋(์ ํ)
๐ ํ์ต ๋ฆฌ์์ค์ ๋ค์ ๋จ๊ณ
์ฌ๊ธฐ๊น์ง ์๋ค๋ฉด ์ ๋ง ๋๋จํด! ํ์ง๋ง ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์ ๋์ด ์๋ ๋ถ์ผ์ผ. ๊ณ์ ๊ณต๋ถํ ์ ์๋ ๋ฐฉํฅ์ ์๋ ค์ค๊ฒ. ๐์ถ์ฒ ๋์
โข "Unix Network Programming" by W. Richard Stevens - ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์ ๋ฐ์ด๋ธ์ด์ผ. ์์ผ API๋ถํฐ ๊ณ ๊ธ ๊ธฐ๋ฒ๊น์ง ๋ชจ๋ ๋ค๋ค.โข "TCP/IP Illustrated" by W. Richard Stevens - TCP/IP ํ๋กํ ์ฝ์ ๋ด๋ถ ๋์์ ๊น์ด ์๊ฒ ์ค๋ช ํด.
โข "Beej's Guide to Network Programming" - ๋ฌด๋ฃ ์จ๋ผ์ธ ๊ฐ์ด๋์ธ๋ฐ ์ด๋ณด์์๊ฒ ์ ๋ง ์ข์!
์ค์ต ํ๊ฒฝ
โข ๊ฐ์ ๋จธ์ : VirtualBox๋ VMware๋ก ์ฌ๋ฌ ๋์ ๊ฐ์ ๋จธ์ ์ ๋ง๋ค์ด์ ๋คํธ์ํฌ ๊ตฌ์ฑ ์ฐ์ตโข Docker: ์ปจํ ์ด๋๋ก ๊ฒฉ๋ฆฌ๋ ๋คํธ์ํฌ ํ๊ฒฝ ๊ตฌ์ถ
โข Wireshark: ํจํท ๋ถ์ ๋๊ตฌ๋ก ๋คํธ์ํฌ ํธ๋ํฝ ๊ด์ฐฐ
โข netcat, tcpdump: ๋ช ๋ น์ค ๋คํธ์ํน ๋๊ตฌ๋ค
๋ค์ ํ์ต ์ฃผ์
๐น ๊ณ ๊ธ ํ๋กํ ์ฝ ๊ตฌํ
โข WebSocket ํ๋กํ ์ฝ
โข QUIC (HTTP/3์ ๊ธฐ๋ฐ)
โข gRPC
โข MQTT (IoT ํ๋กํ ์ฝ)
โข QUIC (HTTP/3์ ๊ธฐ๋ฐ)
โข gRPC
โข MQTT (IoT ํ๋กํ ์ฝ)
๐น ์ฑ๋ฅ ์ต์ ํ
โข io_uring (์ต์ ๋ฆฌ๋
์ค ๋น๋๊ธฐ I/O)
โข DPDK (Data Plane Development Kit)
โข XDP (eXpress Data Path)
โข ์ปค๋ ๋ฐ์ดํจ์ค ๊ธฐ๋ฒ
โข DPDK (Data Plane Development Kit)
โข XDP (eXpress Data Path)
โข ์ปค๋ ๋ฐ์ดํจ์ค ๊ธฐ๋ฒ
๐น ๋ถ์ฐ ์์คํ
โข ๋ก๋ ๋ฐธ๋ฐ์ฑ
โข ์๋น์ค ๋์ค์ปค๋ฒ๋ฆฌ
โข ๋ฉ์์ง ํ (RabbitMQ, Kafka)
โข ๋ง์ดํฌ๋ก์๋น์ค ์ํคํ ์ฒ
โข ์๋น์ค ๋์ค์ปค๋ฒ๋ฆฌ
โข ๋ฉ์์ง ํ (RabbitMQ, Kafka)
โข ๋ง์ดํฌ๋ก์๋น์ค ์ํคํ ์ฒ
์ปค๋ฎค๋ํฐ์ ์คํ์์ค
์ค๋ ฅ์ ํค์ฐ๋ ๊ฐ์ฅ ์ข์ ๋ฐฉ๋ฒ์ ์คํ์์ค ํ๋ก์ ํธ์ ์ฐธ์ฌํ๋ ๊ฑฐ์ผ!โข libuv: Node.js์ ๊ธฐ๋ฐ์ด ๋๋ ๋น๋๊ธฐ I/O ๋ผ์ด๋ธ๋ฌ๋ฆฌ
โข nginx: ๊ณ ์ฑ๋ฅ ์น ์๋ฒ (C๋ก ์์ฑ๋จ)
โข Redis: ์ธ๋ฉ๋ชจ๋ฆฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค (C๋ก ์์ฑ๋จ)
โข curl: HTTP ํด๋ผ์ด์ธํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
์ด๋ฐ ํ๋ก์ ํธ๋ค์ ์์ค ์ฝ๋๋ฅผ ์ฝ์ด๋ณด๋ฉด ์ ๋ง ๋ง์ ๊ฑธ ๋ฐฐ์ธ ์ ์์ด. ์ค๋ฌด์์ ์ฌ์ฉ๋๋ ์ฝ๋ ์คํ์ผ, ์๋ฌ ์ฒ๋ฆฌ, ์ต์ ํ ๊ธฐ๋ฒ ๋ฑ์ ์ง์ ๋ณผ ์ ์๊ฑฐ๋ ! ๐
๐ฌ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์, ์ฌ๊ธฐ๊น์ง ์ ๋ง ๊ธด ์ฌ์ ์ด์์ด! C์ธ์ด๋ก ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ค๋ ๊ฑด ์ปดํจํฐ ๊ณผํ์ ํต์ฌ์ ์ง์ ๋ค๋ฃจ๋ ๊ฑฐ์ผ. ์์ผ ํ๋ก๊ทธ๋๋ฐ๋ถํฐ TCP/IP ์คํ ๊ตฌํ, ๊ณ ๊ธ ๋คํธ์ํน ๊ธฐ๋ฒ๊น์ง... ์ ๋ง ๋ง์ ๋ด์ฉ์ ๋ค๋ค์ง? ๐๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์ ์ฒ์์ ์ด๋ ต๊ฒ ๋๊ปด์ง ์ ์์ด. ๋ฐ์ดํธ ์์, ํฌ์ธํฐ, ์์คํ ์ฝ... ์ ๊ฒฝ ์จ์ผ ํ ๊ฒ ํ๋ ๊ฐ์ง๊ฐ ์๋๊ฑฐ๋ . ํ์ง๋ง ํ ๋ฒ ์ต์ํด์ง๋ฉด ์ ๋ง ๊ฐ๋ ฅํ ๋๊ตฌ๊ฐ ๋ผ. ์ธํฐ๋ท์ ๊ธฐ๋ฐ์ด ๋๋ ๊ธฐ์ ์ ์ง์ ๋ค๋ฃฐ ์ ์๋ค๋ ๊ฑด ์์ฒญ๋ ๋ฅ๋ ฅ์ด์ผ! ๐ช
ํต์ฌ ํฌ์ธํธ๋ฅผ ์ ๋ฆฌํ๋ฉด:
1. ๊ธฐ์ด๋ฅผ ํํํ: ์์ผ API, TCP/IP ํ๋กํ ์ฝ ์คํ์ ๊ฐ ๊ณ์ธต์ ํ์คํ ์ดํดํด์ผ ํด.
2. ์ค์ ๊ฒฝํ: ์ง์ ์ฝ๋๋ฅผ ์์ฑํ๊ณ ํ ์คํธํด๋ด. ์๋ฌ๋ฅผ ๋ง๋๊ณ ํด๊ฒฐํ๋ ๊ณผ์ ์์ ๊ฐ์ฅ ๋ง์ด ๋ฐฐ์.
3. ๋ณด์ ์์: ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์ ํญ์ ๋ณด์์ ์ผ๋์ ๋๊ณ ๊ฐ๋ฐํด์ผ ํด. ๋ฒํผ ์ค๋ฒํ๋ก์ฐ, ์ ๋ ฅ ๊ฒ์ฆ, ์ํธํ ๋ฑ์ ์์ง ๋ง!
4. ์ฑ๋ฅ ์ต์ ํ: ๋ ผ๋ธ๋กํน I/O, ๋ฉํฐํ๋ ์ฑ, Zero-Copy ๊ฐ์ ๊ธฐ๋ฒ๋ค์ ์ ์ ํ ํ์ฉํด.
5. ์ง์์ ์ธ ํ์ต: ๋คํธ์ํฌ ๊ธฐ์ ์ ๊ณ์ ๋ฐ์ ํด. ์๋ก์ด ํ๋กํ ์ฝ, ์ต์ ํ ๊ธฐ๋ฒ๋ค์ ๊พธ์คํ ๊ณต๋ถํ์!
์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ ์ฌ๋ฅ์ ๊ณต์ ํ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ. ๋ค๋ฅธ ์ฌ๋์ ๊ฐ๋ฅด์น๋ฉด์ ์์ ์ ์ง์๋ ๋ ๊น์ด์ง๊ฑฐ๋ . ๊ทธ๋ฆฌ๊ณ ์ค๋ฌด ํ๋ก์ ํธ ๊ฒฝํ์ ์์ผ๋ฉด์ ํฌํธํด๋ฆฌ์ค๋ ๋ง๋ค ์ ์๊ณ ! ๐
๋ง์ง๋ง์ผ๋ก ํ ๊ฐ์ง ํ: ์ ๋ ํฌ๊ธฐํ์ง ๋ง! ๋คํธ์ํฌ ํ๋ก๊ทธ๋๋ฐ์ ์ฒ์์ ์ด๋ ต์ง๋ง, ํ ๊ฑธ์์ฉ ๋์๊ฐ๋ค ๋ณด๋ฉด ์ด๋์ ๋ฉ์ง ๋คํธ์ํน ๋๊ตฌ๋ฅผ ๋ง๋ค๊ณ ์๋ ์์ ์ ๋ฐ๊ฒฌํ๊ฒ ๋ ๊ฑฐ์ผ. ์ธ๊ทธ๋ฉํ ์ด์ ํดํธ๊ฐ ๋๋, ํ์์์์ด ๋ฐ์ํด๋, ํจํท์ด ์ ์ค๋ผ๋... ๊ทธ๊ฒ ๋ค ๋ฐฐ์์ ๊ณผ์ ์ด์ผ! ๐
์, ์ด์ ๋น์ ์ฐจ๋ก์ผ! ํค๋ณด๋๋ฅผ ๋๋๋ฆฌ๊ณ , ์์ผ์ ์ด๊ณ , ํจํท์ ๋ณด๋ด๋ณด์. ์ธํฐ๋ท์ ๋ง๋ฒ ๊ฐ์ ์ธ๊ณ๊ฐ ๋น์ ์ ๊ธฐ๋ค๋ฆฌ๊ณ ์์ด! ํ์ดํ ! ๐ปโจ
๐ Happy Networking! ๐
May your packets never be dropped,
and your connections always be stable! ๐ก
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
๋๊ธ์ ๋ถ๋ฌ์ค๋ ์ค์
๋๋ค.
๋๊ธ์ ๋ถ๋ฌ์ค์ง ๋ชปํ์ต๋๋ค. ์ฐ๊ฒฐ ์ํ๋ฅผ ํ์ธํ๊ณ ๋ค์ ์๋ํด ์ฃผ์ธ์.
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.