~ubuntu-branches/ubuntu/wily/apparmor/wily

« back to all changes in this revision

Viewing changes to tests/regression/subdomain/netdomain/test_multi_send.c

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2011-04-27 10:38:07 UTC
  • mfrom: (5.1.118 natty)
  • Revision ID: james.westby@ubuntu.com-20110427103807-ym3rhwys6o84ith0
Tags: 2.6.1-2
debian/copyright: clarify for some full organization names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Multiple iteration sending test. */
2
 
 
3
 
#include <stdio.h>
4
 
#include <stdlib.h>
5
 
#include <netinet/in.h>
6
 
#include <sys/socket.h>
7
 
#include <unistd.h>
8
 
#include <arpa/inet.h>
9
 
#include <errno.h>
10
 
#include <string.h>
11
 
 
12
 
int send_udp(char *bind_ip, char *bind_port, char *remote_ip, char *remote_port, char *message);
13
 
int send_tcp(char *bind_ip, char *bind_port, char *remote_ip, char *remote_port, char *message);
14
 
 
15
 
int main(int argc, char *argv[])
16
 
{
17
 
        int send_ret;
18
 
 
19
 
        if (argc < 7)
20
 
        {
21
 
                printf("Usage: %s bind_ip bind_port remote_ip remote_port proto message\n", argv[0]);
22
 
                exit(1);
23
 
        }
24
 
 
25
 
        send_ret = -1;
26
 
        if (strcmp(argv[5], "udp") == 0)
27
 
        {
28
 
                send_ret = send_udp(argv[1], argv[2], argv[3], argv[4], argv[6]);
29
 
        }
30
 
        else if (strcmp(argv[5], "tcp") == 0)
31
 
        {
32
 
                send_ret = send_tcp(argv[1], argv[2], argv[3], argv[4], argv[6]);
33
 
        }
34
 
        else
35
 
        {
36
 
                printf("Unknown protocol.\n");
37
 
        }
38
 
 
39
 
        if (send_ret == -1)
40
 
        {
41
 
                printf("Send message failed.\n");
42
 
                exit(1);
43
 
        }
44
 
 
45
 
        exit(0);
46
 
}
47
 
 
48
 
int send_udp(char *bind_ip, char *bind_port, char *remote_ip, char *remote_port, char *message)
49
 
{
50
 
        int sock;
51
 
        struct sockaddr_in remote, local;
52
 
 
53
 
        if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
54
 
        {
55
 
                perror("Could not open socket: ");
56
 
                return(-1);
57
 
        }
58
 
 
59
 
        remote.sin_family = AF_INET;
60
 
        remote.sin_port = htons(atoi(remote_port));
61
 
        inet_aton(remote_ip, &remote.sin_addr);
62
 
 
63
 
        local.sin_family  = AF_INET;
64
 
        local.sin_port = htons(atoi(bind_port));
65
 
        inet_aton(bind_ip, &local.sin_addr);
66
 
 
67
 
        if (bind(sock, (struct sockaddr *) &local, sizeof(local)) < 0)
68
 
        {
69
 
                perror("Could not bind: ");
70
 
                return(-1);
71
 
        }
72
 
 
73
 
        printf("Sending \"%s\"\n", message);
74
 
        if (sendto(sock, message, strlen(message), 0, (struct sockaddr *) &remote, sizeof(remote)) <= 0)
75
 
        {
76
 
                perror("Send failed: ");
77
 
                return(-1);
78
 
        }
79
 
        close(sock);
80
 
        return(0);
81
 
        
82
 
}
83
 
 
84
 
int send_tcp(char *bind_ip, char *bind_port, char *remote_ip, char *remote_port, char *message)
85
 
{
86
 
        int sock;
87
 
        struct sockaddr_in remote, local;
88
 
 
89
 
        if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
90
 
        {
91
 
                perror("Could not open socket: ");
92
 
                return(-1);
93
 
        }
94
 
 
95
 
        remote.sin_family = AF_INET;
96
 
        remote.sin_port = htons(atoi(remote_port));
97
 
        inet_aton(remote_ip, &remote.sin_addr);
98
 
 
99
 
        local.sin_family  = AF_INET;
100
 
        local.sin_port = htons(atoi(bind_port));
101
 
        inet_aton(bind_ip, &local.sin_addr);
102
 
 
103
 
        if (bind(sock, (struct sockaddr *) &local, sizeof(local)) < 0)
104
 
        {
105
 
                perror("Could not bind: ");
106
 
                return(-1);
107
 
        }
108
 
        if (connect(sock, (struct sockaddr *) &remote, sizeof(remote)) < 0)
109
 
        {
110
 
                perror("Could not connect: ");
111
 
                return(-1);
112
 
        }
113
 
 
114
 
        printf("Sending \"%s\"\n", message);
115
 
        if (send(sock, message, strlen(message), 0) <= 0)
116
 
        {
117
 
                perror("Send failed: ");
118
 
                return(-1);
119
 
        }
120
 
        close(sock);
121
 
        return(0);
122
 
 
123
 
 
124
 
}
125