Return to stream_unix.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / strongswan / src / libstrongswan / networking / streams |
1.1 misho 1: /* 2: * Copyright (C) 2013 Martin Willi 3: * Copyright (C) 2013 revosec AG 4: * 5: * This program is free software; you can redistribute it and/or modify it 6: * under the terms of the GNU General Public License as published by the 7: * Free Software Foundation; either version 2 of the License, or (at your 8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. 9: * 10: * This program is distributed in the hope that it will be useful, but 11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13: * for more details. 14: */ 15: 16: #include <library.h> 17: #include <errno.h> 18: #include <unistd.h> 19: #include <limits.h> 20: 21: #include "stream_unix.h" 22: 23: /** 24: * See header 25: */ 26: int stream_parse_uri_unix(char *uri, struct sockaddr_un *addr) 27: { 28: if (!strpfx(uri, "unix://")) 29: { 30: return -1; 31: } 32: uri += strlen("unix://"); 33: 34: memset(addr, 0, sizeof(*addr)); 35: addr->sun_family = AF_UNIX; 36: strncpy(addr->sun_path, uri, sizeof(addr->sun_path)); 37: addr->sun_path[sizeof(addr->sun_path)-1] = '\0'; 38: 39: return offsetof(struct sockaddr_un, sun_path) + strlen(addr->sun_path); 40: } 41: 42: /** 43: * See header 44: */ 45: stream_t *stream_create_unix(char *uri) 46: { 47: struct sockaddr_un addr; 48: int len, fd; 49: 50: len = stream_parse_uri_unix(uri, &addr); 51: if (len == -1) 52: { 53: DBG1(DBG_NET, "invalid stream URI: '%s'", uri); 54: return NULL; 55: } 56: fd = socket(AF_UNIX, SOCK_STREAM, 0); 57: if (fd < 0) 58: { 59: DBG1(DBG_NET, "opening socket '%s' failed: %s", uri, strerror(errno)); 60: return NULL; 61: } 62: if (connect(fd, (struct sockaddr*)&addr, len) < 0) 63: { 64: DBG1(DBG_NET, "connecting to '%s' failed: %s", uri, strerror(errno)); 65: close(fd); 66: return NULL; 67: } 68: return stream_create_from_fd(fd); 69: }