Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
- #include <net/if.h>
- #include <pthread.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <linux/if_packet.h>
- #include <netinet/if_ether.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #define NBNS_NBSTATREQ_LEN 50
- /**
- * @brief static NetBIOS NBSTAT request.
- *
- * RFC 1002, Paragraph 4.2.17
- */
- uint8_t nbns_nbstat_request[NBNS_NBSTATREQ_LEN] = {
- 0x82, 0x28, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x20, 0x43, 0x4B, 0x41, 0x41, 0x41, 0x41, 0x41,
- 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
- 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
- 0x41, 0x41, 0x41, 0x41, 0x41, 0x00, 0x00, 0x21, 0x00, 0x01
- };
- char running = 1;
- //pid_t receiver_pid;
- long unsigned int total, bad;
- void *sender(void *arg) {
- int sockfd;
- struct sockaddr_in addr;
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_port = htons(137);
- addr.sin_addr.s_addr = htonl(0x0AA9A7FE); // 10.169.167.254
- connect(sockfd, (struct sockaddr *) &addr, sizeof(addr));
- while(running) {
- write(sockfd, nbns_nbstat_request, NBNS_NBSTATREQ_LEN);
- usleep(100);
- }
- sendto(sockfd, nbns_nbstat_request, NBNS_NBSTATREQ_LEN, 0, (struct sockaddr *) &addr, sizeof(addr));
- return NULL;
- }
- void stop(int signal) {
- running = 0;
- }
- static int
- iface_get_id(const char *device)
- {
- struct ifreq ifr;
- int fd;
- // this is the main difference from pcap:
- // we use another socket for perform ioctl interrogations
- fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
- memset(&ifr, 0, sizeof(ifr));
- strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
- if (ioctl(fd, SIOCGIFINDEX, &ifr) == -1) {
- fprintf(stderr, "SIOCGIFINDEX: %s\n", strerror(errno));
- close(fd);
- return -1;
- }
- close(fd);
- return ifr.ifr_ifindex;
- }
- void *receiver(void *arg) {
- int sockfd;
- int ifindex;
- struct sockaddr_ll sll;
- int err;
- socklen_t errlen = sizeof(err);
- socklen_t fromlen;
- size_t packet_len;
- char buffer[1514];
- /* pcap_open_live */
- sockfd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
- ifindex = iface_get_id((char *) arg);
- if(ifindex==-1) return NULL;
- memset(&sll, 0, sizeof(sll));
- sll.sll_family = AF_PACKET;
- sll.sll_ifindex = ifindex;
- sll.sll_protocol = htons(ETH_P_ALL);
- if (bind(sockfd, (struct sockaddr *) &sll, sizeof(sll)) == -1) {
- fprintf(stderr, "bind: %s\n", strerror(errno));
- return NULL;
- }
- /* Any pending errors, e.g., network is down? */
- if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
- fprintf(stderr, "getsockopt: %s\n", strerror(errno));
- return NULL;
- }
- if (err > 0) {
- fprintf(stderr, "bind: %s\n", strerror(err));
- return NULL;
- }
- /* pcap_open_live ends */
- /* pcap_loop */
- fromlen = sizeof(sll);
- while(running) {
- do {
- packet_len = recvfrom(sockfd, buffer, 1514, MSG_TRUNC,
- (struct sockaddr *) &sll, &fromlen);
- } while(running && packet_len == -1 && (errno == EINTR || errno == EAGAIN));
- if(running && packet_len == -1) {
- fprintf(stderr, "recvfrom: %s\n", strerror(errno));
- return NULL;
- }
- if(packet_len > 14) {
- total++;
- fprintf(stderr, "sll: { sll_family=%d, sll_protocol=%d, sll_ifindex=%d, sll_hatype=%d, sll_pkttype=%d, sll_halen=%d, sll_addr=[%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX] }\n",
- sll.sll_family, sll.sll_protocol, sll.sll_ifindex, sll.sll_hatype,
- sll.sll_pkttype, sll.sll_halen, sll.sll_addr[0], sll.sll_addr[1],
- sll.sll_addr[2], sll.sll_addr[3], sll.sll_addr[4], sll.sll_addr[5],
- sll.sll_addr[6], sll.sll_addr[7]);
- fprintf(stderr, "%02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX => %02hhX:%02hhX:%02hhX:%02hhX:%02hhX:%02hhX\n",
- buffer[6], buffer[7], buffer[8], buffer[9], buffer[10], buffer[11],
- buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5]);
- fflush(stderr);
- if(memcmp(buffer, sll.sll_addr, 6) && memcmp(buffer + 6, sll.sll_addr, 6)) {
- bad++;
- }
- }
- }
- /* pcap_loop ends */
- close(sockfd);
- return NULL;
- }
- int main(int argc, char **argv) {
- if(argc != 2) return 1;
- if(signal(SIGINT, stop) == SIG_ERR) return 2;
- bad = total = 0;
- #ifdef SENDER
- sender((void *) argv[1]);
- #else
- receiver((void *) argv[1]);
- #endif
- running=0;
- #ifndef SENDER
- fprintf(stderr, "total packets: %lu\nbad ones: %lu\n", total, bad);
- #endif
- exit(0); // close all unclear stuff [ hey, is a test program ;) ]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement