summaryrefslogtreecommitdiffstats
path: root/server/net.c
blob: 02db9aa9f75931adc463673b04367c9bfceba972 (plain) (blame)
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
/*
 * net.c - network stuff for the server
 * Copyright (C) 2006 Thorsten Zitterell <thorsten@zitterell.de>
 */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

#define DNBD_USERSPACE		1
#include "../common/dnbd-cliserv.h"

#include "net.h"

struct listener_s {
	pthread_t tid;
	net_request_t *request;
};

typedef struct listener_s listener_t;
listener_t listener;

/* 
 * function net_tx(): send a server reply 
 */
void net_tx(net_info_t * net_info, net_reply_t * reply)
{
	if (sendto
	    (net_info->sock, reply->data, reply->len, 0,
	     (struct sockaddr *) &net_info->groupnet,
	     sizeof(net_info->groupnet)) < 0)
		fprintf(stderr, "net_tx: mcast sendproblem\n");

}

/* 
 * function net_rx(): receive a client request 
 * returns: 1 on correct size of reply, otherwise 0
 */
int net_rx(net_info_t * net_info, net_request_t * request)
{
	ssize_t n;

	request->clientlen = sizeof(request->client);

	n = recvfrom(net_info->sock, &request->data,
		     sizeof(request->data), 0,
		     &request->client, &request->clientlen);

	/* sizeof of request must be size of a DNBD request */
	return (n == sizeof(request->data) ? 1 : 0);
}

/* 
 * function net_init(): initialize network for multicast 
 * returns: structure with network related information
 */
net_info_t *net_init(const char *mnet)
{
	struct ip_mreq mreq;
	const int ttl = 64;	/* TTL of 64 should be enough */
	u_char loop = 0;

	net_info_t *net_info = NULL;

	net_info = (net_info_t *) malloc(sizeof(net_info_t));
	if (!net_info)
		return NULL;

	memset(net_info, 0, sizeof(net_info_t));

	/* network setup */
	net_info->server.sin_family = AF_INET;
	net_info->server.sin_port = htons(DNBD_PORT);
	net_info->sock = socket(PF_INET, SOCK_DGRAM, 0);

	if (!inet_aton(mnet, &net_info->server.sin_addr)) {
		fprintf(stderr,
			"ERROR: multicast group %s is not a valid address!\n",
			mnet);
		goto out_free;
	}

	if (bind
	    (net_info->sock, (struct sockaddr *) &net_info->server,
	     sizeof(net_info->server)) < 0) {
		fprintf(stderr, "ERROR: binding socket!\n");
		goto out_free;
	}

	if (!inet_aton(mnet, &net_info->groupnet.sin_addr)) {
		fprintf(stderr,
			"ERROR: multicast group %s is not a valid address!\n",
			mnet);
		goto out_free;
	}

	/* multicast setup */
	net_info->groupnet.sin_family = AF_INET;
	net_info->groupnet.sin_port = htons(DNBD_PORT);

	mreq.imr_interface.s_addr = htonl(INADDR_ANY);
	memcpy(&mreq.imr_multiaddr, &net_info->groupnet.sin_addr,
	       sizeof(struct in_addr));

	if (setsockopt
	    (net_info->sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
	     sizeof(mreq)) < 0) {
		fprintf(stderr,
			"ERROR: cannot add multicast membership!\n");
		goto out_free;
	}

	if (setsockopt(net_info->sock, IPPROTO_IP, IP_MULTICAST_TTL,
		       &ttl, sizeof(ttl)) < 0) {
		fprintf(stderr, "ERROR: Setting TTL to 2\n");
		goto out_free;
	}

	/* no looping, please */
	if (setsockopt
	    (net_info->sock, IPPROTO_IP, IP_MULTICAST_LOOP, &loop,
	     sizeof(loop)) < 0) {
		fprintf(stderr,
			"ERROR: cannot disable multicast looping!\n");
		goto out_free;
	}

	goto out;

      out_free:
	fprintf(stderr,
		"hint: check kernel multicast support, multicast routing\n");
	if (net_info)
		free(net_info);

	net_info = NULL;
      out:
	return net_info;
}