Last active
May 7, 2019 06:57
-
-
Save soutys/ffbe2e76a86835a9cc6b to your computer and use it in GitHub Desktop.
abstract namespace unix socket problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
char *socket_path = "/var/tmp/sock.tmp"; | |
int main(int argc, char *argv[]) { | |
struct sockaddr_un addr; | |
char buf[100]; | |
int fd,rc; | |
if (argc > 1) socket_path=argv[1]; | |
if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { | |
perror("socket error"); | |
exit(-1); | |
} | |
memset(&addr, 0, sizeof(addr)); | |
addr.sun_family = AF_UNIX; | |
strncpy(&addr.sun_path[1], socket_path, sizeof(addr.sun_path) - 2); | |
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { | |
perror("connect error"); | |
exit(-1); | |
} | |
while( (rc=read(STDIN_FILENO, buf, sizeof(buf))) > 0) { | |
if (write(fd, buf, rc) != rc) { | |
if (rc > 0) fprintf(stderr,"partial write"); | |
else { | |
perror("write error"); | |
exit(-1); | |
} | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import socket | |
import os | |
print("Connecting...") | |
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
client.connect(b"\0/var/tmp/sock.tmp") | |
print("Ready.") | |
print("Ctrl-C to quit.") | |
print("Sending 'DONE' shuts down the server and quits.") | |
while True: | |
try: | |
x = input("> ") | |
if "" != x: | |
print("SEND:", x) | |
client.send(x.encode('utf-8')) | |
if "DONE" == x: | |
print("Shutting down.") | |
break | |
except KeyboardInterrupt as k: | |
print("Shutting down.") | |
client.close() | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import socket | |
import os | |
print("Connecting...") | |
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
client.connect("\0/var/tmp/sock.tmp" + "\0" * 90) | |
print("Ready.") | |
print("Ctrl-C to quit.") | |
print("Sending 'DONE' shuts down the server and quits.") | |
while True: | |
try: | |
x = input("> ") | |
if "" != x: | |
print("SEND:", x) | |
client.send(x.encode('utf-8')) | |
if "DONE" == x: | |
print("Shutting down.") | |
break | |
except KeyboardInterrupt as k: | |
print("Shutting down.") | |
client.close() | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <stdlib.h> | |
#include <sys/unistd.h> | |
#include <string.h> | |
char *socket_path = "/var/tmp/sock.tmp"; | |
int main(int argc, char *argv[]) { | |
struct sockaddr_un addr; | |
char buf[100]; | |
int fd,cl,rc; | |
if (argc > 1) socket_path=argv[1]; | |
if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { | |
perror("socket error"); | |
exit(-1); | |
} | |
memset(&addr, 0, sizeof(addr)); | |
addr.sun_family = AF_UNIX; | |
strncpy(&addr.sun_path[1], socket_path, sizeof(addr.sun_path) - 2); | |
if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { | |
perror("bind error"); | |
exit(-1); | |
} | |
if (listen(fd, 5) == -1) { | |
perror("listen error"); | |
exit(-1); | |
} | |
while (1) { | |
if ( (cl = accept(fd, NULL, NULL)) == -1) { | |
perror("accept error"); | |
continue; | |
} | |
while ( (rc=read(cl,buf,sizeof(buf))) > 0) { | |
printf("read %u bytes: %.*s\n", rc, rc, buf); | |
} | |
if (rc == -1) { | |
perror("read"); | |
exit(-1); | |
} | |
else if (rc == 0) { | |
printf("EOF\n"); | |
close(cl); | |
} | |
} | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import socket | |
import os | |
print("Opening socket...") | |
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
print("socket.create") | |
server.bind(b"\0/var/tmp/sock.tmp") | |
print("socket.bind") | |
server.listen(10) | |
print("socket.listen") | |
print("Listening...") | |
connection, client_address = server.accept() | |
print("socket.accept") | |
while True: | |
datagram = connection.recv(1024) | |
if not datagram: | |
break | |
else: | |
print("-" * 20) | |
print(datagram.decode('utf-8')) | |
if "DONE" == datagram.decode('utf-8'): | |
break | |
print("-" * 20) | |
print("Shutting down...") | |
connection.close() | |
server.close() | |
print("Done") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
char *socket_path = "/var/tmp/sock.tmp"; | |
int main(int argc, char *argv[]) { | |
struct sockaddr_un addr; | |
char buf[100]; | |
int fd,rc; | |
if (argc > 1) socket_path=argv[1]; | |
if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { | |
perror("socket error"); | |
exit(-1); | |
} | |
memset(&addr, 0, sizeof(addr)); | |
addr.sun_family = AF_UNIX; | |
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); | |
if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { | |
perror("connect error"); | |
exit(-1); | |
} | |
while( (rc=read(STDIN_FILENO, buf, sizeof(buf))) > 0) { | |
if (write(fd, buf, rc) != rc) { | |
if (rc > 0) fprintf(stderr,"partial write"); | |
else { | |
perror("write error"); | |
exit(-1); | |
} | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import socket | |
import os | |
print("Connecting...") | |
client = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
client.connect("/var/tmp/sock.tmp") | |
print("Ready.") | |
print("Ctrl-C to quit.") | |
print("Sending 'DONE' shuts down the server and quits.") | |
while True: | |
try: | |
x = raw_input("> ") | |
if "" != x: | |
print("SEND:", x) | |
client.send(x.encode('utf-8')) | |
if "DONE" == x: | |
print("Shutting down.") | |
break | |
except KeyboardInterrupt as k: | |
print("Shutting down.") | |
client.close() | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <sys/un.h> | |
#include <stdlib.h> | |
#include <sys/unistd.h> | |
#include <string.h> | |
char *socket_path = "/var/tmp/sock.tmp"; | |
int main(int argc, char *argv[]) { | |
struct sockaddr_un addr; | |
char buf[100]; | |
int fd,cl,rc; | |
if (argc > 1) socket_path=argv[1]; | |
if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { | |
perror("socket error"); | |
exit(-1); | |
} | |
memset(&addr, 0, sizeof(addr)); | |
addr.sun_family = AF_UNIX; | |
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1); | |
unlink(socket_path); | |
if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) { | |
perror("bind error"); | |
exit(-1); | |
} | |
if (listen(fd, 5) == -1) { | |
perror("listen error"); | |
exit(-1); | |
} | |
while (1) { | |
if ( (cl = accept(fd, NULL, NULL)) == -1) { | |
perror("accept error"); | |
continue; | |
} | |
while ( (rc=read(cl,buf,sizeof(buf))) > 0) { | |
printf("read %u bytes: %.*s\n", rc, rc, buf); | |
} | |
if (rc == -1) { | |
perror("read"); | |
exit(-1); | |
} | |
else if (rc == 0) { | |
printf("EOF\n"); | |
close(cl); | |
} | |
} | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import socket | |
import os | |
print("Opening socket...") | |
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | |
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
print("socket.create") | |
server.bind("/var/tmp/sock.tmp") | |
print("socket.bind") | |
server.listen(10) | |
print("socket.listen") | |
print("Listening...") | |
connection, client_address = server.accept() | |
print("socket.accept") | |
while True: | |
datagram = connection.recv(1024) | |
if not datagram: | |
break | |
else: | |
print("-" * 20) | |
print(datagram.decode('utf-8')) | |
if "DONE" == datagram.decode('utf-8'): | |
break | |
print("-" * 20) | |
print("Shutting down...") | |
connection.close() | |
server.close() | |
print("Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment