/*****************************************************************************
* __________________ _________ _____ _____ .__ ._.
* \______ \______ \ / _____/ / \ / _ \ |__| ____ | |
* | | _/| | \ \_____ \ / \ / \ / /_\ \| _/ __ \ | |
* | | \| ` \/ / Y \ / | | \ ___/ \|
* |______ /_______ /_______ \____|__ / /\ \____|__ |__|\___ | __
* \/ \/ \/ \/ )/ \/ \/ \/
*
* This file is part of liBDSM. Copyright © 2014-2015 VideoLabs SAS
*
* Author: Julien 'Lta' BALLET <contact@lta.io>
* Thomas Guillem <tguillem@videolabs.io>
*
* liBDSM is released under LGPLv2.1 (or later) and is also available
* under a commercial license.
*****************************************************************************
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#ifdef HAVE_SYS_QUEUE_H
# include <sys/queue.h>
#endif
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef _WIN32
# include <winsock2.h>
# include <ws2tcpip.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
# include <sys/socket.h>
#endif
#include <bdsm/netbios_ns.h>
#include "bdsm_debug.h"
#include "netbios_query.h"
#include "netbios_utils.h"
#include "compat.h"
enum name_query_type {
NAME_QUERY_TYPE_INVALID,
NAME_QUERY_TYPE_NB,
NAME_QUERY_TYPE_NBSTAT
};
static char name_query_broadcast[] = NETBIOS_WILDCARD;
enum ns_entry_flag {
NS_ENTRY_FLAG_INVALID = 0x00,
NS_ENTRY_FLAG_VALID_IP = 0x01,
NS_ENTRY_FLAG_VALID_NAME = 0x02,
};
struct netbios_ns_entry
{
TAILQ_ENTRY(netbios_ns_entry) next;
struct in_addr address;
char name[NETBIOS_NAME_LENGTH + 1];
char group[NETBIOS_NAME_LENGTH + 1];
char type;
int flag;
time_t last_time_seen;
};
typedef TAILQ_HEAD(, netbios_ns_entry) NS_ENTRY_QUEUE;
#define RECV_BUFFER_SIZE 1500 // Max MTU frame size for ethernet
struct netbios_ns
{
int socket;
struct sockaddr_in addr;
uint16_t last_trn_id; // Last transaction id used;
NS_ENTRY_QUEUE entry_queue;
uint8_t buffer[RECV_BUFFER_SIZE];
#ifdef HAVE_PIPE
int abort_pipe[2];
#else
pthread_mutex_t abort_lock;
bool aborted;
#endif
unsigned int discover_broadcast_timeout;
pthread_t discover_thread;
bool discover_started;
netbios_ns_discover_callbacks discover_callbacks;
};
typedef struct netbios_ns_name_query netbios_ns_name_query;
struct netbios_ns_name_query
{
enum name_query_type type;
union {
struct {
uint32_t ip;
} nb;
struct {
const char *name;
const char *group;
char type;
} nbstat;
}u;
};
static netbios_ns_entry *netbios_ns_inverse_internal(netbios_ns *ns,
uint32_t ip);
static int ns_open_socket(netbios_ns *ns)
{
int sock_opt;
if ((ns->socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
goto error;
sock_opt = 1;
if (setsockopt(ns->socket, SOL_SOCKET, SO_BROADCAST,
(void *)&sock_opt, sizeof(sock_opt)) < 0)
goto error;
sock_opt = 0;
if (setsockopt(ns->socket, IPPROTO_IP, IP_MULTICAST_LOOP,
(void *)&sock_opt, sizeof(sock_opt)) < 0)
goto error;
ns->addr.sin_family = AF_INET;
ns->addr.sin_port = htons(0);
ns->addr.sin_addr.s_addr = 0;
if (bind(ns->socket, (struct sockaddr *)&ns->addr, sizeof(ns->addr)) < 0)
goto error;
return 1;
error:
BDSM_perror("netbios_ns_new, open_socket: ");
return 0;
}
#ifdef HAVE_PIPE
static int ns_open_abort_pipe(netbios_ns *ns)
{
int flags;
if (pipe(ns->abort_pipe) == -1)
return -1;
#ifndef _WIN32
if ((flags = fcntl(ns->abort_pipe[0], F_GETFL, 0)) == -1)
return -1;
if (fcntl(ns->abort_pipe[0], F_SETFL, flags | O_NONBLOCK) == -1)
return -1;
#endif
return 0;
}
static void ns_close_abort_pipe(netbios_ns *ns)
{
if (ns->abort_pipe[0] != -1 && ns->abort_pipe[1] != -1)
{
closesocket(ns->abort_pipe[0]);
closesocket(ns->abort_pipe[1]);
ns->abort_pipe[0] = ns->abort_pipe[1] = -1;
}
}
static bool netbios_ns_is_aborted(netbios_ns *ns)
{
fd_set read_fds;
int res;
struct timeval timeout = {0, 0};
FD_ZERO(&read_fds);
FD_SET(ns->abort_pipe[0], &read_fds);
res = select(ns->abort_pipe[0] + 1, &read_fds, NULL, NULL, &timeout);
return (res < 0 || FD_ISSET(ns->abort_pipe[0], &read_fds));
}
static void netbios_ns_abort(netbios_ns *ns)
{
uint8_t buf = '\0';
write(ns->abort_pipe[1], &buf, sizeof(uint8_t));
}
#else
static int ns_open_abort_pipe(netbios_ns *ns)
{
return pthread_mutex_init(&ns->abort_lock, NULL);
}
static void ns_close_abort_pipe(netbios_ns *ns)
{
pthread_mutex_destroy(&ns->abort_lock);
}
static bool netbios_ns_is_aborted(netbios_ns *ns)
{
pthread_mutex_lock(&ns->abort_lock);
bool res = ns->aborted;
pthread_mutex_unlock(&ns->abort_lock);
return res;
}
static void netbios_ns_abort(netbios_ns *ns)
{
pthread_mutex_lock(&ns->abort_lock);
ns->aborted = true;
pthread_mutex_unlock(&ns->abort_lock);
}
#endif
static uint16_t query_type_nb = 0x2000;
static uint16_t query_type_nbstat = 0x2100;
static uint16_t query_class_in = 0x0100;
static int netbios_ns_send_name_query(netbios_ns *ns,
uint32_t ip,
enum name_query_type type,
const char *name,
uint16_t query_flag)
{
struct sockaddr_in addr;
ssize_t sent;
uint16_t trn_id;
uint16_t query_type;
netbios_query *q;
assert(ns != NULL);
switch (type)
{
case NAME_QUERY_TYPE_NB:
query_type = query_type_nb;
break;
case NAME_QUERY_TYPE_NBSTAT:
query_type = query_type_nbstat; // NBSTAT/IP;
break;
default:
BDSM_dbg("netbios_ns_send_name_query: unknow name_query_type");
return -1;
}
// Prepare packet
q = netbios_query_new(34 + 4, 1, NETBIOS_OP_NAME_QUERY);
if (query_flag)
netbios_query_set_flag(q, query_flag, 1);
// Append the queried name to the packet
netbios_query_append(q, name, strlen(name) + 1);
// Magic footer (i.e. Question type (Netbios) / class (IP)
netbios_query_append(q, (const char *)&query_type, 2);
netbios_query_append(q, (const char *)&query_class_in, 2);
q->packet->queries = htons(1);
if (ip == 0)
ip = 0xFFF
没有合适的资源?快使用搜索试试~ 我知道了~
libdsm0.2.5
需积分: 10 3 下载量 42 浏览量
2016-06-26
18:50:34
上传
评论
收藏 453KB GZ 举报
温馨提示
共104个文件
h:39个
c:31个
m4:13个
最新的vlc编译需要的库,下了好久了
资源推荐
资源详情
资源评论
收起资源包目录
libdsm0.2.5 (104个子文件)
configure.ac 4KB
Makefile.am 3KB
spnego.asn1 2KB
AUTHORS 258B
netbios_ns.c 25KB
smb_trans2.c 16KB
smb_share.c 14KB
smb_file.c 12KB
smb_ntlm.c 12KB
smb_spnego.c 12KB
smb_session.c 10KB
md5.c 9KB
netbios_session.c 8KB
md4.c 8KB
smb_message.c 7KB
dsm.c 7KB
smb_fd.c 5KB
smb_dir.c 4KB
netbios_utils.c 4KB
netbios_query.c 4KB
smb_utils.c 4KB
rc4.c 4KB
smb_stat.c 3KB
smb_transport.c 3KB
hmac_md5.c 3KB
smb_session_msg.c 3KB
discover.c 3KB
clock_gettime.c 3KB
inverse.c 3KB
lookup.c 2KB
smb_buffer.c 2KB
spnego_asn1.c 2KB
strndup.c 2KB
strlcpy.c 2KB
compat.c 175B
compile 7KB
configure 498KB
COPYING 27KB
depcomp 23KB
config.guess 43KB
smb_packets.h 17KB
smb_defs.h 10KB
smb_defs.h 8KB
queue.h 8KB
netbios_ns.h 6KB
smb_stat.h 6KB
smb_file.h 5KB
smb_session.h 5KB
smb_types.h 5KB
smb_ntlm.h 5KB
smb_share.h 4KB
netbios_defs.h 4KB
smb_message.h 3KB
netbios_session.h 3KB
smb_types.h 3KB
smb_utils.h 3KB
smb_dir.h 3KB
bdsm_debug.h 3KB
smb_session.h 3KB
smb_buffer.h 2KB
netbios_query.h 2KB
rc4.h 2KB
compat.h 2KB
smb_fd.h 2KB
netbios_utils.h 2KB
smb_transport.h 2KB
smb_session_msg.h 2KB
bdsm.h 2KB
hmac_md5.h 2KB
netbios_defs.h 2KB
bdsm_common.h 2KB
smb_spnego.h 2KB
smb_dir.h 2KB
smb_stat.h 2KB
smb_file.h 2KB
smb_share.h 2KB
md5.h 1KB
md4.h 1KB
spnego_asn1.h 164B
Doxyfile.in 98KB
Makefile.in 44KB
config.h.in 4KB
libdsm.pc.in 334B
install-sh 15KB
libtool.m4 299KB
aclocal.m4 41KB
lib-link.m4 32KB
ltoptions.m4 14KB
ax_pthread.m4 13KB
attributes.m4 9KB
iconv.m4 9KB
lib-prefix.m4 8KB
pkg.m4 8KB
lt~obsolete.m4 6KB
dolt.m4 5KB
ltsugar.m4 4KB
ltversion.m4 699B
missing 7KB
NEWS 3KB
ltmain.sh 317KB
共 104 条
- 1
- 2
资源评论
shuikun26
- 粉丝: 1
- 资源: 14
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于CarNet实现裂缝检测python源码+文档说明+数据+图片(课程设计)
- 课程设计-基于耐火材料裂缝剥落检测python源码+课件
- 基于OpenCV的视频道路车道检测python源码+文档说明+实验演示+图片+使用方法(高分毕业设计)
- 基于OpenCV的案例:图像边缘、角点和轮廓检测,图像分割,图像增强;图片拼接;运动目标检测,颜色直方图比较,三帧帧差法,抠图
- SmartPlug-html大一笔记
- SmartPlug-proteusdemo
- Preliminary Findings on Handmade Rattan Baby Crib andBassinet Designs Regarding.zip
- aveebfq_v1.2.83_downyi.com.apk
- 基于有机发光二极管(OLED)的建模优化算法的matlab仿真源码+数据+文档说明+项目说明(高分课程设计)
- hash01-test.c 本人哈希表(一)的示例代码,仅供参考!
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功