📄 socket.c
字号:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <errno.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netinet/tcp.h>#include <netinet/ip.h>#include <arpa/inet.h>#include "socket.h"#include "tun.h"extern int debug;intudp_socket(struct sockaddr_in *laddr, struct sockaddr_in *raddr) { int fd; if ( (fd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 ) { if (debug) perror("socket()"); return -1; } if ( bind(fd, (struct sockaddr *)laddr, sizeof(struct sockaddr_in)) == -1 ) { if (debug) perror("bind()"); return -1; } return fd;}void *udp_read_tun_write( void *args ) { struct udp_tun *foo; foo = (struct udp_tun *)args; int bytes = 0; size_t fromlen; char buf[1500]; while (1) { if ( (bytes = recvfrom(foo->skfd, buf, sizeof(buf), 0, (struct sockaddr *)foo->addr, &fromlen)) == -1) { if (debug) perror("recvfrom()"); } else { if (debug) printf("recv(): %d\n", bytes); } if ( (bytes = tun_write(foo->tunfd, buf, bytes) ) < 0) fprintf(stderr, "tun_write(): %d\n", bytes); } return 0;}//---------------------------------------------------char tmp[64];char *bits = "01";char *int_bits(unsigned int n, int n_bits) { tmp[63] = '\0'; int i = 62; int c; for (c = 0; c < n_bits; c++) { tmp[i] = bits[n & 1]; i--; n = n >> 1; } return &tmp[i + 1];}char *inaddr_txt(u_int32_t address) { struct in_addr addr; addr.s_addr = (in_addr_t)address; return(inet_ntoa(addr));}void analizar_buffer(char buffer[], int size) { struct iphdr *ip_hdr = (struct iphdr *)buffer; int ip_header_length = htons(ip_hdr->ihl); if (ip_header_length > size) { printf(" !!!!! error al analizar ihl\n"); return; } printf("ihl: %d\n", ip_header_length); printf("version: %d\n", htons(ip_hdr->version)); printf("tos: %s\n", int_bits(ip_hdr->tos, 8)); printf("tot_len: %d\n", htons(ip_hdr->tot_len)); printf("id: %d\n", htons(ip_hdr->id)); printf("frag_off: %d\n", htons(ip_hdr->frag_off)); printf("ttl: %d\n", htons(ip_hdr->ttl)); printf("protocol: %d\n", htons(ip_hdr->protocol)); printf("check: %d\n", htons(ip_hdr->check)); printf("saddr: %s\n", inaddr_txt(ip_hdr->saddr)); printf("daddr: %s\n", inaddr_txt(ip_hdr->daddr)); struct tcphdr *tcp = (struct tcphdr *)&buffer[ip_header_length]; printf("s_port: %d\n", htons(tcp->source)); printf("dest: %d\n", htons(tcp->dest)); printf("seq: %d\n", htonl(tcp->seq)); printf("ack_seq: %d\n", htonl(tcp->ack_seq)); printf("doff: %d\n", htons(tcp->doff)); printf("flags: "); if (tcp->urg) printf("|URG"); if (tcp->ack) printf("|ACK"); if (tcp->psh) printf("|PSH"); if (tcp->rst) printf("|RST"); if (tcp->syn) printf("|SYN"); if (tcp->fin) printf("|FIN"); printf("|\n");}//----------------------------------------------------void *tun_read_udp_write( void *args ) { struct udp_tun *foo; foo = (struct udp_tun *)args; int bytes = 0; char buf[1500]; while (1) { if ( (bytes = read(foo->tunfd, buf, sizeof(buf))) < 0 ) { if (debug) perror("read()"); } else { if (debug) printf("read(): %d\n", bytes); } analizar_buffer(buf, bytes); if ( (bytes = sendto(foo->skfd, buf, bytes, 0, (struct sockaddr *)foo->addr, sizeof(struct sockaddr_in))) == -1) { if (debug) perror("sendto()"); } else { if (debug) printf("send(): %d\n", bytes); } fflush(stdout); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -