fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
socket_connect.c
Go to the documentation of this file.
1#include <sys/types.h>
2#include <sys/param.h>
3#include <sys/socket.h>
4#include <netinet/in.h>
5#include <errno.h>
6#include "byte.h"
7#include "socket_if.h"
8#include "ip.h"
9
17int socket_connect6(int s,const char ip[16],uint16 port,uint32 scope_id)
18{
19 struct sockaddr_in6 sa;
20
21 byte_zero(&sa,sizeof(sa));
22 sa.sin6_family = AF_INET6;
23 uint16_pack_big((char *)&sa.sin6_port,port);
24 sa.sin6_flowinfo = 0;
25 sa.sin6_scope_id = scope_id;
26 byte_copy((char *)&sa.sin6_addr,16,ip);
27
28 return connect(s,(struct sockaddr *)&sa,sizeof(sa));
29}
30
31/* this explizit declaration is needed to prevent compiler warnings */
32int read(int _str, void *_buf, int _b);
33
34int socket_connect4(int s,const char ip[4],uint16 port)
35{
36 struct sockaddr_in sa;
37
38 byte_zero(&sa,sizeof(sa));
39 sa.sin_family = AF_INET;
40 uint16_pack_big((char *)&sa.sin_port,port);
41 byte_copy((char *)&sa.sin_addr,4,ip);
42
43 return connect(s,(struct sockaddr *)&sa,sizeof(sa));
44}
45
46int socket_connect(int s,const char ip[16],uint16 port,uint32 scope_id)
47{
48 if (ip6_isv4mapped(ip))
49 return socket_connect4(s,ip + 12,port);
50
51 return socket_connect6(s,ip,port,scope_id);
52}
53
55{
56 struct sockaddr_in6 sa;
57 int dummy;
58 char ch;
59
60 dummy = sizeof(sa);
61 if (getpeername(s,(struct sockaddr *)&sa,(socklen_t *)&dummy) == -1) {
62 read(s,&ch,1); /* sets errno */
63 return 0;
64 }
65 return 1;
66}
int socket_connect6(int s, const char ip[16], uint16 port, uint32 scope_id)
int socket_connect(int s, const char ip[16], uint16 port, uint32 scope_id)
int socket_connect4(int s, const char ip[4], uint16 port)
int read(int _str, void *_buf, int _b)
int socket_connected(int s)
void uint16_pack_big(char[16], uint16)
uint16_t uint16
Definition: uint_t.h:29
uint32_t uint32
Definition: uint_t.h:40
#define ip6_isv4mapped(ip)
Definition: ip.h:105
void byte_copy(void *, unsigned int, const void *)
Definition: byte.c:20
void byte_zero(void *, unsigned int)
Definition: byte.c:65