일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 인증
- openssl
- MFA
- 앱스토어
- albumbook
- Xcode
- 애플
- fido
- FIDO2
- MYSQL
- apple
- SSL
- 앱리소스
- css
- SwiftUI
- Nodejs
- WebAuthn
- otpkey
- appres
- OSX
- MSYS2
- OTP
- Android
- SWIFT
- 안드로이드
- kmip
- git
- 2FA
- 앨범북
- SSH
Archives
- Today
- Total
인디노트
eth0 에서 recvfrom 함수로 읽어서 그대로 eth1 으로 write 함수로 전송 하는 C 코드 본문
개발 플랫폼 및 언어/네트워크 기술
eth0 에서 recvfrom 함수로 읽어서 그대로 eth1 으로 write 함수로 전송 하는 C 코드
인디개발자 2023. 4. 13. 16:44#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <net/ethernet.h>
#include <arpa/inet.h>
#include <net/if.h>
#define ETH_FRAME_LEN 1518
int main() {
int sockfd_in = 0, sockfd_out = 0, ret = 0;
char buffer[ETH_FRAME_LEN] = {0};
struct sockaddr_ll dest = {0};
// create socket for eth0
sockfd_in = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sockfd_in == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// bind socket to eth0
struct sockaddr_ll addr = {0};
addr.sll_family = AF_PACKET;
addr.sll_ifindex = if_nametoindex("eth0");
if (addr.sll_ifindex == 0) {
perror("if_nametoindex");
exit(EXIT_FAILURE);
}
ret = bind(sockfd_in, (struct sockaddr*)&addr, sizeof(struct sockaddr_ll));
if (ret == -1) {
perror("bind");
exit(EXIT_FAILURE);
}
// create socket for eth1
sockfd_out = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (sockfd_out == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
// set destination address
memset(&dest, 0, sizeof(struct sockaddr_ll));
dest.sll_family = AF_PACKET;
dest.sll_protocol = htons(ETH_P_ALL);
dest.sll_ifindex = if_nametoindex("eth1");
if (dest.sll_ifindex == 0) {
perror("if_nametoindex");
exit(EXIT_FAILURE);
}
// loop to receive and forward Ethernet frames
while (1) {
// receive Ethernet frame from eth0
ret = recvfrom(sockfd_in, buffer, ETH_FRAME_LEN, 0, NULL, NULL);
if (ret == -1) {
perror("recvfrom");
exit(EXIT_FAILURE);
}
// send Ethernet frame to eth1
ret = write(sockfd_out, buffer, ret);
if (ret == -1) {
perror("write");
exit(EXIT_FAILURE);
}
}
// close sockets
close(sockfd_in);
close(sockfd_out);
return 0;
}
이 코드에서는 recvfrom과 write 함수를 사용하여 패킷을 읽고 쓰기를 수행합니다. bind 함수를 사용하여 소켓을 eth0 인터페이스에 바인딩하고, dest 구조체를 사용하여 소켓을 eth1 인터페이스로 설정합니다. 이 코드는 패킷을 수정하지 않고 그대로 전달합니다.
반응형
'개발 플랫폼 및 언어 > 네트워크 기술' 카테고리의 다른 글
Multicast Packet 로부터 상대방 IP 알아내는 C 코드 (0) | 2023.04.14 |
---|---|
PF_PACKET을 사용하여 C 코드에서 패킷을 보내는 예시 (0) | 2023.04.14 |
ethhdr 의 src 및 dst 를 변경하는 C 코드 (0) | 2023.04.13 |
iphdr 를 이용해서 tcphdr 를 찾는 C 코드 (0) | 2023.04.13 |
ICMP ping 구현의 C 코드 (0) | 2023.04.12 |
Comments