SPXstack/client-stream.c
Matjaz Mesnjak c1e8ab1593 Initial commit.
2022-01-14 20:42:16 +01:00

174 lines
5.5 KiB
C

/* a client in the unix domain */
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include "ipx.h"
#include "ipxstack.h"
void *mmData = "AK> hello \0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxM";
ipx_response_t *conn;
void error(const char *);
int main(int argc, char *argv[])
{
int sockfd, servlen,n;
int sockfd2, servlen2;
int sockfd3, servlen3;
struct sockaddr_un serv_addr;
struct sockaddr_un serv_addr2;
struct sockaddr_un serv_addr3;
char buffer[1024];
// uint32_t conn_pid;
ipx_request_t *init_connection;
ipx_request_t *close_connection;
init_connection = malloc(sizeof(ipx_request_t));
if(!init_connection)
error("Out of memory: init_connection");
close_connection = malloc(sizeof(ipx_request_t));
if(!close_connection)
error("Out of memory: close_connection");
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, argv[1]);
servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0)
error("Creating socket");
if (connect(sockfd, (struct sockaddr *) &serv_addr, servlen) < 0)
error("Connecting");
// printf("Please enter your message: ");
bzero(buffer,1002);
init_connection->request_type = OPEN_STREAM;
init_connection->src_socket = 0x1453;
init_connection->dest_net = 0x00000000;
init_connection->dest_node[0] = 0xffff;
init_connection->dest_node[1] = 0xffff;
init_connection->dest_node[2] = 0xffff;
init_connection->dest_socket = 0x869b;
// init_connection->pid = 1234;
// printf("Init connection: request_type=%i; src_socket=%0x; pid=%i\n", init_connection->request_type, init_connection->src_socket, init_connection->pid);
memcpy(buffer, init_connection, sizeof(ipx_request_t));
/* memcpy(buffer, frame, 1000);*/
//fgets(buffer,80,stdin);
/*
ethernet = (ethernet_frame_t *) &buffer;
ipx = (ipx_frame_t *) &ethernet->ipxpacket;
ipx_data = (void *)ipx->data;
printf("data: %s", ipx_data);
*/
// write(sockfd,buffer,1000);
printf("Setting up the Connection\n");
write(sockfd, init_connection, sizeof(ipx_request_t));
printf("Waiting for ansver\n");
n=read(sockfd,buffer,280);
printf("The return message was\n");
conn = (ipx_response_t *)buffer;
if(conn->error != NO_ERROR) {
printf("Error: socket is already opened\n");
exit(2);
}
// printf("My connection: socket=%0x; socket name=%s; pid=%i\n", conn->src_socket, conn->socket_name, conn->pid);
// conn_pid = conn->pid;
write(1,buffer,n);
close(sockfd);
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
sleep(2);
char input_string[200];
int loop = 1;
ipx_frame_t *ipx_packet;
printf("Sending data on data sockeet\n");
bzero(buffer, 1002);
bzero((char *)&serv_addr3, sizeof(serv_addr3));
serv_addr3.sun_family = AF_UNIX;
strcpy(serv_addr3.sun_path, "./socket.1453");
servlen3 = strlen(serv_addr3.sun_path) + sizeof(serv_addr3.sun_family);
if((sockfd3 = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
error("Creating data socket");
if(connect(sockfd3, (struct sockaddr *) &serv_addr3, servlen3) < 0)
error("Connecting data socket");
while(loop) {
bzero(buffer, 1002);
gets(input_string);
if(strncmp(input_string, "exit", 4) == 0)
{
loop = 0;
}
strncpy(buffer+6, input_string, sizeof(input_string));
write(sockfd3, buffer, 1000);
/* bzero(buffer, 1002);
read(sockfd3, buffer, 1000);
ipx_packet = (ipx_frame_t *)buffer;
printf((char *)ipx_packet->data + 6);*/
}
close(sockfd3);
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
sleep(2);
printf("Closing connection.\n");
bzero(buffer, 1002);
close_connection->request_type = CLOSE_SOCKET;
close_connection->src_socket = 0x1453;
// close_connection->pid = conn_pid;
// printf("Closing connection: request_type=%i; src_socket=%0x; pid=%i\n", close_connection->request_type, close_connection->src_socket, close_connection->pid);
bzero((char *)&serv_addr2,sizeof(serv_addr2));
serv_addr2.sun_family = AF_UNIX;
strcpy(serv_addr2.sun_path, argv[1]);
servlen2 = strlen(serv_addr2.sun_path) + sizeof(serv_addr2.sun_family);
if ((sockfd2 = socket(AF_UNIX, SOCK_STREAM,0)) < 0)
error("Creating socket");
if (connect(sockfd2, (struct sockaddr *) &serv_addr2, servlen2) < 0)
error("Connecting");
bzero(buffer,1002);
memcpy(buffer, close_connection, sizeof(ipx_request_t));
printf("Closing connection.....\n");
write(sockfd2,buffer,1000);
printf("Closing connection......\n");
// sleep(2);
close(sockfd2);
free(init_connection);
free(close_connection);
return 0;
}
void error(const char *msg)
{
perror(msg);
exit(0);
}