File: socket-connect.c

package info (click to toggle)
viewglob 2.0.4-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,740 kB
  • ctags: 968
  • sloc: ansic: 13,940; sh: 897; makefile: 315
file content (169 lines) | stat: -rw-r--r-- 4,285 bytes parent folder | download | duplicates (2)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (c) 1993 W. Richard Stevens.  All rights reserved.
 * Permission to use or modify this software and its documentation only for
 * educational purposes and without fee is hereby granted, provided that
 * the above copyright notice appear in all copies.  The author makes no
 * representations about the suitability of this software for any purpose.
 * It is provided "as is" without express or implied warranty.
 */

/*
	Copyright (C) 2004, 2005 Stephen Bach
	This file is part of the Viewglob package.

	Viewglob 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; either version 2 of the License, or
	(at your option) any later version.

	Viewglob 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 Viewglob; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "common.h"
#include "socket-connect.h"

#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <arpa/inet.h>
#include <netdb.h>

#include <string.h>

gint unix_connect(const char* port) {
	gint sockfd;
	struct sockaddr_un servaddr;
	gchar* home;
	gchar* name;

	if ((home = getenv("HOME")) == NULL) {
		g_critical("User does not have a home!");
		return -1;
	}

	name = g_strconcat(home, "/.viewglob/.", port, NULL);

	if (strlen(name) + 1 > sizeof(servaddr.sun_path)) {
		g_critical("Socket name too long");
		return -1;
	}

	sockfd = socket(AF_LOCAL, SOCK_STREAM, 0);
	if (sockfd < 0) {
		g_critical("Could not create socket: %s", g_strerror(errno));
		return -1;
	}

	memset(&servaddr, 0, sizeof(servaddr));
	servaddr.sun_family = AF_LOCAL;
	strcpy(servaddr.sun_path, name);
	g_free(name);

	if (connect(sockfd, (struct sockaddr*) &servaddr,
				sizeof(servaddr)) == -1) {
		g_critical("Connection error to %s: %s", servaddr.sun_path,
				g_strerror(errno));
		return -1;
	}

	return sockfd;
}


gint tcp_connect(const char *host, const char *serv) {
	gint sockfd;

#ifdef HAVE_GETADDRINFO
	gint				n;
	struct addrinfo	hints, *res, *ressave;

	(void) memset(&hints, 0, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;

	if ( (n = getaddrinfo(host, serv, &hints, &res)) != 0) {
		g_critical("tcp_connect error for %s, %s: %s",
				 host, serv, gai_strerror(n));
		return -1;
	}
	ressave = res;

	do {
		sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
		if (sockfd < 0)
			continue;	/* ignore this one */

		if (connect(sockfd, res->ai_addr, res->ai_addrlen) == 0)
			break;		/* success */

		(void) close(sockfd);	/* ignore this one */
	} while ( (res = res->ai_next) != NULL);

	if (res == NULL) {	/* errno set from final connect() */
		g_critical("tcp_connect error for %s, %s: %s", host, serv,
				g_strerror(errno));
		return -1;
	}

	freeaddrinfo(ressave);

#else
	struct sockaddr_in servaddr;
	struct in_addr** pptr;
	struct in_addr* inetaddrp[2];
	struct in_addr inetaddr;
	struct hostent* hp;

	int port = atoi(serv);

	if ((hp = gethostbyname(host)) == NULL) {
		if (inet_aton(host, &inetaddr) == 0) {
			g_critical("Could not convert hostname: %s",
					g_strerror(errno));
			return -1;
		}
		else {
			inetaddrp[0] = &inetaddr;
			inetaddrp[1] = NULL;
			pptr = inetaddrp;
		}
	}
	else
		pptr = (struct in_addr**) hp->h_addr_list;

	for ( ; *pptr != NULL; pptr++) {
		sockfd = socket(AF_INET, SOCK_STREAM, 0);
		if (sockfd < 0) {
			g_critical("Could not create socket: %s", g_strerror(errno));
			return -1;
		}

		memset(&servaddr, 0, sizeof(servaddr));
		servaddr.sin_family = AF_INET;
		servaddr.sin_port = htons(port);
		memcpy(&servaddr.sin_addr, *pptr, sizeof(struct in_addr));

		if (connect(sockfd, (struct sockaddr*) &servaddr,
					sizeof(servaddr)) == 0)
			break;   /* success */

		g_warning("Connection error: %s", g_strerror(errno));
		(void) close(sockfd);
	}
	
	if (*pptr == NULL) {
		g_critical("Unable to connect.");
		return -1;
	}
#endif

	return sockfd;
}