Import Upstream version 0.5

This commit is contained in:
Leo Antunes
2016-09-28 19:40:56 +02:00
commit 0e2c96c9ef
17 changed files with 11920 additions and 0 deletions
+166
View File
@@ -0,0 +1,166 @@
/*
* knock.c
*
* Copyright (c) 2004-2005 by Judd Vinet <jvinet@zeroflux.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <resolv.h>
#include <getopt.h>
#include <fcntl.h>
static char version[] = "0.5";
#define PROTO_TCP 1
#define PROTO_UDP 2
/* function prototypes */
void vprint(char *fmt, ...);
void ver();
void usage();
int o_verbose = 0;
int o_udp = 0;
int main(int argc, char** argv)
{
int sd;
struct hostent* host;
struct sockaddr_in addr;
int opt, optidx = 1;
static struct option opts[] =
{
{"verbose", no_argument, 0, 'v'},
{"udp", no_argument, 0, 'u'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'V'},
{0, 0, 0, 0}
};
while((opt = getopt_long(argc, argv, "vuhV", opts, &optidx))) {
if(opt < 0) {
break;
}
switch(opt) {
case 0: break;
case 'v': o_verbose = 1; break;
case 'u': o_udp = 1; break;
case 'V': ver();
case 'h': /* fallthrough */
default: usage();
}
}
if((argc - optind) < 2) {
usage();
}
host = gethostbyname(argv[optind++]);
if(host == NULL) {
fprintf(stderr, "Cannot resolve hostname\n");
exit(1);
}
for(; optind < argc; optind++) {
unsigned short port, proto = PROTO_TCP;
char *ptr, *arg = strdup(argv[optind]);
if((ptr = strchr(arg, ':'))) {
*ptr = '\0';
port = atoi(arg);
arg = ++ptr;
if(!strcmp(arg, "udp")) {
proto = PROTO_UDP;
} else {
proto = PROTO_TCP;
}
} else {
port = atoi(arg);
}
if(o_udp || proto == PROTO_UDP) {
sd = socket(PF_INET, SOCK_DGRAM, 0);
if(sd == -1) {
fprintf(stderr, "Cannot open socket\n");
exit(1);
}
} else {
int flags;
sd = socket(PF_INET, SOCK_STREAM, 0);
if(sd == -1) {
fprintf(stderr, "Cannot open socket\n");
exit(1);
}
flags = fcntl(sd, F_GETFL, 0);
fcntl(sd, F_SETFL, flags | O_NONBLOCK);
}
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = *((long*)host->h_addr_list[0]);
addr.sin_port = htons(port);
if(o_udp || proto == PROTO_UDP) {
vprint("hitting udp %s:%u\n", inet_ntoa(addr.sin_addr), port);
connect(sd, (struct sockaddr*)&addr, sizeof(struct sockaddr));
send(sd, NULL, 0, MSG_DONTWAIT);
} else {
vprint("hitting tcp %s:%u\n", inet_ntoa(addr.sin_addr), port);
connect(sd, (struct sockaddr*)&addr, sizeof(struct sockaddr));
}
close(sd);
}
return(0);
}
void vprint(char *fmt, ...)
{
va_list args;
if(o_verbose) {
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
fflush(stdout);
}
}
void usage() {
printf("usage: knock [options] <host> <port[:proto]> [port[:proto]] ...\n");
printf("options:\n");
printf(" -u, --udp make all ports hits use UDP (default is TCP)\n");
printf(" -v, --verbose be verbose\n");
printf(" -V, --version display version\n");
printf(" -h, --help this help\n");
printf("\n");
printf("example: knock myserver.example.com 123:tcp 456:udp 789:tcp\n");
printf("\n");
exit(1);
}
void ver() {
printf("knock %s\n", version);
printf("Copyright (C) 2004-2005 Judd Vinet <jvinet@zeroflux.org>\n");
exit(0);
}
/* vim: set ts=2 sw=2 noet: */
+1516
View File
File diff suppressed because it is too large Load Diff
+248
View File
@@ -0,0 +1,248 @@
/*
* list.c
*
* Copyright (c) 2002-2005 by Judd Vinet <jvinet@zeroflux.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "list.h"
PMList* list_new()
{
PMList *list = NULL;
list = (PMList*)malloc(sizeof(PMList));
if(list == NULL) {
return(NULL);
}
list->data = NULL;
list->prev = NULL;
list->next = NULL;
return(list);
}
void list_free(PMList *list)
{
if(list == NULL) {
return;
}
if(list->data != NULL) {
free(list->data);
list->data = NULL;
}
if(list->next != NULL) {
list_free(list->next);
}
free(list);
return;
}
PMList* list_add(PMList *list, void *data)
{
PMList *ptr, *lp;
ptr = list;
if(ptr == NULL) {
ptr = list_new();
}
lp = list_last(ptr);
if(lp == ptr && lp->data == NULL) {
/* nada */
} else {
lp->next = list_new();
if(lp->next == NULL) {
return(NULL);
}
lp->next->prev = lp;
lp = lp->next;
}
lp->data = data;
return(ptr);
}
PMList* list_remove(PMList* list, void* data)
{
PMList *ptr, *lp;
ptr = list;
for(lp = list; lp; lp = lp->next) {
if(lp->data == data) {
if(lp->prev != NULL) {
lp->prev->next = lp->next;
}
if(lp->next != NULL) {
lp->next->prev = lp->prev;
}
/* test if we just removed the head */
if(lp == ptr) {
ptr = lp->next;
}
}
}
return ptr;
}
int list_count(PMList *list)
{
int i;
PMList *lp;
for(lp = list, i = 0; lp; lp = lp->next, i++);
return(i);
}
int list_isin(PMList *haystack, void *needle)
{
PMList *lp;
for(lp = haystack; lp; lp = lp->next) {
if(lp->data == needle) {
return(1);
}
}
return(0);
}
/* Test for existence of a string in a PMList
*/
int is_in(char *needle, PMList *haystack)
{
PMList *lp;
for(lp = haystack; lp; lp = lp->next) {
if(lp->data && !strcmp(lp->data, needle)) {
return(1);
}
}
return(0);
}
/* List one is extended and returned
*/
PMList* list_merge(PMList *one, PMList *two)
{
PMList *lp, *ptr;
if(two == NULL) {
return one;
}
ptr = one;
if(ptr == NULL) {
ptr = list_new();
}
for(lp = two; lp; lp = lp->next) {
if(lp->data) {
ptr = list_add(ptr, lp->data);
lp->data = NULL;
}
}
return(ptr);
}
PMList* list_last(PMList *list)
{
PMList *ptr;
for(ptr = list; ptr && ptr->next; ptr = ptr->next);
return(ptr);
}
/* Helper function for sorting a list of strings
*/
int list_strcmp(const void *s1, const void *s2)
{
char **str1 = (char **)s1;
char **str2 = (char **)s2;
return(strcmp(*str1, *str2));
}
PMList *list_sort(PMList *list)
{
char **arr = NULL;
PMList *lp;
unsigned int arrct;
int i;
if(list == NULL) {
return(NULL);
}
arrct = list_count(list);
arr = (char **)malloc(arrct*sizeof(char*));
for(lp = list, i = 0; lp; lp = lp->next) {
arr[i++] = (char *)lp->data;
}
qsort(arr, (size_t)arrct, sizeof(char *), list_strcmp);
lp = NULL;
for(i = 0; i < arrct; i++) {
lp = list_add(lp, strdup(arr[i]));
}
if(arr) {
free(arr);
arr = NULL;
}
return(lp);
}
void list_display(const char *title, PMList *list)
{
PMList *lp;
int cols, len, maxcols = 80;
char *cenv = NULL;
cenv = getenv("COLUMNS");
if(cenv) {
maxcols = atoi(cenv);
}
len = strlen(title);
printf("%s ", title);
if(list) {
for(lp = list, cols = len; lp; lp = lp->next) {
int s = strlen((char*)lp->data)+1;
if(s+cols >= maxcols) {
int i;
cols = len;
printf("\n");
for (i = 0; i < len+1; i++) {
printf(" ");
}
}
printf("%s ", (char*)lp->data);
cols += s;
}
printf("\n");
} else {
printf("None\n");
}
}
/* vim: set ts=2 sw=2 noet: */
+48
View File
@@ -0,0 +1,48 @@
/*
* list.h
*
* Copyright (c) 2002-2005 by Judd Vinet <jvinet@zeroflux.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#ifndef _PAC_LIST_H
#define _PAC_LIST_H
#define FREELIST(p) { list_free(p); p = NULL; }
/* your average linked list */
typedef struct __pmlist_t {
void* data;
struct __pmlist_t* prev;
struct __pmlist_t* next;
} PMList;
PMList* list_new();
void list_free(PMList* list);
PMList* list_add(PMList* list, void* data);
PMList* list_remove(PMList* list, void* data);
int list_count(PMList* list);
int list_isin(PMList *haystack, void *needle);
int is_in(char *needle, PMList *haystack);
PMList* list_merge(PMList *one, PMList *two);
PMList* list_last(PMList* list);
int list_strcmp(const void *s1, const void *s2);
PMList *list_sort(PMList *list);
void list_display(const char *title, PMList *list);
#endif
/* vim: set ts=2 sw=2 noet: */