Ticket #252: main.c

File main.c, 1.2 KB (added by jhujhiti, 12 years ago)

configure test to set TR_NEED_V4SOCKET (see comment)

Line 
1#include <stdio.h>
2#include <netinet/in.h>
3#include <sys/types.h>
4#include <arpa/inet.h>
5#include <string.h>
6#include <sys/socket.h>
7#include <string.h>
8#include <unistd.h>
9
10#define PORT 51413
11
12/* returns:
13 * 0 if both sockets bound (need to set TR_NEED_V4SOCKET)
14 * 1 if ipv6 socket didn't bind (die and tell the user to ensure the port
15 *     isn't in use)
16 * 2 if only ipv6 socket bound (do not need to set TR_NEED_V4SOCKET)
17 */
18
19int main(int argc, const char** argv) {
20    struct sockaddr_in sock4;
21    struct sockaddr_in6 sock6;
22    int s;
23    int s2;
24    memset(&sock6, 0, sizeof(sock6));
25    sock6.sin6_family = AF_INET6;
26    sock6.sin6_port = htons(PORT);
27    memcpy(&sock6.sin6_addr, &in6addr_any, sizeof(struct in6_addr));
28    s = socket(AF_INET6, SOCK_STREAM, 0);
29    if(bind(s, (struct sockaddr*)&sock6, sizeof(sock6))) {
30        close(s);
31        return 1;
32    }
33    memset(&sock4, 0, sizeof(sock4));
34    sock4.sin_family = AF_INET;
35    sock4.sin_addr.s_addr = INADDR_ANY;
36    sock4.sin_port = htons(PORT);
37    s2 = socket(AF_INET, SOCK_STREAM, 0);
38    if(bind(s, (struct sockaddr*)&sock4, sizeof(sock4))) {
39        close(s);
40        close(s2);
41        return 2;
42    }
43    close(s);
44    close(s2);
45    return 0;
46}