Skip to content

Commit

Permalink
smb_transport_tcp: try port 139 if 445 fails
Browse files Browse the repository at this point in the history
  • Loading branch information
tguillem committed Jul 6, 2016
1 parent 1e7ea09 commit aa3a619
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
3 changes: 3 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
Changes between 0.2.5 and 0.2.6:
* Fix connection with a smb server listening on the port 139

Changes between 0.2.4 and 0.2.5:
--------------------------------

Expand Down
1 change: 1 addition & 0 deletions src/netbios_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#define NETBIOS_PORT_NAME 137 // UDP
#define NETBIOS_PORT_SESSION 139 // TCP
#define NETBIOS_PORT_DIRECT 445 // TCP
#define NETBIOS_PORT_DIRECT_SECONDARY 139 // TCP

#define NETBIOS_NAME_LENGTH 15

Expand Down
40 changes: 28 additions & 12 deletions src/netbios_session.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,34 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>

#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <errno.h>

#include "smb_defs.h"
#include "bdsm_debug.h"
#include "compat.h"
#include "netbios_session.h"
#include "netbios_utils.h"

static int open_socket_and_connect(netbios_session *s)
static int open_socket_and_connect(netbios_session *s)
{
if ((s->socket = socket(AF_INET, SOCK_STREAM, 0)) < 0)
goto error;
if (connect(s->socket, (struct sockaddr *)&s->remote_addr, sizeof(s->remote_addr)) <0)
goto error;

return 1;
return DSM_SUCCESS;

error:
BDSM_perror("netbios_session_new, open_socket: ");
return 0;
return DSM_ERROR_NETWORK;
}

static int session_buffer_realloc(netbios_session *s, size_t new_size)
Expand Down Expand Up @@ -115,23 +118,36 @@ void netbios_session_destroy(netbios_session *s)
free(s);
}

int netbios_session_connect(uint32_t ip,
netbios_session *s,
const char *name,
int direct_tcp)
int netbios_session_connect(uint32_t ip, netbios_session *s,
const char *name, int direct_tcp)
{
ssize_t recv_size;
char *encoded_name = NULL;
uint16_t ports[2];
unsigned int nb_ports;
bool opened = false;

assert(s != NULL && s->packet != NULL);

if (direct_tcp)
s->remote_addr.sin_port = htons(NETBIOS_PORT_DIRECT);
{
ports[0] = htons(NETBIOS_PORT_DIRECT);
ports[1] = htons(NETBIOS_PORT_DIRECT_SECONDARY);
nb_ports = 2;
}
else
s->remote_addr.sin_port = htons(NETBIOS_PORT_SESSION);
s->remote_addr.sin_family = AF_INET;
s->remote_addr.sin_addr.s_addr = ip;
if (!open_socket_and_connect(s))
{
ports[0] = htons(NETBIOS_PORT_SESSION);
nb_ports = 1;
}
for (unsigned int i = 0; i < nb_ports && !opened; ++i)
{
s->remote_addr.sin_port = ports[i];
s->remote_addr.sin_family = AF_INET;
s->remote_addr.sin_addr.s_addr = ip;
opened = open_socket_and_connect(s) == DSM_SUCCESS;
}
if (!opened)
goto error;

if (!direct_tcp)
Expand Down

0 comments on commit aa3a619

Please sign in to comment.