add some comments
[tridge/junkcode.git] / ux_dgram.c
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdarg.h>
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8 #include <sys/types.h>
9 #include <sys/socket.h>
10 #include <sys/un.h>
11 #include <sys/poll.h>
12
13
14 /*
15   connect to a unix domain socket
16 */
17 int ux_socket_connect(const char *name)
18 {
19         int fd;
20         struct sockaddr_un addr;
21
22         memset(&addr, 0, sizeof(addr));
23         addr.sun_family = AF_UNIX;
24         strncpy(addr.sun_path, name, sizeof(addr.sun_path));
25
26         fd = socket(AF_UNIX, SOCK_DGRAM, 0);
27         if (fd == -1) {
28                 return -1;
29         }
30         
31         if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
32                 close(fd);
33                 return -1;
34         }
35
36         return fd;
37 }
38
39
40 /*
41   create a unix domain socket and bind it
42   return a file descriptor open on the socket 
43 */
44 static int ux_socket_bind(const char *name)
45 {
46         int fd;
47         struct sockaddr_un addr;
48
49         /* get rid of any old socket */
50         unlink(name);
51
52         fd = socket(AF_UNIX, SOCK_DGRAM, 0);
53         if (fd == -1) return -1;
54
55         memset(&addr, 0, sizeof(addr));
56         addr.sun_family = AF_UNIX;
57         strncpy(addr.sun_path, name, sizeof(addr.sun_path));
58
59         if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
60                 close(fd);
61                 return -1;
62         }       
63
64         return fd;
65 }
66
67
68 void *x_malloc(size_t size)
69 {
70         void *ret;
71         ret = malloc(size);
72         if (!ret) {
73                 fprintf(stderr,"Out of memory for size %d\n", (int)size);
74                 exit(1);
75         }
76         return ret;
77 }
78
79 void *x_realloc(void *ptr, size_t size)
80 {
81         void *ret;
82         ret = realloc(ptr, size);
83         if (!ret) {
84                 fprintf(stderr,"Out of memory for size %d\n", (int)size);
85                 exit(1);
86         }
87         return ret;
88 }
89
90 /*
91   keep writing until its all sent
92 */
93 int write_all(int fd, const void *buf, size_t len)
94 {
95         size_t total = 0;
96         while (len) {
97                 int n = write(fd, buf, len);
98                 if (n <= 0) return total;
99                 buf = n + (char *)buf;
100                 len -= n;
101                 total += n;
102         }
103         return total;
104 }
105
106
107
108 int main(void)
109 {
110         const char *fname = "ux_dgram.dat";
111         int fd, fd2, r;
112         ssize_t s;
113         char buf[1024];
114         struct sockaddr_un from;
115         socklen_t fromlen=sizeof(from);
116
117         fd = ux_socket_bind(fname);
118         
119         fd2 = ux_socket_connect(fname);
120
121         dprintf(fd2, "Hello world\n");
122
123         memset(&from, 0, sizeof(from));
124
125         s = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&from, &fromlen);
126         printf("Received %d bytes from '%s' family=%d\n", s, 
127                from.sun_path, from.sun_family);
128
129         r = connect(fd, (struct sockaddr *)&from, sizeof(from));
130         printf("connect gave %d\n", r);
131         
132         s = sendto(fd, buf, s, 0, (struct sockaddr *)&from, sizeof(from));
133         printf("Sent %d bytes\n", s);
134
135         return 0;
136 }