File: ip.c

package info (click to toggle)
mysecureshell 2.00%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 888 kB
  • sloc: ansic: 7,421; sh: 700; perl: 264; makefile: 116
file content (124 lines) | stat: -rw-r--r-- 2,654 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 MySecureShell permit to add restriction to modified sftp-server
 when using MySecureShell as shell.
 Copyright (C) 2007-2014 MySecureShell Team

 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 (version 2)

 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 "../config.h"
#ifdef HAVE_CYGWIN_SOCKET_H
#include <cygwin/socket.h>
#endif
#include <sys/types.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include "hash.h"
#include "ip.h"

/*@null@*/ char *get_ip(int resolv)
{
	struct hostent *h;
	in_addr_t addr;
	char *env, *ip = NULL;

	if ((env = getenv("SSH_CONNECTION")) != NULL)
	{
		char *ptr;

		env = strdup(env);
		if ((ptr = strchr(env, ' ')))
			*ptr = '\0';
		if (resolv == 0)
			ip = strdup(env);
		else if ((int) (addr = inet_addr(env)) != -1)
			if ((h = gethostbyaddr((char *) &addr, sizeof(addr), AF_INET)) != NULL)//FIXME inet_ntop ???
				if (h != NULL && h->h_name != NULL && strlen(h->h_name) > 0)//check if a name is defined
					ip = strdup(h->h_name);
		free(env);
		if (ip == NULL)
			ip = strdup("");
	}
	else
		ip = strdup("");
	return (ip);
}

/*@null@*/ char *get_ip_server()
{
	char *env;
	char *ip, *ptr;

	if ((env = getenv("SSH_CONNECTION")) != NULL)
	{
		env = strdup(env);
		ip = env;
		if ((ptr = strrchr(env, ' ')) != NULL)
			*ptr = '\0';
		if ((ptr = strrchr(env, ' ')) != NULL)
			ip = ptr + 1;
		ip = strdup(ip);
		free(env);
	}
	else
		ip = strdup("");
	return (ip);
}

int get_port_client()
{
	char *ip, *ptr;
	int port = -1;

	if ((ip = getenv("SSH_CONNECTION")) != NULL)
	{
		if ((ptr = strchr(ip, ' ')) != NULL)
		{
			char	*portClient;

			ip = ptr + 1;
			portClient = strdup(ip);
			if ((ptr = strchr(portClient, ' ')) != NULL)
			{
				*ptr = '\0';
				port = atoi(portClient);
			}
			free(portClient);
		}
	}
	return (port);
}

int get_port_server()
{
	char *ip, *ptr;
	int port = -1;

	if ((ip = getenv("SSH_CONNECTION")) != NULL)
	{
		if ((ptr = strrchr(ip, ' ')) != NULL)
		{
			ip = ptr + 1;
			port = atoi(ip);
		}
	}
	return (port);
}