int main(int argc, char* argv[]) { int local_port; char* local_interface; char* remote_host; int remote_port; int isClient = 0; if (argc < 3 || argc > 4) { perror("Error: improper number of args entered\nProper format: ./cs352proxy <port> <local_interface> or ./cs352proxy <remote_host> <remote_port> <local_interface>\n"); return 0; } else if (argc == 3) { local_port = atoi(argv[1]); if (local_port < 1024 || local_port > 65536) { printf("port out of range\n"); return 1; } local_interface = argv[2]; isClient = 0; } else { remote_host = argv[1]; remote_port = atoi(argv[2]); if (remote_port < 1024 || remote_port > 65536) { printf("port out of range\n"); return 1; } local_interface = argv[3]; isClient = 1; } if ( (tap_fd = allocate_tunnel(local_interface, IFF_TAP | IFF_NO_PI)) < 0) { perror("Opening tap interface failed! \n"); exit(1); } int socketfd = socket(AF_INET, SOCK_STREAM, 0); if (socketfd < 0) { perror("Socket failed"); exit(1); } if (!isClient) { TCPSocketfd = createServer(socketfd, local_port); } else { TCPSocketfd = createClient(socketfd, remote_port, remote_host); } pthread_t TAPthread, TCPthread; pthread_create(&TAPthread, NULL, TAPHandle, NULL); pthread_create(&TCPthread, NULL, TCPHandle, NULL); pthread_join(TAPthread, NULL); pthread_join(TCPthread, NULL); return 0; }
void tapHandler(void* tapSock) { /*Get tap fd and mac address*/ localTapMac = malloc( sizeof(uint8_t)*6 ); if ( (tap_fd = allocate_tunnel(TAP_NAME, IFF_TAP | IFF_NO_PI, localTapMac)) < 0 ) { perror("tapHandler"); exit(1); } int socket_fd = (int)tapSock; int ret_status; unsigned short int data_size = 0; /*Read from tap */ while(globalStatus) { char* tapPayload = malloc(sizeof(char) * PAYLOAD_SIZE); data_size = readSock(tap_fd, &tapPayload, PAYLOAD_SIZE); if(data_size == 0) { continue; } /*SNIP*/ /* Create a Data packet */ struct data_packet* pktFromTap = malloc(sizeof(struct data_packet)); pktFromTap->head.packet_type = 0xABCD; pktFromTap->head.packet_length = data_size; pktFromTap->datagram = tapPayload; /*Send from tap to dest*/ broadcast(routesList, (void*)pktFromTap); } }
int main (int argc, char *argv[]) { char buffer[MAXBUFFSIZE]; char if_name[IFNAMSIZ] = ""; // int port; char buffer2[MAXBUFFSIZE]; int size; pthread_t thread; // switch(argc) { // case 3: /* Server Mode */ // host = NULL; // port = atoi(argv[1]); // if (port < 1024 || port > 65535) { // puts("Port number must be in the range of 1024 to 65535."); // return EXIT_FAILURE; // } // strncpy(if_name, argv[2], IFNAMSIZ - 1); // break; // case 4: /* Client Mode */ // host = argv[1]; // port = atoi(argv[2]);; // if (port < 1024 || port > 65535) { // puts("Port number must be in the range of 1024 to 65535."); // return EXIT_FAILURE; // } // strncpy(if_name, argv[3], IFNAMSIZ - 1); // break; // default: Syntax Error // print_usage(); // return EXIT_FAILURE; // } unsigned char dest[ETH_ALEN] = { 0x00, 0x12, 0x34, 0x56, 0x78, 0x90 }; unsigned short proto = 0x1234; unsigned char *data = "hello world~!!!"; unsigned short data_len = strlen(data); unsigned char source[ETH_ALEN] = { 0x61, 0x12, 0x34, 0x56, 0x78, 0x90 }; union ethframe frame; memcpy(frame.field.header.h_dest, dest, ETH_ALEN); memcpy(frame.field.header.h_source, source, ETH_ALEN); frame.field.header.h_proto = htons(proto); memcpy(frame.field.data, data, data_len); unsigned int frame_len = data_len + ETH_HLEN; strncpy(if_name, argv[1], IFNAMSIZ - 1); strcpy(buffer, "teskdkak3dakdkTEATTESTWHOO"); printf("Attempting to open %s...\n", if_name); /* Open tap interface */ if ((tap_fd = allocate_tunnel(if_name, IFF_TAP | IFF_NO_PI)) < 0) { perror("Opening tap interface failed!"); return EXIT_FAILURE; } else { printf("Successfully opened %s interface...\n", if_name); } if ((size = write(tap_fd, frame.buffer, frame_len)) < 0) { perror("write to tap failed"); close(tap_fd); exit(1); } else { printf("%d bytes sent to tap..\n", size); } if (pthread_create(&socket_thread, NULL, handle_tap, NULL) != 0 ) { perror("pthread_create failed for tap thread"); exit(1); } pthread_exit(NULL); // /* Create TCP Socket */ // if ((sock_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { // perror("could not create socket"); // return EXIT_FAILURE; // } // /* Check for server or client mode */ // if (host == NULL) { // server(port, sock_fd, if_name); // } else { // client(port, sock_fd, host, if_name); // } // pthread_exit(NULL); // close(net_fd); NU // close(tap_fd); pthread_join(thread, NULL); close(tap_fd); return 0; }
int main(int argc, char* argv[]) { // Check for correct syntax printf("argc=%d\n", argc); if(argc < 3 || argc > 4) { printf("Invalid arguments.\n"); } struct addrinfo hints, *res; int status, connect_sock; char *addr; char *port; struct socks_h socks; pthread_t tid_incoming, tid_outgoing; // Set up structure for getaddrinfo memset(&hints, 0, sizeof hints); hints.ai_family = AF_INET; // Use IPv4 hints.ai_socktype = SOCK_STREAM; //Use tcp if(argc == 3) { // I'm a server printf("I'm a server\n"); hints.ai_flags = AI_PASSIVE; addr = NULL; port = argv[1]; } else { // I'm a client printf("I'm a client\n"); addr = argv[1]; port = argv[2]; } // getaddrinfo will populate res with the connection info if((status = getaddrinfo(addr, port, &hints, &res)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status)); return(EXIT_FAILURE); } // Create socket if((connect_sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) { perror("Getting socket"); return(EXIT_FAILURE); } if(argc == 3) { // I'm a server if(bind(connect_sock, res->ai_addr, res->ai_addrlen) < 0) { perror("Bind"); return(EXIT_FAILURE); } if(listen(connect_sock,2) < 0) { perror("Listen"); return(EXIT_FAILURE); } // Put file descriptor for the connection into the socks struct if((socks.remote_sock = accept(connect_sock, NULL, NULL)) < 0) { perror("Accept"); return(EXIT_FAILURE); } printf("Successful connection from client\n"); } else { // I'm a client if(connect(connect_sock, res->ai_addr, res->ai_addrlen) < 0) { perror("Connect"); return(EXIT_FAILURE); } // Put file descriptor for the connection into the socks struct socks.remote_sock = connect_sock; printf("Successful connection to server\n"); } // Put file descriptor for the tap into the socks struct if ( (socks.tun_sock = allocate_tunnel(argv[argc-1], IFF_TAP | IFF_NO_PI)) < 0 ) { perror("Opening tap interface failed! \n"); exit(1); } // Free the addrinfo structure freeaddrinfo(res); // Create thread to process outgoing data pthread_create(&tid_outgoing, NULL, outgoing, (void *)&socks); // Create thread to process incoming data pthread_create(&tid_incoming, NULL, incoming, (void *)&socks); // These might be useful if the threads didn't loop forever pthread_join(tid_incoming, NULL); pthread_join(tid_outgoing, NULL); return EXIT_SUCCESS; }
int main(int argc, char **argv){ char *c; unsigned short port; thread_param tp; pthread_t eth_tid, tap_tid; char *remote_host; int remote_port; char *local_interface; if(argc != 4 || argc != 3){ printf("Error\n\t Usage for first proxy:\n\t\t cs352proxy <port> <local interface> \n\t Usage for second proxy: \n\t\t cs352proxy <remote host> <remote port> <local interface>\n"); return -1; } switch(argc){ // 1st proxy case 3: for(c=argv[1]; isdigit(*c)||*c=='\0'; c++); if(*c=='\0'){ tp.ethfd=atoi(argv[1]); if(tp.ethfd<1024||tp.ethfd>65535){ perror("ERROR: port must be from " "1024-65535.\n"); exit(1); } port=(unsigned short)tp.ethfd; } else{ perror("ERROR: port parameter " "not a decimal number.\n"); exit(1); } if((tp.ethfd=open_listenfd(port))<0){ perror("error opening ethernet device\n"); exit(1); } if((tp.tapfd=allocate_tunnel(argv[2], IFF_TAP|IFF_NO_PI))<0){ perror("error opening tap device\n"); exit(1); } /** * 1st thread listens to TCP socket * 2nd thread listens to tap device */ Pthread_create(ð_tid, NULL, eth_thread, &tp); Pthread_create(&tap_tid, NULL, tap_thread, &tp); Pthread_join(eth_tid, NULL); Pthread_join(tap_tid, NULL); close(tp.ethfd); close(tp.tapfd); break; // 2nd proxy case 4: for(c=argv[2]; isdigit(*c)||*c=='\0'; c++); if(*c=='\0'){ tp.ethfd=atoi(argv[2]); if(tp.ethfd<1024||tp.ethfd>65535){ perror("ERROR: port must be from " "1024-65535.\n"); exit(1); } port=(unsigned short)tp.ethfd; } else{ perror("ERROR: port parameter " "not a decimal number.\n"); exit(1); } if((tp.ethfd=open_clientfd(argv[1], port))<0){ perror("error opening ethernet device\n"); exit(1); } if((tp.tapfd=allocate_tunnel(argv[3], IFF_TAP|IFF_NO_PI))<0){ perror("error opening tap device\n"); exit(1); } /** * 1st thread listens to TCP socket * 2nd thread listens to tap device */ Pthread_create(ð_tid, NULL, eth_thread, &tp); Pthread_create(&tap_tid, NULL, tap_thread, &tp); Pthread_join(eth_tid, NULL); Pthread_join(tap_tid, NULL); close(tp.ethfd); close(tp.tapfd); break; default: perror("ERROR: invalid parameters.\n"); exit(1); } return 0; }