File: socket.c

package info (click to toggle)
centerim 4.22.10-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,520 kB
  • sloc: cpp: 50,525; ansic: 44,676; sh: 12,243; makefile: 723; perl: 327; yacc: 288; python: 93; sed: 16; awk: 14
file content (237 lines) | stat: -rw-r--r-- 5,416 bytes parent folder | download | duplicates (6)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
 *  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; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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.
 *
 *  Jabber
 *  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
 */

#include "libxode.h"
#include "connwrap.h"

/* socket.c
 *
 * Simple wrapper to make socket creation easy.
 * type = NETSOCKET_SERVER is local listening socket
 * type = NETSOCKET_CLIENT is connection socket
 * type = NETSOCKET_UDP
 */

int make_netsocket(u_short port, char *host, int type, int ssl)
{
    int s, flag = 1;
    struct sockaddr_in sa;
    struct in_addr *saddr;
    int socket_type;

    /* is this a UDP socket or a TCP socket? */
    socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM;

    bzero((void *)&sa,sizeof(struct sockaddr_in));

    if((s = socket(AF_INET,socket_type,0)) < 0)
	return(-1);
    if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0)
	return(-1);

    saddr = make_addr(host);
    if(saddr == NULL)
	return(-1);
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);

    if(type == NETSOCKET_SERVER)
    {
	/* bind to specific address if specified */
	if(host != NULL)
	    sa.sin_addr.s_addr = saddr->s_addr;

	if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
	{
	    close(s);
	    return(-1);
	}
    }
    if(type == NETSOCKET_CLIENT)
    {
	sa.sin_addr.s_addr = saddr->s_addr;
	if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0)
	{
	    close(s);
	    return(-1);
	}
    }
    if(type == NETSOCKET_UDP)
    {
	/* bind to all addresses for now */
	if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
	{
	    close(s);
	    return(-1);
	}

	/* specify default recipient for read/write */
	sa.sin_addr.s_addr = saddr->s_addr;
	if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0)
	{
	    close(s);
	    return(-1);
	}
    }


    return(s);
}

void change_socket_to_blocking(int s)
     /* make socket blocking */
{
    int val;
    val = fcntl(s, F_GETFL, 0);
    fcntl(s, F_SETFL, val & (~O_NONBLOCK));
}

void change_socket_to_nonblocking(int s)
     /* make socket non-blocking */
{
    int val;
    val = fcntl(s, F_GETFL, 0);
    fcntl(s, F_SETFL, val | O_NONBLOCK);
}

/* socket.c
 *
 * Simple wrapper to make non-blocking client socket creation easy.
 * type = NETSOCKET_SERVER is local listening socket
 * type = NETSOCKET_CLIENT is connection socket
 * type = NETSOCKET_UDP
 */

int make_nb_netsocket(u_short port, char *host, int type, int ssl, int* state)
{
    int s, flag = 1;
    struct sockaddr_in sa;
    struct in_addr *saddr;
    int socket_type;

    /* is this a UDP socket or a TCP socket? */
    socket_type = (type == NETSOCKET_UDP)?SOCK_DGRAM:SOCK_STREAM;

    bzero((void *)&sa,sizeof(struct sockaddr_in));

    if((s = socket(AF_INET,socket_type,0)) < 0)
	return(-1);
    if(setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&flag, sizeof(flag)) < 0)
	return(-1);
    change_socket_to_nonblocking(s);

    saddr = make_addr(host);
    if(saddr == NULL)
	return(-1);
    sa.sin_family = AF_INET;
    sa.sin_port = htons(port);

    if(type == NETSOCKET_SERVER)
    {
	/* bind to specific address if specified */
	if(host != NULL)
	    sa.sin_addr.s_addr = saddr->s_addr;

	if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
	{
	    close(s);
	    return(-1);
	}
    }
    if(type == NETSOCKET_CLIENT)
    {
	int rc;
	sa.sin_addr.s_addr = saddr->s_addr;
	rc = cw_nb_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl, state);
	if (rc == -1 )
	{
	    close(s);
	    return(-1);
	}
    }
    if(type == NETSOCKET_UDP)
    {
	/* bind to all addresses for now */
	if(bind(s,(struct sockaddr*)&sa,sizeof sa) < 0)
	{
	    close(s);
	    return(-1);
	}

	/* specify default recipient for read/write */
	sa.sin_addr.s_addr = saddr->s_addr;
	if(cw_connect(s,(struct sockaddr*)&sa,sizeof sa,ssl) < 0)
	{
	    close(s);
	    return(-1);
	}
    }


    return(s);
}

struct in_addr *make_addr(char *host)
{
    struct hostent *hp;
    static struct in_addr addr;
    char myname[MAXHOSTNAMELEN + 1];

    if(host == NULL || strlen(host) == 0)
    {
	gethostname(myname,MAXHOSTNAMELEN);
	hp = gethostbyname(myname);
	if(hp != NULL)
	{
	    return (struct in_addr *) *hp->h_addr_list;
	}
    }else{
	addr.s_addr = inet_addr(host);
	if(addr.s_addr != -1)
	{
	    return &addr;
	}
	hp = gethostbyname(host);
	if(hp != NULL)
	{
	    return (struct in_addr *) *hp->h_addr_list;
	}
    }
    return NULL;
}

/* Sets a file descriptor to close on exec.  "flag" is 1 to close on exec, 0 to
 * leave open across exec.
 * -- EJB 7/31/2000
 */
int set_fd_close_on_exec(int fd, int flag)
{
    int oldflags = fcntl(fd,F_GETFL);
    int newflags;

    if(flag)
	newflags = oldflags | FD_CLOEXEC;
    else
	newflags = oldflags & (~FD_CLOEXEC);

    if(newflags==oldflags)
	return 0;
    return fcntl(fd,F_SETFL,(long)newflags);
}