-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_application.c
More file actions
78 lines (66 loc) · 1.84 KB
/
cli_application.c
File metadata and controls
78 lines (66 loc) · 1.84 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
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
/*
############################################################################################
# File: cli_application.c
# Author: Smeet Raj
# Date: 18th Feb 2024
# Description: The CLI application allows users to interact with the packet analyzer.
# It provides commands to start, stop, and exit the packet capture process.
#
# Copyright (c) 2024 Smeet Raj
# All rights reserved.
############################################################################################
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
int main(int argc, char *argv[])
{
// Create a Unix domain socket
int sockfd;
struct sockaddr_un serv_addr;
if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
{
perror("socket");
return 1;
}
// Set server address
serv_addr.sun_family = AF_UNIX;
strncpy(serv_addr.sun_path, SOCK_PATH, sizeof(serv_addr.sun_path) - 1);
// Connect to the packet analyzer
if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
perror("connect");
return 1;
}
// Interactive CLI
char cmd[10];
while (1)
{
printf("Enter command: ");
scanf("%s", cmd);
// Send command to the packet analyzer
if (send(sockfd, cmd, strlen(cmd), 0) < 0)
{
perror("send");
break;
}
// Toggle start/stop on 'S' key
if (strcmp(cmd, "S") == 0)
{
printf("Toggle start/stop\n");
continue;
}
// Exit on 'exit' command
if (strcmp(cmd, "exit") == 0)
{
printf("Exiting...\n");
break;
}
}
// Close the socket
close(sockfd);
return 0;
}