-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
49 lines (45 loc) · 1.35 KB
/
client.cpp
File metadata and controls
49 lines (45 loc) · 1.35 KB
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
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "constants.h"
#include <string>
#include <thread>
int main() {
auto fd = socket(AF_UNIX, SOCK_STREAM, 0);
std::string str = "I'm the client.";
sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, kSocketPath, sizeof(addr.sun_path)-1);
int val = 1;
if (setsockopt (fd, SOL_SOCKET, SO_PASSCRED, &val, sizeof (val)) < 0) {
perror("unable to set local per credentials");
exit(1);
}
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("connect error");
exit(1);
}
ucred ucred;
socklen_t len = sizeof(ucred);
if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) {
perror("getsockopt error");
exit(1);
}
printf("CLIENT (PID: %ld) ===> Credentials from SO_PEERCRED (server credentials): pid=%ld, euid=%ld, egid=%ld\n",
(long)getpid(), (long) ucred.pid, (long) ucred.uid, (long) ucred.gid);
//sending the address of the string
struct msg {
uint64_t address;
uint64_t size;
} msg;
msg.address = (uint64_t)str.data();
msg.size = str.size();
write(fd, &msg, sizeof(msg));
close(fd);
printf("Waiting a bit till the server ends\n");
std::this_thread::sleep_for(std::chrono::seconds(1));
printf("Ending the client\n");
}