/* tinypckt.c a small TCP packet sender in C by thedoors */ #include #include #include #include #include #include #include #include #include #include #include #include #include #define DEFAULT_PORT 80 #define SIGNATURE "tinypckt" #define CHUNK_SIZE 100 #define TINYPCKT_VER "tinypckt.0-2" void usage(char *); int main(int argc, char **argv) { char buffer[CHUNK_SIZE]; ssize_t n, sent; int options; char *p, *file = NULL; unsigned char mode = 0; unsigned short int PORT; extern char *optarg; int tmpfd, sockfd; struct hostent *he; struct sockaddr_in server; if(argc == 1) usage(argv[0]); if(strncmp(argv[1], "--version", sizeof("--version")) == 0) { printf(TINYPCKT_VER "\r\n"); return 0; } p = strrchr(argv[argc-1], ':'); // checking if a port is given if(p == NULL) PORT = DEFAULT_PORT; else { *p = 0; PORT = (unsigned short int)strtol(p+1, NULL, 10); } if((he = gethostbyname(argv[argc-1])) == NULL) { perror("gesthostbyname"); return 1; } while((options = getopt(argc, argv, "qf:")) != -1) { switch(options) { case 'q': mode = 1; break; case 'f': file = optarg; break; default: printf("unknown option %c\r\n", options); return 1; break; } } if(file != NULL) { // reading from stdin or specified file tmpfd = open(file, O_RDONLY); if(tmpfd < 0) { perror("open"); return 1; } } else tmpfd = STDIN_FILENO; if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); close(tmpfd); return 1; } server.sin_family = AF_INET; server.sin_port = htons(PORT); server.sin_addr = *((struct in_addr *)he->h_addr); memset(&(server.sin_zero), 0, 8); if(connect(sockfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) { perror("connect"); close(tmpfd); return 1; } while((n = read(tmpfd, buffer, sizeof(buffer))) > 0) { // reading 100 bytes from file sent = send(sockfd, buffer, n, 0); // while there is data to read and sending it if(mode == 1) continue; // skipping output on quiet mode printf("%ld/%ld sent.\r\n", (signed long int)sent, (signed long int)n); } close(sockfd); close(tmpfd); return 0; } void usage(char *__s) { printf( "\t\t%s\r\n" "usage: %s -options server:port\r\n" "-f [file] -- read from file instead of stdin\r\n" "-q quiet -- do not report packet status\r\n" "--version to output version and exit\r\n", SIGNATURE, __s ); exit(0); }