aboutsummaryrefslogtreecommitdiff
path: root/libssh/socket.c
blob: 2d82ec666a19c01cda5ce48feaeb15c7e4f375cc (plain)
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
170
171
172
173
174
175
/* socket.c */
/* the Socket class */
/*
 * Copyright 2008 Aris Adamantiadis
 *
 * This file is part of the SSH Library
 *
 * The SSH Library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * The SSH Library 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 Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SSH Library; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA. */

#include <unistd.h>
#include <errno.h>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#endif
#include "libssh/priv.h"

struct socket {
	socket_t fd;
	int last_errno;
};

/*
 * \internal
 * \brief inits the socket system (windows specific)
 */
void ssh_socket_init(){
#ifdef _WIN32
    struct WSAData wsaData;
    if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
        ssh_say(1,"Error initialising Windows sockets.\n");
    }
#endif
}
/*
 * \internal
 * \brief creates a new Socket object
 */
struct socket *ssh_socket_new(){
	struct socket *s=malloc(sizeof(struct socket));
	s->fd=-1;
	s->last_errno=-1;
	return s;
}

/* \internal
 * \brief Deletes a socket object
 */
void ssh_socket_free(struct socket *s){
    ssh_socket_close(s);
	free(s);
}

/* \internal
 * \brief closes a socket
 */
void ssh_socket_close(struct socket *s){
	if(ssh_socket_is_open(s)){
#ifdef _WIN32
		closesocket(s->fd);
		s->last_errno=WSAGetLastError();
#else
		close(s->fd);
		s->last_errno=errno;
#endif		
		s->fd=-1;
	}
}

/* \internal
 * \brief sets the file descriptor of the socket
 */
void ssh_socket_set_fd(struct socket *s, socket_t fd){
	s->fd=fd;
}

/* \internal
 * \brief returns the file descriptor of the socket
 */
socket_t ssh_socket_get_fd(struct socket *s){
	return s->fd;
}

/* \internal
 * \brief returns nonzero if the socket is open
 */
int ssh_socket_is_open(struct socket *s){
	return s->fd != -1;
}

/* \internal
 * \brief read len bytes from socket into buffer
 */
int ssh_socket_read(struct socket *s, void *buffer, int len){
	int r=recv(s->fd,buffer,len,0);
#ifndef _WIN32	
    s->last_errno=errno;
#else
    s->last_errno=WSAGetLastError();
#endif	
	return r;
}

/* \internal
 * \brief writes len bytes from byffer to socket
 */
int ssh_socket_write(struct socket *s,const void *buffer, int len){
	int w=send(s->fd,buffer,len,0);
#ifndef _WIN32
    s->last_errno=errno;
#else
    s->last_errno=WSAGetLastError();
#endif

	return w;
}

/* \internal
 * \brief returns nonzero if the current socket is in the fd_set
 */
int ssh_socket_fd_isset(struct socket *s, fd_set *set){
	if(s->fd==-1)
		return 0;
	return FD_ISSET(s->fd,set);
}

/* \internal
 * \brief sets the current fd in a fd_set and updates the fd_max
 */

void ssh_socket_fd_set(struct socket *s, fd_set *set, int *fd_max){
	if(s->fd==-1)
		return;
	FD_SET(s->fd,set);
	if(s->fd>= *fd_max){
		*fd_max=s->fd+1;
	}
}

/* \internal
 * \brief reads blocking until len bytes have been read
 */
int ssh_socket_completeread(struct socket *s, void *buffer, int len){
    int r;
    int total=0;
    int toread=len;
    if(!ssh_socket_is_open(s))
        return SSH_ERROR;
    while((r=ssh_socket_read(s,buffer+total,toread))){
        if(r==-1)
            return SSH_ERROR;
        total += r;
        toread-=r;
        if(total==len)
            return len;
        if(r==0)
            return 0;
    }
    return total ; /* connection closed */
}