Subversion Repositories planix.SVN

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
68 7u83 1
/*
2
 
3
Copyright (c) 2019, The PLANIX Project
4
All rights reserved.
5
 
6
Redistribution and use in source and binary forms, with or without
7
modification, are permitted provided that the following conditions are met:
8
 
9
1. Redistributions of source code must retain the above copyright notice, this
10
   list of conditions and the following disclaimer.
11
2. Redistributions in binary form must reproduce the above copyright notice,
12
   this list of conditions and the following disclaimer in the documentation
13
   and/or other materials provided with the distribution.
14
 
15
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 
26
*/
27
 
28
 
29
#include <stdlib.h>
30
#include <stdio.h>
31
#include <string.h>
32
 
33
#include <netinet/in.h>
34
#include <arpa/inet.h>
35
#include <errno.h>
36
 
37
#include <stdint.h>
38
 
39
 
40
#include "c9.h"
41
#include "sock.h"
42
 
43
/**
44
 * Convert a string to a sockaddr struct.
45
 * The string can contain an ipv4 or ipv6 address, including a port number.
46
 * @param s address string, the address string MUST not be longer 
47
 * than 128 characters
48
 * @param saout output buffer
49
 * @return 1 on success,  otherwise no success, consult errno!
50
 */
51
int sock_strtoaddr(const char * s,struct sockaddr * saout){
52
 
53
	char ips[128],*ps;
54
	struct in_addr ia;
55
	int port;
56
 
57
#ifndef WITHOUT_IPV6
58
	struct in6_addr ia6;
59
#endif
60
	int rc;
61
 
62
	/* copy the string */
63
	strcpy(ips,s);
64
 
65
	/* search for a collon to separate the port */
66
	ps = strchr(ips,':');
67
	if (ps != NULL){
68
		*ps='\0';
69
		ps++;
70
	}
71
	else
72
		ps="0";
73
 
74
	/* try to parse ip4 address */
75
	rc = inet_pton(AF_INET,ips,&ia);
76
 
77
 
78
	if (rc==1)
79
	{
80
		port = atoi(ps);
81
		if (port > 65535)
82
			rc=0;
83
	}
84
 
85
 
86
 
87
	if (rc==1){
88
		/* it's an ipv4 address */
89
		struct sockaddr_in * sa;
90
		sa = (struct sockaddr_in*)saout;
91
		memset(sa,0,sizeof(struct sockaddr_in));
92
#ifdef HAVE_SIN_LEN
93
		sa->sin_len=sizeof(struct sockaddr_in);
94
#endif
95
		sa->sin_family = AF_INET;
96
		sa->sin_addr=ia;
97
		sa->sin_port=htons(port);
98
	}
99
 
100
#ifndef WITHOUT_IPV6
101
	if (rc==0){
102
		strcpy(ips,s);
103
 
104
		ps = strchr(ips,'.');
105
		if (ps != NULL){
106
			*ps='\0';
107
			ps++;
108
		}
109
		else
110
			ps="0";
111
 
112
 
113
		rc = inet_pton(AF_INET6,s,&ia6);
114
 
115
 
116
		if (rc==1){
117
			/* it's an ipv6 address */
118
			struct sockaddr_in6 * sa;
119
			sa = (struct sockaddr_in6*)saout;
120
			memset(sa,0,sizeof(struct sockaddr_in6));
121
 
122
#ifdef HAVE_SIN_LEN
123
			sa->sin6_len=sizeof(struct sockaddr_in6);
124
#endif
125
			sa->sin6_family = AF_INET6;
126
			sa->sin6_addr=ia6;
127
			sa->sin6_port=htons(atoi(ps));
128
 
129
 
130
		}
131
 
132
	}
133
#endif
134
 
135
	if (rc!=1){
136
		if (rc!=-1)
137
			errno=EINVAL;
138
	}
139
 
140
	return rc;
141
 
142
}
143
 
144
struct sockdata {
145
	int fd;
146
};
147
 
148
 
149
int 
150
end_fun(C9ctx *ctx)
151
{
152
	printf("The end sender is called\n");	
153
//	n = send (fd, pkt, l,0);
154
	return 0;	
155
}
156
 
157
 
158
uint8_t *
159
begin_fun( C9ctx *ctx, uint32_t size ){
160
	printf ("The begin fun\n");
161
	return malloc(size);
162
}
163
 
164
C9ctx * 
165
sock_connect(const char *addrstr)
166
{
167
 
168
	C9ctx * ctx = NULL;
169
	struct sockdata  * priv = NULL;
170
	struct sockaddr_storage sa;
171
	int rc;
172
 
173
	ctx = malloc (sizeof (C9ctx));
174
	if (ctx == NULL)
175
		goto errX;
176
 
177
	priv = malloc (sizeof(struct sockdata));
178
	if (priv==NULL)
179
		goto errX;
180
 
181
	if ( sock_strtoaddr(addrstr,(struct sockaddr*)&sa) != 1){
182
		goto errX;
183
	};
184
 
185
	priv->fd = socket(sa.ss_family, SOCK_STREAM,0);
186
	if (priv->fd == -1){
187
		goto errX;
188
	}
189
 
190
	printf("Socket is %d\n",priv->fd);
191
 
192
	rc = connect (priv->fd, (struct sockaddr*)&sa, sizeof(struct sockaddr));
193
 
194
	printf ("Crc = %d\n",rc);
195
 
196
	if (rc==-1){
197
		goto errX;
198
	}
199
 
200
	ctx->aux = priv;
201
	ctx->end = end_fun;
202
	ctx->begin = begin_fun;
203
	return ctx;	
204
errX:
205
	free(priv);
206
	free(ctx);
207
	return NULL;
208
}
209