📄 socket.c.svn-base
字号:
#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/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;}#define MTU 1500#define BUFFER_SIZE MTU*3void *udp_read_tun_write( void *args ) { struct udp_tun *foo; foo = (struct udp_tun *)args; size_t fromlen; char buf[BUFFER_SIZE]; // buffer para almacenar el paquete completo while (1) { int b_leidos = recvfrom(foo->skfd, buf, sizeof(buf), 0, (struct sockaddr *)foo->addr, &fromlen); if (b_leidos == -1) perror("\nno se pudo leer desde el socket (1)\n"); if (debug) { printf("sock -> %d leidos", b_leidos); if (b_leidos > MTU) printf("\n[!!] bytes leidos exceden el MTU\n"); } struct iphdr *header = (struct iphdr *)buf; int tot_len = ntohs(header->tot_len); // BIG-ENDIAN!!! if (debug) { // algunas comprobaciones idotas... printf("| header.tot_len: %d", tot_len); if (tot_len > sizeof(buf)) printf("\n!!!!! esto no va a caber!!!\n"); } int resto = tot_len - b_leidos; // ahora un while leyendo desde el socket mas paquete y juntandolos // en buf[], hasta completar los IPHEADER.TOTAL_LENGTH bytes while (resto > 0) { int resto_leido = recvfrom(foo->skfd, &buf[b_leidos], resto, 0, (struct sockaddr *)foo->addr, &fromlen); if (debug) printf("| sock -> %d leidos", resto_leido); b_leidos = b_leidos + resto_leido; resto = resto - b_leidos; } tun_write(foo->tunfd, buf, tot_len); // escribo el buf en el tun if (debug) printf("| %d bytes -> tun\n", tot_len); } return 0;}void *tun_read_udp_write( void *args ) { struct udp_tun *foo; foo = (struct udp_tun *)args; char buf[BUFFER_SIZE]; while (1) { // leo todo un paquete desde tun int b_leidos = read(foo->tunfd, buf, MTU); if (debug) printf("| tun -> leidos %d bytes", b_leidos); struct iphdr *header = (struct iphdr *)buf; int tot_len = ntohs(header->tot_len); // unas comprobaciones no mas... if (debug && (tot_len > b_leidos)) perror("\nse leyo desde tun menos paquete que lo que dice el header\n"); if (debug && (tot_len < b_leidos)) perror("\nse leyo desde tun MAS paquete que lo que dice el header\n"); // se intenta enviar todos los datos int b_enviados = sendto(foo->skfd, buf, b_leidos, 0, (struct sockaddr *)foo->addr, sizeof(struct sockaddr_in)); if (debug) printf("| %d bytes -> sock", b_enviados); int resto = tot_len - b_enviados; // ahora un while tratando de enviar el resto de los datos // desde buf[], hasta completar los IPHEADER.TOTAL_LENGTH bytes while (resto > 0) { int resto_enviado = sendto(foo->skfd, &buf[b_enviados], resto, 0, (struct sockaddr *)foo->addr, sizeof(struct sockaddr_in)); if (debug) printf("| restantes %d bytes -> sock", resto_enviado); b_enviados = b_enviados + resto_enviado; resto = tot_len - b_enviados; } printf("\n"); } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -