libixp

git clone git://oldgit.suckless.org/libixp/
Log | Files | Refs | LICENSE

commit bff2a30e10fc3b7f1659fa15a3afca6b490a8b9a
parent e1b1ece5f1933034883919ab5bfc9c82c93c328d
Author: Anselm R. Garbe <arg@10kloc.org>
Date:   Thu, 12 Oct 2006 13:27:51 +0200

removed useless lines

Diffstat:
client.c | 47++++++++++-------------------------------------
convert.c | 56+++++++++++++++++++-------------------------------------
intmap.c | 36+++++++++++++-----------------------
message.c | 21++++++---------------
request.c | 26++++----------------------
server.c | 26++++++++------------------
socket.c | 36++++++++----------------------------
transport.c | 18++++++------------
8 files changed, 74 insertions(+), 192 deletions(-)

diff --git a/client.c b/client.c @@ -1,8 +1,7 @@ -/* - * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> +/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> * See LICENSE file for license details. */ - +#include "ixp.h" #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -10,11 +9,8 @@ #include <sys/types.h> #include <unistd.h> -#include "ixp.h" - int -ixp_client_do_fcall(IXPClient *c) -{ +ixp_client_do_fcall(IXPClient *c) { static unsigned char msg[IXP_MAX_MSG]; unsigned int msize = ixp_fcall2msg(msg, &c->ifcall, IXP_MAX_MSG); @@ -31,19 +27,15 @@ ixp_client_do_fcall(IXPClient *c) c->errstr = c->ofcall.ename; return -1; } - return 0; } int -ixp_client_dial(IXPClient *c, char *sockfile, unsigned int rootfid) -{ - +ixp_client_dial(IXPClient *c, char *sockfile, unsigned int rootfid) { if((c->fd = ixp_connect_sock(sockfile)) < 0) { c->errstr = "cannot connect server"; return -1; } - c->ifcall.type = TVERSION; c->ifcall.tag = IXP_NOTAG; c->ifcall.msize = IXP_MAX_MSG; @@ -61,7 +53,6 @@ ixp_client_dial(IXPClient *c, char *sockfile, unsigned int rootfid) } free(c->ofcall.version); c->root_fid = rootfid; - c->ifcall.type = TATTACH; c->ifcall.tag = IXP_NOTAG; c->ifcall.fid = c->root_fid; @@ -74,20 +65,16 @@ ixp_client_dial(IXPClient *c, char *sockfile, unsigned int rootfid) return -1; } c->root_qid = c->ofcall.qid; - return 0; } int -ixp_client_remove(IXPClient *c, unsigned int newfid, char *filepath) -{ - +ixp_client_remove(IXPClient *c, unsigned int newfid, char *filepath) { if(ixp_client_walk(c, newfid, filepath) == -1) return -1; c->ifcall.type = TREMOVE; c->ifcall.tag = IXP_NOTAG; c->ifcall.fid = newfid; - return ixp_client_do_fcall(c); } @@ -105,8 +92,7 @@ ixp_client_create(IXPClient *c, unsigned int dirfid, char *name, } int -ixp_client_walk(IXPClient *c, unsigned int newfid, char *filepath) -{ +ixp_client_walk(IXPClient *c, unsigned int newfid, char *filepath) { unsigned int i; char *wname[IXP_MAX_WELEM]; @@ -124,12 +110,9 @@ ixp_client_walk(IXPClient *c, unsigned int newfid, char *filepath) } int -ixp_client_stat(IXPClient *c, unsigned int newfid, char *filepath) -{ - +ixp_client_stat(IXPClient *c, unsigned int newfid, char *filepath) { if(ixp_client_walk(c, newfid, filepath) == -1) return -1; - c->ifcall.type = TSTAT; c->ifcall.tag = IXP_NOTAG; c->ifcall.fid = newfid; @@ -137,9 +120,7 @@ ixp_client_stat(IXPClient *c, unsigned int newfid, char *filepath) } int -ixp_client_open(IXPClient *c, unsigned int newfid, unsigned char mode) -{ - +ixp_client_open(IXPClient *c, unsigned int newfid, unsigned char mode) { c->ifcall.type = TOPEN; c->ifcall.tag = IXP_NOTAG; c->ifcall.fid = newfid; @@ -151,7 +132,6 @@ int ixp_client_walkopen(IXPClient *c, unsigned int newfid, char *filepath, unsigned char mode) { - if(ixp_client_walk(c, newfid, filepath) == -1) return -1; return ixp_client_open(c, newfid, mode); @@ -172,7 +152,6 @@ ixp_client_read(IXPClient *c, unsigned int fid, unsigned long long offset, return -1; memcpy(result, c->ofcall.data, c->ofcall.count); free(c->ofcall.data); - return c->ofcall.count; } @@ -181,12 +160,10 @@ ixp_client_write(IXPClient *c, unsigned int fid, unsigned long long offset, unsigned int count, unsigned char *data) { - if(count > c->ofcall.iounit) { c->errstr = "iounit exceeded"; return -1; } - c->ifcall.type = TWRITE; c->ifcall.tag = IXP_NOTAG; c->ifcall.fid = fid; @@ -195,14 +172,11 @@ ixp_client_write(IXPClient *c, unsigned int fid, c->ifcall.data = (void *)data; if(ixp_client_do_fcall(c) == -1) return -1; - return c->ofcall.count; } int -ixp_client_close(IXPClient *c, unsigned int fid) -{ - +ixp_client_close(IXPClient *c, unsigned int fid) { c->ifcall.type = TCLUNK; c->ifcall.tag = IXP_NOTAG; c->ifcall.fid = fid; @@ -210,8 +184,7 @@ ixp_client_close(IXPClient *c, unsigned int fid) } void -ixp_client_hangup(IXPClient *c) -{ +ixp_client_hangup(IXPClient *c) { /* session finished, now shutdown */ if(c->fd) { shutdown(c->fd, SHUT_RDWR); diff --git a/convert.c b/convert.c @@ -1,31 +1,26 @@ -/* - * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> +/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> * See LICENSE file for license details. */ - +#include "ixp.h" #include <stdlib.h> #include <string.h> -#include "ixp.h" /* packode/unpackode stuff */ void -ixp_pack_u8(unsigned char **msg, int *msize, unsigned char val) -{ +ixp_pack_u8(unsigned char **msg, int *msize, unsigned char val) { if(!msize || (*msize -= 1) >= 0) *(*msg)++ = val; } void -ixp_unpack_u8(unsigned char **msg, int *msize, unsigned char *val) -{ +ixp_unpack_u8(unsigned char **msg, int *msize, unsigned char *val) { if(!msize || (*msize -= 1) >= 0) *val = *(*msg)++; } void -ixp_pack_u16(unsigned char **msg, int *msize, unsigned short val) -{ +ixp_pack_u16(unsigned char **msg, int *msize, unsigned short val) { if(!msize || (*msize -= 2) >= 0) { *(*msg)++ = val; *(*msg)++ = val >> 8; @@ -33,8 +28,7 @@ ixp_pack_u16(unsigned char **msg, int *msize, unsigned short val) } void -ixp_unpack_u16(unsigned char **msg, int *msize, unsigned short *val) -{ +ixp_unpack_u16(unsigned char **msg, int *msize, unsigned short *val) { if(!msize || (*msize -= 2) >= 0) { *val = *(*msg)++; *val |= *(*msg)++ << 8; @@ -42,8 +36,7 @@ ixp_unpack_u16(unsigned char **msg, int *msize, unsigned short *val) } void -ixp_pack_u32(unsigned char **msg, int *msize, unsigned int val) -{ +ixp_pack_u32(unsigned char **msg, int *msize, unsigned int val) { if(!msize || (*msize -= 4) >= 0) { *(*msg)++ = val; *(*msg)++ = val >> 8; @@ -53,8 +46,7 @@ ixp_pack_u32(unsigned char **msg, int *msize, unsigned int val) } void -ixp_unpack_u32(unsigned char **msg, int *msize, unsigned int *val) -{ +ixp_unpack_u32(unsigned char **msg, int *msize, unsigned int *val) { if(!msize || (*msize -= 4) >= 0) { *val = *(*msg)++; *val |= *(*msg)++ << 8; @@ -64,8 +56,7 @@ ixp_unpack_u32(unsigned char **msg, int *msize, unsigned int *val) } void -ixp_pack_u64(unsigned char **msg, int *msize, unsigned long long val) -{ +ixp_pack_u64(unsigned char **msg, int *msize, unsigned long long val) { if(!msize || (*msize -= 8) >= 0) { *(*msg)++ = val; *(*msg)++ = val >> 8; @@ -79,8 +70,7 @@ ixp_pack_u64(unsigned char **msg, int *msize, unsigned long long val) } void -ixp_unpack_u64(unsigned char **msg, int *msize, unsigned long long *val) -{ +ixp_unpack_u64(unsigned char **msg, int *msize, unsigned long long *val) { if(!msize || (*msize -= 8) >= 0) { *val |= *(*msg)++; *val |= *(*msg)++ << 8; @@ -94,8 +84,7 @@ ixp_unpack_u64(unsigned char **msg, int *msize, unsigned long long *val) } void -ixp_pack_string(unsigned char **msg, int *msize, const char *s) -{ +ixp_pack_string(unsigned char **msg, int *msize, const char *s) { unsigned short len = s ? strlen(s) : 0; ixp_pack_u16(msg, msize, len); if(s) @@ -122,7 +111,6 @@ ixp_unpack_strings(unsigned char **msg, int *msize, unsigned short n, char **str ixp_unpack_u16(msg, msize, &len); if(!msize || (*msize -= len) < 0) return; - memcpy(s, *msg, len); s[len] = '\0'; strings[i] = (char *)s; @@ -132,8 +120,7 @@ ixp_unpack_strings(unsigned char **msg, int *msize, unsigned short n, char **str } void -ixp_unpack_string(unsigned char **msg, int *msize, char **string, unsigned short *len) -{ +ixp_unpack_string(unsigned char **msg, int *msize, char **string, unsigned short *len) { ixp_unpack_u16(msg, msize, len); *string = NULL; if (!msize || (*msize -= *len) >= 0) { @@ -146,8 +133,7 @@ ixp_unpack_string(unsigned char **msg, int *msize, char **string, unsigned short } void -ixp_pack_data(unsigned char **msg, int *msize, unsigned char *data, unsigned int datalen) -{ +ixp_pack_data(unsigned char **msg, int *msize, unsigned char *data, unsigned int datalen) { if(!msize || (*msize -= datalen) >= 0) { memcpy(*msg, data, datalen); *msg += datalen; @@ -155,8 +141,7 @@ ixp_pack_data(unsigned char **msg, int *msize, unsigned char *data, unsigned int } void -ixp_unpack_data(unsigned char **msg, int *msize, unsigned char **data, unsigned int datalen) -{ +ixp_unpack_data(unsigned char **msg, int *msize, unsigned char **data, unsigned int datalen) { if(!msize || (*msize -= datalen) >= 0) { *data = ixp_emallocz(datalen); memcpy(*data, *msg, datalen); @@ -185,24 +170,21 @@ ixp_unpack_prefix(unsigned char **msg, unsigned int *size, unsigned char *id, } void -ixp_pack_qid(unsigned char **msg, int *msize, Qid * qid) -{ +ixp_pack_qid(unsigned char **msg, int *msize, Qid * qid) { ixp_pack_u8(msg, msize, qid->type); ixp_pack_u32(msg, msize, qid->version); ixp_pack_u64(msg, msize, qid->path); } void -ixp_unpack_qid(unsigned char **msg, int *msize, Qid * qid) -{ +ixp_unpack_qid(unsigned char **msg, int *msize, Qid * qid) { ixp_unpack_u8(msg, msize, &qid->type); ixp_unpack_u32(msg, msize, &qid->version); ixp_unpack_u64(msg, msize, &qid->path); } void -ixp_pack_stat(unsigned char **msg, int *msize, Stat * stat) -{ +ixp_pack_stat(unsigned char **msg, int *msize, Stat * stat) { ixp_pack_u16(msg, msize, ixp_sizeof_stat(stat) - sizeof(unsigned short)); ixp_pack_u16(msg, msize, stat->type); ixp_pack_u32(msg, msize, stat->dev); @@ -218,9 +200,9 @@ ixp_pack_stat(unsigned char **msg, int *msize, Stat * stat) } void -ixp_unpack_stat(unsigned char **msg, int *msize, Stat * stat) -{ +ixp_unpack_stat(unsigned char **msg, int *msize, Stat * stat) { unsigned short dummy; + *msg += sizeof(unsigned short); ixp_unpack_u16(msg, msize, &stat->type); ixp_unpack_u32(msg, msize, &stat->dev); diff --git a/intmap.c b/intmap.c @@ -1,7 +1,7 @@ /* This file is derived from src/lib9p/intmap.c from plan9port */ /* See LICENCE.p9p for terms of use */ -#include <stdlib.h> #include "ixp.h" +#include <stdlib.h> #define USED(v) if(v){}else{} struct Intlist { @@ -12,27 +12,23 @@ struct Intlist { }; static unsigned long -hashid(Intmap *map, unsigned long id) -{ +hashid(Intmap *map, unsigned long id) { return id%map->nhash; } static void -nop(void *v) -{ +nop(void *v) { USED(v); } void -initmap(Intmap *m, unsigned long nhash, void *hash) -{ +initmap(Intmap *m, unsigned long nhash, void *hash) { m->nhash = nhash; m->hash = hash; } static Intlist** -llookup(Intmap *map, unsigned long id) -{ +llookup(Intmap *map, unsigned long id) { Intlist **lf; for(lf=&map->hash[hashid(map, id)]; *lf; lf=&(*lf)->link) @@ -42,8 +38,7 @@ llookup(Intmap *map, unsigned long id) } void -freemap(Intmap *map, void (*destroy)(void*)) -{ +freemap(Intmap *map, void (*destroy)(void*)) { int i; Intlist *p, *nlink; @@ -58,8 +53,7 @@ freemap(Intmap *map, void (*destroy)(void*)) } } void -execmap(Intmap *map, void (*run)(void*)) -{ +execmap(Intmap *map, void (*run)(void*)) { int i; Intlist *p, *nlink; @@ -71,9 +65,8 @@ execmap(Intmap *map, void (*run)(void*)) } } -void* -lookupkey(Intmap *map, unsigned long id) -{ +void * +lookupkey(Intmap *map, unsigned long id) { Intlist *f; void *v; @@ -84,9 +77,8 @@ lookupkey(Intmap *map, unsigned long id) return v; } -void* -insertkey(Intmap *map, unsigned long id, void *v) -{ +void * +insertkey(Intmap *map, unsigned long id, void *v) { Intlist *f; void *ov; unsigned long h; @@ -108,8 +100,7 @@ insertkey(Intmap *map, unsigned long id, void *v) } int -caninsertkey(Intmap *map, unsigned long id, void *v) -{ +caninsertkey(Intmap *map, unsigned long id, void *v) { Intlist *f; int rv; unsigned long h; @@ -129,8 +120,7 @@ caninsertkey(Intmap *map, unsigned long id, void *v) } void* -deletekey(Intmap *map, unsigned long id) -{ +deletekey(Intmap *map, unsigned long id) { Intlist **lf, *f; void *ov; diff --git a/message.c b/message.c @@ -1,25 +1,21 @@ -/* - * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> +/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> * See LICENSE file for license details. */ - +#include "ixp.h" #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "ixp.h" #define IXP_QIDSZ (sizeof(unsigned char) + sizeof(unsigned int)\ + sizeof(unsigned long long)) static unsigned short -sizeof_string(const char *s) -{ +sizeof_string(const char *s) { return sizeof(unsigned short) + strlen(s); } unsigned short -ixp_sizeof_stat(Stat * stat) -{ +ixp_sizeof_stat(Stat * stat) { return IXP_QIDSZ + 2 * sizeof(unsigned short) + 4 * sizeof(unsigned int) @@ -31,8 +27,7 @@ ixp_sizeof_stat(Stat * stat) } unsigned int -ixp_fcall2msg(void *msg, Fcall *fcall, unsigned int msglen) -{ +ixp_fcall2msg(void *msg, Fcall *fcall, unsigned int msglen) { unsigned int i = sizeof(unsigned char) + sizeof(unsigned short) + sizeof(unsigned int); int msize = msglen - i; @@ -127,18 +122,15 @@ ixp_fcall2msg(void *msg, Fcall *fcall, unsigned int msglen) ixp_pack_data(&p, &msize, fcall->stat, fcall->nstat); break; } - if(msize < 0) return 0; - msize = msglen - msize; ixp_pack_prefix(msg, msize, fcall->type, fcall->tag); return msize; } unsigned int -ixp_msg2fcall(Fcall *fcall, void *msg, unsigned int msglen) -{ +ixp_msg2fcall(Fcall *fcall, void *msg, unsigned int msglen) { int msize; unsigned int i, tsize; unsigned short len; @@ -236,7 +228,6 @@ ixp_msg2fcall(Fcall *fcall, void *msg, unsigned int msglen) ixp_unpack_data(&p, &msize, &fcall->stat, len); break; } - if(msize > 0) return tsize; return 0; diff --git a/request.c b/request.c @@ -1,13 +1,11 @@ -/* - * (C)opyright MMVI Kris Maglione <fbsdaemon at gmail dot com> +/* (C)opyright MMVI Kris Maglione <fbsdaemon at gmail dot com> * See LICENSE file for license details. */ - +#include "ixp.h" #include <stdlib.h> #include <stdio.h> #include <string.h> #include <sys/socket.h> -#include "ixp.h" static void ixp_handle_req(P9Req *r); @@ -71,8 +69,7 @@ destroyfid(P9Conn *pc, unsigned long fid) { } void -ixp_server_handle_fcall(IXPConn *c) -{ +ixp_server_handle_fcall(IXPConn *c) { Fcall fcall = {0}; P9Conn *pc = c->aux; P9Req *req; @@ -83,25 +80,20 @@ ixp_server_handle_fcall(IXPConn *c) goto Fail; if(!(msize = ixp_msg2fcall(&fcall, pc->buf, IXP_MAX_MSG))) goto Fail; - req = ixp_emallocz(sizeof(P9Req)); req->conn = pc; req->ifcall = fcall; pc->conn = c; - if(lookupkey(&pc->tagmap, fcall.tag)) return respond(req, Eduptag); - insertkey(&pc->tagmap, fcall.tag, req); return ixp_handle_req(req); - Fail: ixp_server_close_conn(c); } static void -ixp_handle_req(P9Req *r) -{ +ixp_handle_req(P9Req *r) { P9Conn *pc = r->conn; P9Srv *srv = pc->srv; @@ -282,7 +274,6 @@ respond(P9Req *r, char *error) { break; /* Still to be implemented: flush, wstat, auth */ } - r->ofcall.tag = r->ifcall.tag; if(!error) r->ofcall.type = r->ifcall.type + 1; @@ -290,10 +281,8 @@ respond(P9Req *r, char *error) { r->ofcall.type = RERROR; r->ofcall.ename = error; } - if(pc->conn) ixp_server_respond_fcall(pc->conn, &r->ofcall); - switch(r->ofcall.type) { case RSTAT: free(r->ofcall.stat); @@ -302,10 +291,8 @@ respond(P9Req *r, char *error) { free(r->ofcall.data); break; } - deletekey(&pc->tagmap, r->ifcall.tag);; free(r); - if(!pc->conn && pc->ref == 0) free_p9conn(pc); } @@ -318,7 +305,6 @@ ixp_void_request(void *t) { r = t; pc = r->conn; - tr = ixp_emallocz(sizeof(P9Req)); tr->conn = pc; tr->ifcall.type = TFLUSH; @@ -336,7 +322,6 @@ ixp_void_fid(void *t) { f = t; pc = f->conn; - tr = ixp_emallocz(sizeof(P9Req)); tr->fid = f; tr->conn = pc; @@ -382,13 +367,10 @@ serve_9pcon(IXPConn *c) { P9Conn *pc = ixp_emallocz(sizeof(P9Conn)); pc->srv = c->aux; - /* XXX */ pc->msize = 1024; pc->buf = ixp_emallocz(pc->msize); - initmap(&pc->tagmap, TAG_BUCKETS, &pc->taghash); initmap(&pc->fidmap, FID_BUCKETS, &pc->fidhash); - ixp_server_open_conn(c->srv, fd, pc, ixp_server_handle_fcall, ixp_cleanup_conn); } diff --git a/server.c b/server.c @@ -1,8 +1,7 @@ -/* - * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> +/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> * See LICENSE file for license details. */ - +#include "ixp.h" #include <errno.h> #include <fcntl.h> #include <signal.h> @@ -14,8 +13,6 @@ #include <sys/un.h> #include <unistd.h> -#include "ixp.h" - static unsigned char *msg[IXP_MAX_MSG]; IXPConn * @@ -34,8 +31,7 @@ ixp_server_open_conn(IXPServer *s, int fd, void *aux, } void -ixp_server_close_conn(IXPConn *c) -{ +ixp_server_close_conn(IXPConn *c) { IXPServer *s = c->srv; IXPConn **tc; for(tc=&s->conn; *tc && *tc != c; tc=&(*tc)->next); @@ -51,8 +47,7 @@ ixp_server_close_conn(IXPConn *c) } static void -prepare_select(IXPServer *s) -{ +prepare_select(IXPServer *s) { IXPConn **c; FD_ZERO(&s->rd); for(c=&s->conn; *c; *c && (c=&(*c)->next)) { @@ -64,8 +59,7 @@ prepare_select(IXPServer *s) } static void -handle_conns(IXPServer *s) -{ +handle_conns(IXPServer *s) { IXPConn *c, *n; for((c=s->conn) && (n=c->next); c; (c=n) && (n=c->next)) if(FD_ISSET(c->fd, &s->rd) && c->read) @@ -73,15 +67,13 @@ handle_conns(IXPServer *s) } char * -ixp_server_loop(IXPServer *s) -{ +ixp_server_loop(IXPServer *s) { int r; s->running = 1; /* main loop */ while(s->running) { prepare_select(s); - r = select(s->maxfd + 1, &s->rd, 0, 0, 0); if(r == -1 && errno == EINTR) continue; @@ -106,8 +98,7 @@ ixp_server_receive_fcall(IXPConn *c, Fcall *fcall) } int -ixp_server_respond_fcall(IXPConn *c, Fcall *fcall) -{ +ixp_server_respond_fcall(IXPConn *c, Fcall *fcall) { char *errstr; unsigned int msize = ixp_fcall2msg(msg, fcall, IXP_MAX_MSG); if(c->closed) @@ -120,8 +111,7 @@ ixp_server_respond_fcall(IXPConn *c, Fcall *fcall) } void -ixp_server_close(IXPServer *s) -{ +ixp_server_close(IXPServer *s) { IXPConn *c, *next; for(c=s->conn; c; c=next) { next=c->next; diff --git a/socket.c b/socket.c @@ -1,8 +1,7 @@ -/* - * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> +/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> * See LICENSE file for license details. */ - +#include "ixp.h" #include <signal.h> #include <stdio.h> #include <stdlib.h> @@ -15,11 +14,8 @@ #include <sys/un.h> #include <unistd.h> -#include "ixp.h" - static int -connect_unix_sock(char *address) -{ +connect_unix_sock(char *address) { int fd = 0; struct sockaddr_un addr = { 0 }; socklen_t su_len; @@ -28,7 +24,6 @@ connect_unix_sock(char *address) addr.sun_family = AF_UNIX; strncpy(addr.sun_path, address, sizeof(addr.sun_path)); su_len = sizeof(struct sockaddr) + strlen(addr.sun_path); - if((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) return -1; if(connect(fd, (struct sockaddr *) &addr, su_len)) { @@ -39,8 +34,7 @@ connect_unix_sock(char *address) } static int -connect_inet_sock(char *host) -{ +connect_inet_sock(char *host) { int fd = 0; struct sockaddr_in addr = { 0 }; struct hostent *hp; @@ -53,7 +47,6 @@ connect_inet_sock(char *host) port++; if(sscanf(port, "%d", &prt) != 1) return -1; - /* init */ if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) return -1; @@ -61,7 +54,6 @@ connect_inet_sock(char *host) addr.sin_family = AF_INET; addr.sin_port = htons(prt); bcopy(hp->h_addr, &addr.sin_addr, hp->h_length); - if(connect(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) { close(fd); return -1; @@ -70,14 +62,12 @@ connect_inet_sock(char *host) } int -ixp_connect_sock(char *address) -{ +ixp_connect_sock(char *address) { char *p; if((p = strchr(address, '!'))) { *p = 0; p++; - if(!strncmp(address, "unix", 5)) return connect_unix_sock(p); else if(!strncmp(address, "tcp", 4)) @@ -87,8 +77,7 @@ ixp_connect_sock(char *address) } static int -create_inet_sock(char *host, char **errstr) -{ +create_inet_sock(char *host, char **errstr) { int fd; struct sockaddr_in addr = { 0 }; struct hostent *hp; @@ -112,7 +101,6 @@ create_inet_sock(char *host, char **errstr) } addr.sin_family = AF_INET; addr.sin_port = htons(prt); - if(!strncmp(host, "*", 2)) addr.sin_addr.s_addr = htonl(INADDR_ANY); else if((hp = gethostbyname(host))) @@ -121,13 +109,11 @@ create_inet_sock(char *host, char **errstr) *errstr = "cannot translate hostname to an address"; return -1; } - if(bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0) { *errstr = "cannot bind socket"; close(fd); return -1; } - if(listen(fd, IXP_MAX_CACHE) < 0) { *errstr = "cannot listen on socket"; close(fd); @@ -137,8 +123,7 @@ create_inet_sock(char *host, char **errstr) } static int -create_unix_sock(char *file, char **errstr) -{ +create_unix_sock(char *file, char **errstr) { int fd; int yes = 1; struct sockaddr_un addr = { 0 }; @@ -158,7 +143,6 @@ create_unix_sock(char *file, char **errstr) addr.sun_family = AF_UNIX; strncpy(addr.sun_path, file, sizeof(addr.sun_path)); su_len = sizeof(struct sockaddr) + strlen(addr.sun_path); - unlink(file); /* remove old socket, if any */ if(bind(fd, (struct sockaddr *) &addr, su_len) < 0) { *errstr = "cannot bind socket"; @@ -166,7 +150,6 @@ create_unix_sock(char *file, char **errstr) return -1; } chmod(file, S_IRWXU); - if(listen(fd, IXP_MAX_CACHE) < 0) { *errstr = "cannot listen on socket"; close(fd); @@ -176,8 +159,7 @@ create_unix_sock(char *file, char **errstr) } int -ixp_create_sock(char *address, char **errstr) -{ +ixp_create_sock(char *address, char **errstr) { char *p = strchr(address, '!'); char *addr, *type; @@ -186,10 +168,8 @@ ixp_create_sock(char *address, char **errstr) return -1; } *p = 0; - addr = &p[1]; type = address; /* unix, inet */ - if(!strncmp(type, "unix", 5)) return create_unix_sock(addr, errstr); else if(!strncmp(type, "tcp", 4)) diff --git a/transport.c b/transport.c @@ -1,8 +1,7 @@ -/* - * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> +/* (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> * See LICENSE file for license details. */ - +#include "ixp.h" #include <errno.h> #include <fcntl.h> #include <stdio.h> @@ -13,11 +12,8 @@ #include <sys/un.h> #include <unistd.h> -#include "ixp.h" - unsigned int -ixp_send_message(int fd, void *msg, unsigned int msize, char **errstr) -{ +ixp_send_message(int fd, void *msg, unsigned int msize, char **errstr) { unsigned int num = 0; int r; @@ -36,8 +32,7 @@ ixp_send_message(int fd, void *msg, unsigned int msize, char **errstr) } static unsigned int -ixp_recv_data(int fd, void *msg, unsigned int msize, char **errstr) -{ +ixp_recv_data(int fd, void *msg, unsigned int msize, char **errstr) { unsigned int num = 0; int r = 0; @@ -56,8 +51,7 @@ ixp_recv_data(int fd, void *msg, unsigned int msize, char **errstr) } unsigned int -ixp_recv_message(int fd, void *msg, unsigned int msglen, char **errstr) -{ +ixp_recv_message(int fd, void *msg, unsigned int msglen, char **errstr) { unsigned int msize; /* receive header */ @@ -71,7 +65,7 @@ ixp_recv_message(int fd, void *msg, unsigned int msglen, char **errstr) } /* receive message */ if(ixp_recv_data(fd, msg, msize - sizeof(unsigned int), errstr) - != msize - sizeof(unsigned int)) + != msize - sizeof(unsigned int)) return 0; return msize; }