/usr/share/dx/samples/outboard/socket.c is in dxsamples 4.4.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | #include <dx/dx.h>
#if DXD_HAS_UNIX_SYS_INCLUDES
#include <sys/file.h>
#endif
#include <stdio.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#if DXD_NEEDS_SYS_SELECT_H
#include <sys/select.h>
#endif
#if DXD_HAS_UNIX_SYS_INCLUDES
#include <sys/utsname.h>
#endif
#define SOCK_QUEUE_LENGTH 1
#define SOCK_ACCEPT_TIMEOUT 60 /* Seconds */
extern Error CreateSocket(int *connect_fd, short port)
{
int i, fd, write_fd, sts, length;
fd_set fds;
struct linger sl;
struct sockaddr_un userver;
struct timeval to;
#if DXD_HAS_GETDTABLESIZE
int width = getdtablesize();
#else
int width = MAXFUPLIM;
#endif
/* set up the socket */
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0) {
DXSetError(ERROR_INTERNAL, "failure to create socket");
return ERROR;
}
/* set the socket options */
sl.l_onoff = 1;
sl.l_linger = 0;
setsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&sl, sizeof(sl));
memset((char *)&userver, 0, sizeof(userver));
userver.sun_family = AF_UNIX;
sprintf(userver.sun_path,"/tmp/.DX-unix/DX%d", port);
length = sizeof (userver) - sizeof(userver.sun_path) +
strlen (userver.sun_path);
unlink(userver.sun_path);
sts = bind(fd, (struct sockaddr *)&userver, length);
if (sts < 0) {
DXSetError(ERROR_INTERNAL,"failure of bind");
return ERROR;
}
/* listen on the file-descriptor for a connection */
if (listen(fd, SOCK_QUEUE_LENGTH) < 0) {
DXSetError(ERROR_INTERNAL, "failure of listen");
return ERROR;
}
/* wait until a connection is made on file-descriptor fd */
FD_ZERO(&fds);
FD_SET(fd, &fds);
to.tv_sec = SOCK_ACCEPT_TIMEOUT;
to.tv_usec = 0;
sts = select(width, (SelectPtr) &fds, NULL, NULL, &to);
if (sts < 0) {
DXSetError(ERROR_INTERNAL,"failure of select");
return ERROR;
}
else if (sts == 0) {
DXSetError(ERROR_INTERNAL,"connection timed out");
return ERROR;
}
/* accept the connection */
*connect_fd = accept(fd, (struct sockaddr *)&userver, &length);
close(fd);
return OK;
}
extern Error ConnectSocket(int *fd, int port)
{
struct sockaddr_un userver;
int length;
memset((char *)&userver, 0, sizeof(userver));
userver.sun_family = AF_UNIX;
sprintf(userver.sun_path, "/tmp/.DX-unix/DX%d", port);
length = sizeof (userver) - sizeof(userver.sun_path) +
strlen (userver.sun_path);
*fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (connect(*fd, (struct sockaddr *) &userver, length) < 0)
{
DXSetError(ERROR_INTERNAL,"failure to connect to socket");
return ERROR;
}
return OK;
}
|