Originally Posted by
Codeplug
I've always just used send() and recv() on sockets.
Yes, it would seem irrefutable now.
If anyone has time and can see what's wrong, this is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
short int loclconn (char *socket, int fd) {
size_t size;
struct sockaddr_un peer;
peer.sun_family=AF_LOCAL;
strcpy(peer.sun_path,socket);
size=SUN_LEN(&peer);
return connect(fd,(struct sockaddr*)&peer,size);
}
short int loclsckt (char *file) {
struct sockaddr_un name;
short int sock;
size_t size;
if ((strlen(file)) > 107) return -2;
if ((sock=socket(PF_LOCAL,SOCK_STREAM,0)) < 0) return -1;
name.sun_family = AF_LOCAL;
strcpy(name.sun_path, file);
size=SUN_LEN(&name);
if ((bind(sock,(struct sockaddr*)&name,size) < 0)) return -3;
return sock;
}
int main (int argc, char *argv[]) {
short int pid, CHLD, PARN, OUT, IN;
size_t len;
int bytes;
char *buffer, *buffer2;
struct stat finfo;
FILE *fstRO = fopen(argv[1], "rb"), * sstOUT;
FILE *fstW = fopen("/root/test/image.jpg", "wb");
struct sockaddr addr;
if (fstRO == NULL) {
puts("valid filename required");
return -1;}
stat(argv[1],&finfo);
bytes=finfo.st_size;
buffer=malloc(bytes);
buffer2=malloc(bytes);
printf("%d\n",bytes);
fread(buffer,bytes,1,fstRO);
fclose(fstRO);
pid=fork();
if (pid == 0) {
puts("child");
CHLD=loclsckt("/root/test/sock1");
OUT=loclconn("/root/test/sock2",CHLD);
if ((sstOUT=fdopen(OUT,"wb"))==NULL) perror("fdopen");
write(OUT,buffer,bytes);
close(OUT);
close(CHLD);
unlink("/root/test/sock1");
return;
}
puts("parent");
PARN=loclsckt("/root/test/sock2");
listen(IN,0);
IN=accept(PARN,&addr,&len);
read(IN,buffer2,bytes);
close(IN);
close(PARN);
unlink("/root/test/sock2");
fwrite(buffer2,bytes,1,fstW);
fclose(fstW);
}
As is, it does everything I mentioned -- a normal read/write on the descriptors (producing a defuct file) and an attempt to create a stream on the child (send) descriptor, which fails and gives perror.