Revision 941 libsylph/socket.c

socket.c (revision 941)
1 1
/*
2 2
 * LibSylph -- E-Mail client library
3
 * Copyright (C) 1999-2005 Hiroyuki Yamamoto
3
 * Copyright (C) 1999-2006 Hiroyuki Yamamoto
4 4
 *
5 5
 * This library is free software; you can redistribute it and/or
6 6
 * modify it under the terms of the GNU Lesser General Public
......
56 56

  
57 57
#define BUFFSIZE	8192
58 58

  
59
#ifdef G_OS_WIN32
60
#define SockDesc		SOCKET
61
#define SOCKET_IS_VALID(s)	((s) != INVALID_SOCKET)
62
#else
63
#define SockDesc		gint
64
#define SOCKET_IS_VALID(s)	((s) >= 0)
65
#define INVALID_SOCKET		(-1)
66
#endif
67

  
59 68
typedef gint (*SockAddrFunc)	(GList		*addr_list,
60 69
				 gpointer	 data);
61 70

  
......
127 136
					 const gchar	*hostname,
128 137
					 gushort	 port);
129 138
#else
130
static gint sock_connect_by_getaddrinfo	(const gchar	*hostname,
131
					 gushort	 port);
139
static SockDesc sock_connect_by_getaddrinfo	(const gchar	*hostname,
140
						 gushort	 port);
132 141
#endif
133 142

  
134 143
#ifdef G_OS_UNIX
......
186 195

  
187 196
gint fd_connect_inet(gushort port)
188 197
{
189
#ifdef G_OS_WIN32
190
	SOCKET sock;
191
#else
192
	gint sock;
193
#endif
198
	SockDesc sock;
194 199
	struct sockaddr_in addr;
195 200

  
201
	sock = socket(AF_INET, SOCK_STREAM, 0);
202
	if (!SOCKET_IS_VALID(sock)) {
196 203
#ifdef G_OS_WIN32
197
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
198 204
		g_warning("fd_connect_inet(): socket() failed: %ld\n",
199 205
			  WSAGetLastError());
200 206
#else
201
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
202 207
		perror("fd_connect_inet(): socket");
203 208
#endif
204 209
		return -1;
......
219 224

  
220 225
gint fd_open_inet(gushort port)
221 226
{
222
#ifdef G_OS_WIN32
223
	SOCKET sock;
224
#else
225
	gint sock;
226
#endif
227
	SockDesc sock;
227 228
	struct sockaddr_in addr;
228 229
	gint val;
229 230

  
231
	sock = socket(AF_INET, SOCK_STREAM, 0);
232
	if (!SOCKET_IS_VALID(sock)) {
230 233
#ifdef G_OS_WIN32
231
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
232 234
		g_warning("fd_open_inet(): socket() failed: %ld\n",
233 235
			  WSAGetLastError());
234 236
#else
235
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
236 237
		perror("fd_open_inet(): socket");
237 238
#endif
238 239
		return -1;
......
649 650
}
650 651
#endif
651 652

  
652
static gint sock_connect_by_getaddrinfo(const gchar *hostname, gushort	port)
653
static SockDesc sock_connect_by_getaddrinfo(const gchar *hostname, gushort port)
653 654
{
654
#ifdef G_OS_WIN32
655
	SOCKET sock = INVALID_SOCKET;
656
#else
657
	gint sock = -1;
658
#endif
655
	SockDesc sock = INVALID_SOCKET;
659 656
	gint gai_error;
660 657
	struct addrinfo hints, *res, *ai;
661 658
	gchar port_str[6];
......
672 669
	if ((gai_error = getaddrinfo(hostname, port_str, &hints, &res)) != 0) {
673 670
		fprintf(stderr, "getaddrinfo for %s:%s failed: %s\n",
674 671
			hostname, port_str, gai_strerror(gai_error));
675
		return -1;
672
		return INVALID_SOCKET;
676 673
	}
677 674

  
678 675
	for (ai = res; ai != NULL; ai = ai->ai_next) {
679 676
		sock = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
680
#ifdef G_OS_WIN32
681
		if (sock == INVALID_SOCKET)
682
#else
683
		if (sock < 0)
684
#endif
677
		if (!SOCKET_IS_VALID(sock))
685 678
			continue;
686 679

  
687 680
		if (sock_connect_with_timeout
......
695 688
		freeaddrinfo(res);
696 689

  
697 690
	if (ai == NULL)
698
		return -1;
691
		return INVALID_SOCKET;
699 692

  
700 693
	return sock;
701 694
}
......
703 696

  
704 697
SockInfo *sock_connect(const gchar *hostname, gushort port)
705 698
{
706
#ifdef G_OS_WIN32
707
	SOCKET sock;
708
#else
709
	gint sock;
710
#endif
699
	SockDesc sock;
711 700
	SockInfo *sockinfo;
712 701

  
713 702
#ifdef INET6
714
#ifdef G_OS_WIN32
715
	if ((sock = sock_connect_by_getaddrinfo(hostname, port))
716
	    == INVALID_SOCKET)
717
#else
718
	if ((sock = sock_connect_by_getaddrinfo(hostname, port)) < 0)
719
#endif /* G_OS_WIN32 */
703
	sock = sock_connect_by_getaddrinfo(hostname, port);
704
	if (!SOCKET_IS_VALID(sock))
720 705
		return NULL;
721 706
#else
707
	sock = socket(AF_INET, SOCK_STREAM, 0);
708
	if (!SOCKET_IS_VALID(sock)) {
722 709
#ifdef G_OS_WIN32
723
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
724 710
		g_warning("socket() failed: %ld\n", WSAGetLastError());
725 711
#else
726
	if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
727 712
		perror("socket");
728 713
#endif /* G_OS_WIN32 */
729 714
		return NULL;

Also available in: Unified diff