GMS Linux: start?

This commit is contained in:
2025-12-30 01:20:20 +00:00
commit ac241a9148
48 changed files with 2028 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h> // ifreq
#include <arpa/inet.h> // inet_pton, sockaddr_in
#include <errno.h>
// 사용법: ifconfig eth0 10.0.2.15
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: ifconfig <interface> <ip_address>\n");
return 1;
}
char *ifname = argv[1];
char *ip_str = argv[2];
// 1. 커널과 대화하기 위한 소켓 생성 (DGRAM: UDP)
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
return 1;
}
struct ifreq ifr;
memset(&ifr, 0, sizeof(ifr));
// 인터페이스 이름 설정 (예: eth0)
strncpy(ifr.ifr_name, ifname, IFNAMSIZ - 1);
// 2. IP 주소 설정 (SIOCSIFADDR)
struct sockaddr_in *addr = (struct sockaddr_in *)&ifr.ifr_addr;
addr->sin_family = AF_INET;
// 문자열 IP -> 바이너리 변환
if (inet_pton(AF_INET, ip_str, &addr->sin_addr) != 1) {
fprintf(stderr, "Invalid IP format\n");
close(fd);
return 1;
}
if (ioctl(fd, SIOCSIFADDR, &ifr) < 0) {
perror("ioctl(SIOCSIFADDR) - Failed to set IP");
close(fd);
return 1;
}
printf("IP %s assigned to %s\n", ip_str, ifname);
// 3. 인터페이스 활성화 (UP & RUNNING) (SIOCGIFFLAGS -> SIOCSIFFLAGS)
// 현재 플래그 가져오기
if (ioctl(fd, SIOCGIFFLAGS, &ifr) < 0) {
perror("ioctl(SIOCGIFFLAGS)");
close(fd);
return 1;
}
// UP 플래그와 RUNNING 플래그 추가
ifr.ifr_flags |= (IFF_UP | IFF_RUNNING);
// 플래그 다시 설정
if (ioctl(fd, SIOCSIFFLAGS, &ifr) < 0) {
perror("ioctl(SIOCSIFFLAGS) - Failed to set UP");
close(fd);
return 1;
}
printf("Interface %s is now UP\n", ifname);
close(fd);
return 0;
}