Changeset fab2746 in mainline
- Timestamp:
- 2015-04-08T21:25:30Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 99ea91b2
- Parents:
- ba0eac5
- Location:
- uspace
- Files:
-
- 3 added
- 38 deleted
- 37 edited
- 7 moved
Legend:
- Unmodified
- Added
- Removed
-
uspace/Makefile
rba0eac5 rfab2746 77 77 app/usbinfo \ 78 78 app/vuhid \ 79 app/netecho \80 app/netspeed \81 app/nettest1 \82 app/nettest2 \83 app/nettest3 \84 79 app/nic \ 85 80 app/ping \ … … 226 221 lib/draw \ 227 222 lib/math \ 228 lib/net \229 223 lib/nic \ 230 224 lib/ext4 \ -
uspace/Makefile.common
rba0eac5 rfab2746 141 141 LIBHOUND_PREFIX = $(LIB_PREFIX)/hound 142 142 LIBPCM_PREFIX = $(LIB_PREFIX)/pcm 143 LIBNET_PREFIX = $(LIB_PREFIX)/net144 143 LIBNIC_PREFIX = $(LIB_PREFIX)/nic 145 144 LIBMINIX_PREFIX = $(LIB_PREFIX)/minix … … 201 200 202 201 ifeq ($(CONFIG_OPTIMIZE_FOR_SIZE),y) 203 OPTIMIZATION = s204 else 205 OPTIMIZATION = 3202 OPTIMIZATION = 0 203 else 204 OPTIMIZATION = 0 206 205 endif 207 206 … … 262 261 263 262 ifeq ($(CONFIG_LINE_DEBUG),y) 264 GCC_CFLAGS += -g 263 GCC_CFLAGS += -ggdb 265 264 ICC_CFLAGS += -g 266 265 CLANG_CFLAGS += -g -
uspace/app/download/main.c
rba0eac5 rfab2746 36 36 */ 37 37 38 #include <errno.h> 38 39 #include <stdio.h> 39 40 #include <stdlib.h> … … 42 43 #include <task.h> 43 44 #include <macros.h> 44 45 #include <net/in.h>46 #include <net/inet.h>47 #include <net/socket.h>48 45 49 46 #include <http/http.h> -
uspace/app/nterm/conn.c
rba0eac5 rfab2746 38 38 #include <fibril.h> 39 39 #include <inet/dnsr.h> 40 #include < net/inet.h>41 #include < net/socket.h>40 #include <inet/endpoint.h> 41 #include <inet/tcp.h> 42 42 #include <stdio.h> 43 43 #include <stdlib.h> … … 48 48 #include "nterm.h" 49 49 50 static int conn_fd;51 static fid_t rcv_fid;50 static tcp_t *tcp; 51 static tcp_conn_t *conn; 52 52 53 53 #define RECV_BUF_SIZE 1024 54 54 static uint8_t recv_buf[RECV_BUF_SIZE]; 55 55 56 static int rcv_fibril(void *arg) 56 static void conn_conn_reset(tcp_conn_t *); 57 static void conn_data_avail(tcp_conn_t *); 58 59 static tcp_cb_t conn_cb = { 60 .conn_reset = conn_conn_reset, 61 .data_avail = conn_data_avail 62 }; 63 64 static void conn_conn_reset(tcp_conn_t *conn) 57 65 { 58 ssize_t nr; 66 printf("\n[Connection reset]\n"); 67 } 68 69 static void conn_data_avail(tcp_conn_t *conn) 70 { 71 int rc; 72 size_t nrecv; 59 73 60 74 while (true) { 61 nr = recv(conn_fd, recv_buf, RECV_BUF_SIZE, 0); 62 if (nr < 0) 75 rc = tcp_conn_recv(conn, recv_buf, RECV_BUF_SIZE, &nrecv); 76 if (rc != EOK) { 77 printf("\n[Receive error]\n"); 63 78 break; 79 } 64 80 65 nterm_received(recv_buf, nr); 81 nterm_received(recv_buf, nrecv); 82 83 if (nrecv != RECV_BUF_SIZE) 84 break; 66 85 } 67 68 if (nr == ENOTCONN)69 printf("\n[Other side has closed the connection]\n");70 else71 printf("'\n[Receive errror (%s)]\n", str_error(nr));72 73 exit(0);74 return 0;75 86 } 76 87 77 88 int conn_open(const char *host, const char *port_s) 78 89 { 79 int conn_fd = -1; 80 struct sockaddr *saddr = NULL; 81 socklen_t saddrlen; 82 90 inet_ep2_t epp; 91 83 92 /* Interpret as address */ 84 93 inet_addr_t iaddr; 85 94 int rc = inet_addr_parse(host, &iaddr); 86 95 87 96 if (rc != EOK) { 88 97 /* Interpret as a host name */ 89 98 dnsr_hostinfo_t *hinfo = NULL; 90 99 rc = dnsr_name2host(host, &hinfo, ip_any); 91 100 92 101 if (rc != EOK) { 93 102 printf("Error resolving host '%s'.\n", host); 94 103 goto error; 95 104 } 96 105 97 106 iaddr = hinfo->addr; 98 107 } 99 108 100 109 char *endptr; 101 110 uint16_t port = strtol(port_s, &endptr, 10); … … 104 113 goto error; 105 114 } 106 107 rc = inet_addr_sockaddr(&iaddr, port, &saddr, &saddrlen); 108 if (rc != EOK) { 109 assert(rc == ENOMEM); 110 printf("Out of memory.\n"); 111 return ENOMEM; 112 } 113 115 116 inet_ep2_init(&epp); 117 epp.remote.addr = iaddr; 118 epp.remote.port = port; 119 114 120 printf("Connecting to host %s port %u\n", host, port); 115 116 conn_fd = socket(saddr->sa_family, SOCK_STREAM, 0); 117 if (conn_fd < 0) 118 goto error; 119 120 rc = connect(conn_fd, saddr, saddrlen); 121 122 rc = tcp_create(&tcp); 121 123 if (rc != EOK) 122 124 goto error; 123 124 rc v_fid = fibril_create(rcv_fibril, NULL);125 if (rc v_fid == 0)125 126 rc = tcp_conn_create(tcp, &epp, &conn_cb, NULL, &conn); 127 if (rc != EOK) 126 128 goto error; 127 128 fibril_add_ready(rcv_fid); 129 130 free(saddr); 129 130 rc = tcp_conn_wait_connected(conn); 131 if (rc != EOK) 132 goto error; 133 131 134 return EOK; 132 135 error: 133 if (conn_fd >= 0) { 134 closesocket(conn_fd); 135 conn_fd = -1; 136 } 137 free(saddr); 138 136 tcp_conn_destroy(conn); 137 tcp_destroy(tcp); 138 139 139 return EIO; 140 140 } … … 142 142 int conn_send(void *data, size_t size) 143 143 { 144 int rc = send(conn_fd, data, size, 0);144 int rc = tcp_conn_send(conn, data, size); 145 145 if (rc != EOK) 146 146 return EIO; 147 147 148 148 return EOK; 149 149 } -
uspace/app/websrv/websrv.c
rba0eac5 rfab2746 44 44 #include <task.h> 45 45 46 #include < net/in.h>47 #include < net/inet.h>48 #include < net/socket.h>46 #include <inet/addr.h> 47 #include <inet/endpoint.h> 48 #include <inet/tcp.h> 49 49 50 50 #include <arg_parse.h> … … 56 56 57 57 #define DEFAULT_PORT 8080 58 #define BACKLOG_SIZE 359 58 60 59 #define WEB_ROOT "/data/web" … … 62 61 /** Buffer for receiving the request. */ 63 62 #define BUFFER_SIZE 1024 63 64 static void websrv_new_conn(tcp_listener_t *, tcp_conn_t *); 65 66 static tcp_listen_cb_t listen_cb = { 67 .new_conn = websrv_new_conn 68 }; 69 70 static tcp_cb_t conn_cb = { 71 .connected = NULL 72 }; 64 73 65 74 static uint16_t port = DEFAULT_PORT; … … 122 131 123 132 /** Receive one character (with buffering) */ 124 static int recv_char(int fd, char *c) 125 { 133 static int recv_char(tcp_conn_t *conn, char *c) 134 { 135 size_t nrecv; 136 int rc; 137 126 138 if (rbuf_out == rbuf_in) { 127 139 rbuf_out = 0; 128 140 rbuf_in = 0; 129 141 130 ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0);131 if (rc <= 0) {132 fprintf(stderr, "recv() failed (% zd)\n", rc);142 rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv); 143 if (rc != EOK) { 144 fprintf(stderr, "recv() failed (%d)\n", rc); 133 145 return rc; 134 146 } 135 147 136 rbuf_in = rc;148 rbuf_in = nrecv; 137 149 } 138 150 … … 142 154 143 155 /** Receive one line with length limit */ 144 static int recv_line( int fd)156 static int recv_line(tcp_conn_t *conn) 145 157 { 146 158 char *bp = lbuf; … … 149 161 while (bp < lbuf + BUFFER_SIZE) { 150 162 char prev = c; 151 int rc = recv_char( fd, &c);163 int rc = recv_char(conn, &c); 152 164 153 165 if (rc != EOK) … … 187 199 } 188 200 189 static int send_response( int conn_sd, const char *msg)201 static int send_response(tcp_conn_t *conn, const char *msg) 190 202 { 191 203 size_t response_size = str_size(msg); … … 194 206 fprintf(stderr, "Sending response\n"); 195 207 196 ssize_t rc = send(conn_sd, (void *) msg, response_size, 0);197 if (rc < 0) {198 fprintf(stderr, " send() failed\n");208 int rc = tcp_conn_send(conn, (void *) msg, response_size); 209 if (rc != EOK) { 210 fprintf(stderr, "tcp_conn_send() failed\n"); 199 211 return rc; 200 212 } … … 203 215 } 204 216 205 static int uri_get(const char *uri, int conn_sd)217 static int uri_get(const char *uri, tcp_conn_t *conn) 206 218 { 207 219 if (str_cmp(uri, "/") == 0) … … 215 227 int fd = open(fname, O_RDONLY); 216 228 if (fd < 0) { 217 rc = send_response(conn _sd, msg_not_found);229 rc = send_response(conn, msg_not_found); 218 230 free(fname); 219 231 return rc; … … 222 234 free(fname); 223 235 224 rc = send_response(conn _sd, msg_ok);236 rc = send_response(conn, msg_ok); 225 237 if (rc != EOK) 226 238 return rc; … … 236 248 } 237 249 238 rc = send(conn_sd, fbuf, nr, 0);239 if (rc < 0) {240 fprintf(stderr, " send() failed\n");250 rc = tcp_conn_send(conn, fbuf, nr); 251 if (rc != EOK) { 252 fprintf(stderr, "tcp_conn_send() failed\n"); 241 253 close(fd); 242 254 return rc; … … 249 261 } 250 262 251 static int req_process( int conn_sd)252 { 253 int rc = recv_line(conn _sd);263 static int req_process(tcp_conn_t *conn) 264 { 265 int rc = recv_line(conn); 254 266 if (rc != EOK) { 255 267 fprintf(stderr, "recv_line() failed\n"); … … 261 273 262 274 if (str_lcmp(lbuf, "GET ", 4) != 0) { 263 rc = send_response(conn _sd, msg_not_implemented);275 rc = send_response(conn, msg_not_implemented); 264 276 return rc; 265 277 } … … 277 289 278 290 if (!uri_is_valid(uri)) { 279 rc = send_response(conn _sd, msg_bad_request);280 return rc; 281 } 282 283 return uri_get(uri, conn _sd);291 rc = send_response(conn, msg_bad_request); 292 return rc; 293 } 294 295 return uri_get(uri, conn); 284 296 } 285 297 … … 346 358 } 347 359 360 static void websrv_new_conn(tcp_listener_t *lst, tcp_conn_t *conn) 361 { 362 int rc; 363 364 if (verbose) 365 fprintf(stderr, "New connection, waiting for request\n"); 366 367 rbuf_out = 0; 368 rbuf_in = 0; 369 370 rc = req_process(conn); 371 if (rc != EOK) { 372 fprintf(stderr, "Error processing request (%s)\n", 373 str_error(rc)); 374 } 375 } 376 348 377 int main(int argc, char *argv[]) 349 378 { 379 inet_ep_t ep; 380 tcp_listener_t *lst; 381 tcp_t *tcp; 382 int rc; 383 350 384 /* Parse command line arguments */ 351 385 for (int i = 1; i < argc; i++) { 352 386 if (argv[i][0] == '-') { 353 intrc = parse_option(argc, argv, &i);387 rc = parse_option(argc, argv, &i); 354 388 if (rc != EOK) 355 389 return rc; … … 360 394 } 361 395 362 struct sockaddr_in addr; 363 364 addr.sin_family = AF_INET; 365 addr.sin_port = htons(port); 366 367 int rc = inet_pton(AF_INET, "127.0.0.1", (void *) 368 &addr.sin_addr.s_addr); 396 printf("%s: HelenOS web server\n", NAME); 397 398 if (verbose) 399 fprintf(stderr, "Creating listener\n"); 400 401 inet_ep_init(&ep); 402 ep.port = port; 403 404 rc = tcp_create(&tcp); 369 405 if (rc != EOK) { 370 fprintf(stderr, "Error parsing network address (%s)\n", 371 str_error(rc)); 406 fprintf(stderr, "Error initializing TCP.\n"); 372 407 return 1; 373 408 } 374 375 printf("%s: HelenOS web server\n", NAME); 376 377 if (verbose) 378 fprintf(stderr, "Creating socket\n"); 379 380 int listen_sd = socket(PF_INET, SOCK_STREAM, 0); 381 if (listen_sd < 0) { 382 fprintf(stderr, "Error creating listening socket (%s)\n", 383 str_error(listen_sd)); 409 410 rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL, 411 &lst); 412 if (rc != EOK) { 413 fprintf(stderr, "Error creating listener.\n"); 384 414 return 2; 385 }386 387 rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr));388 if (rc != EOK) {389 fprintf(stderr, "Error binding socket (%s)\n",390 str_error(rc));391 return 3;392 }393 394 rc = listen(listen_sd, BACKLOG_SIZE);395 if (rc != EOK) {396 fprintf(stderr, "listen() failed (%s)\n", str_error(rc));397 return 4;398 415 } 399 416 … … 402 419 403 420 task_retval(0); 404 405 while (true) { 406 struct sockaddr_in raddr; 407 socklen_t raddr_len = sizeof(raddr); 408 int conn_sd = accept(listen_sd, (struct sockaddr *) &raddr, 409 &raddr_len); 410 411 if (conn_sd < 0) { 412 fprintf(stderr, "accept() failed (%s)\n", str_error(rc)); 413 continue; 414 } 415 416 if (verbose) { 417 fprintf(stderr, "Connection accepted (sd=%d), " 418 "waiting for request\n", conn_sd); 419 } 420 421 rbuf_out = 0; 422 rbuf_in = 0; 423 424 rc = req_process(conn_sd); 425 if (rc != EOK) 426 fprintf(stderr, "Error processing request (%s)\n", 427 str_error(rc)); 428 429 rc = closesocket(conn_sd); 430 if (rc != EOK) { 431 fprintf(stderr, "Error closing connection socket (%s)\n", 432 str_error(rc)); 433 closesocket(listen_sd); 434 return 5; 435 } 436 437 if (verbose) 438 fprintf(stderr, "Connection closed\n"); 439 } 421 async_manager(); 440 422 441 423 /* Not reached */ -
uspace/drv/bus/usb/usbhub/usbhub.c
rba0eac5 rfab2746 192 192 if (!tries--) { 193 193 usb_log_error("Can't remove hub, still running.\n"); 194 return E INPROGRESS;194 return EBUSY; 195 195 } 196 196 } -
uspace/lib/c/Makefile
rba0eac5 rfab2746 97 97 generic/futex.c \ 98 98 generic/inet/addr.c \ 99 generic/inet/endpoint.c \ 100 generic/inet/tcp.c \ 101 generic/inet/udp.c \ 99 102 generic/inet.c \ 100 103 generic/inetcfg.c \ … … 137 140 generic/adt/list.c \ 138 141 generic/adt/hash_table.c \ 139 generic/adt/dynamic_fifo.c \140 generic/adt/char_map.c \141 142 generic/adt/prodcons.c \ 142 143 generic/time.c \ … … 145 146 generic/vfs/vfs.c \ 146 147 generic/vfs/canonify.c \ 147 generic/net/inet.c \148 generic/net/socket_client.c \149 generic/net/socket_parse.c \150 148 generic/setjmp.c \ 151 149 generic/stack.c \ -
uspace/lib/c/generic/inet/addr.c
rba0eac5 rfab2746 36 36 #include <errno.h> 37 37 #include <unistd.h> 38 #include <net/socket_codes.h>39 38 #include <inet/addr.h> 40 #include <net/inet.h>41 39 #include <stdio.h> 42 40 #include <malloc.h> … … 44 42 45 43 #define INET_PREFIXSTRSIZE 5 44 45 #define INET6_ADDRSTRLEN (8 * 4 + 7 + 1) 46 46 47 47 #if !(defined(__BE__) ^ defined(__LE__)) … … 178 178 naddr->addr6[15] = h & 0xff; 179 179 naddr->prefix = prefix; 180 }181 182 /** Determine address version.183 *184 * @param text Address in common notation.185 * @param af Place to store address version.186 *187 * @return EOK on success, EINVAL if input is not in valid format.188 *189 */190 static int inet_addr_version(const char *text, ip_ver_t *ver)191 {192 char *dot = str_chr(text, '.');193 if (dot != NULL) {194 *ver = ip_v4;195 return EOK;196 }197 198 char *collon = str_chr(text, ':');199 if (collon != NULL) {200 *ver = ip_v6;201 return EOK;202 }203 204 return EINVAL;205 }206 207 static int ipver_af(ip_ver_t ver)208 {209 switch (ver) {210 case ip_any:211 return AF_NONE;212 case ip_v4:213 return AF_INET;214 case ip_v6:215 return AF_INET6;216 default:217 assert(false);218 return EINVAL;219 }220 }221 222 ip_ver_t ipver_from_af(int af)223 {224 switch (af) {225 case AF_NONE:226 return ip_any;227 case AF_INET:228 return ip_v4;229 case AF_INET6:230 return ip_v6;231 default:232 assert(false);233 return EINVAL;234 }235 180 } 236 181 … … 343 288 } 344 289 290 static int inet_addr_parse_v4(const char *str, inet_addr_t *raddr, 291 int *prefix) 292 { 293 uint32_t a = 0; 294 uint8_t b; 295 char *cur = (char *)str; 296 size_t i = 0; 297 298 while (i < 4) { 299 int rc = str_uint8_t(cur, (const char **)&cur, 10, false, &b); 300 if (rc != EOK) 301 return rc; 302 303 a = (a << 8) + b; 304 305 i++; 306 307 if (*cur == 0) 308 break; 309 310 if (*cur != '.') 311 return EINVAL; 312 313 if (i < 4) 314 cur++; 315 } 316 317 if (prefix != NULL) { 318 *prefix = strtoul(cur, &cur, 10); 319 if (*prefix > 32) 320 return EINVAL; 321 } 322 323 if (i != 4 || (*cur != 0)) 324 return EINVAL; 325 326 raddr->version = ip_v4; 327 raddr->addr = a; 328 329 return EOK; 330 } 331 332 static int inet_addr_parse_v6(const char *str, inet_addr_t *raddr, int *prefix) 333 { 334 /* XXX */ 335 return EINVAL; 336 } 337 345 338 /** Parse node address. 346 339 * … … 353 346 int inet_addr_parse(const char *text, inet_addr_t *addr) 354 347 { 355 int rc = inet_addr_version(text, &addr->version); 356 if (rc != EOK) 357 return rc; 358 359 uint8_t buf[16]; 360 rc = inet_pton(ipver_af(addr->version), text, buf); 361 if (rc != EOK) 362 return rc; 363 364 switch (addr->version) { 365 case ip_v4: 366 addr->addr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | 367 buf[3]; 368 break; 369 case ip_v6: 370 memcpy(addr->addr6, buf, 16); 371 break; 372 default: 373 return EINVAL; 374 } 375 376 return EOK; 348 int rc; 349 350 rc = inet_addr_parse_v4(text, addr, NULL); 351 if (rc == EOK) 352 return EOK; 353 354 rc = inet_addr_parse_v6(text, addr, NULL); 355 if (rc == EOK) 356 return EOK; 357 358 return EINVAL; 377 359 } 378 360 … … 387 369 int inet_naddr_parse(const char *text, inet_naddr_t *naddr) 388 370 { 389 char *slash = str_chr(text, '/'); 390 if (slash == NULL) 391 return EINVAL; 392 393 *slash = 0; 394 395 int rc = inet_addr_version(text, &naddr->version); 396 if (rc != EOK) 397 return rc; 398 399 uint8_t buf[16]; 400 rc = inet_pton(ipver_af(naddr->version), text, buf); 401 *slash = '/'; 402 403 if (rc != EOK) 404 return rc; 405 406 slash++; 407 uint8_t prefix; 408 409 switch (naddr->version) { 410 case ip_v4: 411 prefix = strtoul(slash, &slash, 10); 412 if (prefix > 32) 371 int rc; 372 inet_addr_t addr; 373 int prefix; 374 375 rc = inet_addr_parse_v4(text, &addr, &prefix); 376 if (rc == EOK) { 377 inet_addr_naddr(&addr, prefix, naddr); 378 return EOK; 379 } 380 381 rc = inet_addr_parse_v6(text, &addr, &prefix); 382 if (rc == EOK) { 383 inet_addr_naddr(&addr, prefix, naddr); 384 return EOK; 385 } 386 387 return EINVAL; 388 } 389 390 static int inet_ntop6(const uint8_t *data, char *address, size_t length) 391 { 392 /* Check output buffer size */ 393 if (length < INET6_ADDRSTRLEN) 394 return ENOMEM; 395 396 /* Find the longest zero subsequence */ 397 398 uint16_t zeroes[8]; 399 uint16_t bioctets[8]; 400 401 for (size_t i = 8; i > 0; i--) { 402 size_t j = i - 1; 403 404 bioctets[j] = (data[j << 1] << 8) | data[(j << 1) + 1]; 405 406 if (bioctets[j] == 0) { 407 zeroes[j] = 1; 408 if (j < 7) 409 zeroes[j] += zeroes[j + 1]; 410 } else 411 zeroes[j] = 0; 412 } 413 414 size_t wildcard_pos = (size_t) -1; 415 size_t wildcard_size = 0; 416 417 for (size_t i = 0; i < 8; i++) { 418 if (zeroes[i] > wildcard_size) { 419 wildcard_pos = i; 420 wildcard_size = zeroes[i]; 421 } 422 } 423 424 char *cur = address; 425 size_t rest = length; 426 bool tail_zero = false; 427 int ret; 428 429 for (size_t i = 0; i < 8; i++) { 430 if ((i == wildcard_pos) && (wildcard_size > 1)) { 431 ret = snprintf(cur, rest, ":"); 432 i += wildcard_size - 1; 433 tail_zero = true; 434 } else if (i == 0) { 435 ret = snprintf(cur, rest, "%" PRIx16, bioctets[i]); 436 tail_zero = false; 437 } else { 438 ret = snprintf(cur, rest, ":%" PRIx16, bioctets[i]); 439 tail_zero = false; 440 } 441 442 if (ret < 0) 413 443 return EINVAL; 414 444 415 naddr->addr = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | 416 buf[3]; 417 naddr->prefix = prefix; 418 419 break; 420 case ip_v6: 421 prefix = strtoul(slash, &slash, 10); 422 if (prefix > 128) 445 cur += ret; 446 rest -= ret; 447 } 448 449 if (tail_zero) { 450 ret = snprintf(cur, rest, ":"); 451 if (ret < 0) 423 452 return EINVAL; 424 425 memcpy(naddr->addr6, buf, 16);426 naddr->prefix = prefix;427 428 break;429 default:430 return ENOTSUP;431 453 } 432 454 433 455 return EOK; 434 456 } 457 435 458 436 459 /** Format node address. … … 462 485 return ENOMEM; 463 486 464 return inet_ntop (AF_INET6,addr->addr6, *bufp, INET6_ADDRSTRLEN);487 return inet_ntop6(addr->addr6, *bufp, INET6_ADDRSTRLEN); 465 488 default: 489 asprintf(bufp, "<ver=%d>", addr->version); 466 490 return ENOTSUP; 467 491 } … … 503 527 return ENOMEM; 504 528 505 rc = inet_ntop (AF_INET6,naddr->addr6, *bufp,529 rc = inet_ntop6(naddr->addr6, *bufp, 506 530 INET6_ADDRSTRLEN + INET_PREFIXSTRSIZE); 507 531 if (rc != EOK) { … … 586 610 } 587 611 588 void inet_sockaddr_in_addr(const sockaddr_in_t *sockaddr_in, inet_addr_t *addr)589 {590 addr->version = ip_v4;591 addr->addr = uint32_t_be2host(sockaddr_in->sin_addr.s_addr);592 }593 594 612 void inet_addr_set6(addr128_t v6, inet_addr_t *addr) 595 613 { … … 605 623 } 606 624 607 void inet_sockaddr_in6_addr(const sockaddr_in6_t *sockaddr_in6,608 inet_addr_t *addr)609 {610 addr->version = ip_v6;611 addr128_t_be2host(sockaddr_in6->sin6_addr.s6_addr, addr->addr6);612 }613 614 uint16_t inet_addr_sockaddr_in(const inet_addr_t *addr,615 sockaddr_in_t *sockaddr_in, sockaddr_in6_t *sockaddr_in6)616 {617 switch (addr->version) {618 case ip_v4:619 if (sockaddr_in != NULL) {620 sockaddr_in->sin_family = AF_INET;621 sockaddr_in->sin_addr.s_addr = host2uint32_t_be(addr->addr);622 }623 break;624 case ip_v6:625 if (sockaddr_in6 != NULL) {626 sockaddr_in6->sin6_family = AF_INET6;627 host2addr128_t_be(addr->addr6, sockaddr_in6->sin6_addr.s6_addr);628 }629 break;630 default:631 assert(false);632 break;633 }634 635 return ipver_af(addr->version);636 }637 638 int inet_addr_sockaddr(const inet_addr_t *addr, uint16_t port,639 sockaddr_t **nsockaddr, socklen_t *naddrlen)640 {641 sockaddr_in_t *sa4;642 sockaddr_in6_t *sa6;643 644 switch (addr->version) {645 case ip_v4:646 sa4 = calloc(1, sizeof(sockaddr_in_t));647 if (sa4 == NULL)648 return ENOMEM;649 650 sa4->sin_family = AF_INET;651 sa4->sin_port = host2uint16_t_be(port);652 sa4->sin_addr.s_addr = host2uint32_t_be(addr->addr);653 if (nsockaddr != NULL)654 *nsockaddr = (sockaddr_t *)sa4;655 if (naddrlen != NULL)656 *naddrlen = sizeof(*sa4);657 break;658 case ip_v6:659 sa6 = calloc(1, sizeof(sockaddr_in6_t));660 if (sa6 == NULL)661 return ENOMEM;662 663 sa6->sin6_family = AF_INET6;664 sa6->sin6_port = host2uint16_t_be(port);665 host2addr128_t_be(addr->addr6, sa6->sin6_addr.s6_addr);666 if (nsockaddr != NULL)667 *nsockaddr = (sockaddr_t *)sa6;668 if (naddrlen != NULL)669 *naddrlen = sizeof(*sa6);670 break;671 default:672 assert(false);673 break;674 }675 676 return EOK;677 }678 679 625 /** @} 680 626 */ -
uspace/lib/c/generic/inet/endpoint.c
rba0eac5 rfab2746 1 1 /* 2 * Copyright (c) 201 1Jiri Svoboda2 * Copyright (c) 2015 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 /** @addtogroup tcp29 /** @addtogroup libc 30 30 * @{ 31 31 */ 32 /** @file Socket provider32 /** @file Internet endpoint 33 33 */ 34 34 35 #i fndef SOCK_H36 # define SOCK_H35 #include <inet/endpoint.h> 36 #include <mem.h> 37 37 38 #include <async.h> 38 void inet_ep_init(inet_ep_t *ep) 39 { 40 memset(ep, 0, sizeof(*ep)); 41 } 39 42 40 extern int tcp_sock_init(void); 41 42 #endif 43 void inet_ep2_init(inet_ep2_t *ep2) 44 { 45 memset(ep2, 0, sizeof(*ep2)); 46 } 43 47 44 48 /** @} -
uspace/lib/c/generic/inet/tcp.c
rba0eac5 rfab2746 1 1 /* 2 * Copyright (c) 20 09 Lukas Mejdrech2 * Copyright (c) 2015 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 28 28 29 29 /** @addtogroup libc 30 * @{ 30 * @{ 31 */ 32 /** @file UDP API 31 33 */ 32 34 33 /** @file 34 * Socket application program interface (API). 35 * This is a part of the network application library. 36 * Based on the BSD socket interface. 37 */ 35 #include <errno.h> 36 #include <inet/endpoint.h> 37 #include <inet/tcp.h> 38 38 39 #ifndef LIBC_SOCKET_H_ 40 #define LIBC_SOCKET_H_ 39 int tcp_create(tcp_t **rtcp) 40 { 41 return 0; 42 } 41 43 42 #include <net/socket_codes.h> 43 #include <net/in.h> 44 #include <net/in6.h> 45 #include <net/inet.h> 46 #include <errno.h> 47 #include <byteorder.h> 44 void tcp_destroy(tcp_t *tcp) 45 { 46 } 48 47 49 /** @name Socket application programming interface 50 */ 51 /*@{*/ 48 int tcp_conn_create(tcp_t *tcp, inet_ep2_t *epp, tcp_cb_t *cb, void *arg, 49 tcp_conn_t **rconn) 50 { 51 return 0; 52 } 52 53 53 extern int socket(int, int, int); 54 extern int bind(int, const struct sockaddr *, socklen_t); 55 extern int listen(int, int); 56 extern int accept(int, struct sockaddr *, socklen_t *); 57 extern int connect(int, const struct sockaddr *, socklen_t); 58 extern int closesocket(int); 59 extern int send(int, const void *, size_t, int); 60 extern int sendto(int, const void *, size_t, int, const struct sockaddr *, 61 socklen_t); 62 extern ssize_t recv(int, void *, size_t, int); 63 extern ssize_t recvfrom(int, void *, size_t, int, struct sockaddr *, socklen_t *); 64 extern int getsockopt(int, int, int, void *, size_t *); 65 extern int setsockopt(int, int, int, const void *, size_t); 54 void tcp_conn_destroy(tcp_conn_t *conn) 55 { 56 } 66 57 67 /*@}*/ 58 void *tcp_conn_userptr(tcp_conn_t *conn) 59 { 60 return NULL; 61 } 68 62 69 #endif 63 int tcp_listener_create(tcp_t *tcp, inet_ep_t *ep, tcp_listen_cb_t *lcb, 64 void *larg, tcp_cb_t *cb, void *arg, tcp_listener_t **rlst) 65 { 66 return 0; 67 } 68 69 void tcp_listener_destroy(tcp_listener_t *lst) 70 { 71 } 72 73 void *tcp_listener_userptr(tcp_listener_t *lst) 74 { 75 return NULL; 76 } 77 78 79 int tcp_conn_wait_connected(tcp_conn_t *conn) 80 { 81 return 0; 82 } 83 84 int tcp_conn_send(tcp_conn_t *conn, const void *data, size_t bytes) 85 { 86 return 0; 87 } 88 89 int tcp_conn_send_fin(tcp_conn_t *conn) 90 { 91 return 0; 92 } 93 94 int tcp_conn_push(tcp_conn_t *conn) 95 { 96 return 0; 97 } 98 99 void tcp_conn_reset(tcp_conn_t *conn) 100 { 101 } 102 103 104 int tcp_conn_recv(tcp_conn_t *conn, void *buf, size_t bsize, size_t *nrecv) 105 { 106 return 0; 107 } 108 109 int tcp_conn_recv_wait(tcp_conn_t *conn, void *buf, size_t bsize, size_t *nrecv) 110 { 111 return 0; 112 } 113 70 114 71 115 /** @} -
uspace/lib/c/include/errno.h
rba0eac5 rfab2746 68 68 #define ENAK (-303) 69 69 70 /** An API function is called while another blocking function is in progress. */71 #define EINPROGRESS (-10036)72 73 /** The socket identifier is not valid. */74 #define ENOTSOCK (-10038)75 76 /** The destination address required. */77 #define EDESTADDRREQ (-10039)78 79 /** Protocol is not supported. */80 #define EPROTONOSUPPORT (-10043)81 82 /** Socket type is not supported. */83 #define ESOCKTNOSUPPORT (-10044)84 85 /** Protocol family is not supported. */86 #define EPFNOSUPPORT (-10046)87 88 /** Address family is not supported. */89 #define EAFNOSUPPORT (-10047)90 91 /** Address is already in use. */92 #define EADDRINUSE (-10048)93 94 /** The socket is not connected or bound. */95 #define ENOTCONN (-10057)96 97 #define ECONNREFUSED (-10058)98 99 #define ECONNABORTED (-10059)100 101 70 /** The requested operation was not performed. Try again later. */ 102 71 #define EAGAIN (-11002) 103 104 /** No data. */105 #define NO_DATA (-11004)106 72 107 73 #endif -
uspace/lib/c/include/inet/addr.h
rba0eac5 rfab2746 37 37 38 38 #include <stdint.h> 39 #include <net/in.h>40 #include <net/in6.h>41 #include <net/socket.h>42 39 43 40 typedef uint32_t addr32_t; … … 126 123 extern void inet_addr_set(addr32_t, inet_addr_t *); 127 124 extern void inet_naddr_set(addr32_t, uint8_t, inet_naddr_t *); 128 extern void inet_sockaddr_in_addr(const sockaddr_in_t *, inet_addr_t *);129 125 130 126 extern void inet_addr_set6(addr128_t, inet_addr_t *); 131 127 extern void inet_naddr_set6(addr128_t, uint8_t, inet_naddr_t *); 132 extern void inet_sockaddr_in6_addr(const sockaddr_in6_t *, inet_addr_t *);133 134 extern uint16_t inet_addr_sockaddr_in(const inet_addr_t *, sockaddr_in_t *,135 sockaddr_in6_t *);136 137 extern ip_ver_t ipver_from_af(int af);138 extern int inet_addr_sockaddr(const inet_addr_t *, uint16_t, sockaddr_t **,139 socklen_t *);140 128 141 129 #endif -
uspace/lib/c/include/inet/endpoint.h
rba0eac5 rfab2746 1 1 /* 2 * Copyright (c) 20 09 Lukas Mejdrech2 * Copyright (c) 2015 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 28 28 29 29 /** @addtogroup libc 30 * @{ 30 * @{ 31 */ 32 /** @file 31 33 */ 32 34 33 /** @file 34 * Internet protocol numbers according to the on-line IANA - Assigned Protocol 35 * numbers: 36 * 37 * http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xml 38 */ 35 #ifndef LIBC_INET_ASSOC_H_ 36 #define LIBC_INET_ASSOC_H_ 39 37 40 #ifndef LIBC_IP_PROTOCOLS_H_ 41 #define LIBC_IP_PROTOCOLS_H_ 38 #include <stdint.h> 39 #include <inet/addr.h> 40 #include <loc.h> 42 41 43 /** @name IP protocols definitions */ 44 /*@{*/ 42 /** Internet endpoint (address-port pair), a.k.a. socket */ 43 typedef struct { 44 inet_addr_t addr; 45 uint16_t port; 46 } inet_ep_t; 45 47 46 #define IPPROTO_ICMP 1 47 #define IPPROTO_TCP 6 48 #define IPPROTO_UDP 17 49 #define IPPROTO_ICMPV6 58 48 /** Internet endpoint pair */ 49 typedef struct { 50 service_id_t local_link; 51 inet_ep_t local; 52 inet_ep_t remote; 53 } inet_ep2_t; 50 54 51 /*@}*/ 55 extern void inet_ep_init(inet_ep_t *); 56 extern void inet_ep2_init(inet_ep2_t *); 52 57 53 58 #endif -
uspace/lib/c/include/inet/tcp.h
rba0eac5 rfab2746 1 1 /* 2 * Copyright (c) 20 09 Lukas Mejdrech2 * Copyright (c) 2015 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 28 28 29 29 /** @addtogroup libc 30 * @{ 30 * @{ 31 */ 32 /** @file 31 33 */ 32 34 33 /** @file 34 * Socket codes and definitions. 35 * This is a part of the network application library. 36 */ 35 #ifndef LIBC_INET_TCP_H_ 36 #define LIBC_INET_TCP_H_ 37 37 38 #ifndef LIBC_SOCKET_CODES_H_ 39 #define LIBC_SOCKET_CODES_H_ 38 #include <inet/addr.h> 39 #include <inet/endpoint.h> 40 #include <inet/inet.h> 40 41 41 #include <sys/types.h> 42 typedef struct { 43 } tcp_conn_t; 42 44 43 /** @name Address families definitions */ 44 /*@{*/ 45 typedef struct { 46 } tcp_listener_t; 45 47 46 enum { 47 AF_NONE = 0, 48 AF_INET, /* IPv4 address */ 49 AF_INET6 /* IPv6 address */ 50 }; 48 typedef struct { 49 void (*connected)(tcp_conn_t *); 50 void (*conn_failed)(tcp_conn_t *); 51 void (*conn_reset)(tcp_conn_t *); 52 void (*data_avail)(tcp_conn_t *); 53 void (*urg_data)(tcp_conn_t *); 54 } tcp_cb_t; 51 55 52 /*@}*/ 56 typedef struct { 57 void (*new_conn)(tcp_listener_t *, tcp_conn_t *); 58 } tcp_listen_cb_t; 53 59 54 /** @name Protocol families definitions 55 * Same as address families. 56 */ 57 /*@{*/ 60 typedef struct { 61 } tcp_t; 58 62 59 #define PF_INET AF_INET 60 #define PF_INET6 AF_INET6 63 extern int tcp_create(tcp_t **); 64 extern void tcp_destroy(tcp_t *); 65 extern int tcp_conn_create(tcp_t *, inet_ep2_t *, tcp_cb_t *, void *, 66 tcp_conn_t **); 67 extern void tcp_conn_destroy(tcp_conn_t *); 68 extern void *tcp_conn_userptr(tcp_conn_t *); 69 extern int tcp_listener_create(tcp_t *, inet_ep_t *, tcp_listen_cb_t *, void *, 70 tcp_cb_t *, void *, tcp_listener_t **); 71 extern void tcp_listener_destroy(tcp_listener_t *); 72 extern void *tcp_listener_userptr(tcp_listener_t *); 61 73 62 /*@}*/ 74 extern int tcp_conn_wait_connected(tcp_conn_t *); 75 extern int tcp_conn_send(tcp_conn_t *, const void *, size_t); 76 extern int tcp_conn_send_fin(tcp_conn_t *); 77 extern int tcp_conn_push(tcp_conn_t *); 78 extern void tcp_conn_reset(tcp_conn_t *); 63 79 64 /** Socket types. */ 65 typedef enum sock_type { 66 /** Stream (connection oriented) socket. */ 67 SOCK_STREAM = 1, 68 /** Datagram (connectionless oriented) socket. */ 69 SOCK_DGRAM = 2, 70 /** Raw socket. */ 71 SOCK_RAW = 3 72 } sock_type_t; 80 extern int tcp_conn_recv(tcp_conn_t *, void *, size_t, size_t *); 81 extern int tcp_conn_recv_wait(tcp_conn_t *, void *, size_t, size_t *); 73 82 74 /** Type definition of the socket length. */75 typedef int32_t socklen_t;76 77 /* Socket options */78 79 enum {80 SOL_SOCKET = 1,81 82 /* IP link to transmit on */83 SO_IPLINK84 };85 83 86 84 #endif -
uspace/lib/c/include/inet/udp.h
rba0eac5 rfab2746 1 1 /* 2 * Copyright (c) 20 09 Lukas Mejdrech2 * Copyright (c) 2015 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 28 28 29 29 /** @addtogroup libc 30 * @{ 30 * @{ 31 */ 32 /** @file 31 33 */ 32 34 33 /** @file 34 * Character string to integer map. 35 */ 35 #ifndef LIBC_INET_UDP_H_ 36 #define LIBC_INET_UDP_H_ 36 37 37 #ifndef LIBC_CHAR_MAP_H_ 38 #define LIBC_CHAR_MAP_H_ 38 #include <async.h> 39 #include <inet/addr.h> 40 #include <inet/endpoint.h> 41 #include <inet/inet.h> 39 42 40 #include <libarch/types.h> 43 typedef enum { 44 udp_ls_down, 45 udp_ls_up 46 } udp_link_state_t; 41 47 42 /** Invalid assigned value used also if an entry does not exist. */ 43 #define CHAR_MAP_NULL (-1) 48 typedef struct { 49 struct udp *udp; 50 sysarg_t assoc_id; 51 size_t size; 52 inet_ep_t remote_ep; 53 } udp_rmsg_t; 44 54 45 /** Type definition of the character string to integer map. 46 * @see char_map 47 */ 48 typedef struct char_map char_map_t; 55 typedef struct { 56 } udp_rerr_t; 49 57 50 /** Character string to integer map item. 51 * 52 * This structure recursivelly contains itself as a character by character tree. 53 * The actually mapped character string consists of all the parent characters 54 * and the actual one. 55 */ 56 struct char_map { 57 /** Actually mapped character. */ 58 uint8_t c; 59 /** Stored integral value. */ 60 int value; 61 /** Next character array size. */ 62 int size; 63 /** First free position in the next character array. */ 64 int next; 65 /** Next character array. */ 66 char_map_t **items; 67 /** Consistency check magic value. */ 68 int magic; 69 }; 58 typedef struct { 59 struct udp *udp; 60 link_t ludp; 61 sysarg_t id; 62 struct udp_cb *cb; 63 void *cb_arg; 64 } udp_assoc_t; 70 65 71 extern int char_map_initialize(char_map_t *); 72 extern void char_map_destroy(char_map_t *); 73 extern int char_map_exclude(char_map_t *, const uint8_t *, size_t); 74 extern int char_map_add(char_map_t *, const uint8_t *, size_t, const int); 75 extern int char_map_find(const char_map_t *, const uint8_t *, size_t); 76 extern int char_map_update(char_map_t *, const uint8_t *, size_t, const int); 66 typedef struct udp_cb { 67 void (*recv_msg)(udp_assoc_t *, udp_rmsg_t *); 68 void (*recv_err)(udp_assoc_t *, udp_rerr_t *); 69 void (*link_state)(udp_assoc_t *, udp_link_state_t); 70 } udp_cb_t; 71 72 typedef struct udp { 73 /** UDP session */ 74 async_sess_t *sess; 75 /** List of associations */ 76 list_t assoc; /* of udp_assoc_t */ 77 } udp_t; 78 79 extern int udp_create(udp_t **); 80 extern void udp_destroy(udp_t *); 81 extern int udp_assoc_create(udp_t *, inet_ep2_t *, udp_cb_t *, void *, 82 udp_assoc_t **); 83 extern void udp_assoc_destroy(udp_assoc_t *); 84 extern int udp_assoc_send_msg(udp_assoc_t *, inet_ep_t *, void *, size_t); 85 extern void *udp_assoc_userptr(udp_assoc_t *); 86 extern size_t udp_rmsg_size(udp_rmsg_t *); 87 extern int udp_rmsg_read(udp_rmsg_t *, size_t, void *, size_t); 88 extern void udp_rmsg_remote_ep(udp_rmsg_t *, inet_ep_t *); 89 extern uint8_t udp_rerr_type(udp_rerr_t *); 90 extern uint8_t udp_rerr_code(udp_rerr_t *); 77 91 78 92 #endif -
uspace/lib/c/include/ipc/services.h
rba0eac5 rfab2746 49 49 SERVICE_IRC = FOURCC('i', 'r', 'c', ' '), 50 50 SERVICE_CLIPBOARD = FOURCC('c', 'l', 'i', 'p'), 51 SERVICE_UDP = FOURCC('u', 'd', 'p', ' '),52 SERVICE_TCP = FOURCC('t', 'c', 'p', ' ')53 51 } services_t; 54 52 … … 61 59 #define SERVICE_NAME_INETPING6 "net/inetping6" 62 60 #define SERVICE_NAME_NETCONF "net/netconf" 61 #define SERVICE_NAME_UDP "net/udp" 62 #define SERVICE_NAME_TCP "net/tcp" 63 63 64 64 #endif -
uspace/lib/c/include/ipc/udp.h
rba0eac5 rfab2746 1 1 /* 2 * Copyright (c) 20 09 Lukas Mejdrech2 * Copyright (c) 2015 Jiri Svobda 3 3 * All rights reserved. 4 4 * … … 27 27 */ 28 28 29 /** @addtogroup socket29 /** @addtogroup libcipc 30 30 * @{ 31 31 */ 32 33 32 /** @file 34 * Command-line argument parsing functions related to networking.35 33 */ 36 34 37 extern int socket_parse_address_family(const char *, int *); 38 extern int socket_parse_protocol_family(const char *, int *); 39 extern int socket_parse_socket_type(const char *, int *); 35 #ifndef LIBC_IPC_UDP_H_ 36 #define LIBC_IPC_UDP_H_ 37 38 #include <ipc/common.h> 39 40 typedef enum { 41 UDP_CALLBACK_CREATE = IPC_FIRST_USER_METHOD, 42 UDP_ASSOC_CREATE, 43 UDP_ASSOC_DESTROY, 44 UDP_ASSOC_SEND_MSG, 45 UDP_RMSG_INFO, 46 UDP_RMSG_READ, 47 UDP_RMSG_DISCARD 48 } udp_request_t; 49 50 typedef enum { 51 UDP_EV_DATA = IPC_FIRST_USER_METHOD 52 } udp_event_t; 53 54 #endif 40 55 41 56 /** @} -
uspace/lib/http/include/http/http.h
rba0eac5 rfab2746 37 37 #define HTTP_HTTP_H_ 38 38 39 #include <net/socket.h>40 39 #include <adt/list.h> 41 40 #include <inet/addr.h> 41 #include <inet/tcp.h> 42 42 43 43 #include "receive-buffer.h" … … 48 48 inet_addr_t addr; 49 49 50 bool connected;51 int conn_sd;52 50 tcp_t *tcp; 51 tcp_conn_t *conn; 52 53 53 size_t buffer_size; 54 54 receive_buffer_t recv_buffer; -
uspace/lib/http/src/http.c
rba0eac5 rfab2746 34 34 */ 35 35 36 #include <errno.h> 36 37 #include <stdio.h> 37 38 #include <stdlib.h> … … 39 40 #include <macros.h> 40 41 41 #include <net/socket.h>42 42 #include <inet/dnsr.h> 43 #include <inet/tcp.h> 43 44 44 45 #include <http/http.h> … … 48 49 { 49 50 http_t *http = client_data; 50 return recv(http->conn_sd, buf, buf_size, 0); 51 size_t nrecv; 52 int rc; 53 54 rc = tcp_conn_recv(http->conn, buf, buf_size, &nrecv); 55 if (rc != EOK) 56 return rc; 57 58 return nrecv; 51 59 } 52 60 … … 77 85 int http_connect(http_t *http) 78 86 { 79 if (http->conn ected)87 if (http->conn != NULL) 80 88 return EBUSY; 81 89 … … 95 103 } 96 104 97 struct sockaddr *saddr; 98 socklen_t saddrlen; 105 inet_ep2_t epp; 99 106 100 rc = inet_addr_sockaddr(&http->addr, http->port, &saddr, &saddrlen); 101 if (rc != EOK) { 102 assert(rc == ENOMEM); 103 return ENOMEM; 104 } 107 inet_ep2_init(&epp); 108 epp.remote.addr = http->addr; 109 epp.remote.port = http->port; 105 110 106 http->conn_sd = socket(saddr->sa_family, SOCK_STREAM, 0);107 if ( http->conn_sd < 0)108 return http->conn_sd;111 rc = tcp_create(&http->tcp); 112 if (rc != EOK) 113 return rc; 109 114 110 rc = connect(http->conn_sd, saddr, saddrlen); 111 free(saddr); 115 rc = tcp_conn_create(http->tcp, &epp, NULL, NULL, &http->conn); 116 if (rc != EOK) 117 return rc; 118 119 rc = tcp_conn_wait_connected(http->conn); 120 if (rc != EOK) 121 return rc; 112 122 113 123 return rc; … … 116 126 int http_close(http_t *http) 117 127 { 118 if ( !http->connected)128 if (http->conn == NULL) 119 129 return EINVAL; 120 130 121 return closesocket(http->conn_sd); 131 tcp_conn_destroy(http->conn); 132 tcp_destroy(http->tcp); 133 return EOK; 122 134 } 123 135 -
uspace/lib/http/src/request.c
rba0eac5 rfab2746 34 34 */ 35 35 36 #include <errno.h> 36 37 #include <stdio.h> 37 38 #include <stdlib.h> … … 39 40 #include <macros.h> 40 41 41 #include < net/socket.h>42 #include <inet/tcp.h> 42 43 43 44 #include <http/http.h> … … 149 150 return rc; 150 151 151 rc = send(http->conn_sd, buf, buf_size, 0);152 rc = tcp_conn_send(http->conn, buf, buf_size); 152 153 free(buf); 153 154 -
uspace/lib/posix/include/posix/errno.h
rba0eac5 rfab2746 69 69 extern int *__posix_errno(void); 70 70 71 #define __TOP_ERRNO (- NO_DATA)71 #define __TOP_ERRNO (-EAGAIN) 72 72 73 73 enum { 74 74 POSIX_E2BIG = __TOP_ERRNO + 1, 75 75 POSIX_EACCES = __TOP_ERRNO + 2, 76 POSIX_EADDRINUSE = -EADDRINUSE,77 76 POSIX_EADDRNOTAVAIL = -EADDRNOTAVAIL, 78 POSIX_EAFNOSUPPORT = -EAFNOSUPPORT,79 77 POSIX_EAGAIN = -EAGAIN, 80 78 POSIX_EALREADY = __TOP_ERRNO + 3, … … 88 86 POSIX_ECONNRESET = __TOP_ERRNO + 9, 89 87 POSIX_EDEADLK = __TOP_ERRNO + 10, 90 POSIX_EDESTADDRREQ = -EDESTADDRREQ,91 88 POSIX_EDOM = __TOP_ERRNO + 11, 92 89 POSIX_EDQUOT = __TOP_ERRNO + 12, … … 97 94 POSIX_EIDRM = __TOP_ERRNO + 16, 98 95 POSIX_EILSEQ = __TOP_ERRNO + 17, 99 POSIX_EINPROGRESS = -EINPROGRESS,100 96 POSIX_EINTR = -EINTR, 101 97 POSIX_EINVAL = -EINVAL, … … 114 110 POSIX_ENFILE = __TOP_ERRNO + 25, 115 111 POSIX_ENOBUFS = __TOP_ERRNO + 26, 116 POSIX_ENODATA = -NO_DATA,117 112 POSIX_ENODEV = __TOP_ERRNO + 27, 118 113 POSIX_ENOENT = -ENOENT, … … 127 122 POSIX_ENOSTR = __TOP_ERRNO + 34, 128 123 POSIX_ENOSYS = __TOP_ERRNO + 35, 129 POSIX_ENOTCONN = -ENOTCONN,130 124 POSIX_ENOTDIR = -ENOTDIR, 131 125 POSIX_ENOTEMPTY = -ENOTEMPTY, 132 126 POSIX_ENOTRECOVERABLE = __TOP_ERRNO + 36, 133 POSIX_ENOTSOCK = -ENOTSOCK,134 127 POSIX_ENOTSUP = -ENOTSUP, 135 128 POSIX_ENOTTY = __TOP_ERRNO + 37, … … 141 134 POSIX_EPIPE = __TOP_ERRNO + 41, 142 135 POSIX_EPROTO = __TOP_ERRNO + 42, 143 POSIX_EPROTONOSUPPORT = -EPROTONOSUPPORT,144 136 POSIX_EPROTOTYPE = __TOP_ERRNO + 43, 145 137 POSIX_ERANGE = -ERANGE, … … 153 145 POSIX_EWOULDBLOCK = __TOP_ERRNO + 51, 154 146 POSIX_EXDEV = -EXDEV, 147 POSIX_EINPROGRESS = __TOP_ERRNO + 52, 148 POSIX_ENOTSOCK = __TOP_ERRNO + 53, 149 POSIX_EDESTADDRREQ = __TOP_ERRNO + 54, 150 POSIX_EPROTONOSUPPORT = __TOP_ERRNO + 55, 151 POSIX_EAFNOSUPPORT = __TOP_ERRNO + 56, 152 POSIX_EADDRINUSE = __TOP_ERRNO + 57, 153 POSIX_ENOTCONN = __TOP_ERRNO + 58, 154 POSIX_ENODATA = __TOP_ERRNO + 59, 155 155 }; 156 156 -
uspace/lib/usbdev/src/hub.c
rba0eac5 rfab2746 151 151 * @retval EADDRNOTAVAIL Failed retrieving free address from host controller. 152 152 * @retval EBUSY Failed reserving default USB address. 153 * @retval EN OTCONNProblem connecting to the host controller via USB pipe.153 * @retval ENXIO Problem connecting to the host controller via USB pipe. 154 154 * @retval ESTALL Problem communication with device (either SET_ADDRESS 155 155 * request or requests for descriptors when creating match ids). … … 184 184 &dev_conn, hc_conn, USB_ADDRESS_DEFAULT); 185 185 if (rc != EOK) { 186 rc = EN OTCONN;186 rc = ENXIO; 187 187 goto leave_release_free_address; 188 188 } … … 192 192 rc = usb_pipe_initialize_default_control(&ctrl_pipe, &dev_conn); 193 193 if (rc != EOK) { 194 rc = EN OTCONN;194 rc = ENXIO; 195 195 goto leave_release_free_address; 196 196 } … … 217 217 rc = usb_pipe_register(&ctrl_pipe, 0); 218 218 if (rc != EOK) { 219 rc = EN OTCONN;219 rc = ENXIO; 220 220 goto leave_release_default_address; 221 221 } … … 278 278 if (rc != EOK) { 279 279 /* The child function is already created. */ 280 rc = E DESTADDRREQ;280 rc = EIO; 281 281 goto leave_release_free_address; 282 282 } -
uspace/srv/hid/remcons/remcons.c
rba0eac5 rfab2746 44 44 #include <fibril_synch.h> 45 45 #include <task.h> 46 #include < net/in.h>47 #include < net/inet.h>48 #include < net/socket.h>46 #include <inet/addr.h> 47 #include <inet/endpoint.h> 48 #include <inet/tcp.h> 49 49 #include <io/console.h> 50 50 #include <inttypes.h> … … 99 99 }; 100 100 101 static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn); 102 103 static tcp_listen_cb_t listen_cb = { 104 .new_conn = remcons_new_conn 105 }; 106 107 static tcp_cb_t conn_cb = { 108 .connected = NULL 109 }; 110 101 111 static telnet_user_t *srv_to_user(con_srv_t *srv) 102 112 { … … 111 121 112 122 /* Force character mode. */ 113 send(user->socket, (void *)telnet_force_character_mode_command,114 telnet_force_character_mode_command_count , 0);123 (void) tcp_conn_send(user->conn, (void *)telnet_force_character_mode_command, 124 telnet_force_character_mode_command_count); 115 125 116 126 return EOK; … … 298 308 while (!user_can_be_destroyed_no_lock(user)) { 299 309 if (user->task_finished) { 300 closesocket(user->socket); 310 tcp_conn_destroy(user->conn); 311 user->conn = NULL; 301 312 user->socket_closed = true; 302 313 user->srvs.aborted = true; … … 324 335 } 325 336 337 static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn) 338 { 339 telnet_user_t *user = telnet_user_create(conn); 340 assert(user); 341 342 con_srvs_init(&user->srvs); 343 user->srvs.ops = &con_ops; 344 user->srvs.sarg = user; 345 user->srvs.abort_timeout = 1000; 346 347 telnet_user_add(user); 348 349 fid_t fid = fibril_create(network_user_fibril, user); 350 assert(fid); 351 fibril_add_ready(fid); 352 } 353 326 354 int main(int argc, char *argv[]) 327 355 { 328 int port = 2223; 329 356 int rc; 357 tcp_listener_t *lst; 358 tcp_t *tcp; 359 inet_ep_t ep; 360 330 361 async_set_client_connection(client_connection); 331 intrc = loc_server_register(NAME);362 rc = loc_server_register(NAME); 332 363 if (rc != EOK) { 333 364 fprintf(stderr, "%s: Unable to register server\n", NAME); 334 365 return rc; 335 366 } 336 337 struct sockaddr_in addr; 338 339 addr.sin_family = AF_INET; 340 addr.sin_port = htons(port); 341 342 rc = inet_pton(AF_INET, "127.0.0.1", (void *) 343 &addr.sin_addr.s_addr); 344 if (rc != EOK) { 345 fprintf(stderr, "Error parsing network address: %s.\n", 346 str_error(rc)); 347 return 2; 348 } 349 350 int listen_sd = socket(PF_INET, SOCK_STREAM, 0); 351 if (listen_sd < 0) { 352 fprintf(stderr, "Error creating listening socket: %s.\n", 353 str_error(listen_sd)); 354 return 3; 355 } 356 357 rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr)); 358 if (rc != EOK) { 359 fprintf(stderr, "Error binding socket: %s.\n", 360 str_error(rc)); 361 return 4; 362 } 363 364 rc = listen(listen_sd, BACKLOG_SIZE); 365 if (rc != EOK) { 366 fprintf(stderr, "listen() failed: %s.\n", str_error(rc)); 367 return 5; 367 368 rc = tcp_create(&tcp); 369 if (tcp != EOK) { 370 fprintf(stderr, "%s: Error initialzing TCP.\n", NAME); 371 return rc; 372 } 373 374 inet_ep_init(&ep); 375 ep.port = 2223; 376 377 rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL, 378 &lst); 379 if (rc != EOK) { 380 fprintf(stderr, "%s: Error creating listener.\n", NAME); 381 return rc; 368 382 } 369 383 370 384 printf("%s: HelenOS Remote console service\n", NAME); 371 385 task_retval(0); 372 373 while (true) { 374 struct sockaddr_in raddr; 375 socklen_t raddr_len = sizeof(raddr); 376 int conn_sd = accept(listen_sd, (struct sockaddr *) &raddr, 377 &raddr_len); 378 379 if (conn_sd < 0) { 380 fprintf(stderr, "accept() failed: %s.\n", 381 str_error(rc)); 382 continue; 383 } 384 385 telnet_user_t *user = telnet_user_create(conn_sd); 386 assert(user); 387 388 con_srvs_init(&user->srvs); 389 user->srvs.ops = &con_ops; 390 user->srvs.sarg = user; 391 user->srvs.abort_timeout = 1000; 392 393 telnet_user_add(user); 394 395 fid_t fid = fibril_create(network_user_fibril, user); 396 assert(fid); 397 fibril_add_ready(fid); 398 } 399 386 async_manager(); 387 388 /* Not reached */ 400 389 return 0; 401 390 } -
uspace/srv/hid/remcons/remcons.h
rba0eac5 rfab2746 38 38 #define NAME "remcons" 39 39 #define NAMESPACE "term" 40 #define BACKLOG_SIZE 541 40 42 41 #endif -
uspace/srv/hid/remcons/user.c
rba0eac5 rfab2746 44 44 #include <fibril_synch.h> 45 45 #include <task.h> 46 #include <net/in.h> 47 #include <net/inet.h> 48 #include <net/socket.h> 46 #include <inet/tcp.h> 49 47 #include <io/console.h> 50 48 #include <inttypes.h> … … 58 56 /** Create new telnet user. 59 57 * 60 * @param socket Socket the user communicates through.58 * @param conn Incoming connection. 61 59 * @return New telnet user or NULL when out of memory. 62 60 */ 63 telnet_user_t *telnet_user_create( int socket)61 telnet_user_t *telnet_user_create(tcp_conn_t *conn) 64 62 { 65 63 static int telnet_user_id_counter = 0; … … 78 76 } 79 77 80 user-> socket = socket;78 user->conn = conn; 81 79 user->service_id = (service_id_t) -1; 82 80 prodcons_initialize(&user->in_events); … … 193 191 /* No more buffered data? */ 194 192 if (user->socket_buffer_len <= user->socket_buffer_pos) { 195 int recv_length = recv(user->socket, user->socket_buffer, BUFFER_SIZE, 0); 196 if ((recv_length == 0) || (recv_length == ENOTCONN)) { 193 int rc; 194 size_t recv_length; 195 196 rc = tcp_conn_recv_wait(user->conn, user->socket_buffer, 197 BUFFER_SIZE, &recv_length); 198 if (rc != EOK) 199 return rc; 200 201 if (recv_length == 0) { 197 202 user->socket_closed = true; 198 203 user->srvs.aborted = true; 199 204 return ENOENT; 200 205 } 201 if (recv_length < 0) { 202 return recv_length; 203 } 206 204 207 user->socket_buffer_len = recv_length; 205 208 user->socket_buffer_pos = 0; … … 359 362 360 363 361 int rc = send(user->socket, converted, converted_size, 0);364 int rc = tcp_conn_send(user->conn, converted, converted_size); 362 365 free(converted); 363 366 -
uspace/srv/hid/remcons/user.h
rba0eac5 rfab2746 38 38 #include <adt/prodcons.h> 39 39 #include <fibril_synch.h> 40 #include <inet/tcp.h> 40 41 #include <inttypes.h> 41 42 #include <io/con_srv.h> … … 51 52 /** Internal id, used for creating locfs entries. */ 52 53 int id; 53 /** Associated socket. */54 int socket;54 /** Associated connection. */ 55 tcp_conn_t *conn; 55 56 /** Location service id assigned to the virtual terminal. */ 56 57 service_id_t service_id; … … 80 81 } telnet_user_t; 81 82 82 extern telnet_user_t *telnet_user_create( int);83 extern telnet_user_t *telnet_user_create(tcp_conn_t *); 83 84 extern void telnet_user_add(telnet_user_t *); 84 85 extern void telnet_user_destroy(telnet_user_t *); -
uspace/srv/hid/rfb/main.c
rba0eac5 rfab2746 150 150 } 151 151 152 static int socket_fibril(void *unused)153 {154 rfb_accept(&rfb);155 156 /* Not reached */157 return EOK;158 }159 160 152 int main(int argc, char **argv) 161 153 { … … 267 259 } 268 260 269 fid_t fib = fibril_create(socket_fibril, NULL);270 if (!fib) {271 fprintf(stderr, NAME ": Unable to create socket fibril.\n");272 return 2;273 }274 275 fibril_add_ready(fib);276 277 261 printf("%s: Accepting connections\n", NAME); 278 262 task_retval(0); -
uspace/srv/hid/rfb/rfb.c
rba0eac5 rfab2746 31 31 #include <stdlib.h> 32 32 #include <fibril_synch.h> 33 #include <inet/addr.h> 34 #include <inet/endpoint.h> 35 #include <inet/tcp.h> 33 36 #include <inttypes.h> 34 37 #include <str.h> … … 38 41 #include <io/log.h> 39 42 40 #include <net/in.h>41 #include <net/inet.h>42 #include <net/socket.h>43 44 43 #include "rfb.h" 44 45 static void rfb_new_conn(tcp_listener_t *, tcp_conn_t *); 46 47 static tcp_listen_cb_t listen_cb = { 48 .new_conn = rfb_new_conn 49 }; 50 51 static tcp_cb_t conn_cb = { 52 .connected = NULL 53 }; 45 54 46 55 /** Buffer for receiving the request. */ … … 53 62 54 63 /** Receive one character (with buffering) */ 55 static int recv_char(int fd, char *c) 56 { 64 static int recv_char(tcp_conn_t *conn, char *c) 65 { 66 size_t nrecv; 67 int rc; 68 57 69 if (rbuf_out == rbuf_in) { 58 70 rbuf_out = 0; 59 71 rbuf_in = 0; 60 72 61 ssize_t rc = recv(fd, rbuf, BUFFER_SIZE, 0);62 if (rc <= 0)73 rc = tcp_conn_recv_wait(conn, rbuf, BUFFER_SIZE, &nrecv); 74 if (rc != EOK) 63 75 return rc; 64 76 65 rbuf_in = rc;77 rbuf_in = nrecv; 66 78 } 67 79 … … 71 83 72 84 /** Receive count characters (with buffering) */ 73 static int recv_chars( int fd, char *c, size_t count)85 static int recv_chars(tcp_conn_t *conn, char *c, size_t count) 74 86 { 75 87 for (size_t i = 0; i < count; i++) { 76 int rc = recv_char( fd, c);88 int rc = recv_char(conn, c); 77 89 if (rc != EOK) 78 90 return rc; … … 82 94 } 83 95 84 static int recv_skip_chars( int fd, size_t count)96 static int recv_skip_chars(tcp_conn_t *conn, size_t count) 85 97 { 86 98 for (size_t i = 0; i < count; i++) { 87 99 char c; 88 int rc = recv_char( fd, &c);100 int rc = recv_char(conn, &c); 89 101 if (rc != EOK) 90 102 return rc; … … 204 216 } 205 217 206 static int recv_message( int conn_sd, char type, void *buf, size_t size)218 static int recv_message(tcp_conn_t *conn, char type, void *buf, size_t size) 207 219 { 208 220 memcpy(buf, &type, 1); 209 return recv_chars(conn _sd, ((char *) buf) + 1, size -1);221 return recv_chars(conn, ((char *) buf) + 1, size -1); 210 222 } 211 223 … … 477 489 } 478 490 479 static int rfb_send_framebuffer_update(rfb_t *rfb, int conn_sd, bool incremental) 491 static int rfb_send_framebuffer_update(rfb_t *rfb, tcp_conn_t *conn, 492 bool incremental) 480 493 { 481 494 fibril_mutex_lock(&rfb->lock); … … 540 553 541 554 if (!rfb->pixel_format.true_color) { 542 int rc = send(conn_sd, send_palette, send_palette_size, 0);555 int rc = tcp_conn_send(conn, send_palette, send_palette_size); 543 556 if (rc != EOK) { 544 557 free(buf); … … 547 560 } 548 561 549 int rc = send(conn_sd, buf, buf_size, 0);562 int rc = tcp_conn_send(conn, buf, buf_size); 550 563 free(buf); 551 564 … … 580 593 } 581 594 582 static void rfb_socket_connection(rfb_t *rfb, int conn_sd)595 static void rfb_socket_connection(rfb_t *rfb, tcp_conn_t *conn) 583 596 { 584 597 /* Version handshake */ 585 int rc = send(conn_sd, "RFB 003.008\n", 12, 0);598 int rc = tcp_conn_send(conn, "RFB 003.008\n", 12); 586 599 if (rc != EOK) { 587 600 log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server version %d", rc); … … 590 603 591 604 char client_version[12]; 592 rc = recv_chars(conn _sd, client_version, 12);605 rc = recv_chars(conn, client_version, 12); 593 606 if (rc != EOK) { 594 607 log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client version: %d", rc); … … 607 620 sec_types[0] = 1; /* length */ 608 621 sec_types[1] = RFB_SECURITY_NONE; 609 rc = send(conn_sd, sec_types, 2, 0);622 rc = tcp_conn_send(conn, sec_types, 2); 610 623 if (rc != EOK) { 611 624 log_msg(LOG_DEFAULT, LVL_WARN, … … 615 628 616 629 char selected_sec_type = 0; 617 rc = recv_char(conn _sd, &selected_sec_type);630 rc = recv_char(conn, &selected_sec_type); 618 631 if (rc != EOK) { 619 632 log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving security type: %d", rc); … … 626 639 } 627 640 uint32_t security_result = RFB_SECURITY_HANDSHAKE_OK; 628 rc = send(conn_sd, &security_result, sizeof(uint32_t), 0);641 rc = tcp_conn_send(conn, &security_result, sizeof(uint32_t)); 629 642 if (rc != EOK) { 630 643 log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending security result: %d", rc); … … 634 647 /* Client init */ 635 648 char shared_flag; 636 rc = recv_char(conn _sd, &shared_flag);649 rc = recv_char(conn, &shared_flag); 637 650 if (rc != EOK) { 638 651 log_msg(LOG_DEFAULT, LVL_WARN, "Failed receiving client init: %d", rc); … … 657 670 memcpy(server_init->name, rfb->name, name_length); 658 671 fibril_mutex_unlock(&rfb->lock); 659 rc = send(conn_sd, server_init, msg_length, 0);672 rc = tcp_conn_send(conn, server_init, msg_length); 660 673 if (rc != EOK) { 661 674 log_msg(LOG_DEFAULT, LVL_WARN, "Failed sending server init: %d", rc); … … 665 678 while (true) { 666 679 char message_type = 0; 667 rc = recv_char(conn _sd, &message_type);680 rc = recv_char(conn, &message_type); 668 681 if (rc != EOK) { 669 682 log_msg(LOG_DEFAULT, LVL_WARN, … … 680 693 switch (message_type) { 681 694 case RFB_CMSG_SET_PIXEL_FORMAT: 682 recv_message(conn _sd, message_type, &spf, sizeof(spf));695 recv_message(conn, message_type, &spf, sizeof(spf)); 683 696 rfb_pixel_format_to_host(&spf.pixel_format, &spf.pixel_format); 684 697 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetPixelFormat message"); … … 690 703 break; 691 704 case RFB_CMSG_SET_ENCODINGS: 692 recv_message(conn _sd, message_type, &se, sizeof(se));705 recv_message(conn, message_type, &se, sizeof(se)); 693 706 rfb_set_encodings_to_host(&se, &se); 694 707 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received SetEncodings message"); 695 708 for (uint16_t i = 0; i < se.count; i++) { 696 709 int32_t encoding = 0; 697 rc = recv_chars(conn _sd, (char *) &encoding, sizeof(int32_t));710 rc = recv_chars(conn, (char *) &encoding, sizeof(int32_t)); 698 711 if (rc != EOK) 699 712 return; … … 707 720 break; 708 721 case RFB_CMSG_FRAMEBUFFER_UPDATE_REQUEST: 709 recv_message(conn _sd, message_type, &fbur, sizeof(fbur));722 recv_message(conn, message_type, &fbur, sizeof(fbur)); 710 723 rfb_framebuffer_update_request_to_host(&fbur, &fbur); 711 724 log_msg(LOG_DEFAULT, LVL_DEBUG2, 712 725 "Received FramebufferUpdateRequest message"); 713 rfb_send_framebuffer_update(rfb, conn _sd, fbur.incremental);726 rfb_send_framebuffer_update(rfb, conn, fbur.incremental); 714 727 break; 715 728 case RFB_CMSG_KEY_EVENT: 716 recv_message(conn _sd, message_type, &ke, sizeof(ke));729 recv_message(conn, message_type, &ke, sizeof(ke)); 717 730 rfb_key_event_to_host(&ke, &ke); 718 731 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received KeyEvent message"); 719 732 break; 720 733 case RFB_CMSG_POINTER_EVENT: 721 recv_message(conn _sd, message_type, &pe, sizeof(pe));734 recv_message(conn, message_type, &pe, sizeof(pe)); 722 735 rfb_pointer_event_to_host(&pe, &pe); 723 736 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received PointerEvent message"); 724 737 break; 725 738 case RFB_CMSG_CLIENT_CUT_TEXT: 726 recv_message(conn _sd, message_type, &cct, sizeof(cct));739 recv_message(conn, message_type, &cct, sizeof(cct)); 727 740 rfb_client_cut_text_to_host(&cct, &cct); 728 741 log_msg(LOG_DEFAULT, LVL_DEBUG2, "Received ClientCutText message"); 729 recv_skip_chars(conn _sd, cct.length);742 recv_skip_chars(conn, cct.length); 730 743 break; 731 744 default: … … 737 750 } 738 751 739 int rfb_listen(rfb_t *rfb, uint16_t port) { 740 struct sockaddr_in addr; 741 742 addr.sin_family = AF_INET; 743 addr.sin_port = htons(port); 744 745 int rc = inet_pton(AF_INET, "127.0.0.1", (void *) 746 &addr.sin_addr.s_addr); 752 int rfb_listen(rfb_t *rfb, uint16_t port) 753 { 754 tcp_t *tcp = NULL; 755 tcp_listener_t *lst = NULL; 756 inet_ep_t ep; 757 int rc; 758 759 inet_ep_init(&ep); 760 ep.port = port; 761 762 rc = tcp_listener_create(tcp, &ep, &listen_cb, rfb, &conn_cb, rfb, 763 &lst); 747 764 if (rc != EOK) { 748 log_msg(LOG_DEFAULT, LVL_ERROR, "Error parsing network address (%s)", 749 str_error(rc)); 750 return rc; 751 } 752 753 int listen_sd = socket(PF_INET, SOCK_STREAM, 0); 754 if (listen_sd < 0) { 755 log_msg(LOG_DEFAULT, LVL_ERROR, "Error creating listening socket (%s)", 756 str_error(listen_sd)); 757 return rc; 758 } 759 760 rc = bind(listen_sd, (struct sockaddr *) &addr, sizeof(addr)); 761 if (rc != EOK) { 762 log_msg(LOG_DEFAULT, LVL_ERROR, "Error binding socket (%s)", 763 str_error(rc)); 764 return rc; 765 } 766 767 rc = listen(listen_sd, 2); 768 if (rc != EOK) { 769 log_msg(LOG_DEFAULT, LVL_ERROR, "listen() failed (%s)", str_error(rc)); 770 return rc; 771 } 772 773 rfb->listen_sd = listen_sd; 765 log_msg(LOG_DEFAULT, LVL_ERROR, "Error creating listener.\n"); 766 goto error; 767 } 768 769 rfb->tcp = tcp; 770 rfb->lst = lst; 774 771 775 772 return EOK; 776 } 777 778 void rfb_accept(rfb_t *rfb) 779 { 780 while (true) { 781 struct sockaddr_in raddr; 782 socklen_t raddr_len = sizeof(raddr); 783 int conn_sd = accept(rfb->listen_sd, (struct sockaddr *) &raddr, 784 &raddr_len); 785 786 if (conn_sd < 0) { 787 log_msg(LOG_DEFAULT, LVL_WARN, "accept() failed (%s)", 788 str_error(conn_sd)); 789 continue; 790 } 791 792 log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection accepted"); 793 794 rbuf_out = 0; 795 rbuf_in = 0; 796 797 rfb_socket_connection(rfb, conn_sd); 798 closesocket(conn_sd); 799 } 800 } 773 error: 774 tcp_listener_destroy(lst); 775 tcp_destroy(tcp); 776 return rc; 777 } 778 779 static void rfb_new_conn(tcp_listener_t *lst, tcp_conn_t *conn) 780 { 781 rfb_t *rfb = (rfb_t *)tcp_listener_userptr(lst); 782 log_msg(LOG_DEFAULT, LVL_DEBUG, "Connection accepted"); 783 784 rbuf_out = 0; 785 rbuf_in = 0; 786 787 rfb_socket_connection(rfb, conn); 788 } -
uspace/srv/hid/rfb/rfb.h
rba0eac5 rfab2746 30 30 #define RFB_H__ 31 31 32 #include <inet/tcp.h> 32 33 #include <io/pixelmap.h> 33 34 #include <fibril_synch.h> … … 151 152 rfb_pixel_format_t pixel_format; 152 153 const char *name; 153 int listen_sd; 154 tcp_t *tcp; 155 tcp_listener_t *lst; 154 156 pixelmap_t framebuffer; 155 157 rfb_rectangle_t damage_rect; … … 165 167 extern int rfb_set_size(rfb_t *, uint16_t, uint16_t); 166 168 extern int rfb_listen(rfb_t *, uint16_t); 167 extern void rfb_accept(rfb_t *);168 169 169 170 #endif -
uspace/srv/net/dhcp/dhcp.c
rba0eac5 rfab2746 37 37 #include <adt/list.h> 38 38 #include <bitops.h> 39 #include <byteorder.h> 39 40 #include <errno.h> 40 41 #include <fibril_synch.h> -
uspace/srv/net/dhcp/transport.c
rba0eac5 rfab2746 36 36 37 37 #include <bitops.h> 38 #include <errno.h> 38 39 #include <inet/addr.h> 39 40 #include <inet/dnsr.h> … … 41 42 #include <io/log.h> 42 43 #include <loc.h> 43 #include <net/in.h>44 #include <net/inet.h>45 #include <net/socket.h>46 44 #include <stdio.h> 47 45 #include <stdlib.h> … … 67 65 } dhcp_offer_t; 68 66 69 static int dhcp_recv_fibril(void *); 67 static void dhcp_recv_msg(udp_assoc_t *, udp_rmsg_t *); 68 static void dhcp_recv_err(udp_assoc_t *, udp_rerr_t *); 69 static void dhcp_link_state(udp_assoc_t *, udp_link_state_t); 70 71 static udp_cb_t dhcp_transport_cb = { 72 .recv_msg = dhcp_recv_msg, 73 .recv_err = dhcp_recv_err, 74 .link_state = dhcp_link_state 75 }; 70 76 71 77 int dhcp_send(dhcp_transport_t *dt, void *msg, size_t size) 72 78 { 73 struct sockaddr_in addr;79 inet_ep_t ep; 74 80 int rc; 75 81 76 addr.sin_family = AF_INET;77 addr.sin_port = htons(dhcp_server_port);78 addr.sin_addr.s_addr = htonl(addr32_broadcast_all_hosts);82 inet_ep_init(&ep); 83 ep.port = dhcp_server_port; 84 inet_addr_set(addr32_broadcast_all_hosts, &ep.addr); 79 85 80 rc = sendto(dt->fd, msg, size, 0, 81 (struct sockaddr *)&addr, sizeof(addr)); 86 rc = udp_assoc_send_msg(dt->assoc, &ep, msg, size); 82 87 if (rc != EOK) { 83 log_msg(LOG_DEFAULT, LVL_ERROR, " Sending failed");88 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed sending message"); 84 89 return rc; 85 90 } … … 88 93 } 89 94 90 static int dhcp_recv_msg(dhcp_transport_t *dt, void **rmsg, size_t *rsize)95 static void dhcp_recv_msg(udp_assoc_t *assoc, udp_rmsg_t *rmsg) 91 96 { 92 struct sockaddr_in src_addr; 93 socklen_t src_addr_size; 94 size_t recv_size; 97 dhcp_transport_t *dt; 98 size_t s; 95 99 int rc; 96 100 97 src_addr_size = sizeof(src_addr); 98 rc = recvfrom(dt->fd, msgbuf, MAX_MSG_SIZE, 0, 99 (struct sockaddr *)&src_addr, &src_addr_size); 100 if (rc < 0) { 101 log_msg(LOG_DEFAULT, LVL_ERROR, "recvfrom failed (%d)", rc); 102 return rc; 101 log_msg(LOG_DEFAULT, LVL_NOTE, "dhcp_recv_msg()"); 102 103 dt = (dhcp_transport_t *)udp_assoc_userptr(assoc); 104 s = udp_rmsg_size(rmsg); 105 if (s > MAX_MSG_SIZE) 106 s = MAX_MSG_SIZE; /* XXX */ 107 108 rc = udp_rmsg_read(rmsg, 0, msgbuf, s); 109 if (rc != EOK) { 110 log_msg(LOG_DEFAULT, LVL_ERROR, "Error receiving message."); 111 return; 103 112 } 104 113 105 recv_size = (size_t)rc;106 *rmsg = msgbuf;107 *rsize = recv_size; 114 log_msg(LOG_DEFAULT, LVL_NOTE, "dhcp_recv_msg() - call recv_cb"); 115 dt->recv_cb(dt->cb_arg, msgbuf, s); 116 } 108 117 109 return EOK; 118 static void dhcp_recv_err(udp_assoc_t *assoc, udp_rerr_t *rerr) 119 { 120 log_msg(LOG_DEFAULT, LVL_WARN, "Ignoring ICMP error"); 121 } 122 123 static void dhcp_link_state(udp_assoc_t *assoc, udp_link_state_t ls) 124 { 125 log_msg(LOG_DEFAULT, LVL_NOTE, "Link state change"); 110 126 } 111 127 … … 113 129 dhcp_recv_cb_t recv_cb, void *arg) 114 130 { 115 int fd;116 struct sockaddr_in laddr;117 in t fid;131 udp_t *udp = NULL; 132 udp_assoc_t *assoc = NULL; 133 inet_ep2_t epp; 118 134 int rc; 119 135 120 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp transport_init()");136 log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_transport_init()"); 121 137 122 laddr.sin_family = AF_INET; 123 laddr.sin_port = htons(dhcp_client_port); 124 laddr.sin_addr.s_addr = INADDR_ANY; 138 inet_ep2_init(&epp); 139 epp.local.addr.version = ip_v4; 140 epp.local.port = dhcp_client_port; 141 epp.local_link = link_id; 125 142 126 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 127 if (fd < 0) { 128 rc = EIO; 129 goto error; 130 } 131 132 log_msg(LOG_DEFAULT, LVL_DEBUG, "Bind socket."); 133 rc = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); 143 rc = udp_create(&udp); 134 144 if (rc != EOK) { 135 145 rc = EIO; … … 137 147 } 138 148 139 log_msg(LOG_DEFAULT, LVL_DEBUG, "Set socket options"); 140 rc = setsockopt(fd, SOL_SOCKET, SO_IPLINK, &link_id, sizeof(link_id)); 149 rc = udp_assoc_create(udp, &epp, &dhcp_transport_cb, dt, &assoc); 141 150 if (rc != EOK) { 142 151 rc = EIO; … … 144 153 } 145 154 146 dt->fd = fd; 155 dt->udp = udp; 156 dt->assoc = assoc; 147 157 dt->recv_cb = recv_cb; 148 158 dt->cb_arg = arg; 149 159 150 fid = fibril_create(dhcp_recv_fibril, dt);151 if (fid == 0) {152 rc = ENOMEM;153 goto error;154 }155 156 dt->recv_fid = fid;157 fibril_add_ready(fid);158 159 160 return EOK; 160 161 error: 161 closesocket(fd); 162 udp_assoc_destroy(assoc); 163 udp_destroy(udp); 162 164 return rc; 163 165 } … … 165 167 void dhcp_transport_fini(dhcp_transport_t *dt) 166 168 { 167 closesocket(dt->fd); 168 } 169 170 static int dhcp_recv_fibril(void *arg) 171 { 172 dhcp_transport_t *dt = (dhcp_transport_t *)arg; 173 void *msg; 174 size_t size = (size_t) -1; 175 int rc; 176 177 while (true) { 178 rc = dhcp_recv_msg(dt, &msg, &size); 179 if (rc != EOK) 180 break; 181 182 assert(size != (size_t) -1); 183 184 dt->recv_cb(dt->cb_arg, msg, size); 185 } 186 187 return EOK; 169 udp_assoc_destroy(dt->assoc); 170 udp_destroy(dt->udp); 188 171 } 189 172 -
uspace/srv/net/dhcp/transport.h
rba0eac5 rfab2746 38 38 #define TRANSPORT_H 39 39 40 #include <inet/udp.h> 40 41 #include <ipc/loc.h> 41 42 #include <sys/types.h> … … 47 48 48 49 struct dhcp_transport { 49 /** Transport socket */ 50 int fd; 50 /** UDP */ 51 udp_t *udp; 52 /** UDP association */ 53 udp_assoc_t *assoc; 51 54 /** Receive callback */ 52 55 dhcp_recv_cb_t recv_cb; 53 56 /** Callback argument */ 54 57 void *cb_arg; 55 /** Receive fibril ID */56 int recv_fid;57 58 }; 58 59 -
uspace/srv/net/dnsrsrv/transport.c
rba0eac5 rfab2746 37 37 #include <errno.h> 38 38 #include <fibril_synch.h> 39 #include <inet/addr.h> 40 #include <inet/endpoint.h> 41 #include <inet/udp.h> 39 42 #include <io/log.h> 40 #include <net/in.h>41 #include <net/inet.h>42 #include <net/socket.h>43 43 #include <stdbool.h> 44 44 #include <stdlib.h> … … 72 72 73 73 static uint8_t recv_buf[RECV_BUF_SIZE]; 74 static fid_t recv_fid;75 static int transport_fd = -1;74 static udp_t *transport_udp; 75 static udp_assoc_t *transport_assoc; 76 76 77 77 /** Outstanding requests */ … … 79 79 static FIBRIL_MUTEX_INITIALIZE(treq_lock); 80 80 81 static int transport_recv_fibril(void *arg); 81 static void transport_recv_msg(udp_assoc_t *, udp_rmsg_t *); 82 static void transport_recv_err(udp_assoc_t *, udp_rerr_t *); 83 static void transport_link_state(udp_assoc_t *, udp_link_state_t); 84 85 static udp_cb_t transport_cb = { 86 .recv_msg = transport_recv_msg, 87 .recv_err = transport_recv_err, 88 .link_state = transport_link_state 89 }; 82 90 83 91 int transport_init(void) 84 92 { 85 struct sockaddr_in laddr; 86 int fd; 87 fid_t fid; 93 inet_ep2_t epp; 88 94 int rc; 89 95 90 laddr.sin_family = AF_INET; 91 laddr.sin_port = htons(12345); 92 laddr.sin_addr.s_addr = INADDR_ANY; 93 94 fd = -1; 95 96 fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); 97 if (fd < 0) { 96 inet_ep2_init(&epp); 97 98 rc = udp_create(&transport_udp); 99 if (rc != EOK) { 98 100 rc = EIO; 99 101 goto error; 100 102 } 101 103 102 rc = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); 103 if (rc != EOK) 104 goto error; 105 106 transport_fd = fd; 107 108 fid = fibril_create(transport_recv_fibril, NULL); 109 if (fid == 0) 110 goto error; 111 112 fibril_add_ready(fid); 113 recv_fid = fid; 104 rc = udp_assoc_create(transport_udp, &epp, &transport_cb, NULL, 105 &transport_assoc); 106 if (rc != EOK) { 107 rc = EIO; 108 goto error; 109 } 110 114 111 return EOK; 115 112 error: 116 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing network socket.");117 if (fd >= 0)118 closesocket(fd);113 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing network."); 114 udp_assoc_destroy(transport_assoc); 115 udp_destroy(transport_udp); 119 116 return rc; 120 117 } … … 122 119 void transport_fini(void) 123 120 { 124 if (transport_fd >= 0)125 closesocket(transport_fd);121 udp_assoc_destroy(transport_assoc); 122 udp_destroy(transport_udp); 126 123 } 127 124 … … 182 179 { 183 180 trans_req_t *treq = NULL; 184 struct sockaddr *saddr = NULL; 185 socklen_t saddrlen; 186 181 inet_ep_t ep; 182 187 183 void *req_data; 188 184 size_t req_size; … … 190 186 if (rc != EOK) 191 187 goto error; 192 193 rc = inet_addr_sockaddr(&dns_server_addr, DNS_SERVER_PORT, 194 &saddr, &saddrlen); 195 if (rc != EOK) { 196 assert(rc == ENOMEM); 197 goto error; 198 } 199 188 189 inet_ep_init(&ep); 190 ep.addr = dns_server_addr; 191 ep.port = DNS_SERVER_PORT; 192 200 193 size_t ntry = 0; 201 194 202 195 while (ntry < REQ_RETRY_MAX) { 203 rc = sendto(transport_fd, req_data, req_size, 0,204 saddr, saddrlen);196 rc = udp_assoc_send_msg(transport_assoc, &ep, req_data, 197 req_size); 205 198 if (rc != EOK) 206 199 goto error; 207 200 208 201 treq = treq_create(req); 209 202 if (treq == NULL) { … … 211 204 goto error; 212 205 } 213 206 214 207 fibril_mutex_lock(&treq->done_lock); 215 208 while (treq->done != true) { … … 221 214 } 222 215 } 223 216 224 217 fibril_mutex_unlock(&treq->done_lock); 225 218 226 219 if (rc != ETIMEOUT) 227 220 break; 228 221 } 229 222 230 223 if (ntry >= REQ_RETRY_MAX) { 231 224 rc = EIO; 232 225 goto error; 233 226 } 234 227 235 228 if (treq->status != EOK) { 236 229 rc = treq->status; 237 230 goto error; 238 231 } 239 232 240 233 *rresp = treq->resp; 241 234 treq_destroy(treq); 242 235 free(req_data); 243 free(saddr);244 236 return EOK; 245 237 246 238 error: 247 239 if (treq != NULL) 248 240 treq_destroy(treq); 249 241 250 242 free(req_data); 251 free(saddr);252 243 return rc; 253 244 } 254 245 255 static int transport_recv_msg(dns_message_t **rresp) 256 { 257 struct sockaddr_in src_addr; 258 socklen_t src_addr_size; 259 size_t recv_size; 260 dns_message_t *resp; 261 int rc; 262 263 src_addr_size = sizeof(src_addr); 264 rc = recvfrom(transport_fd, recv_buf, RECV_BUF_SIZE, 0, 265 (struct sockaddr *)&src_addr, &src_addr_size); 266 if (rc < 0) { 267 log_msg(LOG_DEFAULT, LVL_ERROR, "recvfrom returns error - %d", rc); 268 goto error; 269 } 270 271 recv_size = (size_t)rc; 272 273 rc = dns_message_decode(recv_buf, recv_size, &resp); 274 if (rc != EOK) { 275 rc = EIO; 276 goto error; 277 } 278 279 *rresp = resp; 280 return EOK; 281 282 error: 283 return rc; 284 } 285 286 static int transport_recv_fibril(void *arg) 246 static void transport_recv_msg(udp_assoc_t *assoc, udp_rmsg_t *rmsg) 287 247 { 288 248 dns_message_t *resp = NULL; 289 249 trans_req_t *treq; 250 size_t size; 251 inet_ep_t remote_ep; 290 252 int rc; 291 253 292 while (true) { 293 rc = transport_recv_msg(&resp); 294 if (rc != EOK) 295 continue; 296 297 assert(resp != NULL); 298 299 fibril_mutex_lock(&treq_lock); 300 treq = treq_match_resp(resp); 301 if (treq == NULL) { 302 fibril_mutex_unlock(&treq_lock); 303 continue; 304 } 305 306 list_remove(&treq->lreq); 254 size = udp_rmsg_size(rmsg); 255 if (size > RECV_BUF_SIZE) 256 size = RECV_BUF_SIZE; /* XXX */ 257 258 rc = udp_rmsg_read(rmsg, 0, recv_buf, size); 259 if (rc != EOK) { 260 log_msg(LOG_DEFAULT, LVL_ERROR, "Error reading message."); 261 return; 262 } 263 264 udp_rmsg_remote_ep(rmsg, &remote_ep); 265 /* XXX */ 266 267 rc = dns_message_decode(recv_buf, size, &resp); 268 if (rc != EOK) { 269 log_msg(LOG_DEFAULT, LVL_ERROR, "Error decoding message."); 270 return; 271 } 272 273 assert(resp != NULL); 274 275 fibril_mutex_lock(&treq_lock); 276 treq = treq_match_resp(resp); 277 if (treq == NULL) { 307 278 fibril_mutex_unlock(&treq_lock); 308 309 treq_complete(treq, resp); 310 } 311 312 return 0; 313 } 279 return; 280 } 281 282 list_remove(&treq->lreq); 283 fibril_mutex_unlock(&treq_lock); 284 285 treq_complete(treq, resp); 286 } 287 288 static void transport_recv_err(udp_assoc_t *assoc, udp_rerr_t *rerr) 289 { 290 log_msg(LOG_DEFAULT, LVL_WARN, "Ignoring ICMP error"); 291 } 292 293 static void transport_link_state(udp_assoc_t *assoc, udp_link_state_t ls) 294 { 295 log_msg(LOG_DEFAULT, LVL_NOTE, "Link state change"); 296 } 297 314 298 315 299 /** @} -
uspace/srv/net/tcp/Makefile
rba0eac5 rfab2746 28 28 29 29 USPACE_PREFIX = ../../.. 30 LIBS = $(LIBNET_PREFIX)/libnet.a31 EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include32 30 BINARY = tcp 33 31 … … 40 38 segment.c \ 41 39 seq_no.c \ 42 sock.c \43 40 tcp.c \ 44 41 test.c \ -
uspace/srv/net/tcp/tcp.c
rba0eac5 rfab2746 42 42 #include <io/log.h> 43 43 #include <stdio.h> 44 #include <stdlib.h> 44 45 #include <task.h> 45 46 … … 47 48 #include "pdu.h" 48 49 #include "rqueue.h" 49 #include "sock.h"50 50 #include "std.h" 51 51 #include "tcp.h" … … 192 192 } 193 193 194 rc = tcp_sock_init();195 if (rc != EOK) {196 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing socket service.");197 return ENOENT;198 }194 // rc = tcp_sock_init(); 195 // if (rc != EOK) { 196 // log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing socket service."); 197 // return ENOENT; 198 // } 199 199 200 200 return EOK; -
uspace/srv/net/tcp/tcp_type.h
rba0eac5 rfab2746 41 41 #include <fibril.h> 42 42 #include <fibril_synch.h> 43 #include <socket_core.h>44 43 #include <sys/types.h> 45 44 #include <inet/addr.h> … … 321 320 typedef struct { 322 321 async_sess_t *sess; 323 socket_cores_t sockets;322 // socket_cores_t sockets; 324 323 } tcp_client_t; 325 324 … … 330 329 fibril_mutex_t lock; 331 330 /** Socket core */ 332 socket_core_t *sock_core;331 // socket_core_t *sock_core; 333 332 /** Client */ 334 333 tcp_client_t *client; -
uspace/srv/net/udp/Makefile
rba0eac5 rfab2746 28 28 29 29 USPACE_PREFIX = ../../.. 30 LIBS = $(LIBNET_PREFIX)/libnet.a31 EXTRA_CFLAGS = -I$(LIBNET_PREFIX)/include32 30 BINARY = udp 33 31 … … 35 33 assoc.c \ 36 34 msg.c \ 37 sock.c \38 35 pdu.c \ 36 service.c \ 39 37 ucall.c \ 40 38 udp.c \ -
uspace/srv/net/udp/assoc.c
rba0eac5 rfab2746 36 36 37 37 #include <adt/list.h> 38 #include <errno.h> 38 39 #include <stdbool.h> 39 40 #include <fibril_synch.h> … … 62 63 * @return New association or NULL 63 64 */ 64 udp_assoc_t *udp_assoc_new(udp_sock_t *lsock, udp_sock_t *fsock) 65 udp_assoc_t *udp_assoc_new(udp_sock_t *lsock, udp_sock_t *fsock, 66 udp_assoc_cb_t *cb, void *cb_arg) 65 67 { 66 68 udp_assoc_t *assoc = NULL; … … 82 84 if (lsock != NULL) 83 85 assoc->ident.local = *lsock; 84 86 85 87 if (fsock != NULL) 86 88 assoc->ident.foreign = *fsock; 87 89 90 assoc->cb = cb; 91 assoc->cb_arg = cb_arg; 88 92 return assoc; 89 93 error: … … 258 262 int rc; 259 263 260 log_msg(LOG_DEFAULT, LVL_ DEBUG, "udp_assoc_send(%p, %p, %p)",264 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send(%p, %p, %p)", 261 265 assoc, fsock, msg); 262 266 … … 266 270 sp.foreign = *fsock; 267 271 272 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - check addr any"); 273 268 274 if ((inet_addr_is_any(&sp.foreign.addr)) || 269 275 (sp.foreign.port == UDP_PORT_ANY)) 270 276 return EINVAL; 271 277 278 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - check version"); 279 280 if (sp.foreign.addr.version != sp.local.addr.version) 281 return EINVAL; 282 283 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - encode pdu"); 284 272 285 rc = udp_pdu_encode(&sp, msg, &pdu); 273 286 if (rc != EOK) 274 287 return ENOMEM; 275 288 289 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - transmit"); 290 276 291 rc = udp_transmit_pdu(pdu); 277 292 udp_pdu_delete(pdu); … … 280 295 return EIO; 281 296 297 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_send - success"); 282 298 return EOK; 283 299 } … … 292 308 udp_rcv_queue_entry_t *rqe; 293 309 294 log_msg(LOG_DEFAULT, LVL_ DEBUG, "udp_assoc_recv()");310 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_recv()"); 295 311 296 312 fibril_mutex_lock(&assoc->lock); … … 303 319 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_recv() - association was reset"); 304 320 fibril_mutex_unlock(&assoc->lock); 305 return E CONNABORTED;321 return ENXIO; 306 322 } 307 323 308 log_msg(LOG_DEFAULT, LVL_ DEBUG, "udp_assoc_recv() - got a message");324 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_recv() - got a message"); 309 325 link = list_first(&assoc->rcv_queue); 310 326 rqe = list_get_instance(link, udp_rcv_queue_entry_t, link); … … 328 344 int rc; 329 345 330 log_msg(LOG_DEFAULT, LVL_ DEBUG, "udp_assoc_received(%p, %p)", rsp, msg);346 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_received(%p, %p)", rsp, msg); 331 347 332 348 assoc = udp_assoc_find_ref(rsp); 333 349 if (assoc == NULL) { 334 log_msg(LOG_DEFAULT, LVL_ DEBUG, "No association found. Message dropped.");350 log_msg(LOG_DEFAULT, LVL_NOTE, "No association found. Message dropped."); 335 351 /* XXX Generate ICMP error. */ 336 352 /* XXX Might propagate error directly by error return. */ … … 339 355 } 340 356 341 rc = udp_assoc_queue_msg(assoc, rsp, msg); 342 if (rc != EOK) { 343 log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped."); 357 if (0) { 358 rc = udp_assoc_queue_msg(assoc, rsp, msg); 359 if (rc != EOK) { 360 log_msg(LOG_DEFAULT, LVL_DEBUG, "Out of memory. Message dropped."); 344 361 /* XXX Generate ICMP error? */ 362 } 345 363 } 364 365 log_msg(LOG_DEFAULT, LVL_NOTE, "call assoc->cb->recv_msg"); 366 assoc->cb->recv_msg(assoc->cb_arg, rsp, msg); 346 367 } 347 368 … … 387 408 static bool udp_socket_match(udp_sock_t *sock, udp_sock_t *patt) 388 409 { 389 log_msg(LOG_DEFAULT, LVL_DEBUG, 390 "udp_socket_match(sock=(%u), pat=(%u))", sock->port, patt->port); 410 char *sa, *pa; 411 412 sa = pa = (char *)"?"; 413 (void) inet_addr_format(&sock->addr, &sa); 414 (void) inet_addr_format(&patt->addr, &pa); 415 416 log_msg(LOG_DEFAULT, LVL_NOTE, 417 "udp_socket_match(sock=(%s,%u), pat=(%s,%u))", 418 sa, sock->port, pa, patt->port); 391 419 392 420 if ((!inet_addr_is_any(&patt->addr)) && … … 394 422 return false; 395 423 424 log_msg(LOG_DEFAULT, LVL_NOTE, "addr OK"); 425 396 426 if ((patt->port != UDP_PORT_ANY) && 397 427 (patt->port != sock->port)) 398 428 return false; 399 429 400 log_msg(LOG_DEFAULT, LVL_ DEBUG, " -> match");430 log_msg(LOG_DEFAULT, LVL_NOTE, " -> match"); 401 431 402 432 return true; … … 430 460 static udp_assoc_t *udp_assoc_find_ref(udp_sockpair_t *sp) 431 461 { 432 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_assoc_find_ref(%p)", sp); 462 char *la, *ra; 463 464 log_msg(LOG_DEFAULT, LVL_NOTE, "udp_assoc_find_ref(%p)", sp); 433 465 434 466 fibril_mutex_lock(&assoc_list_lock); 435 467 468 log_msg(LOG_DEFAULT, LVL_NOTE, "associations:"); 436 469 list_foreach(assoc_list, link, udp_assoc_t, assoc) { 437 470 udp_sockpair_t *asp = &assoc->ident; 438 471 472 la = ra = NULL; 473 474 (void) inet_addr_format(&asp->local.addr, &la); 475 (void) inet_addr_format(&asp->foreign.addr, &ra); 476 477 log_msg(LOG_DEFAULT, LVL_NOTE, "find_ref:asp=%p la=%s ra=%s", 478 asp, la, ra); 439 479 /* Skip unbound associations */ 440 if (asp->local.port == UDP_PORT_ANY) 480 if (asp->local.port == UDP_PORT_ANY) { 481 log_msg(LOG_DEFAULT, LVL_NOTE, "skip unbound"); 441 482 continue; 483 } 442 484 443 485 if (udp_sockpair_match(sp, asp)) { … … 446 488 fibril_mutex_unlock(&assoc_list_lock); 447 489 return assoc; 490 } else { 491 log_msg(LOG_DEFAULT, LVL_NOTE, "not matched"); 448 492 } 449 493 } 450 494 495 log_msg(LOG_DEFAULT, LVL_NOTE, "associations END"); 451 496 fibril_mutex_unlock(&assoc_list_lock); 452 497 return NULL; -
uspace/srv/net/udp/assoc.h
rba0eac5 rfab2746 40 40 #include "udp_type.h" 41 41 42 extern udp_assoc_t *udp_assoc_new(udp_sock_t *, udp_sock_t *); 42 extern udp_assoc_t *udp_assoc_new(udp_sock_t *, udp_sock_t *, udp_assoc_cb_t *, 43 void *); 43 44 extern void udp_assoc_delete(udp_assoc_t *); 44 45 extern void udp_assoc_add(udp_assoc_t *); -
uspace/srv/net/udp/pdu.c
rba0eac5 rfab2746 41 41 #include <stdlib.h> 42 42 #include <inet/addr.h> 43 #include <net/socket_codes.h>44 43 #include "msg.h" 45 44 #include "pdu.h" -
uspace/srv/net/udp/service.h
rba0eac5 rfab2746 1 1 /* 2 * Copyright (c) 201 2Jiri Svoboda2 * Copyright (c) 2015 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 30 30 * @{ 31 31 */ 32 /** @file Socket provider32 /** @file HelenOS service implementation 33 33 */ 34 34 35 #ifndef S OCK_H36 #define S OCK_H35 #ifndef SERVICE_H 36 #define SERVICE_H 37 37 38 #include <async.h> 39 40 extern int udp_sock_init(void); 38 extern int udp_service_init(void); 41 39 42 40 #endif -
uspace/srv/net/udp/ucall.c
rba0eac5 rfab2746 35 35 */ 36 36 37 #include <errno.h> 37 38 #include <io/log.h> 38 39 #include <macros.h> 40 #include <mem.h> 39 41 40 42 #include "assoc.h" … … 48 50 49 51 log_msg(LOG_DEFAULT, LVL_DEBUG, "udp_uc_create()"); 50 nassoc = udp_assoc_new(NULL, NULL );52 nassoc = udp_assoc_new(NULL, NULL, NULL, NULL); 51 53 if (nassoc == NULL) 52 54 return UDP_ENORES; … … 125 127 case EOK: 126 128 break; 127 case E CONNABORTED:129 case ENXIO: 128 130 return UDP_ERESET; 129 131 default: -
uspace/srv/net/udp/udp.c
rba0eac5 rfab2746 41 41 #include <task.h> 42 42 43 #include "service.h" 43 44 #include "udp_inet.h" 44 #include "sock.h"45 45 46 46 #define NAME "udp" … … 58 58 } 59 59 60 rc = udp_s ock_init();60 rc = udp_service_init(); 61 61 if (rc != EOK) { 62 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing socketservice.");62 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed initializing UDP service."); 63 63 return ENOENT; 64 64 } -
uspace/srv/net/udp/udp_type.h
rba0eac5 rfab2746 36 36 #define UDP_TYPE_H 37 37 38 #include <async.h> 38 39 #include <fibril.h> 39 40 #include <fibril_synch.h> 40 41 #include <ipc/loc.h> 41 #include <socket_core.h>42 42 #include <sys/types.h> 43 43 #include <inet/addr.h> … … 99 99 100 100 typedef struct { 101 async_sess_t *sess; 102 socket_cores_t sockets; 103 } udp_client_t; 101 void (*recv_msg)(void *, udp_sockpair_t *, udp_msg_t *); 102 } udp_assoc_cb_t; 104 103 105 104 /** UDP association … … 131 130 /** Receive queue CV. Broadcast when new datagram is inserted */ 132 131 fibril_condvar_t rcv_queue_cv; 132 133 udp_assoc_cb_t *cb; 134 void *cb_arg; 133 135 } udp_assoc_t; 134 136 135 137 typedef struct { 136 138 } udp_assoc_status_t; 137 138 typedef struct udp_sockdata {139 /** Lock */140 fibril_mutex_t lock;141 /** Socket core */142 socket_core_t *sock_core;143 /** Client */144 udp_client_t *client;145 /** Connection */146 udp_assoc_t *assoc;147 /** User-configured IP link */148 service_id_t iplink;149 /** Receiving fibril */150 fid_t recv_fibril;151 uint8_t recv_buffer[UDP_FRAGMENT_SIZE];152 size_t recv_buffer_used;153 udp_sock_t recv_fsock;154 fibril_mutex_t recv_buffer_lock;155 fibril_condvar_t recv_buffer_cv;156 udp_error_t recv_error;157 } udp_sockdata_t;158 139 159 140 typedef struct { … … 166 147 } udp_rcv_queue_entry_t; 167 148 149 typedef struct udp_cassoc { 150 /** Association */ 151 udp_assoc_t *assoc; 152 /** Association ID for the client */ 153 sysarg_t id; 154 /** Client */ 155 struct udp_client *client; 156 link_t lclient; 157 } udp_cassoc_t; 158 159 typedef struct { 160 /** Link to receive queue */ 161 link_t link; 162 /** Socket pair */ 163 udp_sockpair_t sp; 164 /** Message */ 165 udp_msg_t *msg; 166 /** Client association */ 167 udp_cassoc_t *cassoc; 168 } udp_crcv_queue_entry_t; 169 170 typedef struct udp_client { 171 /** Client callback session */ 172 async_sess_t *sess; 173 /** Client assocations */ 174 list_t cassoc; /* of udp_cassoc_t */ 175 /** Client receive queue */ 176 list_t crcv_queue; 177 } udp_client_t; 178 168 179 #endif 169 180
Note:
See TracChangeset
for help on using the changeset viewer.