File: udp.c

package info (click to toggle)
gnutls28 3.8.12-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 82,380 kB
  • sloc: ansic: 392,233; asm: 117,804; sh: 18,758; makefile: 6,804; yacc: 1,858; python: 1,399; cpp: 1,243; perl: 995; sed: 39
file content (66 lines) | stat: -rw-r--r-- 1,321 bytes parent folder | download | duplicates (5)
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
/* This example code is placed in the public domain. */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <unistd.h>

/* udp.c */
int udp_connect(void);
void udp_close(int sd);

/* Connects to the peer and returns a socket
 * descriptor.
 */
extern int udp_connect(void)
{
	const char *PORT = "5557";
	const char *SERVER = "127.0.0.1";
	int err, sd;
#if defined(IP_DONTFRAG) || defined(IP_MTU_DISCOVER)
	int optval;
#endif
	struct sockaddr_in sa;

	/* connects to server
	 */
	sd = socket(AF_INET, SOCK_DGRAM, 0);

	memset(&sa, '\0', sizeof(sa));
	sa.sin_family = AF_INET;
	sa.sin_port = htons(atoi(PORT));
	inet_pton(AF_INET, SERVER, &sa.sin_addr);

#if defined(IP_DONTFRAG)
	optval = 1;
	setsockopt(sd, IPPROTO_IP, IP_DONTFRAG, (const void *)&optval,
		   sizeof(optval));
#elif defined(IP_MTU_DISCOVER)
	optval = IP_PMTUDISC_DO;
	setsockopt(sd, IPPROTO_IP, IP_MTU_DISCOVER, (const void *)&optval,
		   sizeof(optval));
#endif

	err = connect(sd, (struct sockaddr *)&sa, sizeof(sa));
	if (err < 0) {
		fprintf(stderr, "Connect error\n");
		exit(1);
	}

	return sd;
}

/* closes the given socket descriptor.
 */
extern void udp_close(int sd)
{
	close(sd);
}