fehQlibs 31
Qlibs
Loading...
Searching...
No Matches
socket_send.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 "byte.h"
6#include "ip.h"
7#include "socket_if.h"
8#include "error.h"
9
17#define WHO "socket_send"
18
19int socket_send4(int s,const char *buf,unsigned int len,const char ip[4],uint16 port)
20{
21 struct sockaddr_in sa;
22
23 byte_zero((char *)&sa,sizeof(sa));
24
25 sa.sin_family = AF_INET;
26 uint16_pack_big((char *)&sa.sin_port,port);
27 byte_copy((char *)&sa.sin_addr,4,ip);
28
29 return sendto(s,buf,len,0,(struct sockaddr *)&sa,sizeof(sa));
30}
31
32int socket_send6(int s,const char *buf,unsigned int len,const char ip[16],uint16 port,uint32 scope_id)
33{
34 struct sockaddr_in6 sa;
35
36 byte_zero((char *)&sa,sizeof(sa));
37
38 sa.sin6_family = AF_INET6;
39 sa.sin6_scope_id = scope_id;
40 uint16_pack_big((char *)&sa.sin6_port,port);
41 byte_copy((char *)&sa.sin6_addr,16,ip);
42
43 return sendto(s,buf,len,0,(struct sockaddr *)&sa,sizeof(sa));
44}
45
46int socket_send(int s,const char *buf,unsigned int len,const char ip[16],uint16 port,uint32 scope_id)
47{
48 struct sockaddr_storage addr;
49 socklen_t len_sas = sizeof(addr);
50
51 if (getsockname(s,(struct sockaddr *)&addr,&len_sas) == -1) {
52 errno = EPROTO;
53 return -1;
54 }
55
56 if (addr.ss_family == AF_INET)
57 return socket_send4(s,buf,len,ip + 12,port);
58 else
59 return socket_send6(s,buf,len,ip,port,scope_id);
60}
61
62int socket_broadcast4(int s,const char *buf,unsigned int len,uint16 port)
63{
64 struct sockaddr_in sa;
65
66 byte_zero((char *)&sa,sizeof(sa));
67
68 sa.sin_family = AF_INET;
69 uint16_pack_big((char *)&sa.sin_port,port);
70 byte_copy((char *)&sa.sin_addr,4,V4broadcast);
71
72 return sendto(s,buf,len,0,(struct sockaddr *)&sa,sizeof(sa));
73}
int socket_send6(int s, const char *buf, unsigned int len, const char ip[16], uint16 port, uint32 scope_id)
Definition: socket_send.c:32
int socket_send4(int s, const char *buf, unsigned int len, const char ip[4], uint16 port)
Definition: socket_send.c:19
int socket_broadcast4(int s, const char *buf, unsigned int len, uint16 port)
Definition: socket_send.c:62
int socket_send(int s, const char *buf, unsigned int len, const char ip[16], uint16 port, uint32 scope_id)
Definition: socket_send.c:46
uint16_t uint16
Definition: uint_t.h:62
void uint16_pack_big(char[2], uint16)
Definition: uint16p.c:16
uint32_t uint32
Definition: uint_t.h:73
void byte_copy(char *, unsigned int, const char *)
Definition: byte.c:20
void byte_zero(char *, unsigned int)
Definition: byte.c:65
#define EPROTO
Definition: error.h:7