2 * Copyright 2001, 2002, 2003 David Mansfield and Cobite, Inc.
3 * See COPYING file for license information
18 #else /* not windows */
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
27 #include <netinet/tcp.h>
30 #endif /* if windows */
32 #include "tcpsocket.h"
40 RCSID("$Id: tcpsocket.c,v 1.6 1999/12/27 20:35:34 david Exp $");
43 tcp_create_socket(int reuse_addr
)
48 if ((retval
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0)
50 debug(DEBUG_TCP
, "tcp: can't create socket");
56 setsockopt( retval
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&yes
, sizeof(int));
59 debug(DEBUG_TCP
, "tcp: socket created");
61 return get_fd(retval
, WIN32_SOCKET
);
68 tcp_bind_and_listen(int sockfd
, unsigned short tcp_port
)
70 struct sockaddr_in addr
;
72 memset((char *) &addr
, 0, sizeof(struct sockaddr_in
));
73 addr
.sin_family
= AF_INET
;
74 addr
.sin_addr
.s_addr
= htonl(INADDR_ANY
);
75 addr
.sin_port
= htons(tcp_port
);
78 sockfd
= win32_file_table
[sockfd
].win32id
;
81 if (bind(sockfd
, (struct sockaddr
*)&addr
, sizeof(addr
)) < 0)
83 debug(DEBUG_ERROR
, "tcp: can't bind to socket");
88 if (listen(sockfd
, LISTEN_QUEUE_SIZE
) < 0)
90 debug(DEBUG_ERROR
, "tcp: can't listen on socket");
94 debug(DEBUG_TCP
, "tcp: socket bound and listening");
100 tcp_accept_connection(int sockfd
)
102 struct sockaddr_in remaddr
;
107 sockfd
= win32_file_table
[sockfd
].win32id
;
110 addrlen
= sizeof(struct sockaddr_in
);
113 if ((retval
= accept(sockfd
, (struct sockaddr
*) &remaddr
, &addrlen
)) == INVALID_SOCKET
)
115 debug(DEBUG_APPERROR
, "tcp: error accepting connection");
119 if ((retval
= accept(sockfd
, (struct sockaddr
*) &remaddr
, &addrlen
)) < 0)
122 debug(DEBUG_ERROR
, "tcp: error accepting connection");
128 debug(DEBUG_TCP
, "tcp: got connection (fd=%d)", retval
);
134 tcp_get_client_ip(int fd
)
136 struct sockaddr_in remaddr
;
142 fd
= win32_file_table
[fd
].win32id
;
145 addrlen
= sizeof(struct sockaddr_in
);
147 if ((retval
= getpeername(fd
, (struct sockaddr
*) &remaddr
, &addrlen
)) < 0)
149 debug(DEBUG_ERROR
, "tcp: error getting remote's ip address");
153 saddr
= ntohl(remaddr
.sin_addr
.s_addr
);
159 tcp_connect(int sockfd
, const char *rem_addr
, unsigned short port
)
161 struct sockaddr_in addr
;
166 sockfd
= win32_file_table
[sockfd
].win32id
;
169 if ( convert_address(&ipno
, rem_addr
) < 0 )
174 addrlen
= sizeof(struct sockaddr_in
);
176 memset((char *) &addr
, 0, sizeof(struct sockaddr_in
));
177 addr
.sin_family
= AF_INET
;
178 addr
.sin_addr
.s_addr
= ipno
;
179 addr
.sin_port
= htons(port
);
181 if (connect(sockfd
, (struct sockaddr
*)&addr
, addrlen
) < 0)
183 debug(DEBUG_ERROR
, "connect error");
187 debug(DEBUG_STATUS
, "tcp: connection established on port %d", port
);
192 convert_address(long *dest
, const char *addr_str
)
200 /* first try converting "numbers and dots" notation */
202 if ( inet_aton(addr_str
, &ip
) )
204 memcpy(dest
, &ip
.s_addr
, sizeof(ip
.s_addr
));
207 if ( (*dest
= inet_addr(addr_str
)) != -1)
212 else /* if it fails, do a gethostbyname() */
214 struct hostent
*host
;
215 if ((host
= gethostbyname(addr_str
)) == NULL
)
220 strcpy(errstr
, "HOST_NOT_FOUND");
224 strcpy(errstr
, "NO_ADDRESS");
228 strcpy(errstr
, "NO_RECOVERY");
232 strcpy(errstr
, "TRY_AGAIN");
236 debug(DEBUG_ERROR
, "gethostbyname failed for %s: ", addr_str
, errstr
);
241 memcpy(dest
, host
->h_addr_list
[0], sizeof(unsigned long));
248 int tcp_get_local_address(int sockfd
, unsigned int *ip
, unsigned short *port
)
250 struct sockaddr_in addr
;
251 int addrlen
= sizeof(struct sockaddr_in
);
253 if(getsockname(sockfd
, (struct sockaddr
*)&addr
, &addrlen
) < 0)
255 debug(DEBUG_SYSERROR
, "getsockname failed" );
259 *ip
= ntohl( addr
.sin_addr
.s_addr
);
260 *port
= ntohs( addr
.sin_port
);