/* Prints out the matchinfo. */ static void print(const void *ip, const struct xt_entry_match *match, int numeric) { printf("LAYER7 "); print_protocol(((struct xt_layer7_info *)match->data)->protocol, ((struct xt_layer7_info *)match->data)->invert, numeric); }
/* POSIX.1 defines several new functions to allow an application to map * from a host name and a service name to an address and vice versa. * These functions replace the older gethostbyname() and gethostbyaddr() * functions. The getaddrinfo() function allows us to map a host name and * a service name to an address. * #include <sys/socket.h> * #include <netdb.h> * int getaddrinfo(const char *host, const char *service, * const struct *hint, struct addrinfo **res); * Returns: 0 if OK, nonzero error code on error * void freeaddrinfo(struct addrinfo *ai); * * We need to provide the host name, the service name, or both. If we * provide only one name, the other should be a null pointer. The host * name can be either a node name or the host address in dotted-decimal * notation. 如 www.baidu.com, www.google.com 等 * The getaddrinfo() function returns a linked list of addrinfo structures. * We can use freeaddrinfo() to free one or more of these structures, * depending on how many structures are linked together using the ai_next * field. The addrinfo structure is defined to include at least the * following members: * struct addrinfo { * int ai_flags; // customize behavior * int ai_family; // address family * int ai_socktype; // socket type * int ai_protocol; // protocol * socklen_t ai_addrlen; // length in bytes of address * struct sockaddr *ai_addr; // address * char *ai_canonname; // canonical name of host * struct addrinfo *ai_next; // next in list * }; * * We can supply an optional hint to select addresses that meet certain * criteria. The hint is a template used for filtering addresses and uses * only the ai_family, ai_flags, ai_protocol, and ai_socktype fields. The * remainning integer fields must be set to 0, and the pointer fields must * be null. * * If getaddrinfo() fails, we can't use perror() or strerror() to generate * an error message. Instead, we need to call gai_strerror() to convert * the error code returned into an error message. * #include <netdb.h> * const char *gai_strerror(int error); * Returns: a pointer to a string describing the error * * getaddrinfo() 函数的host参数可以是本地的主机名(通过hostname命令来获取), * 也可以是网络上的域名,如www.baidu.com, www.google.com 等,此时就能获取 * 到www.baidu.com对应的地址信息,如ip地址,服务的端口地址,协议类型等. * * The getnameinfo() function converts an address into a host name and * a service name. * #include <sys/socket.h> * #include <netdb.h> * int getnameinfo(const struct sockaddr *addr, socklen_t alen, char *host, * socklen_t hostlen, char *service, socklen_t servlen, * unsigned int flags); * Returns: 0 if OK, nonzero on error * The socket address (addr) is translated into a host name and a service * name. If host is non-null, it points to a buffer hostlen bytes long * that will be used to return the host name. Similarly, if service is * non-null, it points to a buffer servlen bytes long that will be used to * return the service name. * * The flags argument gives us some control over how the translation is done */ int main(int argc, char *argv[]) { struct addrinfo *aliases, *aip; struct addrinfo hint; struct sockaddr_in *sinp; const char *addr; int err; char abuf[INET_ADDRSTRLEN]; if (argc != 3) { printf("usage: %s nodename servicename\n", argv[0]); return 1; } hint.ai_flags = AI_CANONNAME; hint.ai_family = 0; hint.ai_socktype = 0; hint.ai_protocol = 0; hint.ai_addrlen = 0; hint.ai_addr = NULL; hint.ai_canonname = NULL; hint.ai_next = NULL; if ((err = getaddrinfo(argv[1], argv[2], &hint, &aliases)) != 0) { printf("getaddrinfo error: %s\n", gai_strerror(err)); return 1; } /* If multiple protocols provide the given service for the given host, * the program will print more than one entry. */ for (aip = aliases; aip != NULL; aip = aip->ai_next) { print_flags(aip); print_family(aip); print_type(aip); print_protocol(aip); printf("\n\thost: %s", aip->ai_canonname?aip->ai_canonname:"-"); if (aip->ai_family == AF_INET) { sinp = (struct sockaddr_in *)aip->ai_addr; /* inet_ntop() 函数返回 const char * 类型指针,所以 addr 也要 * 定义为 const char * 类型,否则编译报警: * assignment discards qualifiers from pointer target type */ addr = inet_ntop(AF_INET, &sinp->sin_addr, abuf, INET_ADDRSTRLEN); printf(", address: %s", addr ? addr : "unknown"); printf(", port: %d", ntohs(sinp->sin_port)); } printf("\n"); } freeaddrinfo(aliases); return 0; }
/* * Check options and call the proper handler */ static int print_one_device(int skfd, int format, int wtype, const char* ifname) { int ret; /* Check wtype */ switch(wtype) { case WTYPE_AP: /* Try to print an AP */ ret = print_ap(skfd, ifname, format); break; case WTYPE_CHANNEL: /* Try to print channel */ ret = print_channel(skfd, ifname, format); break; case WTYPE_FREQ: /* Try to print frequency */ ret = print_freq(skfd, ifname, format); break; case WTYPE_MODE: /* Try to print the mode */ ret = print_mode(skfd, ifname, format); break; case WTYPE_PROTO: /* Try to print the protocol */ ret = print_protocol(skfd, ifname, format); break; default: /* Try to print an ESSID */ ret = print_essid(skfd, ifname, format); if(ret < 0) { /* Try to print a nwid */ ret = print_nwid(skfd, ifname, format); } } return(ret); }
int main(int argc, char *argv[]) { struct addrinfo *ailist, *aip; struct addrinfo hint; struct sockaddr_in *sinp; const char *addr; int err; char abuf[INET_ADDRSTRLEN]; if (argc != 3) { printf("usage: %s nodename service\n", argv[0]); exit(0); } hint.ai_flags = AI_CANONNAME; hint.ai_family = 0; hint.ai_socktype = 0; hint.ai_protocol = 0; hint.ai_addrlen = 0; hint.ai_canonname = NULL; hint.ai_addr = NULL; hint.ai_next = NULL; if ((err = getaddrinfo(argv[1], argv[2], &hint, &ailist)) != 0) { printf("getaddrinfo error: %s", gai_strerror(err)); exit(0); } for (aip = ailist; aip != NULL; aip = aip->ai_next) { printf("~~~~~~~~~~~~~~~~~~~~~~~~\n"); print_flags(aip); print_family(aip); print_type(aip); print_protocol(aip); printf("\n\thost %s", aip->ai_canonname?aip->ai_canonname:"-"); if (aip->ai_family == AF_INET) { sinp = (struct sockaddr_in *)aip->ai_addr; addr = inet_ntop(AF_INET, &sinp->sin_addr, abuf, INET_ADDRSTRLEN); printf(" address %s", addr?addr:"unknown"); printf(" port %d", ntohs(sinp->sin_port)); } printf("\n"); } exit(0); }
int main(int argc, char *argv[]) { struct addrinfo hint; struct addrinfo *ailist, *aip; const char *addr; struct sockaddr_in *sinp; int err; char abuf[INET_ADDRSTRLEN]; if (argc != 3) { fprintf(stderr, "Usage: %s <host> <service>\n", argv[0]); exit(0); } hint.ai_flags = AI_CANONNAME; hint.ai_family = 0; hint.ai_socktype = 0; hint.ai_protocol = 0; hint.ai_addrlen = 0; hint.ai_canonname = NULL; hint.ai_addr = NULL; hint.ai_next = NULL; if (getaddrinfo(argv[1], argv[2], &hint, &ailist) < 0) { fprintf(stderr, "Getaddrinfo error\n"); exit(0); } for (aip = ailist; aip != NULL; aip = aip -> ai_next) { print_protocol(aip); if (aip -> ai_family = AF_INET) { sinp = (struct sockaddr_in *)(aip -> ai_addr); addr = inet_ntop(AF_INET, &(sinp -> sin_addr), abuf, INET_ADDRSTRLEN); printf("Host name: %s\n", addr ? addr : "Unknown"); printf("Port: %d\n", ntohs(sinp -> sin_port)); } } exit(0); }
/** @brief Filters the socket system call received from the applications. Redirect the calls * to the right destination which is either the fins_socket function or sending it to the * original corresponding function in the GNU C library * @param domain * @param type * @param protocol */ int socket(int domain, int type, int protocol) { /**TODO These static vars are not thread safe * we either find another way to keep tracking or * protect them with multi-processes semaphore */ // static int numberOfcalls=0; // Define a locker object to protect the static variable static int numberOfSockets=0; int retval; struct socket *sock; int flags; int fins_sock; char *errormsg; /** with the first interception takes place , we initialize the socket channel */ if (numberOfSockets == 0) { init_socketChannel(); } PRINT_DEBUG("#Sockets = %d \n",numberOfSockets); print_protocol(domain, type, protocol); _socket = (int (*)(int domain, int type, int protocol)) dlsym(RTLD_NEXT, "socket"); errormsg = dlerror(); if (errormsg != NULL) { PRINT_DEBUG("\n failed to load the original symbol %s", errormsg); } if( (domain == AF_UNIX) | (domain == AF_INET6 ) | (domain == AF_NETLINK ) | (domain == AF_PACKET) ) { retval = _socket(domain, type, protocol); return (retval); } else if ( domain == AF_INET ) { if (type == SOCK_DGRAM ) /** Handle UDP sockets */ { PRINT_DEBUG("123"); retval = fins_socket(domain,type, protocol); if (retval != -1) { // TODO lock the locker protect the static variable numberOfSockets = numberOfSockets +1; // TODO unlock the locker protect the static variable } return(retval); } else /** Handle sockets other than UDP */ { PRINT_DEBUG("original socket will be called, not a udp socket"); PRINT_DEBUG("domain = %d,type = %d",domain, type); retval = _socket(domain, type, protocol); return(retval); } } else { printf("UNKNOWN SOCKET FAMILY !!!!! \n"); PRINT_DEBUG("domain = %d,type = %d",domain, type); return (-1); } }